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