watch_socket_unittests.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <dhcp_ddns/watch_socket.h>
  15. #include <test_utils.h>
  16. #include <gtest/gtest.h>
  17. #include <sys/select.h>
  18. #include <sys/ioctl.h>
  19. using namespace std;
  20. using namespace isc;
  21. using namespace isc::dhcp_ddns;
  22. namespace {
  23. /// @brief Tests the basic functionality of WatchSocket.
  24. TEST(WatchSocketTest, basics) {
  25. WatchSocketPtr watch;
  26. /// Verify that we can construct a WatchSocket.
  27. ASSERT_NO_THROW(watch.reset(new WatchSocket()));
  28. ASSERT_TRUE(watch);
  29. /// Verify that post-construction the state the select-fd is valid.
  30. int select_fd = watch->getSelectFd();
  31. EXPECT_NE(select_fd, WatchSocket::INVALID_SOCKET);
  32. /// Verify that isReady() is false and that a call to select agrees.
  33. EXPECT_FALSE(watch->isReady());
  34. EXPECT_EQ(0, selectCheck(select_fd));
  35. /// Verify that the socket can be marked ready.
  36. ASSERT_NO_THROW(watch->markReady());
  37. /// Verify that we have exactly one marker waiting to be read.
  38. int count = 0;
  39. EXPECT_FALSE(ioctl(select_fd, FIONREAD, &count));
  40. EXPECT_EQ(sizeof(WatchSocket::MARKER), count);
  41. /// Verify that we can call markReady again without error.
  42. ASSERT_NO_THROW(watch->markReady());
  43. /// Verify that we STILL have exactly one marker waiting to be read.
  44. EXPECT_FALSE(ioctl(select_fd, FIONREAD, &count));
  45. EXPECT_EQ(sizeof(WatchSocket::MARKER), count);
  46. /// Verify that isReady() is true and that a call to select agrees.
  47. EXPECT_TRUE(watch->isReady());
  48. EXPECT_EQ(1, selectCheck(select_fd));
  49. /// Verify that the socket can be cleared.
  50. ASSERT_NO_THROW(watch->clearReady());
  51. /// Verify that isReady() is false and that a call to select agrees.
  52. EXPECT_FALSE(watch->isReady());
  53. EXPECT_EQ(0, selectCheck(select_fd));
  54. }
  55. /// @brief Checks behavior when select_fd is closed externally while in the
  56. /// "cleared" state.
  57. TEST(WatchSocketTest, closedWhileClear) {
  58. WatchSocketPtr watch;
  59. /// Verify that we can construct a WatchSocket.
  60. ASSERT_NO_THROW(watch.reset(new WatchSocket()));
  61. ASSERT_TRUE(watch);
  62. /// Verify that post-construction the state the select-fd is valid.
  63. int select_fd = watch->getSelectFd();
  64. ASSERT_NE(select_fd, WatchSocket::INVALID_SOCKET);
  65. // Verify that socket does not appear ready.
  66. ASSERT_EQ(0, watch->isReady());
  67. // Interfere by closing the fd.
  68. ASSERT_EQ(0, close(select_fd));
  69. // Verify that socket does not appear ready.
  70. ASSERT_EQ(0, watch->isReady());
  71. // Verify that clear does NOT throw.
  72. ASSERT_NO_THROW(watch->clearReady());
  73. // Verify that trying to mark it fails.
  74. ASSERT_THROW(watch->markReady(), WatchSocketError);
  75. // Verify that clear does NOT throw.
  76. ASSERT_NO_THROW(watch->clearReady());
  77. // Verify that getSelectFd() returns invalid socket.
  78. ASSERT_EQ(WatchSocket::INVALID_SOCKET, watch->getSelectFd());
  79. }
  80. /// @brief Checks behavior when select_fd has closed while in the "ready"
  81. /// state.
  82. TEST(WatchSocketTest, closedWhileReady) {
  83. WatchSocketPtr watch;
  84. /// Verify that we can construct a WatchSocket.
  85. ASSERT_NO_THROW(watch.reset(new WatchSocket()));
  86. ASSERT_TRUE(watch);
  87. /// Verify that post-construction the state the select-fd is valid.
  88. int select_fd = watch->getSelectFd();
  89. ASSERT_NE(select_fd, WatchSocket::INVALID_SOCKET);
  90. /// Verify that the socket can be marked ready.
  91. ASSERT_NO_THROW(watch->markReady());
  92. EXPECT_EQ(1, selectCheck(select_fd));
  93. // Interfere by closing the fd.
  94. ASSERT_EQ(0, close(select_fd));
  95. // Verify that trying to clear it does not throw.
  96. ASSERT_NO_THROW(watch->clearReady());
  97. // Verify the select_fd fails as socket is invalid/closed.
  98. EXPECT_EQ(-1, selectCheck(select_fd));
  99. // Verify that subsequent attempts to mark it will fail.
  100. ASSERT_THROW(watch->markReady(), WatchSocketError);
  101. }
  102. /// @brief Checks behavior when select_fd has been marked ready but then
  103. /// emptied by an external read.
  104. TEST(WatchSocketTest, emptyReadySelectFd) {
  105. WatchSocketPtr watch;
  106. /// Verify that we can construct a WatchSocket.
  107. ASSERT_NO_THROW(watch.reset(new WatchSocket()));
  108. ASSERT_TRUE(watch);
  109. /// Verify that post-construction the state the select-fd is valid.
  110. int select_fd = watch->getSelectFd();
  111. ASSERT_NE(select_fd, WatchSocket::INVALID_SOCKET);
  112. /// Verify that the socket can be marked ready.
  113. ASSERT_NO_THROW(watch->markReady());
  114. EXPECT_EQ(1, selectCheck(select_fd));
  115. // Interfere by reading the fd. This should empty the read pipe.
  116. uint32_t buf = 0;
  117. ASSERT_EQ((read (select_fd, &buf, sizeof(buf))), sizeof(buf));
  118. ASSERT_EQ(WatchSocket::MARKER, buf);
  119. // Really nothing that can be done to protect against this, but let's
  120. // make sure we aren't in a weird state.
  121. ASSERT_NO_THROW(watch->clearReady());
  122. // Verify the select_fd fails as socket is invalid/closed.
  123. EXPECT_EQ(0, selectCheck(select_fd));
  124. // Verify that getSelectFd() returns is still good.
  125. ASSERT_EQ(select_fd, watch->getSelectFd());
  126. }
  127. /// @brief Checks behavior when select_fd has been marked ready but then
  128. /// contents have been "corrupted" by a partial read.
  129. TEST(WatchSocketTest, badReadOnClear) {
  130. WatchSocketPtr watch;
  131. /// Verify that we can construct a WatchSocket.
  132. ASSERT_NO_THROW(watch.reset(new WatchSocket()));
  133. ASSERT_TRUE(watch);
  134. /// Verify that post-construction the state the select-fd is valid.
  135. int select_fd = watch->getSelectFd();
  136. ASSERT_NE(select_fd, WatchSocket::INVALID_SOCKET);
  137. /// Verify that the socket can be marked ready.
  138. ASSERT_NO_THROW(watch->markReady());
  139. EXPECT_EQ(1, selectCheck(select_fd));
  140. // Interfere by reading the fd. This should empty the read pipe.
  141. uint32_t buf = 0;
  142. ASSERT_EQ((read (select_fd, &buf, 1)), 1);
  143. ASSERT_NE(WatchSocket::MARKER, buf);
  144. // Really nothing that can be done to protect against this, but let's
  145. // make sure we aren't in a weird state.
  146. /// @todo maybe clear should never throw, log only
  147. ASSERT_THROW(watch->clearReady(), WatchSocketError);
  148. // Verify the select_fd does not evalute to ready.
  149. EXPECT_NE(1, selectCheck(select_fd));
  150. // Verify that getSelectFd() returns INVALID.
  151. ASSERT_EQ(WatchSocket::INVALID_SOCKET, watch->getSelectFd());
  152. // Verify that subsequent attempt to mark it fails.
  153. ASSERT_THROW(watch->markReady(), WatchSocketError);
  154. }
  155. } // end of anonymous namespace