client_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2011 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 <sys/socket.h>
  15. #include <string.h>
  16. #include <string>
  17. #include <sstream>
  18. #include <boost/scoped_ptr.hpp>
  19. #include <acl/ip_check.h>
  20. #include <asiolink/io_address.h>
  21. #include <asiolink/io_socket.h>
  22. #include <asiolink/io_message.h>
  23. #include <server_common/client.h>
  24. #include <gtest/gtest.h>
  25. using namespace boost;
  26. using namespace isc::acl;
  27. using namespace isc::asiolink;
  28. using namespace isc::server_common;
  29. namespace {
  30. class ClientTest : public ::testing::Test {
  31. protected:
  32. ClientTest() {
  33. endpoint4.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"),
  34. 53214));
  35. endpoint6.reset(IOEndpoint::create(IPPROTO_TCP,
  36. IOAddress("2001:db8::1"), 53216));
  37. request4.reset(new IOMessage(NULL, 0, IOSocket::getDummyUDPSocket(),
  38. *endpoint4));
  39. request6.reset(new IOMessage(NULL, 0, IOSocket::getDummyTCPSocket(),
  40. *endpoint6));
  41. client4.reset(new Client(*request4));
  42. client6.reset(new Client(*request6));
  43. }
  44. scoped_ptr<const IOEndpoint> endpoint4;
  45. scoped_ptr<const IOEndpoint> endpoint6;
  46. scoped_ptr<const IOMessage> request4;
  47. scoped_ptr<const IOMessage> request6;
  48. scoped_ptr<const Client> client4;
  49. scoped_ptr<const Client> client6;
  50. };
  51. TEST_F(ClientTest, constructIPv4) {
  52. EXPECT_EQ(AF_INET, client4->getRequestSourceEndpoint().getFamily());
  53. EXPECT_EQ(IPPROTO_UDP, client4->getRequestSourceEndpoint().getProtocol());
  54. EXPECT_EQ("192.0.2.1",
  55. client4->getRequestSourceEndpoint().getAddress().toText());
  56. EXPECT_EQ(53214, client4->getRequestSourceEndpoint().getPort());
  57. const uint8_t expected_data[] = { 192, 0, 2, 1 };
  58. EXPECT_EQ(AF_INET, client4->getRequestSourceIPAddress().getFamily());
  59. ASSERT_EQ(4, client4->getRequestSourceIPAddress().getLength());
  60. EXPECT_EQ(0, memcmp(expected_data,
  61. client4->getRequestSourceIPAddress().getData(), 4));
  62. }
  63. TEST_F(ClientTest, constructIPv6) {
  64. EXPECT_EQ(AF_INET6, client6->getRequestSourceEndpoint().getFamily());
  65. EXPECT_EQ(IPPROTO_TCP, client6->getRequestSourceEndpoint().getProtocol());
  66. EXPECT_EQ("2001:db8::1",
  67. client6->getRequestSourceEndpoint().getAddress().toText());
  68. EXPECT_EQ(53216, client6->getRequestSourceEndpoint().getPort());
  69. const uint8_t expected_data[] = { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x01 };
  72. EXPECT_EQ(AF_INET6, client6->getRequestSourceIPAddress().getFamily());
  73. ASSERT_EQ(16, client6->getRequestSourceIPAddress().getLength());
  74. EXPECT_EQ(0, memcmp(expected_data,
  75. client6->getRequestSourceIPAddress().getData(), 16));
  76. }
  77. TEST_F(ClientTest, toText) {
  78. EXPECT_EQ("192.0.2.1#53214", client4->toText());
  79. EXPECT_EQ("2001:db8::1#53216", client6->toText());
  80. }
  81. // test operator<<. We simply confirm it appends the result of toText().
  82. TEST_F(ClientTest, LeftShiftOperator) {
  83. std::ostringstream oss;
  84. oss << *client4 << "more text";
  85. EXPECT_EQ(client4->toText() + std::string("more text"), oss.str());
  86. }
  87. }