blob: 42bba951fe561cffbdd95f49f170369d8d4c2e0d [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.LogManager;
18import java.util.logging.Logger;
Andrew Brownc46c1602015-02-18 10:45:56 -080019import junit.framework.Assert;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080020import net.named_data.jndn.Data;
21import net.named_data.jndn.Face;
22import net.named_data.jndn.Interest;
23import net.named_data.jndn.Name;
24import net.named_data.jndn.encoding.EncodingException;
25import net.named_data.jndn.util.Blob;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080026import org.junit.Test;
27import static org.junit.Assert.*;
28
29/**
30 * Test whether the decoding for the face management service is working
31 * correctly
32 *
33 * @author Andrew Brown <andrew.brown@intel.com>
34 */
35public class FaceStatusTest {
Andrew Brownc46c1602015-02-18 10:45:56 -080036
Andrew Brown63bed362015-02-18 11:28:50 -080037 private static final Logger logger = Logger.getLogger(FaceStatusTest.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080038
Andrew Brown211d2b62015-02-18 11:12:02 -080039 /**
40 * Test encoding/decoding
41 *
42 * @throws java.lang.Exception
43 */
44 @Test
45 public void testEncodeDecode() throws Exception {
46 FaceStatus status = new FaceStatus();
47 status.setFaceId(42);
48 status.setUri("...");
49 status.setLocalUri("...");
Andrew Brownc46c1602015-02-18 10:45:56 -080050
Andrew Brown211d2b62015-02-18 11:12:02 -080051 // encode
52 Blob encoded = status.wireEncode();
Andrew Brownc46c1602015-02-18 10:45:56 -080053
Andrew Brown211d2b62015-02-18 11:12:02 -080054 // decode
55 FaceStatus decoded = new FaceStatus();
56 decoded.wireDecode(encoded.buf());
Andrew Brownc46c1602015-02-18 10:45:56 -080057
Andrew Brown211d2b62015-02-18 11:12:02 -080058 // test
59 Assert.assertEquals(status.getFaceId(), decoded.getFaceId());
60 Assert.assertEquals(status.getUri(), decoded.getUri());
61 Assert.assertEquals(status.getLocalUri(), decoded.getLocalUri());
62 Assert.assertEquals(status.getExpirationPeriod(), decoded.getExpirationPeriod());
63 Assert.assertEquals(status.getFaceScope(), decoded.getFaceScope());
64 Assert.assertEquals(status.getFacePersistency(), decoded.getFacePersistency());
65 Assert.assertEquals(status.getLinkType(), decoded.getLinkType());
66 Assert.assertEquals(status.getInBytes(), decoded.getInBytes());
67 Assert.assertEquals(status.getOutBytes(), decoded.getOutBytes());
68 }
Andrew Brownc46c1602015-02-18 10:45:56 -080069
Andrew Brown211d2b62015-02-18 11:12:02 -080070 /**
71 * Test of decode method, of class FaceStatus.
72 *
73 * @throws java.lang.Exception
74 */
75 @Test
76 public void testDecodeFakeData() throws Exception {
77 Data data = getFaceData(true);
78 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
79 assertTrue(results.size() > 4);
80 for (FaceStatus f : results) {
81 // the first face (face 1) should always be the internal face
82 if (f.getFaceId() == 1) {
83 assertEquals("internal://", f.getUri());
84 assertEquals("internal://", f.getLocalUri());
85 }
86 }
87 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080088
Andrew Brown211d2b62015-02-18 11:12:02 -080089 /**
90 * Integration test to run on actual system
91 *
92 * @param args
93 * @throws EncodingException
94 */
95 public static void main(String[] args) throws Exception {
96 Data data = getFaceData(false);
97 List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
98 assertTrue(results.size() > 4);
99 for (FaceStatus f : results) {
100 // the first face (face 1) should always be the internal face
101 if (f.getFaceId() == 1) {
102 assertEquals("internal://", f.getUri());
103 assertEquals("internal://", f.getLocalUri());
104 }
105 }
106 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800107
Andrew Brown211d2b62015-02-18 11:12:02 -0800108 /**
109 * Retrieve a TLV encoded representation of the face list data
110 *
111 * @param usePreComputedData to avoid errors when local NFD is not present
112 * @return
113 */
114 private static Data getFaceData(boolean usePreComputedData) {
115 // use pre-computed data to avoid errors when local NFD is not present
116 if (usePreComputedData) {
117 Data data = new Data();
118 data.setContent(new Blob(hexStringToByteArray(DATA)));
119 return data;
120 } // alternately, query the actual localhost for current data
121 else {
122 Face forwarder = new Face("localhost");
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800123
Andrew Brown211d2b62015-02-18 11:12:02 -0800124 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
125 Interest interest = new Interest(new Name("/localhost/nfd/faces/list"));
126 interest.setMustBeFresh(true);
127 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
128 interest.setInterestLifetimeMilliseconds(2000.0);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800129
Andrew Brown211d2b62015-02-18 11:12:02 -0800130 // send packet
131 Data data = Client.getDefault().getSync(forwarder, interest);
132 String hex = data.getContent().toHex();
133 logger.info("Hex dump of face list: " + hex);
134 return data;
135 }
136 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800137
Andrew Brown211d2b62015-02-18 11:12:02 -0800138 /**
139 * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04
140 */
141 private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100";
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800142
Andrew Brown211d2b62015-02-18 11:12:02 -0800143 /**
144 * Convert hex string to bytes; special thanks to
145 * http://stackoverflow.com/questions/140131
146 *
147 * @param s
148 * @return
149 */
150 private static byte[] hexStringToByteArray(String s) {
151 int len = s.length();
152 byte[] data = new byte[len / 2];
153 for (int i = 0; i < len; i += 2) {
154 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
155 + Character.digit(s.charAt(i + 1), 16));
156 }
157 return data;
158 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800159
160}