address_entry_unittest.cc 3.4 KB

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