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 | { |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 31 | 0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, |
| 32 | 0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8, 0x81, |
| 33 | 0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, 0x01, 0x2c, 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01, |
| 34 | 0x90, 0x81, 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4 |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | BOOST_AUTO_TEST_CASE(TestNextHopRecordEncode) |
| 38 | { |
| 39 | NextHopRecord record; |
| 40 | record.setFaceId(10); |
| 41 | record.setCost(200); |
| 42 | |
| 43 | const Block& wire = record.wireEncode(); |
| 44 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestNextHopRecord, |
| 45 | TestNextHopRecord + sizeof(TestNextHopRecord), |
| 46 | wire.begin(), wire.end()); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | BOOST_AUTO_TEST_CASE(TestNextHopRecordDecode) |
| 51 | { |
| 52 | NextHopRecord record; |
| 53 | |
| 54 | BOOST_REQUIRE_NO_THROW(record.wireDecode(Block(TestNextHopRecord, |
| 55 | sizeof(TestNextHopRecord)))); |
| 56 | BOOST_REQUIRE_EQUAL(record.getFaceId(), 10); |
| 57 | BOOST_REQUIRE_EQUAL(record.getCost(), 200); |
| 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopEncode) |
| 61 | { |
| 62 | FibEntry entry; |
| 63 | entry.setPrefix("/this/is/a/test"); |
| 64 | |
| 65 | const Block& wire = entry.wireEncode(); |
| 66 | BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntryNoNextHops, |
| 67 | TestFibEntryNoNextHops + sizeof(TestFibEntryNoNextHops), |
| 68 | wire.begin(), wire.end()); |
| 69 | } |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopsDecode) |
| 72 | { |
| 73 | FibEntry entry; |
| 74 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntryNoNextHops, |
| 75 | sizeof(TestFibEntryNoNextHops)))); |
| 76 | |
| 77 | BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 78 | BOOST_REQUIRE(entry.getNextHopRecords().empty()); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(TestFibEntryEncode) |
| 82 | { |
| 83 | FibEntry entry; |
| 84 | entry.setPrefix("/this/is/a/test"); |
| 85 | |
| 86 | std::list<NextHopRecord> records; |
| 87 | |
| 88 | for (int i = 1; i < 4; i++) |
| 89 | { |
| 90 | NextHopRecord record; |
| 91 | record.setFaceId(i * 10); |
| 92 | record.setCost((i * 100) + 100); |
| 93 | records.push_back(record); |
| 94 | } |
| 95 | |
| 96 | entry.setNextHopRecords(records.begin(), records.end()); |
| 97 | |
| 98 | NextHopRecord oneMore; |
| 99 | oneMore.setFaceId(40); |
| 100 | oneMore.setCost(500); |
| 101 | |
| 102 | entry.addNextHopRecord(oneMore); |
| 103 | |
| 104 | const Block& wire = entry.wireEncode(); |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL_COLLECTIONS(TestFibEntry, |
| 106 | TestFibEntry + sizeof(TestFibEntry), |
| 107 | wire.begin(), wire.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 108 | |
| 109 | // std::ofstream of("out.tmp"); |
| 110 | // of.write((const char*)entry.wireEncode().wire(), |
| 111 | // entry.wireEncode().size()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(TestFibEntryDecode) |
| 115 | { |
| 116 | FibEntry entry; |
| 117 | BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntry, |
| 118 | sizeof(TestFibEntry)))); |
| 119 | |
| 120 | std::list<NextHopRecord> records = entry.getNextHopRecords(); |
| 121 | |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(entry.getPrefix(), "/this/is/a/test"); |
| 123 | BOOST_CHECK_EQUAL(entry.getNextHopRecords().size(), 4); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 124 | |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 125 | size_t value = 1; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 126 | |
| 127 | for (std::list<NextHopRecord>::const_iterator i = records.begin(); |
| 128 | i != records.end(); |
| 129 | ++i) |
| 130 | { |
| 131 | BOOST_CHECK_EQUAL(i->getFaceId(), value * 10); |
| 132 | BOOST_CHECK_EQUAL(i->getCost(), (value * 100) + 100); |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 133 | ++value; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE_END() |
| 138 | |
| 139 | } // namespace nfd |
| 140 | } // namespace ndn |