Andrew Brown | c46c160 | 2015-02-18 10:45:56 -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 | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import com.intel.jndn.management.types.StatusDataset; |
| 17 | import com.intel.jndn.management.types.NextHopRecord; |
| 18 | import com.intel.jndn.management.types.FibEntry; |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 19 | import com.intel.jndn.utils.SimpleClient; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import junit.framework.Assert; |
| 22 | import net.named_data.jndn.Data; |
| 23 | import net.named_data.jndn.Face; |
| 24 | import net.named_data.jndn.Interest; |
| 25 | import net.named_data.jndn.Name; |
| 26 | import net.named_data.jndn.encoding.EncodingException; |
| 27 | import net.named_data.jndn.util.Blob; |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import org.junit.Test; |
| 31 | |
| 32 | /** |
| 33 | * Test encode/decode of FibEntry and NextHopRecord |
| 34 | * |
| 35 | * @author Andrew Brown <andrew.brown@intel.com> |
| 36 | */ |
| 37 | public class FibEntryTest { |
| 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 | NextHopRecord nextHopRecord = new NextHopRecord(); |
| 47 | nextHopRecord.setFaceId(42); |
| 48 | nextHopRecord.setCost(100); |
| 49 | FibEntry entry = new FibEntry(); |
| 50 | entry.setName(new Name("/fib/entry/test")); |
| 51 | entry.getRecords().add(nextHopRecord); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 52 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 53 | // encode |
| 54 | Blob encoded = entry.wireEncode(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 55 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 56 | // decode |
| 57 | FibEntry decoded = new FibEntry(); |
| 58 | decoded.wireDecode(encoded.buf()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 59 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 60 | // test |
| 61 | Assert.assertEquals(entry.getName().toUri(), decoded.getName().toUri()); |
| 62 | Assert.assertEquals(entry.getRecords().get(0).getFaceId(), decoded.getRecords().get(0).getFaceId()); |
| 63 | Assert.assertEquals(entry.getRecords().get(0).getCost(), decoded.getRecords().get(0).getCost()); |
| 64 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 65 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Integration test to run on actual system |
| 68 | * |
| 69 | * @param args |
| 70 | * @throws EncodingException |
| 71 | */ |
| 72 | public static void main(String[] args) throws Exception { |
| 73 | Face forwarder = new Face("localhost"); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 74 | |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 75 | // 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] | 76 | Interest interest = new Interest(new Name("/localhost/nfd/fib/list")); |
| 77 | interest.setMustBeFresh(true); |
| 78 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 79 | interest.setInterestLifetimeMilliseconds(2000.0); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 80 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 81 | // send packet |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 82 | Data data = SimpleClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 83 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 84 | // decode results |
| 85 | List<FibEntry> results = StatusDataset.wireDecode(data.getContent(), FibEntry.class); |
| 86 | assertTrue(results.size() > 0); |
| 87 | assertEquals("/localhost/nfd", results.get(0).getName().toUri()); |
| 88 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 89 | } |