portconfig_unittest.cc 14 KB

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