blob: c4d5ba045943ef7f35c7a4fe010bd59ad07fa796 [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
13import com.intel.jndn.utils.Client;
14import java.util.List;
15import net.named_data.jndn.Data;
16import net.named_data.jndn.Face;
17import net.named_data.jndn.Interest;
18import net.named_data.jndn.Name;
19import net.named_data.jndn.encoding.EncodingException;
20import net.named_data.jndn.util.Blob;
21import org.apache.logging.log4j.LogManager;
22import org.apache.logging.log4j.Logger;
23import org.junit.Test;
24import static org.junit.Assert.*;
25
26/**
27 * Test whether the decoding for the face management service is working
28 * correctly
29 *
30 * @author Andrew Brown <andrew.brown@intel.com>
31 */
32public class FaceStatusTest {
33
34 private static final Logger logger = LogManager.getLogger();
35
36 /**
37 * Test of decode method, of class FaceStatus.
38 *
39 * @throws java.lang.Exception
40 */
41 @Test
42 public void testDecode() throws Exception {
43 Data data = getFaceData(true);
44 List<FaceStatus> results = FaceStatus.decode(data);
45 assertTrue(results.size() > 4);
46 for (FaceStatus f : results) {
47 // the first face (face 1) should always be the internal face
48 if (f.faceId == 1) {
49 assertEquals("internal://", f.uri);
50 assertEquals("internal://", f.localUri);
51 }
52 }
53 }
54
55 /**
56 * Integration test to run on actual system
57 *
58 * @param args
59 * @throws EncodingException
60 */
61 public static void main(String[] args) throws EncodingException {
62 Data data = getFaceData(false);
63 List<FaceStatus> results = FaceStatus.decode(data);
64 assertTrue(results.size() > 4);
65 for (FaceStatus f : results) {
66 // the first face (face 1) should always be the internal face
67 if (f.faceId == 1) {
68 assertEquals("internal://", f.uri);
69 assertEquals("internal://", f.localUri);
70 }
71 }
72 }
73
74 /**
75 * Retrieve a TLV encoded representation of the face list data
76 *
77 * @param usePreComputedData to avoid errors when local NFD is not present
78 * @return
79 */
80 private static Data getFaceData(boolean usePreComputedData) {
81 // use pre-computed data to avoid errors when local NFD is not present
82 if (usePreComputedData) {
83 Data data = new Data();
84 data.setContent(new Blob(hexStringToByteArray(DATA)));
85 return data;
86 } // alternately, query the actual localhost for current data
87 else {
88 Face forwarder = new Face("localhost");
89
90 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
91 Interest interest = new Interest(new Name("/localhost/nfd/faces/list"));
92 interest.setMustBeFresh(true);
93 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
94 interest.setInterestLifetimeMilliseconds(2000.0);
95
96 // send packet
97 Data data = Client.getDefault().getSync(forwarder, interest);
98 String hex = data.getContent().toHex();
99 logger.info("Hex dump of face list: " + hex);
100 return data;
101 }
102 }
103
104 /**
105 * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04
106 */
107 private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100";
108
109 /**
110 * Convert hex string to bytes; special thanks to
111 * http://stackoverflow.com/questions/140131
112 *
113 * @param s
114 * @return
115 */
116 private static byte[] hexStringToByteArray(String s) {
117 int len = s.length();
118 byte[] data = new byte[len / 2];
119 for (int i = 0; i < len; i += 2) {
120 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
121 + Character.digit(s.charAt(i + 1), 16));
122 }
123 return data;
124 }
125
126}