blob: 00602f387367e7523dacc7df3aec28b8fcfa66eb [file] [log] [blame]
Andrew Brown2f1fdbf2015-01-21 10:52:29 -08001/*
2 * File name: FaceStatusTest.java
3 *
4 * Purpose: Test whether the decoding for the face management service is
5 * working correctly
6 *
7 * © Copyright Intel Corporation. All rights reserved.
8 * Intel Corporation, 2200 Mission College Boulevard,
9 * Santa Clara, CA 95052-8119, USA
10 */
11package com.intel.jndn.management;
12
Andrew Brownc46c1602015-02-18 10:45:56 -080013import com.intel.jndn.management.types.StatusDataset;
14import com.intel.jndn.management.types.FaceStatus;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080015import com.intel.jndn.utils.Client;
16import java.util.List;
Andrew Brown63bed362015-02-18 11:28:50 -080017import java.util.logging.Logger;
Andrew Brownc46c1602015-02-18 10:45:56 -080018import junit.framework.Assert;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080019import net.named_data.jndn.Data;
20import net.named_data.jndn.Face;
21import net.named_data.jndn.Interest;
22import net.named_data.jndn.Name;
23import net.named_data.jndn.encoding.EncodingException;
24import net.named_data.jndn.util.Blob;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080025import org.junit.Test;
26import static org.junit.Assert.*;
27
28/**
29 * Test whether the decoding for the face management service is working
30 * correctly
31 *
32 * @author Andrew Brown <andrew.brown@intel.com>
33 */
34public class FaceStatusTest {
Andrew Brownc46c1602015-02-18 10:45:56 -080035
Andrew Brown63bed362015-02-18 11:28:50 -080036 private static final Logger logger = Logger.getLogger(FaceStatusTest.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080037
Andrew Brown211d2b62015-02-18 11:12:02 -080038 /**
39 * Test encoding/decoding
40 *
41 * @throws java.lang.Exception
42 */
43 @Test
44 public void testEncodeDecode() throws Exception {
45 FaceStatus status = new FaceStatus();
46 status.setFaceId(42);
47 status.setUri("...");
48 status.setLocalUri("...");
Andrew Brownc46c1602015-02-18 10:45:56 -080049
Andrew Brown211d2b62015-02-18 11:12:02 -080050 // encode
51 Blob encoded = status.wireEncode();
Andrew Brownc46c1602015-02-18 10:45:56 -080052
Andrew Brown211d2b62015-02-18 11:12:02 -080053 // decode
54 FaceStatus decoded = new FaceStatus();
55 decoded.wireDecode(encoded.buf());
Andrew Brownc46c1602015-02-18 10:45:56 -080056
Andrew Brown211d2b62015-02-18 11:12:02 -080057 // test
58 Assert.assertEquals(status.getFaceId(), decoded.getFaceId());
59 Assert.assertEquals(status.getUri(), decoded.getUri());
60 Assert.assertEquals(status.getLocalUri(), decoded.getLocalUri());
61 Assert.assertEquals(status.getExpirationPeriod(), decoded.getExpirationPeriod());
62 Assert.assertEquals(status.getFaceScope(), decoded.getFaceScope());
63 Assert.assertEquals(status.getFacePersistency(), decoded.getFacePersistency());
64 Assert.assertEquals(status.getLinkType(), decoded.getLinkType());
65 Assert.assertEquals(status.getInBytes(), decoded.getInBytes());
66 Assert.assertEquals(status.getOutBytes(), decoded.getOutBytes());
67 }
Andrew Brownc46c1602015-02-18 10:45:56 -080068
Andrew Brown211d2b62015-02-18 11:12:02 -080069 /**
70 * Test of decode method, of class FaceStatus.
71 *
72 * @throws java.lang.Exception
73 */
74 @Test
75 public void testDecodeFakeData() throws Exception {
76 Data data = getFaceData(true);
77 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
78 assertTrue(results.size() > 4);
79 for (FaceStatus f : results) {
80 // the first face (face 1) should always be the internal face
81 if (f.getFaceId() == 1) {
82 assertEquals("internal://", f.getUri());
83 assertEquals("internal://", f.getLocalUri());
84 }
85 }
86 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080087
Andrew Brown211d2b62015-02-18 11:12:02 -080088 /**
89 * Integration test to run on actual system
90 *
91 * @param args
92 * @throws EncodingException
93 */
94 public static void main(String[] args) throws Exception {
95 Data data = getFaceData(false);
96 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
97 assertTrue(results.size() > 4);
98 for (FaceStatus f : results) {
99 // the first face (face 1) should always be the internal face
100 if (f.getFaceId() == 1) {
101 assertEquals("internal://", f.getUri());
102 assertEquals("internal://", f.getLocalUri());
103 }
104 }
105 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800106
Andrew Brown211d2b62015-02-18 11:12:02 -0800107 /**
108 * Retrieve a TLV encoded representation of the face list data
109 *
110 * @param usePreComputedData to avoid errors when local NFD is not present
111 * @return
112 */
113 private static Data getFaceData(boolean usePreComputedData) {
114 // use pre-computed data to avoid errors when local NFD is not present
115 if (usePreComputedData) {
116 Data data = new Data();
117 data.setContent(new Blob(hexStringToByteArray(DATA)));
118 return data;
119 } // alternately, query the actual localhost for current data
120 else {
121 Face forwarder = new Face("localhost");
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800122
Andrew Brown211d2b62015-02-18 11:12:02 -0800123 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
124 Interest interest = new Interest(new Name("/localhost/nfd/faces/list"));
125 interest.setMustBeFresh(true);
126 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
127 interest.setInterestLifetimeMilliseconds(2000.0);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800128
Andrew Brown211d2b62015-02-18 11:12:02 -0800129 // send packet
130 Data data = Client.getDefault().getSync(forwarder, interest);
131 String hex = data.getContent().toHex();
132 logger.info("Hex dump of face list: " + hex);
133 return data;
134 }
135 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800136
Andrew Brown211d2b62015-02-18 11:12:02 -0800137 /**
138 * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04
139 */
140 private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100";
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800141
Andrew Brown211d2b62015-02-18 11:12:02 -0800142 /**
143 * Convert hex string to bytes; special thanks to
144 * http://stackoverflow.com/questions/140131
145 *
146 * @param s
147 * @return
148 */
149 private static byte[] hexStringToByteArray(String s) {
150 int len = s.length();
151 byte[] data = new byte[len / 2];
152 for (int i = 0; i < len; i += 2) {
153 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
154 + Character.digit(s.charAt(i + 1), 16));
155 }
156 return data;
157 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800158
159}