portconfig_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <gtest/gtest.h>
  15. #include <server_common/portconfig.h>
  16. #include <testutils/socket_request.h>
  17. #include <testutils/mockups.h>
  18. #include <util/unittests/check_valgrind.h>
  19. #include <util/unittests/resource.h>
  20. #include <cc/data.h>
  21. #include <exceptions/exceptions.h>
  22. #include <asiolink/asiolink.h>
  23. #include <asiodns/asiodns.h>
  24. #include <string>
  25. using namespace isc::server_common::portconfig;
  26. using namespace isc::server_common;
  27. using namespace isc::data;
  28. using namespace isc;
  29. using namespace std;
  30. using namespace isc::asiolink;
  31. using namespace isc::asiodns;
  32. using namespace isc::testutils;
  33. using boost::lexical_cast;
  34. namespace {
  35. /// Testcase for parseAddresses call (struct, nobody cares about private here)
  36. struct ParseAddresses : public ::testing::Test {
  37. AddressList result_;
  38. void empty(ElementPtr config, const string& name) {
  39. SCOPED_TRACE(name);
  40. EXPECT_NO_THROW(result_ = parseAddresses(config, "test"));
  41. EXPECT_TRUE(result_.empty());
  42. }
  43. template<class Exception>
  44. void invalidTest(const string& json, const string& name) {
  45. SCOPED_TRACE(name);
  46. ElementPtr config(Element::fromJSON(json));
  47. EXPECT_THROW(parseAddresses(config, "test"), Exception) <<
  48. "Should throw " << typeid(Exception).name();
  49. }
  50. };
  51. // Parse valid IPv4 address
  52. TEST_F(ParseAddresses, ipv4) {
  53. ElementPtr config(Element::fromJSON("["
  54. " {"
  55. " \"address\": \"192.0.2.1\","
  56. " \"port\": 53"
  57. " }"
  58. "]"));
  59. EXPECT_NO_THROW(result_ = parseAddresses(config, "test"));
  60. ASSERT_EQ(1, result_.size());
  61. EXPECT_EQ("192.0.2.1", result_[0].first);
  62. EXPECT_EQ(53, result_[0].second);
  63. }
  64. // Parse valid IPv6 address
  65. TEST_F(ParseAddresses, ipv6) {
  66. ElementPtr config(Element::fromJSON("["
  67. " {"
  68. " \"address\": \"2001:db8::1\","
  69. " \"port\": 53"
  70. " }"
  71. "]"));
  72. EXPECT_NO_THROW(result_ = parseAddresses(config, "test"));
  73. ASSERT_EQ(1, result_.size());
  74. EXPECT_EQ("2001:db8::1", result_[0].first);
  75. EXPECT_EQ(53, result_[0].second);
  76. }
  77. // Parse multiple addresses at once
  78. // (even the ports are different to see they are not mistaken)
  79. TEST_F(ParseAddresses, multi) {
  80. ElementPtr config(Element::fromJSON("["
  81. " {"
  82. " \"address\": \"2001:db8::1\","
  83. " \"port\": 53"
  84. " },"
  85. " {"
  86. " \"address\": \"192.0.2.1\","
  87. " \"port\": 54"
  88. " }"
  89. "]"));
  90. EXPECT_NO_THROW(result_ = parseAddresses(config, "test"));
  91. ASSERT_EQ(2, result_.size());
  92. EXPECT_EQ("2001:db8::1", result_[0].first);
  93. EXPECT_EQ(53, result_[0].second);
  94. EXPECT_EQ("192.0.2.1", result_[1].first);
  95. EXPECT_EQ(54, result_[1].second);
  96. }
  97. // Parse various versions of empty list
  98. TEST_F(ParseAddresses, empty) {
  99. empty(Element::fromJSON("[]"), "Empty list");
  100. empty(ElementPtr(new NullElement), "Null element");
  101. empty(ElementPtr(), "Null pointer");
  102. }
  103. // Reject invalid configs
  104. TEST_F(ParseAddresses, invalid) {
  105. invalidTest<TypeError>("{}", "Not a list");
  106. invalidTest<BadValue>("[{}]", "Empty element");
  107. invalidTest<TypeError>("[{"
  108. " \"port\": 1.5,"
  109. " \"address\": \"192.0.2.1\""
  110. "}]", "Float port");
  111. invalidTest<BadValue>("[{"
  112. " \"port\": -5,"
  113. " \"address\": \"192.0.2.1\""
  114. "}]", "Negative port");
  115. invalidTest<BadValue>("[{"
  116. " \"port\": 1000000,"
  117. " \"address\": \"192.0.2.1\""
  118. "}]", "Port too big");
  119. invalidTest<IOError>("[{"
  120. " \"port\": 53,"
  121. " \"address\": \"bad_address\""
  122. "}]", "Bad address");
  123. }
  124. // Test fixture for installListenAddresses
  125. struct InstallListenAddresses : public ::testing::Test {
  126. InstallListenAddresses() :
  127. // The empty string is expected parameter of requestSocket,
  128. // not app_name - the request does not fall back to this, it
  129. // is checked to be the same.
  130. sock_requestor_(dnss_, store_, 5288, "")
  131. {
  132. valid_.push_back(AddressPair("127.0.0.1", 5288));
  133. valid_.push_back(AddressPair("::1", 5288));
  134. invalid_.push_back(AddressPair("127.0.0.1", 5288));
  135. invalid_.push_back(AddressPair("192.0.2.2", 1));
  136. }
  137. MockDNSService dnss_;
  138. AddressList store_;
  139. isc::testutils::TestSocketRequestor sock_requestor_;
  140. // We should be able to bind to these addresses
  141. AddressList valid_;
  142. // But this shouldn't work
  143. AddressList invalid_;
  144. // Check that the store_ addresses are the same as expected
  145. void checkAddresses(const AddressList& expected, const string& name) const
  146. {
  147. SCOPED_TRACE(name);
  148. ASSERT_EQ(expected.size(), store_.size()) <<
  149. "Different amount of elements, not checking content";
  150. // Run in parallel through the vectors
  151. for (AddressList::const_iterator ei(expected.begin()),
  152. si(store_.begin()); ei != expected.end(); ++ei, ++si) {
  153. EXPECT_EQ(ei->first, si->first);
  154. EXPECT_EQ(ei->second, si->second);
  155. }
  156. }
  157. };
  158. // Try switching valid addresses
  159. // Check the sockets are correctly requested and returned
  160. TEST_F(InstallListenAddresses, valid) {
  161. // First, bind to the valid addresses
  162. EXPECT_NO_THROW(installListenAddresses(valid_, store_, dnss_));
  163. checkAddresses(valid_, "Valid addresses");
  164. const char* tokens1[] = {
  165. "TCP:127.0.0.1:5288:1",
  166. "UDP:127.0.0.1:5288:2",
  167. "TCP:::1:5288:3",
  168. "UDP:::1:5288:4",
  169. NULL
  170. };
  171. const char* no_tokens[] = { NULL };
  172. sock_requestor_.checkTokens(tokens1, sock_requestor_.given_tokens_,
  173. "Valid given tokens 1");
  174. sock_requestor_.checkTokens(no_tokens, sock_requestor_.released_tokens_,
  175. "Valid no released tokens 1");
  176. // TODO Maybe some test to actually connect to them
  177. // Try setting it back to nothing
  178. sock_requestor_.given_tokens_.clear();
  179. EXPECT_NO_THROW(installListenAddresses(AddressList(), store_, dnss_));
  180. checkAddresses(AddressList(), "No addresses");
  181. sock_requestor_.checkTokens(no_tokens, sock_requestor_.given_tokens_,
  182. "Valid no given tokens");
  183. sock_requestor_.checkTokens(tokens1, sock_requestor_.released_tokens_,
  184. "Valid released tokens");
  185. // Try switching back again
  186. EXPECT_NO_THROW(installListenAddresses(valid_, store_, dnss_));
  187. checkAddresses(valid_, "Valid addresses");
  188. const char* tokens2[] = {
  189. "TCP:127.0.0.1:5288:5",
  190. "UDP:127.0.0.1:5288:6",
  191. "TCP:::1:5288:7",
  192. "UDP:::1:5288:8",
  193. NULL
  194. };
  195. sock_requestor_.checkTokens(tokens2, sock_requestor_.given_tokens_,
  196. "Valid given tokens 2");
  197. sock_requestor_.checkTokens(tokens1, sock_requestor_.released_tokens_,
  198. "Valid released tokens");
  199. }
  200. // Try if rollback works
  201. TEST_F(InstallListenAddresses, rollback) {
  202. // Set some addresses
  203. EXPECT_NO_THROW(installListenAddresses(valid_, store_, dnss_));
  204. checkAddresses(valid_, "Before rollback");
  205. const char* tokens1[] = {
  206. "TCP:127.0.0.1:5288:1",
  207. "UDP:127.0.0.1:5288:2",
  208. "TCP:::1:5288:3",
  209. "UDP:::1:5288:4",
  210. NULL
  211. };
  212. const char* no_tokens[] = { NULL };
  213. sock_requestor_.checkTokens(tokens1, sock_requestor_.given_tokens_,
  214. "Given before rollback");
  215. sock_requestor_.checkTokens(no_tokens, sock_requestor_.released_tokens_,
  216. "Released before rollback");
  217. sock_requestor_.given_tokens_.clear();
  218. // This should not bind them, but should leave the original addresses
  219. EXPECT_THROW(installListenAddresses(invalid_, store_, dnss_),
  220. SocketRequestor::SocketError);
  221. checkAddresses(valid_, "After rollback");
  222. // Now, it should have requested first pair of sockets from the invalids
  223. // and, as the second failed, it should have returned them right away.
  224. const char* released1[] = {
  225. "TCP:127.0.0.1:5288:1",
  226. "UDP:127.0.0.1:5288:2",
  227. "TCP:::1:5288:3",
  228. "UDP:::1:5288:4",
  229. "TCP:127.0.0.1:5288:5",
  230. "UDP:127.0.0.1:5288:6",
  231. NULL
  232. };
  233. // It should request the first pair of sockets, and then request the
  234. // complete set of valid addresses to rollback
  235. const char* tokens2[] = {
  236. "TCP:127.0.0.1:5288:5",
  237. "UDP:127.0.0.1:5288:6",
  238. "TCP:127.0.0.1:5288:7",
  239. "UDP:127.0.0.1:5288:8",
  240. "TCP:::1:5288:9",
  241. "UDP:::1:5288:10",
  242. NULL
  243. };
  244. sock_requestor_.checkTokens(tokens2, sock_requestor_.given_tokens_,
  245. "Given after rollback");
  246. sock_requestor_.checkTokens(released1, sock_requestor_.released_tokens_,
  247. "Released after rollback");
  248. }
  249. // Try it at least releases everything when even the rollback fails.
  250. TEST_F(InstallListenAddresses, brokenRollback) {
  251. EXPECT_NO_THROW(installListenAddresses(valid_, store_, dnss_));
  252. checkAddresses(valid_, "Before rollback");
  253. // Don't check the tokens now, we already do it in rollback and valid tests
  254. sock_requestor_.given_tokens_.clear();
  255. sock_requestor_.break_rollback_ = true;
  256. EXPECT_THROW(installListenAddresses(invalid_, store_, dnss_),
  257. SocketRequestor::NonFatalSocketError);
  258. // No addresses here
  259. EXPECT_TRUE(store_.empty());
  260. // The first pair should be requested in the first part of the failure to
  261. // bind and the second pair in the first part of rollback
  262. const char* tokens[] = {
  263. "TCP:127.0.0.1:5288:5",
  264. "UDP:127.0.0.1:5288:6",
  265. "TCP:127.0.0.1:5288:7",
  266. "UDP:127.0.0.1:5288:8",
  267. NULL
  268. };
  269. // The first set should be released, as well as all the ones we request now
  270. const char* released[] = {
  271. "TCP:127.0.0.1:5288:1",
  272. "UDP:127.0.0.1:5288:2",
  273. "TCP:::1:5288:3",
  274. "UDP:::1:5288:4",
  275. "TCP:127.0.0.1:5288:5",
  276. "UDP:127.0.0.1:5288:6",
  277. "TCP:127.0.0.1:5288:7",
  278. "UDP:127.0.0.1:5288:8",
  279. NULL
  280. };
  281. sock_requestor_.checkTokens(tokens, sock_requestor_.given_tokens_,
  282. "given");
  283. sock_requestor_.checkTokens(released, sock_requestor_.released_tokens_,
  284. "released");
  285. }
  286. // Make sure the death tests are filterable away.
  287. typedef InstallListenAddresses InstallListenAddressesDeathTest;
  288. // There are systems which don't have EXPECT_DEATH. We skip the tests there.
  289. // We're lucky, EXPECT_DEATH is a macro, so we can test for its existence this
  290. // easily.
  291. #ifdef EXPECT_DEATH
  292. // We make the socket requestor throw a "fatal" exception, one where we can't be
  293. // sure the state between processes is consistent. So we abort in that case.
  294. TEST_F(InstallListenAddressesDeathTest, inconsistent) {
  295. if (!isc::util::unittests::runningOnValgrind()) {
  296. AddressList deathAddresses;
  297. deathAddresses.push_back(AddressPair("192.0.2.3", 5288));
  298. // Make sure it actually kills the application (there should be an abort
  299. // in this case)
  300. EXPECT_DEATH({
  301. isc::util::unittests::dontCreateCoreDumps();
  302. try {
  303. installListenAddresses(deathAddresses, store_, dnss_);
  304. } catch (...) {
  305. // Prevent exceptions killing the application, we need
  306. // to make sure it dies the real hard way
  307. };
  308. }, "");
  309. }
  310. }
  311. // If we are unable to tell the b10-init we closed a socket, we abort, as we
  312. // are not consistent with b10-init most probably.
  313. TEST_F(InstallListenAddressesDeathTest, cantClose) {
  314. if (!isc::util::unittests::runningOnValgrind()) {
  315. installListenAddresses(valid_, store_, dnss_);
  316. AddressList empty;
  317. // Instruct it to fail on close
  318. sock_requestor_.break_release_ = true;
  319. EXPECT_DEATH({
  320. isc::util::unittests::dontCreateCoreDumps();
  321. try {
  322. // Setting to empty will close all current sockets.
  323. // And thanks to the break_release_, the close will
  324. // throw, which will make it crash.
  325. installListenAddresses(empty, store_, dnss_);
  326. } catch (...) {
  327. // To make sure it is killed by abort, not by some
  328. // (unhandled) exception
  329. };
  330. }, "");
  331. // And reset it back, so it can safely clean up itself.
  332. sock_requestor_.break_release_ = false;
  333. }
  334. }
  335. #endif // EXPECT_DEATH
  336. }