Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [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 "table/pit.hpp" |
| 8 | #include "../face/dummy-face.hpp" |
| 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 12 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(TablePit) |
| 15 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 16 | BOOST_AUTO_TEST_CASE(EntryInOutRecords) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 17 | { |
| 18 | shared_ptr<Face> face1 = make_shared<DummyFace>(1); |
| 19 | shared_ptr<Face> face2 = make_shared<DummyFace>(2); |
| 20 | Name name("ndn:/KuYfjtRq"); |
| 21 | Interest interest(name); |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 22 | Interest interest1(name, static_cast<ndn::Milliseconds>(2528)); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 23 | interest1.setNonce(25559); |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 24 | Interest interest2(name, static_cast<ndn::Milliseconds>(6464)); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | interest2.setNonce(19004); |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 26 | Interest interest3(name, static_cast<ndn::Milliseconds>(3585)); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 27 | interest3.setNonce(24216); |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 28 | Interest interest4(name, static_cast<ndn::Milliseconds>(8795)); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 29 | interest4.setNonce(17365); |
| 30 | |
| 31 | pit::Entry entry(interest); |
| 32 | |
| 33 | BOOST_CHECK(entry.getInterest().getName().equals(name)); |
| 34 | BOOST_CHECK(entry.getName().equals(name)); |
| 35 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 36 | const pit::InRecordCollection& inRecords1 = entry.getInRecords(); |
| 37 | BOOST_CHECK_EQUAL(inRecords1.size(), 0); |
| 38 | const pit::OutRecordCollection& outRecords1 = entry.getOutRecords(); |
| 39 | BOOST_CHECK_EQUAL(outRecords1.size(), 0); |
| 40 | |
| 41 | // insert InRecord |
| 42 | time::Point before1 = time::now(); |
| 43 | pit::InRecordCollection::iterator in1 = |
| 44 | entry.insertOrUpdateInRecord(face1, interest1); |
| 45 | time::Point after1 = time::now(); |
| 46 | const pit::InRecordCollection& inRecords2 = entry.getInRecords(); |
| 47 | BOOST_CHECK_EQUAL(inRecords2.size(), 1); |
| 48 | BOOST_CHECK(in1 == inRecords2.begin()); |
| 49 | BOOST_CHECK_EQUAL(in1->getFace(), face1); |
| 50 | BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1.getNonce()); |
| 51 | BOOST_CHECK_GE(in1->getLastRenewed(), before1); |
| 52 | BOOST_CHECK_LE(in1->getLastRenewed(), after1); |
| 53 | BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed() |
| 54 | - time::milliseconds(interest1.getInterestLifetime())), |
| 55 | (after1 - before1)); |
| 56 | |
| 57 | // insert OutRecord |
| 58 | time::Point before2 = time::now(); |
| 59 | pit::OutRecordCollection::iterator out1 = |
| 60 | entry.insertOrUpdateOutRecord(face1, interest1); |
| 61 | time::Point after2 = time::now(); |
| 62 | const pit::OutRecordCollection& outRecords2 = entry.getOutRecords(); |
| 63 | BOOST_CHECK_EQUAL(outRecords2.size(), 1); |
| 64 | BOOST_CHECK(out1 == outRecords2.begin()); |
| 65 | BOOST_CHECK_EQUAL(out1->getFace(), face1); |
| 66 | BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1.getNonce()); |
| 67 | BOOST_CHECK_GE(out1->getLastRenewed(), before2); |
| 68 | BOOST_CHECK_LE(out1->getLastRenewed(), after2); |
| 69 | BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed() |
| 70 | - time::milliseconds(interest1.getInterestLifetime())), |
| 71 | (after2 - before2)); |
| 72 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 73 | // update InRecord |
| 74 | time::Point before3 = time::now(); |
| 75 | pit::InRecordCollection::iterator in2 = |
| 76 | entry.insertOrUpdateInRecord(face1, interest2); |
| 77 | time::Point after3 = time::now(); |
| 78 | const pit::InRecordCollection& inRecords3 = entry.getInRecords(); |
| 79 | BOOST_CHECK_EQUAL(inRecords3.size(), 1); |
| 80 | BOOST_CHECK(in2 == inRecords3.begin()); |
| 81 | BOOST_CHECK_EQUAL(in2->getFace(), face1); |
| 82 | BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2.getNonce()); |
| 83 | BOOST_CHECK_LE(std::abs(in2->getExpiry() - in2->getLastRenewed() |
| 84 | - time::milliseconds(interest2.getInterestLifetime())), |
| 85 | (after3 - before3)); |
| 86 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 87 | // insert another InRecord |
| 88 | pit::InRecordCollection::iterator in3 = |
| 89 | entry.insertOrUpdateInRecord(face2, interest3); |
| 90 | const pit::InRecordCollection& inRecords4 = entry.getInRecords(); |
| 91 | BOOST_CHECK_EQUAL(inRecords4.size(), 2); |
| 92 | BOOST_CHECK_EQUAL(in3->getFace(), face2); |
| 93 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 94 | // delete all InRecords |
| 95 | entry.deleteInRecords(); |
| 96 | const pit::InRecordCollection& inRecords5 = entry.getInRecords(); |
| 97 | BOOST_CHECK_EQUAL(inRecords5.size(), 0); |
| 98 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 99 | // insert another OutRecord |
| 100 | pit::OutRecordCollection::iterator out2 = |
| 101 | entry.insertOrUpdateOutRecord(face2, interest4); |
| 102 | const pit::OutRecordCollection& outRecords3 = entry.getOutRecords(); |
| 103 | BOOST_CHECK_EQUAL(outRecords3.size(), 2); |
| 104 | BOOST_CHECK_EQUAL(out2->getFace(), face2); |
| 105 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 106 | // delete OutRecord |
| 107 | entry.deleteOutRecord(face2); |
| 108 | const pit::OutRecordCollection& outRecords4 = entry.getOutRecords(); |
| 109 | BOOST_REQUIRE_EQUAL(outRecords4.size(), 1); |
| 110 | BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1); |
| 111 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(EntryNonce) |
| 115 | { |
| 116 | Name name("ndn:/qtCQ7I1c"); |
| 117 | Interest interest(name); |
| 118 | |
| 119 | pit::Entry entry(interest); |
| 120 | |
| 121 | BOOST_CHECK_EQUAL(entry.addNonce(25559), true); |
| 122 | BOOST_CHECK_EQUAL(entry.addNonce(25559), false); |
| 123 | BOOST_CHECK_EQUAL(entry.addNonce(19004), true); |
| 124 | BOOST_CHECK_EQUAL(entry.addNonce(19004), false); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Insert) |
| 128 | { |
| 129 | Name name1("ndn:/5vzBNnMst"); |
| 130 | Name name2("ndn:/igSGfEIM62"); |
| 131 | Exclude exclude0; |
| 132 | Exclude exclude1; |
| 133 | exclude1.excludeOne(Name::Component("u26p47oep")); |
| 134 | Exclude exclude2; |
| 135 | exclude2.excludeOne(Name::Component("FG1Ni6nYcf")); |
| 136 | |
| 137 | // base |
| 138 | Interest interestA(name1, -1, -1, exclude0, -1, false, -1, -1.0, 0); |
| 139 | // A+exclude1 |
| 140 | Interest interestB(name1, -1, -1, exclude1, -1, false, -1, -1.0, 0); |
| 141 | // A+exclude2 |
| 142 | Interest interestC(name1, -1, -1, exclude2, -1, false, -1, -1.0, 0); |
| 143 | // A+MinSuffixComponents |
| 144 | Interest interestD(name1, 2, -1, exclude0, -1, false, -1, -1.0, 0); |
| 145 | // A+MaxSuffixComponents |
| 146 | Interest interestE(name1, -1, 4, exclude0, -1, false, -1, -1.0, 0); |
| 147 | // A+ChildSelector |
| 148 | Interest interestF(name1, -1, -1, exclude0, 1, false, -1, -1.0, 0); |
| 149 | // A+MustBeFresh |
| 150 | Interest interestG(name1, -1, -1, exclude0, -1, true, -1, -1.0, 0); |
| 151 | // A+Scope |
| 152 | Interest interestH(name1, -1, -1, exclude0, -1, false, 2, -1.0, 0); |
| 153 | // A+InterestLifetime |
| 154 | Interest interestI(name1, -1, -1, exclude0, -1, false, -1, 2000, 0); |
| 155 | // A+Nonce |
| 156 | Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192); |
| 157 | // different Name+exclude1 |
| 158 | Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0); |
| 159 | |
| 160 | Pit pit; |
| 161 | std::pair<shared_ptr<pit::Entry>, bool> insertResult; |
| 162 | |
| 163 | insertResult = pit.insert(interestA); |
| 164 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 165 | |
| 166 | insertResult = pit.insert(interestB); |
| 167 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 168 | |
| 169 | insertResult = pit.insert(interestC); |
| 170 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 171 | |
| 172 | insertResult = pit.insert(interestD); |
| 173 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 174 | |
| 175 | insertResult = pit.insert(interestE); |
| 176 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 177 | |
| 178 | insertResult = pit.insert(interestF); |
| 179 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 180 | |
| 181 | insertResult = pit.insert(interestG); |
| 182 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 183 | |
| 184 | insertResult = pit.insert(interestH); |
| 185 | BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ |
| 186 | |
| 187 | insertResult = pit.insert(interestI); |
| 188 | BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ |
| 189 | |
| 190 | insertResult = pit.insert(interestJ); |
| 191 | BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ |
| 192 | |
| 193 | insertResult = pit.insert(interestK); |
| 194 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE(Remove) |
| 198 | { |
| 199 | Interest interest(Name("ndn:/z88Admz6A2")); |
| 200 | |
| 201 | Pit pit; |
| 202 | std::pair<shared_ptr<pit::Entry>, bool> insertResult; |
| 203 | |
| 204 | insertResult = pit.insert(interest); |
| 205 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 206 | |
| 207 | insertResult = pit.insert(interest); |
| 208 | BOOST_CHECK_EQUAL(insertResult.second, false); |
| 209 | |
| 210 | pit.remove(insertResult.first); |
| 211 | |
| 212 | insertResult = pit.insert(interest); |
| 213 | BOOST_CHECK_EQUAL(insertResult.second, true); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | BOOST_AUTO_TEST_CASE(FindAllDataMatches) |
| 218 | { |
| 219 | Name nameA ("ndn:/A"); |
| 220 | Name nameAB ("ndn:/A/B"); |
| 221 | Name nameABC("ndn:/A/B/C"); |
| 222 | Name nameD ("ndn:/D"); |
| 223 | Interest interestA (nameA ); |
| 224 | Interest interestAB(nameAB); |
| 225 | Interest interestD (nameD ); |
| 226 | |
| 227 | Pit pit; |
| 228 | pit.insert(interestA ); |
| 229 | pit.insert(interestAB); |
| 230 | pit.insert(interestD ); |
| 231 | |
| 232 | Data data(nameABC); |
| 233 | |
| 234 | shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data); |
| 235 | int count = 0; |
| 236 | bool hasA = false; |
| 237 | bool hasAB = false; |
| 238 | bool hasD = false; |
| 239 | for (pit::DataMatchResult::iterator it = matches->begin(); |
| 240 | it != matches->end(); ++it) { |
| 241 | ++count; |
| 242 | shared_ptr<pit::Entry> entry = *it; |
| 243 | if (entry->getName().equals(nameA )) { |
| 244 | hasA = true; |
| 245 | } |
| 246 | if (entry->getName().equals(nameAB)) { |
| 247 | hasAB = true; |
| 248 | } |
| 249 | if (entry->getName().equals(nameD )) { |
| 250 | hasD = true; |
| 251 | } |
| 252 | } |
| 253 | BOOST_CHECK_EQUAL(count, 2); |
| 254 | BOOST_CHECK_EQUAL(hasA , true); |
| 255 | BOOST_CHECK_EQUAL(hasAB, true); |
| 256 | BOOST_CHECK_EQUAL(hasD , false); |
| 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_SUITE_END() |
| 260 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 261 | } // namespace nfd |