address_entry_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <limits.h>
  16. #include <gtest/gtest.h>
  17. // Need to define the following macro to get UINT32_MAX
  18. #ifndef __STDC_LIMIT_MACROS
  19. #define __STDC_LIMIT_MACROS
  20. #endif
  21. #include <stdint.h>
  22. #include "../asiolink.h"
  23. #include "../address_entry.h"
  24. static std::string V4A_TEXT("1.2.3.4");
  25. static std::string V4B_TEXT("5.6.7.8");
  26. static std::string V6A_TEXT("2001:dead:beef::0");
  27. static std::string V6B_TEXT("1984:1985::1986:1987");
  28. using namespace asiolink;
  29. using namespace std;
  30. using namespace isc::nsas;
  31. /// \brief Test Fixture Class
  32. ///
  33. /// Constructs four IOAddress objects, two V4 addresses and two V6 addresses.
  34. /// They are initialised in the constructor owing to the restrictions of
  35. /// the IOAddress class.
  36. class AddressEntryTest : public ::testing::Test {
  37. protected:
  38. AddressEntryTest() :
  39. v4a_(V4A_TEXT), v4b_(V4B_TEXT),
  40. v6a_(V6A_TEXT), v6b_(V6B_TEXT)
  41. {}
  42. IOAddress v4a_; ///< First V4 address
  43. IOAddress v4b_; ///< Second V4 address
  44. IOAddress v6a_; ///< First V6 address
  45. IOAddress v6b_; ///< Second V6 address
  46. };
  47. /// Tests of the various constructors
  48. TEST_F(AddressEntryTest, Constructor) {
  49. // Basic constructor with V4 address
  50. AddressEntry alpha(v4a_);
  51. EXPECT_EQ(V4A_TEXT, alpha.getAddress().toText());
  52. EXPECT_EQ(0, alpha.getRTT());
  53. // General constructor with V4 address
  54. AddressEntry beta(v4b_, 42);
  55. EXPECT_EQ(V4B_TEXT, beta.getAddress().toText());
  56. EXPECT_EQ(42, beta.getRTT());
  57. // Basic constructor with V6 address
  58. AddressEntry gamma(v6a_);
  59. EXPECT_EQ(V6A_TEXT, gamma.getAddress().toText());
  60. EXPECT_EQ(0, gamma.getRTT());
  61. // General constructor with V6 address
  62. AddressEntry delta(v6b_, 75);
  63. EXPECT_EQ(V6B_TEXT, delta.getAddress().toText());
  64. EXPECT_EQ(75, delta.getRTT());
  65. }
  66. /// Setting and getting the round-trip time. Also includes unreachable
  67. /// checks.
  68. TEST_F(AddressEntryTest, RTT) {
  69. AddressEntry alpha(v4a_);
  70. EXPECT_EQ(0, alpha.getRTT());
  71. // Check set and get of RTT
  72. long rtt = 1276;
  73. alpha.setRTT(rtt);
  74. EXPECT_EQ(rtt, alpha.getRTT());
  75. // Check unreachability
  76. alpha.setUnreachable();
  77. EXPECT_TRUE(alpha.isUnreachable());
  78. alpha.setRTT(27);
  79. EXPECT_FALSE(alpha.isUnreachable());
  80. // ... and check the implementation of unreachability
  81. alpha.setUnreachable();
  82. EXPECT_EQ(AddressEntry::UNREACHABLE, alpha.getRTT());
  83. }
  84. /// Checking the address type.
  85. TEST_F(AddressEntryTest, AddressType) {
  86. AddressEntry alpha(v4a_);
  87. EXPECT_TRUE(alpha.isV4());
  88. EXPECT_FALSE(alpha.isV6());
  89. AddressEntry beta(v6a_);
  90. EXPECT_FALSE(beta.isV4());
  91. EXPECT_TRUE(beta.isV6());
  92. }