Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 7e6b9e8 | 2015-03-03 16:11:11 -0800 | [diff] [blame] | 2 | * 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 16 | import com.intel.jndn.management.types.StatusDataset; |
| 17 | import com.intel.jndn.management.types.FaceStatus; |
Andrew Brown | baf21d5 | 2015-07-13 10:32:46 -0700 | [diff] [blame] | 18 | import com.intel.jndn.utils.client.SimpleClient; |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 19 | import java.io.IOException; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 20 | import java.util.List; |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 21 | import java.util.logging.Logger; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 22 | import junit.framework.Assert; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 23 | import net.named_data.jndn.Data; |
| 24 | import net.named_data.jndn.Face; |
| 25 | import net.named_data.jndn.Interest; |
| 26 | import net.named_data.jndn.Name; |
| 27 | import net.named_data.jndn.encoding.EncodingException; |
| 28 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import 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 | */ |
| 38 | public class FaceStatusTest { |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 39 | |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 40 | private static final Logger logger = Logger.getLogger(FaceStatusTest.class.getName()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 41 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 42 | /** |
| 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 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 | // encode |
| 55 | Blob encoded = status.wireEncode(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 56 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 57 | // decode |
| 58 | FaceStatus decoded = new FaceStatus(); |
| 59 | decoded.wireDecode(encoded.buf()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 60 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 61 | // 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 Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 72 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 73 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 91 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 92 | /** |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 110 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 111 | /** |
| 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 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 117 | private static Data getFaceData(boolean usePreComputedData) throws IOException { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 118 | // 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 126 | |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 127 | // 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 Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 128 | 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 132 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 133 | // send packet |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 134 | Data data = SimpleClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 135 | String hex = data.getContent().toHex(); |
| 136 | logger.info("Hex dump of face list: " + hex); |
| 137 | return data; |
| 138 | } |
| 139 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 140 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Pre-computed face list from a vanilla NFD running on Ubuntu 14.04 |
| 143 | */ |
| 144 | private static final String DATA = "803a690101720b696e7465726e616c3a2f2f810b696e7465726e616c3a2f2f840101850100860100900100910201429202063993010094010095010080406901fe720f636f6e74656e7473746f72653a2f2f810f636f6e74656e7473746f72653a2f2f84010185010086010090010091010092010093010094010095010080306901ff72076e756c6c3a2f2f81076e756c6c3a2f2f8401018501008601009001009101009201009301009401009501008053690201007219756470343a2f2f3232342e302e32332e3137303a35363336338117756470343a2f2f31302e35342e31322e373a35363336338401008501008601009001009101009201009301009401009501008056690201017219756470343a2f2f3232342e302e32332e3137303a3536333633811a756470343a2f2f3139322e3136382e35302e35373a3536333633840100850100860100900100910100920100930100940100950100804869020102721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810a6465763a2f2f65746830840100850100860100900100910100920100930100940100950100804969020103721b65746865723a2f2f5b30313a30303a35653a30303a31373a61615d810b6465763a2f2f776c616e30840100850100860100900100910100920100930100940100950100804669020104720766643a2f2f32328114756e69783a2f2f2f72756e2f6e66642e736f636b840101850101860100900206349101019201019302012e940400014079950400041903804e690201197216746370343a2f2f3132372e302e302e313a35363336358115746370343a2f2f3132372e302e302e313a36333633840101850101860100900101910100920100930100940132950100"; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 145 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Convert hex string to bytes; special thanks to |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 148 | * <a href="http://stackoverflow.com/questions/140131">http://stackoverflow.com/questions/140131</a> |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 149 | * |
| 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 162 | |
| 163 | } |