Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "management/nfd-fib-entry.hpp" |
| 8 | |
| 9 | #include <boost/test/unit_test.hpp> |
| 10 | #include <boost/test/output_test_stream.hpp> |
| 11 | |
| 12 | namespace ndn { |
| 13 | namespace nfd { |
| 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(TestNfdFibEntry) |
| 16 | |
| 17 | const uint8_t TestNextHopRecord[] = |
| 18 | { |
| 19 | 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8 |
| 20 | }; |
| 21 | |
| 22 | const uint8_t TestFibEntryNoNextHops[] = |
| 23 | { |
| 24 | 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, |
| 25 | 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74, |
| 26 | 0x65, 0x73, 0x74 |
| 27 | }; |
| 28 | |
| 29 | const uint8_t TestFibEntry[] = |
| 30 | { |
| 31 | 0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, |
| 32 | 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, |
| 33 | 0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, |
| 34 | 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4, |
| 35 | 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01, |
| 36 | 0x90, 0x81, 0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, |
| 37 | 0x01, 0x2c, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, |
| 38 | 0x01, 0xc8 |
| 39 | }; |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(TestNextHopRecordEncode) |
| 42 | { |
| 43 | NextHopRecord record; |
| 44 | record.setFaceId(10); |
| 45 | record.setCost(200); |
| 46 | |
| 47 | const Block& wire = record.wireEncode(); |
| 48 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestNextHopRecord, |
| 49 | TestNextHopRecord + sizeof(TestNextHopRecord), |
| 50 | wire.begin(), wire.end()); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(TestNextHopRecordDecode) |
| 55 | { |
| 56 | NextHopRecord record; |
| 57 | |
| 58 | BOOST_REQUIRE_NO_THROW(record.wireDecode(Block(TestNextHopRecord, |
| 59 | sizeof(TestNextHopRecord)))); |
| 60 | BOOST_REQUIRE_EQUAL(record.getFaceId(), 10); |
| 61 | BOOST_REQUIRE_EQUAL(record.getCost(), 200); |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopEncode) |
| 65 | { |
| 66 | FibEntry entry; |
| 67 | entry.setPrefix("/this/is/a/test"); |
| 68 | |
| 69 | const Block& wire = entry.wireEncode(); |
| 70 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntryNoNextHops, |
| 71 | TestFibEntryNoNextHops + sizeof(TestFibEntryNoNextHops), |
| 72 | wire.begin(), wire.end()); |
| 73 | } |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopsDecode) |
| 76 | { |
| 77 | FibEntry entry; |
| 78 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntryNoNextHops, |
| 79 | sizeof(TestFibEntryNoNextHops)))); |
| 80 | |
| 81 | BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 82 | BOOST_REQUIRE(entry.getNextHopRecords().empty()); |
| 83 | } |
| 84 | |
| 85 | BOOST_AUTO_TEST_CASE(TestFibEntryEncode) |
| 86 | { |
| 87 | FibEntry entry; |
| 88 | entry.setPrefix("/this/is/a/test"); |
| 89 | |
| 90 | std::list<NextHopRecord> records; |
| 91 | |
| 92 | for (int i = 1; i < 4; i++) |
| 93 | { |
| 94 | NextHopRecord record; |
| 95 | record.setFaceId(i * 10); |
| 96 | record.setCost((i * 100) + 100); |
| 97 | records.push_back(record); |
| 98 | } |
| 99 | |
| 100 | entry.setNextHopRecords(records.begin(), records.end()); |
| 101 | |
| 102 | NextHopRecord oneMore; |
| 103 | oneMore.setFaceId(40); |
| 104 | oneMore.setCost(500); |
| 105 | |
| 106 | entry.addNextHopRecord(oneMore); |
| 107 | |
| 108 | const Block& wire = entry.wireEncode(); |
| 109 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntry, |
| 110 | TestFibEntry + sizeof(TestFibEntry), |
| 111 | wire.begin(), wire.end()); |
| 112 | |
| 113 | // std::ofstream of("out.tmp"); |
| 114 | // of.write((const char*)entry.wireEncode().wire(), |
| 115 | // entry.wireEncode().size()); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(TestFibEntryDecode) |
| 120 | { |
| 121 | FibEntry entry; |
| 122 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntry, |
| 123 | sizeof(TestFibEntry)))); |
| 124 | |
| 125 | std::list<NextHopRecord> records = entry.getNextHopRecords(); |
| 126 | |
| 127 | BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 128 | BOOST_REQUIRE_EQUAL(entry.getNextHopRecords().size(), 4); |
| 129 | |
| 130 | size_t value = 4; |
| 131 | |
| 132 | for (std::list<NextHopRecord>::const_iterator i = records.begin(); |
| 133 | i != records.end(); |
| 134 | ++i) |
| 135 | { |
| 136 | BOOST_CHECK_EQUAL(i->getFaceId(), value * 10); |
| 137 | BOOST_CHECK_EQUAL(i->getCost(), (value * 100) + 100); |
| 138 | --value; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() |
| 143 | |
| 144 | } // namespace nfd |
| 145 | } // namespace ndn |