nameserver_entry_unittest.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <limits.h>
  18. #include <boost/foreach.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <gtest/gtest.h>
  21. #include <dns/rdata.h>
  22. #include <dns/rrset.h>
  23. #include <dns/rrclass.h>
  24. #include <dns/rdataclass.h>
  25. #include <dns/rrttl.h>
  26. #include <dns/name.h>
  27. #include <exceptions/exceptions.h>
  28. #include "../asiolink.h"
  29. #include "../address_entry.h"
  30. #include "../nameserver_entry.h"
  31. #include "../nameserver_address.h"
  32. #include "../zone_entry.h"
  33. #include "nsas_test.h"
  34. using namespace isc::nsas;
  35. using namespace asiolink;
  36. using namespace std;
  37. using namespace isc::dns;
  38. using namespace rdata;
  39. using namespace boost;
  40. namespace {
  41. /// \brief Test Fixture Class
  42. class NameserverEntryTest : public TestWithRdata {
  43. protected:
  44. /// \short Just a really stupid callback counting times called
  45. struct Callback : public NameserverEntry::Callback {
  46. size_t count;
  47. virtual void operator()(shared_ptr<NameserverEntry>) {
  48. count ++;
  49. }
  50. Callback() : count(0) { }
  51. };
  52. private:
  53. /**
  54. * \short Fills an rrset into the NameserverEntry trough resolver.
  55. *
  56. * This function is used when we want to pass data to a NameserverEntry
  57. * trough the resolver.
  58. * \param resolver The resolver used by the NameserverEntry
  59. * \param index Index of the query in the resolver.
  60. * \param set The answer. If the pointer is empty, it is taken
  61. * as a failure.
  62. */
  63. void fillSet(shared_ptr<TestResolver> resolver, size_t index,
  64. shared_ptr<BasicRRset> set)
  65. {
  66. if (set) {
  67. resolver->requests[index].second->success(set);
  68. } else {
  69. resolver->requests[index].second->failure();
  70. }
  71. }
  72. protected:
  73. /// Fills the nameserver entry with data trough ask IP
  74. void fillNSEntry(shared_ptr<NameserverEntry> entry,
  75. shared_ptr<BasicRRset> rrv4, shared_ptr<BasicRRset> rrv6)
  76. {
  77. // Prepare data to run askIP
  78. shared_ptr<TestResolver> resolver(new TestResolver);
  79. shared_ptr<Callback> callback(new Callback);
  80. // Let it ask for data
  81. entry->askIP(resolver, callback, ANY_OK);
  82. // Check it really asked and sort the queries
  83. EXPECT_TRUE(resolver->asksIPs(Name(entry->getName()), 0, 1));
  84. // Respond with answers
  85. fillSet(resolver, 0, rrv4);
  86. fillSet(resolver, 1, rrv6);
  87. }
  88. };
  89. /// Tests of the default constructor
  90. TEST_F(NameserverEntryTest, DefaultConstructor) {
  91. // Default constructor should not create any RRsets
  92. NameserverEntry alpha(EXAMPLE_CO_UK, RRClass::IN());
  93. EXPECT_EQ(EXAMPLE_CO_UK, alpha.getName());
  94. EXPECT_EQ(RRClass::IN(), alpha.getClass());
  95. // Also check that no addresses have been created.
  96. NameserverEntry::AddressVector addresses;
  97. alpha.getAddresses(addresses);
  98. EXPECT_TRUE(addresses.empty());
  99. }
  100. // Test the the RTT on tthe created addresses is not 0 and is different
  101. TEST_F(NameserverEntryTest, InitialRTT) {
  102. // Get the RTT for the different addresses
  103. shared_ptr<NameserverEntry> alpha(new NameserverEntry(EXAMPLE_CO_UK,
  104. RRClass::IN()));
  105. fillNSEntry(alpha, rrv4_, rrv6_);
  106. NameserverEntry::AddressVector vec;
  107. alpha->getAddresses(vec);
  108. // Check they are not 0 and they are all small, they should be some kind
  109. // of randomish numbers, so we can't expect much more here
  110. BOOST_FOREACH(NameserverAddress& entry, vec) {
  111. EXPECT_GT(entry.getAddressEntry().getRTT(), 0);
  112. // 20 is some arbitrary small value
  113. EXPECT_LT(entry.getAddressEntry().getRTT(), 20);
  114. }
  115. }
  116. // Set an address RTT to a given value
  117. TEST_F(NameserverEntryTest, SetRTT) {
  118. // Get the RTT for the different addresses
  119. shared_ptr<NameserverEntry> alpha(new NameserverEntry(EXAMPLE_CO_UK,
  120. RRClass::IN()));
  121. fillNSEntry(alpha, rrv4_, rrv6_);
  122. NameserverEntry::AddressVector vec;
  123. alpha->getAddresses(vec);
  124. ASSERT_TRUE(vec.size() > 0);
  125. // Take the first address and change the RTT.
  126. IOAddress first_address = vec[0].getAddress();
  127. uint32_t first_rtt = vec[0].getAddressEntry().getRTT();
  128. uint32_t new_rtt = first_rtt + 42;
  129. alpha->setAddressRTT(first_address, new_rtt);
  130. // Now see if it has changed
  131. NameserverEntry::AddressVector newvec;
  132. alpha->getAddresses(newvec);
  133. int matchcount = 0;
  134. for (NameserverEntry::AddressVectorIterator i = newvec.begin();
  135. i != newvec.end(); ++i) {
  136. if (i->getAddress().equal(first_address)) {
  137. ++matchcount;
  138. EXPECT_EQ(i->getAddressEntry().getRTT(), new_rtt);
  139. }
  140. }
  141. // ... and make sure there was only one match in the set we retrieved
  142. EXPECT_EQ(1, matchcount);
  143. }
  144. // Set an address RTT to be unreachable
  145. TEST_F(NameserverEntryTest, Unreachable) {
  146. // Get the RTT for the different addresses
  147. shared_ptr<NameserverEntry> alpha(new NameserverEntry(EXAMPLE_CO_UK,
  148. RRClass::IN()));
  149. fillNSEntry(alpha, rrv4_, rrv6_);
  150. NameserverEntry::AddressVector vec;
  151. alpha->getAddresses(vec);
  152. ASSERT_TRUE(vec.size() > 0);
  153. // Take the first address and mark as unreachable.
  154. IOAddress first_address = vec[0].getAddress();
  155. EXPECT_FALSE(vec[0].getAddressEntry().isUnreachable());
  156. alpha->setAddressUnreachable(first_address);
  157. // Now see if it has changed
  158. NameserverEntry::AddressVector newvec;
  159. alpha->getAddresses(newvec);
  160. int matchcount = 0;
  161. for (NameserverEntry::AddressVectorIterator i = newvec.begin();
  162. i != newvec.end(); ++i) {
  163. if (i->getAddress().equal(first_address)) {
  164. ++matchcount;
  165. EXPECT_TRUE(i->getAddressEntry().isUnreachable());
  166. }
  167. }
  168. // ... and make sure there was only one match in the set we retrieved
  169. EXPECT_EQ(1, matchcount);
  170. }
  171. // Test that the expiration time of records is set correctly.
  172. //
  173. // Note that for testing purposes we use the three-argument NameserverEntry
  174. // constructor (where we supply the time). It eliminates intermittent errors
  175. // cause when this test is run just as the clock "ticks over" to another second.
  176. // TODO Return the way where we can pass time inside somehow
  177. TEST_F(NameserverEntryTest, ExpirationTime) {
  178. time_t curtime = time(NULL);
  179. time_t expiration = 0;
  180. // Test where there is a single TTL
  181. shared_ptr<NameserverEntry> alpha(new NameserverEntry(EXAMPLE_CO_UK,
  182. RRClass::IN()));
  183. fillNSEntry(alpha, rrv4_, shared_ptr<BasicRRset>());
  184. expiration = alpha->getExpiration();
  185. EXPECT_EQ(expiration, curtime + rrv4_->getTTL().getValue());
  186. shared_ptr<NameserverEntry> beta(new NameserverEntry(EXAMPLE_CO_UK,
  187. RRClass::IN()));
  188. fillNSEntry(beta, shared_ptr<BasicRRset>(), rrv6_);
  189. expiration = beta->getExpiration();
  190. EXPECT_EQ(expiration, curtime + rrv6_->getTTL().getValue());
  191. // Test where there are two different TTLs
  192. EXPECT_NE(rrv4_->getTTL().getValue(), rrv6_->getTTL().getValue());
  193. shared_ptr<NameserverEntry> gamma(new NameserverEntry(EXAMPLE_CO_UK,
  194. RRClass::IN()));
  195. fillNSEntry(gamma, rrv4_, rrv6_);
  196. uint32_t minttl = min(rrv4_->getTTL().getValue(), rrv6_->getTTL().getValue());
  197. expiration = gamma->getExpiration();
  198. EXPECT_EQ(expiration, curtime + minttl);
  199. // Finally check where we don't specify a current time. All we know is
  200. // that the expiration time should be greater than the TTL (as the current
  201. // time is greater than zero).
  202. shared_ptr<NameserverEntry> delta(new NameserverEntry(EXAMPLE_CO_UK,
  203. RRClass::IN()));
  204. fillNSEntry(delta, rrv4_, shared_ptr<BasicRRset>());
  205. EXPECT_GT(delta->getExpiration(), rrv4_->getTTL().getValue());
  206. }
  207. // Test that the name of this nameserver is set correctly.
  208. TEST_F(NameserverEntryTest, CheckName) {
  209. // Default constructor
  210. NameserverEntry alpha(EXAMPLE_CO_UK, RRClass::IN());
  211. EXPECT_EQ(EXAMPLE_CO_UK, alpha.getName());
  212. }
  213. // Check that it can cope with non-IN classes.
  214. TEST_F(NameserverEntryTest, CheckClass) {
  215. // Default constructor
  216. NameserverEntry alpha(EXAMPLE_CO_UK, RRClass::CH());
  217. EXPECT_EQ(RRClass::CH(), alpha.getClass());
  218. }
  219. // Tests if it asks the IP addresses and calls callbacks when it comes
  220. // including the right addresses are returned and that they expire
  221. TEST_F(NameserverEntryTest, IPCallbacks) {
  222. shared_ptr<NameserverEntry> entry(new NameserverEntry(EXAMPLE_CO_UK,
  223. RRClass::IN()));
  224. shared_ptr<Callback> callback(new Callback);
  225. shared_ptr<TestResolver> resolver(new TestResolver);
  226. entry->askIP(resolver, callback, ANY_OK);
  227. // Ensure it becomes IN_PROGRESS
  228. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  229. // Now, there should be two queries in the resolver
  230. EXPECT_EQ(2, resolver->requests.size());
  231. ASSERT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 0, 1));
  232. // Another one might ask
  233. entry->askIP(resolver, callback, V4_ONLY);
  234. // There should still be only two queries in the resolver
  235. ASSERT_EQ(2, resolver->requests.size());
  236. // Another one, with need of IPv6 address
  237. entry->askIP(resolver, callback, V6_ONLY);
  238. // Answer one and see that the callbacks are called
  239. resolver->answer(0, Name(EXAMPLE_CO_UK), RRType::A(),
  240. rdata::in::A("192.0.2.1"));
  241. // Both callbacks that want IPv4 should be called by now
  242. EXPECT_EQ(2, callback->count);
  243. // It should contain one IP address
  244. NameserverEntry::AddressVector addresses;
  245. // Still in progress, waiting for the other address
  246. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getAddresses(addresses));
  247. EXPECT_EQ(1, addresses.size());
  248. // Answer IPv6 address
  249. // It is with zero TTL, so it should expire right away
  250. resolver->answer(1, Name(EXAMPLE_CO_UK), RRType::AAAA(),
  251. rdata::in::AAAA("2001:db8::1"), 0);
  252. // The other callback should appear
  253. EXPECT_EQ(3, callback->count);
  254. // It should return the one address. It should be expired, but
  255. // we ignore it for now
  256. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V6_ONLY, true));
  257. // Another address should appear
  258. EXPECT_EQ(2, addresses.size());
  259. // But when we do not ignore it, it should not appear
  260. EXPECT_EQ(Fetchable::EXPIRED, entry->getAddresses(addresses, V6_ONLY));
  261. EXPECT_EQ(2, addresses.size());
  262. }
  263. // Test the callback is called even when the address is unreachable
  264. TEST_F(NameserverEntryTest, IPCallbacksUnreachable) {
  265. shared_ptr<NameserverEntry> entry(new NameserverEntry(EXAMPLE_CO_UK,
  266. RRClass::IN()));
  267. shared_ptr<Callback> callback(new Callback);
  268. shared_ptr<TestResolver> resolver(new TestResolver);
  269. // Ask for its IP
  270. entry->askIP(resolver, callback, ANY_OK);
  271. // Check it asks the resolver
  272. EXPECT_EQ(2, resolver->requests.size());
  273. ASSERT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 0, 1));
  274. resolver->requests[0].second->failure();
  275. // It should still wait for the second one
  276. EXPECT_EQ(0, callback->count);
  277. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  278. // It should call the callback now and be unrechable
  279. resolver->requests[1].second->failure();
  280. EXPECT_EQ(1, callback->count);
  281. EXPECT_EQ(Fetchable::UNREACHABLE, entry->getState());
  282. NameserverEntry::AddressVector addresses;
  283. EXPECT_EQ(Fetchable::UNREACHABLE, entry->getAddresses(addresses));
  284. EXPECT_EQ(0, addresses.size());
  285. }
  286. /*
  287. * Tests that it works even when we provide the answer right away, directly
  288. * from resolve.
  289. */
  290. TEST_F(NameserverEntryTest, DirectAnswer) {
  291. shared_ptr<NameserverEntry> entry(new NameserverEntry(EXAMPLE_CO_UK,
  292. RRClass::IN()));
  293. shared_ptr<Callback> callback(new Callback);
  294. shared_ptr<TestResolver> resolver(new TestResolver);
  295. resolver->addPresetAnswer(Question(Name(EXAMPLE_CO_UK), RRClass::IN(),
  296. RRType::A()), rrv4_);
  297. resolver->addPresetAnswer(Question(Name(EXAMPLE_CO_UK), RRClass::IN(),
  298. RRType::AAAA()), rrv6_);
  299. resolver->addPresetAnswer(Question(Name(EXAMPLE_NET), RRClass::IN(),
  300. RRType::A()), shared_ptr<AbstractRRset>());
  301. resolver->addPresetAnswer(Question(Name(EXAMPLE_NET), RRClass::IN(),
  302. RRType::AAAA()), shared_ptr<AbstractRRset>());
  303. // A successfull test first
  304. entry->askIP(resolver, callback, ANY_OK);
  305. EXPECT_EQ(0, resolver->requests.size());
  306. EXPECT_EQ(1, callback->count);
  307. NameserverEntry::AddressVector addresses;
  308. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses));
  309. EXPECT_EQ(5, addresses.size());
  310. // An unsuccessfull test
  311. callback->count = 0;
  312. entry.reset(new NameserverEntry(EXAMPLE_NET, RRClass::IN()));
  313. entry->askIP(resolver, callback, ANY_OK);
  314. EXPECT_EQ(0, resolver->requests.size());
  315. EXPECT_EQ(1, callback->count);
  316. addresses.clear();
  317. EXPECT_EQ(Fetchable::UNREACHABLE, entry->getAddresses(addresses));
  318. EXPECT_EQ(0, addresses.size());
  319. }
  320. /*
  321. * This one tests if it works when the data time out and a different
  322. * data is received the next time.
  323. */
  324. TEST_F(NameserverEntryTest, ChangedExpired) {
  325. shared_ptr<NameserverEntry> entry(new NameserverEntry(EXAMPLE_CO_UK,
  326. RRClass::IN()));
  327. shared_ptr<Callback> callback(new Callback);
  328. shared_ptr<TestResolver> resolver(new TestResolver);
  329. // Ask the first time
  330. entry->askIP(resolver, callback, V4_ONLY);
  331. entry->askIP(resolver, callback, V6_ONLY);
  332. EXPECT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 0, 1));
  333. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  334. resolver->answer(0, Name(EXAMPLE_CO_UK), RRType::A(),
  335. rdata::in::A("192.0.2.1"), 0);
  336. resolver->answer(1, Name(EXAMPLE_CO_UK), RRType::AAAA(),
  337. rdata::in::AAAA("2001:db8::1"), 0);
  338. EXPECT_EQ(2, callback->count);
  339. NameserverEntry::AddressVector addresses;
  340. // We must accept expired as well, as the TTL is 0 (and it is OK,
  341. // because we just got the callback)
  342. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V4_ONLY, true));
  343. ASSERT_EQ(1, addresses.size());
  344. EXPECT_EQ("192.0.2.1", addresses[0].getAddress().toText());
  345. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V6_ONLY, true));
  346. ASSERT_EQ(2, addresses.size());
  347. EXPECT_EQ("2001:db8::1", addresses[1].getAddress().toText());
  348. // Ask the second time. The callbacks should not fire right away and it
  349. // should request the addresses again
  350. entry->askIP(resolver, callback, V4_ONLY);
  351. entry->askIP(resolver, callback, V6_ONLY);
  352. EXPECT_EQ(2, callback->count);
  353. EXPECT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 2, 3));
  354. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  355. resolver->answer(0, Name(EXAMPLE_CO_UK), RRType::A(),
  356. rdata::in::A("192.0.2.2"));
  357. resolver->answer(1, Name(EXAMPLE_CO_UK), RRType::AAAA(),
  358. rdata::in::AAAA("2001:db8::2"));
  359. // We should get the new addresses and they should not expire,
  360. // so we should get them without accepting expired
  361. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V4_ONLY));
  362. ASSERT_EQ(3, addresses.size());
  363. EXPECT_EQ("192.0.2.2", addresses[2].getAddress().toText());
  364. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V6_ONLY));
  365. ASSERT_EQ(4, addresses.size());
  366. EXPECT_EQ("2001:db8::2", addresses[3].getAddress().toText());
  367. }
  368. /*
  369. * When the data expire and is asked again, the original RTT is kept.
  370. */
  371. TEST_F(NameserverEntryTest, KeepRTT) {
  372. shared_ptr<NameserverEntry> entry(new NameserverEntry(EXAMPLE_CO_UK,
  373. RRClass::IN()));
  374. shared_ptr<Callback> callback(new Callback);
  375. shared_ptr<TestResolver> resolver(new TestResolver);
  376. // Ask the first time
  377. entry->askIP(resolver, callback, V4_ONLY);
  378. entry->askIP(resolver, callback, V6_ONLY);
  379. EXPECT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 0, 1));
  380. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  381. resolver->answer(0, Name(EXAMPLE_CO_UK), RRType::A(),
  382. rdata::in::A("192.0.2.1"), 0);
  383. resolver->answer(1, Name(EXAMPLE_CO_UK), RRType::AAAA(),
  384. rdata::in::AAAA("2001:db8::1"), 0);
  385. EXPECT_EQ(2, callback->count);
  386. NameserverEntry::AddressVector addresses;
  387. // We must accept expired as well, as the TTL is 0 (and it is OK,
  388. // because we just got the callback)
  389. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V4_ONLY, true));
  390. ASSERT_EQ(1, addresses.size());
  391. EXPECT_EQ("192.0.2.1", addresses[0].getAddress().toText());
  392. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V6_ONLY, true));
  393. ASSERT_EQ(2, addresses.size());
  394. EXPECT_EQ("2001:db8::1", addresses[1].getAddress().toText());
  395. BOOST_FOREACH(const NameserverAddress& address, addresses) {
  396. entry->setAddressRTT(address.getAddress(), 123);
  397. }
  398. // Ask the second time. The callbacks should not fire right away and it
  399. // should request the addresses again
  400. entry->askIP(resolver, callback, V4_ONLY);
  401. entry->askIP(resolver, callback, V6_ONLY);
  402. EXPECT_EQ(2, callback->count);
  403. EXPECT_TRUE(resolver->asksIPs(Name(EXAMPLE_CO_UK), 2, 3));
  404. EXPECT_EQ(Fetchable::IN_PROGRESS, entry->getState());
  405. resolver->answer(0, Name(EXAMPLE_CO_UK), RRType::A(),
  406. rdata::in::A("192.0.2.1"));
  407. resolver->answer(1, Name(EXAMPLE_CO_UK), RRType::AAAA(),
  408. rdata::in::AAAA("2001:db8::1"));
  409. // We should get the new addresses and they should not expire,
  410. // so we should get them without accepting expired
  411. addresses.clear();
  412. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V4_ONLY));
  413. ASSERT_EQ(1, addresses.size());
  414. EXPECT_EQ("192.0.2.1", addresses[0].getAddress().toText());
  415. EXPECT_EQ(Fetchable::READY, entry->getAddresses(addresses, V6_ONLY));
  416. ASSERT_EQ(2, addresses.size());
  417. EXPECT_EQ("2001:db8::1", addresses[1].getAddress().toText());
  418. // They should have the RTT we set to them
  419. BOOST_FOREACH(NameserverAddress& address, addresses) {
  420. EXPECT_EQ(123, address.getAddressEntry().getRTT());
  421. }
  422. }
  423. // Test the RTT is updated smoothly
  424. TEST_F(NameserverEntryTest, UpdateRTT) {
  425. shared_ptr<NameserverEntry> ns(new NameserverEntry(EXAMPLE_CO_UK,
  426. RRClass::IN()));
  427. fillNSEntry(ns, rrv4_, rrv6_);
  428. NameserverEntry::AddressVector vec;
  429. ns->getAddresses(vec);
  430. // Initialize the rtt with a small value
  431. uint32_t init_rtt = 1;
  432. ns->setAddressRTT(vec[0].getAddress(), init_rtt);
  433. // The rtt will be stablized to a large value
  434. uint32_t stable_rtt = 100;
  435. // Update the rtt
  436. vec[0].updateRTT(stable_rtt);
  437. vec.clear();
  438. ns->getAddresses(vec);
  439. uint32_t new_rtt = vec[0].getAddressEntry().getRTT();
  440. // The rtt should not close to new rtt immediately
  441. EXPECT_TRUE((stable_rtt - new_rtt) > (new_rtt - init_rtt));
  442. // Update the rtt for enough times
  443. for(int i = 0; i < 10000; ++i){
  444. vec[0].updateRTT(stable_rtt);
  445. }
  446. vec.clear();
  447. ns->getAddresses(vec);
  448. new_rtt = vec[0].getAddressEntry().getRTT();
  449. // The rtt should be close to stable rtt value
  450. EXPECT_TRUE((stable_rtt - new_rtt) < (new_rtt - init_rtt));
  451. }
  452. } // namespace