Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 11 | package com.intel.jndn.management; |
| 12 | |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 13 | import com.intel.jndn.management.types.StatusDataset; |
| 14 | import com.intel.jndn.management.types.FaceStatus; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 15 | import com.intel.jndn.utils.Client; |
| 16 | import java.util.List; |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame^] | 17 | import java.util.logging.LogManager; |
| 18 | import java.util.logging.Logger; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 19 | import junit.framework.Assert; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 20 | import net.named_data.jndn.Data; |
| 21 | import net.named_data.jndn.Face; |
| 22 | import net.named_data.jndn.Interest; |
| 23 | import net.named_data.jndn.Name; |
| 24 | import net.named_data.jndn.encoding.EncodingException; |
| 25 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import 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 | */ |
| 35 | public class FaceStatusTest { |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 36 | |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame^] | 37 | private static final Logger logger = Logger.getLogger(FaceStatusTest.class.getName()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 38 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 39 | /** |
| 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 Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 50 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 51 | // encode |
| 52 | Blob encoded = status.wireEncode(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 53 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 54 | // decode |
| 55 | FaceStatus decoded = new FaceStatus(); |
| 56 | decoded.wireDecode(encoded.buf()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 57 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 58 | // 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 Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 69 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 70 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 88 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 89 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 107 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 108 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 123 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 124 | // 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 129 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 130 | // 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 137 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04 |
| 140 | */ |
| 141 | private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100"; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 142 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 143 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 159 | |
| 160 | } |