sarr_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2014 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 <dhcp/tests/iface_mgr_test_config.h>
  16. #include <dhcp6/tests/dhcp6_test_utils.h>
  17. #include <dhcp6/tests/dhcp6_client.h>
  18. using namespace isc;
  19. using namespace isc::dhcp;
  20. using namespace isc::dhcp::test;
  21. using namespace isc::test;
  22. namespace {
  23. /// @brief Set of JSON configurations used by the SARR unit tests.
  24. ///
  25. /// - Configuration 0:
  26. /// - one subnet used on eth0 interface
  27. /// - prefixes of length 64, delegated from the pool: 2001:db8:3::/48
  28. const char* CONFIGS[] = {
  29. // Configuration 0
  30. "{ \"interfaces\": [ \"*\" ],"
  31. "\"preferred-lifetime\": 3000,"
  32. "\"rebind-timer\": 2000, "
  33. "\"renew-timer\": 1000, "
  34. "\"subnet6\": [ { "
  35. " \"pd-pools\": ["
  36. " { \"prefix\": \"2001:db8:3::\", "
  37. " \"prefix-len\": 48, "
  38. " \"delegated-len\": 64"
  39. " } ],"
  40. " \"subnet\": \"2001:db8::/32\", "
  41. " \"interface-id\": \"\","
  42. " \"interface\": \"eth0\""
  43. " } ],"
  44. "\"valid-lifetime\": 4000 }"
  45. };
  46. /// @brief Test fixture class for testing 4-way exchange: Solicit-Advertise,
  47. /// Request-Reply.
  48. class SARRTest : public Dhcpv6SrvTest {
  49. public:
  50. /// @brief Constructor.
  51. ///
  52. /// Sets up fake interfaces.
  53. SARRTest()
  54. : Dhcpv6SrvTest(),
  55. iface_mgr_test_config_(true) {
  56. }
  57. /// @brief Interface Manager's fake configuration control.
  58. IfaceMgrTestConfig iface_mgr_test_config_;
  59. };
  60. /// Check that server processes correctly a prefix hint sent by the client.
  61. /// This test checks that the server doesn't allocate colliding prefixes
  62. /// as a result of receiving hints from two clients which set the
  63. /// non-significant bytes of the prefix in their hints. The server should zero
  64. /// the non-significant bytes of the hint and allocate the prefix of the
  65. /// correct (configured) length.
  66. TEST_F(SARRTest, directClientPrefixHint) {
  67. Dhcp6Client client;
  68. // Configure client to request IA_PD.
  69. client.usePD();
  70. configure(CONFIGS[0], *client.getServer());
  71. // Make sure we ended-up having expected number of subnets configured.
  72. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  73. getCfgSubnets6()->getAll();
  74. ASSERT_EQ(1, subnets->size());
  75. // Append IAPREFIX option to the client's message.
  76. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:3:33::33"));
  77. // Perform 4-way exchange.
  78. ASSERT_NO_THROW(client.doSARR());
  79. // Server should have assigned a prefix.
  80. ASSERT_EQ(1, client.getLeaseNum());
  81. Lease6 lease_client = client.getLease(0);
  82. // The server should correctly deal with the least significant bytes
  83. // of the hint being set. It should set them to zero and use the
  84. // valid portion of the hint.
  85. EXPECT_EQ("2001:db8:3:33::", lease_client.addr_.toText());
  86. // Server ignores other parts of the IAPREFIX option.
  87. EXPECT_EQ(64, lease_client.prefixlen_);
  88. EXPECT_EQ(3000, lease_client.preferred_lft_);
  89. EXPECT_EQ(4000, lease_client.valid_lft_);
  90. Lease6Ptr lease_server = checkLease(lease_client);
  91. // Check that the server recorded the lease.
  92. ASSERT_TRUE(lease_server);
  93. // Remove existing lease and modify the DUID of the client to simulate
  94. // the case that different client is trying to get the prefix.
  95. client.clearConfig();
  96. client.modifyDUID();
  97. // Use the hint with some least significant bytes set.
  98. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:3:33::34"));
  99. ASSERT_NO_THROW(client.doSARR());
  100. // Server should assign a lease.
  101. ASSERT_EQ(1, client.getLeaseNum());
  102. lease_client = client.getLease(0);
  103. // The hint collides with the existing lease, so the server should not
  104. // assign for the second client.
  105. EXPECT_NE("2001:db8:3:33::", lease_client.addr_.toText());
  106. EXPECT_NE("2001:db8:3:33::34", lease_client.addr_.toText());
  107. // Check that the assigned prefix belongs to the pool.
  108. (*subnets)[0]->inPool(Lease::TYPE_PD, lease_client.addr_);
  109. EXPECT_EQ(64, lease_client.prefixlen_);
  110. EXPECT_EQ(3000, lease_client.preferred_lft_);
  111. EXPECT_EQ(4000, lease_client.valid_lft_);
  112. lease_server = checkLease(lease_client);
  113. ASSERT_TRUE(lease_server);
  114. }
  115. } // end of anonymous namespace