nameserver_address_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <config.h>
  15. #include <gtest/gtest.h>
  16. #include <dns/name.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrset.h>
  20. #include <dns/rrttl.h>
  21. #include "../nameserver_address.h"
  22. #include "../nameserver_entry.h"
  23. #include "nsas_test.h"
  24. namespace isc {
  25. namespace nsas {
  26. using namespace dns;
  27. using namespace rdata;
  28. #define TEST_ADDRESS_INDEX 1
  29. /// \brief NameserverEntry sample class for testing
  30. class NameserverEntrySample {
  31. public:
  32. NameserverEntrySample():
  33. name_("example.org"),
  34. rrv4_(new RRset(name_, RRClass::IN(), RRType::A(), RRTTL(1200))),
  35. ns_(new NameserverEntry(name_.toText(), RRClass::IN())),
  36. resolver_(new TestResolver())
  37. {
  38. // Add some sample A records
  39. rrv4_->addRdata(ConstRdataPtr(new RdataTest<A>("1.2.3.4")));
  40. rrv4_->addRdata(ConstRdataPtr(new RdataTest<A>("5.6.7.8")));
  41. rrv4_->addRdata(ConstRdataPtr(new RdataTest<A>("9.10.11.12")));
  42. ns_.reset(new NameserverEntry(name_.toText(), RRClass::IN()));
  43. ns_->askIP(resolver_.get(), boost::shared_ptr<Callback>(new Callback), ANY_OK);
  44. resolver_->asksIPs(name_, 0, 1);
  45. resolver_->requests[0].second->success(
  46. isc::util::unittests::createResponseMessage(rrv4_));
  47. }
  48. // Return the sample NameserverEntry
  49. boost::shared_ptr<NameserverEntry>& getNameserverEntry() { return ns_; }
  50. // Return the IOAddress corresponding to the index in rrv4_
  51. isc::asiolink::IOAddress getAddressAtIndex(uint32_t index) {
  52. return ns_.get()->getAddressAtIndex(index, V4_ONLY);
  53. }
  54. // Return the addresses count stored in RRset
  55. unsigned int getAddressesCount() const { return rrv4_->getRdataCount(); }
  56. // Return the RTT of the address
  57. uint32_t getAddressRTTAtIndex(uint32_t index) {
  58. NameserverEntry::AddressVector addresses;
  59. ns_.get()->getAddresses(addresses);
  60. return (addresses[index].getAddressEntry().getRTT());
  61. }
  62. private:
  63. Name name_; ///< Name of the sample
  64. RRsetPtr rrv4_; ///< Standard RRSet - IN, A, lowercase name
  65. boost::shared_ptr<NameserverEntry> ns_; ///< Shared_ptr that points to a NameserverEntry object
  66. boost::shared_ptr<TestResolver> resolver_;
  67. class Callback : public NameserverEntry::Callback {
  68. public:
  69. virtual void operator()(boost::shared_ptr<NameserverEntry>) { }
  70. };
  71. };
  72. /// \brief Test Fixture Class
  73. class NameserverAddressTest : public ::testing::Test {
  74. protected:
  75. // Constructor
  76. NameserverAddressTest():
  77. ns_address_(ns_sample_.getNameserverEntry(),
  78. ns_sample_.getNameserverEntry()->getAddressAtIndex(
  79. TEST_ADDRESS_INDEX, V4_ONLY), V4_ONLY)
  80. {
  81. }
  82. NameserverEntrySample ns_sample_;
  83. // Valid NameserverAddress object
  84. NameserverAddress ns_address_;
  85. };
  86. // Test that the address is equal to the address in NameserverEntry
  87. TEST_F(NameserverAddressTest, Address) {
  88. EXPECT_TRUE(ns_address_.getAddress().equals( ns_sample_.getAddressAtIndex(TEST_ADDRESS_INDEX)));
  89. boost::shared_ptr<NameserverEntry> empty_ne((NameserverEntry*)NULL);
  90. // It will throw an NullNameserverEntryPointer exception with the empty NameserverEntry shared pointer
  91. ASSERT_THROW({NameserverAddress empty_ns_address(empty_ne,
  92. isc::asiolink::IOAddress("127.0.0.1"), V4_ONLY);},
  93. NullNameserverEntryPointer);
  94. }
  95. // Test that the RTT is updated
  96. TEST_F(NameserverAddressTest, UpdateRTT) {
  97. uint32_t old_rtt = ns_sample_.getAddressRTTAtIndex(TEST_ADDRESS_INDEX);
  98. uint32_t new_rtt = old_rtt + 10;
  99. uint32_t old_rtt0 = ns_sample_.getAddressRTTAtIndex(0);
  100. uint32_t old_rtt2 = ns_sample_.getAddressRTTAtIndex(2);
  101. for(int i = 0; i < 10000; ++i){
  102. ns_address_.updateRTT(new_rtt);
  103. }
  104. //The RTT should have been updated
  105. EXPECT_NE(new_rtt, ns_sample_.getAddressRTTAtIndex(TEST_ADDRESS_INDEX));
  106. //The RTTs not been updated should remain unchanged
  107. EXPECT_EQ(old_rtt0, ns_sample_.getAddressRTTAtIndex(0));
  108. EXPECT_EQ(old_rtt2, ns_sample_.getAddressRTTAtIndex(2));
  109. }
  110. } // namespace nsas
  111. } // namespace isc