blob: 679976c3af08009d6159cf170129da76abe66228 [file] [log] [blame]
Andrew Brown2f1fdbf2015-01-21 10:52:29 -08001/*
andrewsbrown7e6b9e82015-03-03 16:11:11 -08002 * jndn-management
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080013 */
14package com.intel.jndn.management;
15
Andrew Brownc46c1602015-02-18 10:45:56 -080016import com.intel.jndn.management.types.StatusDataset;
17import com.intel.jndn.management.types.FaceStatus;
Andrew Brownbaf21d52015-07-13 10:32:46 -070018import com.intel.jndn.utils.client.SimpleClient;
andrewsbrown43b96302015-03-17 21:51:43 +010019import java.io.IOException;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080020import java.util.List;
Andrew Brown63bed362015-02-18 11:28:50 -080021import java.util.logging.Logger;
Andrew Brownc46c1602015-02-18 10:45:56 -080022import junit.framework.Assert;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080023import net.named_data.jndn.Data;
24import net.named_data.jndn.Face;
25import net.named_data.jndn.Interest;
26import net.named_data.jndn.Name;
27import net.named_data.jndn.encoding.EncodingException;
28import net.named_data.jndn.util.Blob;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080029import org.junit.Test;
30import static org.junit.Assert.*;
31
32/**
33 * Test whether the decoding for the face management service is working
34 * correctly
35 *
36 * @author Andrew Brown <andrew.brown@intel.com>
37 */
38public class FaceStatusTest {
Andrew Brownc46c1602015-02-18 10:45:56 -080039
Andrew Brown63bed362015-02-18 11:28:50 -080040 private static final Logger logger = Logger.getLogger(FaceStatusTest.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080041
Andrew Brown211d2b62015-02-18 11:12:02 -080042 /**
43 * Test encoding/decoding
44 *
45 * @throws java.lang.Exception
46 */
47 @Test
48 public void testEncodeDecode() throws Exception {
49 FaceStatus status = new FaceStatus();
50 status.setFaceId(42);
51 status.setUri("...");
52 status.setLocalUri("...");
Andrew Brownc46c1602015-02-18 10:45:56 -080053
Andrew Brown211d2b62015-02-18 11:12:02 -080054 // encode
55 Blob encoded = status.wireEncode();
Andrew Brownc46c1602015-02-18 10:45:56 -080056
Andrew Brown211d2b62015-02-18 11:12:02 -080057 // decode
58 FaceStatus decoded = new FaceStatus();
59 decoded.wireDecode(encoded.buf());
Andrew Brownc46c1602015-02-18 10:45:56 -080060
Andrew Brown211d2b62015-02-18 11:12:02 -080061 // test
62 Assert.assertEquals(status.getFaceId(), decoded.getFaceId());
63 Assert.assertEquals(status.getUri(), decoded.getUri());
64 Assert.assertEquals(status.getLocalUri(), decoded.getLocalUri());
65 Assert.assertEquals(status.getExpirationPeriod(), decoded.getExpirationPeriod());
66 Assert.assertEquals(status.getFaceScope(), decoded.getFaceScope());
67 Assert.assertEquals(status.getFacePersistency(), decoded.getFacePersistency());
68 Assert.assertEquals(status.getLinkType(), decoded.getLinkType());
69 Assert.assertEquals(status.getInBytes(), decoded.getInBytes());
70 Assert.assertEquals(status.getOutBytes(), decoded.getOutBytes());
71 }
Andrew Brownc46c1602015-02-18 10:45:56 -080072
Andrew Brown211d2b62015-02-18 11:12:02 -080073 /**
74 * Test of decode method, of class FaceStatus.
75 *
76 * @throws java.lang.Exception
77 */
78 @Test
79 public void testDecodeFakeData() throws Exception {
80 Data data = getFaceData(true);
81 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
82 assertTrue(results.size() > 4);
83 for (FaceStatus f : results) {
84 // the first face (face 1) should always be the internal face
85 if (f.getFaceId() == 1) {
86 assertEquals("internal://", f.getUri());
87 assertEquals("internal://", f.getLocalUri());
88 }
89 }
90 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080091
Andrew Brown211d2b62015-02-18 11:12:02 -080092 /**
93 * Integration test to run on actual system
94 *
95 * @param args
96 * @throws EncodingException
97 */
98 public static void main(String[] args) throws Exception {
99 Data data = getFaceData(false);
100 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
101 assertTrue(results.size() > 4);
102 for (FaceStatus f : results) {
103 // the first face (face 1) should always be the internal face
104 if (f.getFaceId() == 1) {
105 assertEquals("internal://", f.getUri());
106 assertEquals("internal://", f.getLocalUri());
107 }
108 }
109 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800110
Andrew Brown211d2b62015-02-18 11:12:02 -0800111 /**
112 * Retrieve a TLV encoded representation of the face list data
113 *
114 * @param usePreComputedData to avoid errors when local NFD is not present
115 * @return
116 */
andrewsbrown43b96302015-03-17 21:51:43 +0100117 private static Data getFaceData(boolean usePreComputedData) throws IOException {
Andrew Brown211d2b62015-02-18 11:12:02 -0800118 // use pre-computed data to avoid errors when local NFD is not present
119 if (usePreComputedData) {
120 Data data = new Data();
121 data.setContent(new Blob(hexStringToByteArray(DATA)));
122 return data;
123 } // alternately, query the actual localhost for current data
124 else {
125 Face forwarder = new Face("localhost");
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800126
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700127 // build management Interest packet; see <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a>
Andrew Brown211d2b62015-02-18 11:12:02 -0800128 Interest interest = new Interest(new Name("/localhost/nfd/faces/list"));
129 interest.setMustBeFresh(true);
130 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
131 interest.setInterestLifetimeMilliseconds(2000.0);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800132
Andrew Brown211d2b62015-02-18 11:12:02 -0800133 // send packet
andrewsbrown43b96302015-03-17 21:51:43 +0100134 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown211d2b62015-02-18 11:12:02 -0800135 String hex = data.getContent().toHex();
136 logger.info("Hex dump of face list: " + hex);
137 return data;
138 }
139 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800140
Andrew Brown211d2b62015-02-18 11:12:02 -0800141 /**
142 * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04
143 */
144 private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100";
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800145
Andrew Brown211d2b62015-02-18 11:12:02 -0800146 /**
147 * Convert hex string to bytes; special thanks to
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700148 * <a href="http://stackoverflow.com/questions/140131">http://stackoverflow.com/questions/140131</a>
Andrew Brown211d2b62015-02-18 11:12:02 -0800149 *
150 * @param s
151 * @return
152 */
153 private static byte[] hexStringToByteArray(String s) {
154 int len = s.length();
155 byte[] data = new byte[len / 2];
156 for (int i = 0; i < len; i += 2) {
157 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
158 + Character.digit(s.charAt(i + 1), 16));
159 }
160 return data;
161 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800162
163}