nameserver_address_unittest.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <gtest/gtest.h>
  16. #include "name.h"
  17. #include "nameserver_address.h"
  18. #include "rdata.h"
  19. #include "rrclass.h"
  20. #include "rrset.h"
  21. #include "rrttl.h"
  22. #include "nsas_test.h"
  23. namespace isc {
  24. namespace nsas {
  25. using namespace dns;
  26. using namespace rdata;
  27. #define TEST_ADDRESS_INDEX 1
  28. /// \brief NameserverEntry sample class for testing
  29. class NameserverEntrySample {
  30. public:
  31. NameserverEntrySample():
  32. rrv4_(Name("example.org"), RRClass::IN(), RRType::A(), RRTTL(1200))
  33. {
  34. // Add some sample A records
  35. rrv4_.addRdata(ConstRdataPtr(new RdataTest<A>("1.2.3.4")));
  36. rrv4_.addRdata(ConstRdataPtr(new RdataTest<A>("5.6.7.8")));
  37. rrv4_.addRdata(ConstRdataPtr(new RdataTest<A>("9.10.11.12")));
  38. ns_.reset(new NameserverEntry(&rrv4_, NULL));
  39. }
  40. // Return the sample NameserverEntry
  41. boost::shared_ptr<NameserverEntry>& getNameserverEntry() { return ns_; }
  42. // Return the IOAddress corresponding to the index in rrv4_
  43. asiolink::IOAddress getAddressAtIndex(uint32_t index) { return ns_.get()->getAddressAtIndex(index); }
  44. // Return the RTT of the address
  45. uint32_t getAddressRTTAtIndex(uint32_t index) {
  46. NameserverEntry::AddressVector addresses;
  47. ns_.get()->getAddresses(addresses);
  48. return addresses[index].getRTT();
  49. }
  50. private:
  51. BasicRRset rrv4_; ///< Standard RRSet - IN, A, lowercase name
  52. boost::shared_ptr<NameserverEntry> ns_; ///< Shared_ptr that points to a NameserverEntry object
  53. };
  54. /// \brief Test Fixture Class
  55. class NameserverAddressTest : public ::testing::Test {
  56. protected:
  57. // Constructor
  58. NameserverAddressTest():
  59. ns_address_(ns_sample_.getNameserverEntry(), TEST_ADDRESS_INDEX)
  60. {
  61. }
  62. NameserverEntrySample ns_sample_;
  63. NameserverAddress ns_address_;
  64. };
  65. // Test that the address is equal to the address in NameserverEntry
  66. TEST_F(NameserverAddressTest, Address) {
  67. EXPECT_TRUE(ns_address_.getAddress().equal( ns_sample_.getAddressAtIndex(TEST_ADDRESS_INDEX)));
  68. }
  69. // Test that the RTT is updated
  70. TEST_F(NameserverAddressTest, UpdateRTT) {
  71. uint32_t old_rtt = ns_sample_.getAddressRTTAtIndex(TEST_ADDRESS_INDEX);
  72. uint32_t new_rtt = old_rtt + 1;
  73. ns_address_.updateRTT(new_rtt);
  74. EXPECT_EQ(new_rtt, ns_sample_.getAddressRTTAtIndex(TEST_ADDRESS_INDEX));
  75. }
  76. } // namespace nsas
  77. } // namespace isc