ccsession_unittests.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Copyright (C) 2009 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. // $Id: module_spec_unittests.cc 1321 2010-03-11 10:17:03Z jelte $
  15. #include <gtest/gtest.h>
  16. #include "fake_session.h"
  17. #include <config/ccsession.h>
  18. #include <fstream>
  19. #include "data_def_unittests_config.h"
  20. using namespace isc::data;
  21. using namespace isc::config;
  22. using namespace std;
  23. std::string ccspecfile(const std::string name) {
  24. return std::string(TEST_DATA_PATH) + "/" + name;
  25. }
  26. static ElementPtr
  27. el(const std::string& str)
  28. {
  29. return Element::createFromString(str);
  30. }
  31. // upon creation of a ModuleCCSession, the class
  32. // sends its specification to the config manager
  33. // it expects an ok answer back, so everytime we
  34. // create a ModuleCCSession, we must set an initial
  35. // ok answer
  36. void
  37. initFakeSession()
  38. {
  39. initial_messages = el("[]");
  40. msg_queue = el("[]");
  41. subscriptions = el("[]");
  42. initial_messages->add(createAnswer());
  43. }
  44. void
  45. endFakeSession()
  46. {
  47. initial_messages = ElementPtr();
  48. msg_queue = ElementPtr();
  49. subscriptions = ElementPtr();
  50. }
  51. TEST(CCSession, createAnswer)
  52. {
  53. ElementPtr answer;
  54. answer = createAnswer();
  55. EXPECT_EQ("{\"result\": [ 0 ]}", answer->str());
  56. answer = createAnswer(1, "error");
  57. EXPECT_EQ("{\"result\": [ 1, \"error\" ]}", answer->str());
  58. EXPECT_THROW(createAnswer(1, ElementPtr()), CCSessionError);
  59. EXPECT_THROW(createAnswer(1, Element::create(1)), CCSessionError);
  60. ElementPtr arg = el("[ \"just\", \"some\", \"data\" ]");
  61. answer = createAnswer(0, arg);
  62. EXPECT_EQ("{\"result\": [ 0, [ \"just\", \"some\", \"data\" ] ]}", answer->str());
  63. }
  64. TEST(CCSession, parseAnswer)
  65. {
  66. ElementPtr answer;
  67. ElementPtr arg;
  68. int rcode;
  69. EXPECT_THROW(parseAnswer(rcode, ElementPtr()), CCSessionError);
  70. EXPECT_THROW(parseAnswer(rcode, el("1")), CCSessionError);
  71. EXPECT_THROW(parseAnswer(rcode, el("[]")), CCSessionError);
  72. EXPECT_THROW(parseAnswer(rcode, el("{}")), CCSessionError);
  73. EXPECT_THROW(parseAnswer(rcode, el("{ \"something\": 1 }")), CCSessionError);
  74. EXPECT_THROW(parseAnswer(rcode, el("{ \"result\": 0 }")), CCSessionError);
  75. EXPECT_THROW(parseAnswer(rcode, el("{ \"result\": 1 }")), CCSessionError);
  76. EXPECT_THROW(parseAnswer(rcode, el("{ \"result\": [ 1 ]}")), CCSessionError);
  77. EXPECT_THROW(parseAnswer(rcode, el("{ \"result\": [ 1, 1 ]}")), CCSessionError);
  78. answer = el("{\"result\": [ 0 ]}");
  79. arg = parseAnswer(rcode, answer);
  80. EXPECT_EQ(0, rcode);
  81. EXPECT_TRUE(isNull(arg));
  82. answer = el("{\"result\": [ 1, \"error\"]}");
  83. arg = parseAnswer(rcode, answer);
  84. EXPECT_EQ(1, rcode);
  85. EXPECT_EQ("error", arg->stringValue());
  86. answer = el("{\"result\": [ 0, [ \"just\", \"some\", \"data\" ] ] }");
  87. arg = parseAnswer(rcode, answer);
  88. EXPECT_EQ(0, rcode);
  89. EXPECT_EQ("[ \"just\", \"some\", \"data\" ]", arg->str());
  90. }
  91. TEST(CCSession, createCommand)
  92. {
  93. ElementPtr command;
  94. ElementPtr arg;
  95. command = createCommand("my_command");
  96. ASSERT_EQ("{\"command\": [ \"my_command\" ]}", command->str());
  97. arg = el("1");
  98. command = createCommand("my_command", arg);
  99. ASSERT_EQ("{\"command\": [ \"my_command\", 1 ]}", command->str());
  100. arg = el("[ \"a\", \"b\" ]");
  101. command = createCommand("my_cmd", arg);
  102. ASSERT_EQ("{\"command\": [ \"my_cmd\", [ \"a\", \"b\" ] ]}", command->str());
  103. arg = el("{ \"a\": \"map\" }");
  104. command = createCommand("foo", arg);
  105. ASSERT_EQ("{\"command\": [ \"foo\", {\"a\": \"map\"} ]}", command->str());
  106. }
  107. TEST(CCSession, parseCommand)
  108. {
  109. ElementPtr arg;
  110. std::string cmd;
  111. // should throw
  112. EXPECT_THROW(parseCommand(arg, ElementPtr()), CCSessionError);
  113. EXPECT_THROW(parseCommand(arg, el("1")), CCSessionError);
  114. EXPECT_THROW(parseCommand(arg, el("{}")), CCSessionError);
  115. EXPECT_THROW(parseCommand(arg, el("{\"not a command\": 1 }")), CCSessionError);
  116. EXPECT_THROW(parseCommand(arg, el("{\"command\": 1 }")), CCSessionError);
  117. EXPECT_THROW(parseCommand(arg, el("{\"command\": [] }")), CCSessionError);
  118. EXPECT_THROW(parseCommand(arg, el("{\"command\": [ 1 ] }")), CCSessionError);
  119. cmd = parseCommand(arg, el("{\"command\": [ \"my_command\" ] }"));
  120. EXPECT_EQ("my_command", cmd);
  121. EXPECT_TRUE(isNull(arg));
  122. cmd = parseCommand(arg, el("{\"command\": [ \"my_command\", 1 ] }"));
  123. EXPECT_EQ("my_command", cmd);
  124. EXPECT_EQ("1", arg->str());
  125. parseCommand(arg, el("{\"command\": [ \"my_command\", [ \"some\", \"argument\", \"list\" ] ] }"));
  126. EXPECT_EQ("my_command", cmd);
  127. EXPECT_EQ("[ \"some\", \"argument\", \"list\" ]", arg->str());
  128. }
  129. TEST(CCSession, session1)
  130. {
  131. initFakeSession();
  132. EXPECT_EQ(false, haveSubscription("Spec1", "*"));
  133. ModuleCCSession mccs(ccspecfile("spec1.spec"), NULL, NULL);
  134. EXPECT_EQ(true, haveSubscription("Spec1", "*"));
  135. EXPECT_EQ(1, msg_queue->size());
  136. ElementPtr msg;
  137. std::string group, to;
  138. msg = getFirstMessage(group, to);
  139. EXPECT_EQ("{\"command\": [ \"module_spec\", {\"module_name\": \"Spec1\"} ]}", msg->str());
  140. EXPECT_EQ("ConfigManager", group);
  141. EXPECT_EQ("*", to);
  142. EXPECT_EQ(0, msg_queue->size());
  143. endFakeSession();
  144. }
  145. TEST(CCSession, session2)
  146. {
  147. initFakeSession();
  148. EXPECT_EQ(false, haveSubscription("Spec2", "*"));
  149. ModuleCCSession mccs(ccspecfile("spec2.spec"), NULL, NULL);
  150. EXPECT_EQ(true, haveSubscription("Spec2", "*"));
  151. EXPECT_EQ(1, msg_queue->size());
  152. ElementPtr msg;
  153. std::string group, to;
  154. msg = getFirstMessage(group, to);
  155. EXPECT_EQ("{\"command\": [ \"module_spec\", {\"commands\": [ {\"command_args\": [ {\"item_default\": \"\", \"item_name\": \"message\", \"item_optional\": False, \"item_type\": \"string\"} ], \"command_description\": \"Print the given message to stdout\", \"command_name\": \"print_message\"}, {\"command_args\": [ ], \"command_description\": \"Shut down BIND 10\", \"command_name\": \"shutdown\"} ], \"config_data\": [ {\"item_default\": 1, \"item_name\": \"item1\", \"item_optional\": False, \"item_type\": \"integer\"}, {\"item_default\": 1.1, \"item_name\": \"item2\", \"item_optional\": False, \"item_type\": \"real\"}, {\"item_default\": True, \"item_name\": \"item3\", \"item_optional\": False, \"item_type\": \"boolean\"}, {\"item_default\": \"test\", \"item_name\": \"item4\", \"item_optional\": False, \"item_type\": \"string\"}, {\"item_default\": [ \"a\", \"b\" ], \"item_name\": \"item5\", \"item_optional\": False, \"item_type\": \"list\", \"list_item_spec\": {\"item_default\": \"\", \"item_name\": \"list_element\", \"item_optional\": False, \"item_type\": \"string\"}}, {\"item_default\": {}, \"item_name\": \"item6\", \"item_optional\": False, \"item_type\": \"map\", \"map_item_spec\": [ {\"item_default\": \"default\", \"item_name\": \"value1\", \"item_optional\": True, \"item_type\": \"string\"}, {\"item_name\": \"value2\", \"item_optional\": True, \"item_type\": \"integer\"} ]} ], \"module_name\": \"Spec2\"} ]}", msg->str());
  156. EXPECT_EQ("ConfigManager", group);
  157. EXPECT_EQ("*", to);
  158. EXPECT_EQ(0, msg_queue->size());
  159. endFakeSession();
  160. }
  161. ElementPtr my_config_handler(ElementPtr new_config)
  162. {
  163. if (new_config && new_config->contains("item1") &&
  164. new_config->get("item1")->intValue() == 5) {
  165. return createAnswer(6, "I do not like the number 5");
  166. }
  167. return createAnswer();
  168. }
  169. ElementPtr my_command_handler(const std::string& command, ElementPtr arg UNUSED_PARAM)
  170. {
  171. if (command == "good_command") {
  172. return createAnswer();
  173. } else if (command == "command_with_arg") {
  174. if (arg) {
  175. if (arg->getType() == Element::integer) {
  176. return createAnswer(0, el("2"));
  177. } else {
  178. return createAnswer(1, "arg bad type");
  179. }
  180. } else {
  181. return createAnswer(1, "arg missing");
  182. }
  183. } else {
  184. return createAnswer(1, "bad command");
  185. }
  186. }
  187. TEST(CCSession, session3)
  188. {
  189. initFakeSession();
  190. // client will ask for config
  191. initial_messages->add(createAnswer(0, el("{}")));
  192. EXPECT_EQ(false, haveSubscription("Spec2", "*"));
  193. ModuleCCSession mccs(ccspecfile("spec2.spec"), my_config_handler, my_command_handler);
  194. EXPECT_EQ(true, haveSubscription("Spec2", "*"));
  195. EXPECT_EQ(2, msg_queue->size());
  196. ElementPtr msg;
  197. std::string group, to;
  198. msg = getFirstMessage(group, to);
  199. EXPECT_EQ("{\"command\": [ \"module_spec\", {\"commands\": [ {\"command_args\": [ {\"item_default\": \"\", \"item_name\": \"message\", \"item_optional\": False, \"item_type\": \"string\"} ], \"command_description\": \"Print the given message to stdout\", \"command_name\": \"print_message\"}, {\"command_args\": [ ], \"command_description\": \"Shut down BIND 10\", \"command_name\": \"shutdown\"} ], \"config_data\": [ {\"item_default\": 1, \"item_name\": \"item1\", \"item_optional\": False, \"item_type\": \"integer\"}, {\"item_default\": 1.1, \"item_name\": \"item2\", \"item_optional\": False, \"item_type\": \"real\"}, {\"item_default\": True, \"item_name\": \"item3\", \"item_optional\": False, \"item_type\": \"boolean\"}, {\"item_default\": \"test\", \"item_name\": \"item4\", \"item_optional\": False, \"item_type\": \"string\"}, {\"item_default\": [ \"a\", \"b\" ], \"item_name\": \"item5\", \"item_optional\": False, \"item_type\": \"list\", \"list_item_spec\": {\"item_default\": \"\", \"item_name\": \"list_element\", \"item_optional\": False, \"item_type\": \"string\"}}, {\"item_default\": {}, \"item_name\": \"item6\", \"item_optional\": False, \"item_type\": \"map\", \"map_item_spec\": [ {\"item_default\": \"default\", \"item_name\": \"value1\", \"item_optional\": True, \"item_type\": \"string\"}, {\"item_name\": \"value2\", \"item_optional\": True, \"item_type\": \"integer\"} ]} ], \"module_name\": \"Spec2\"} ]}", msg->str());
  200. EXPECT_EQ("ConfigManager", group);
  201. EXPECT_EQ("*", to);
  202. EXPECT_EQ(1, msg_queue->size());
  203. msg = getFirstMessage(group, to);
  204. EXPECT_EQ("{\"command\": [ \"get_config\", {\"module_name\": \"Spec2\"} ]}", msg->str());
  205. EXPECT_EQ("ConfigManager", group);
  206. EXPECT_EQ("*", to);
  207. EXPECT_EQ(0, msg_queue->size());
  208. endFakeSession();
  209. }
  210. TEST(CCSession, checkCommand)
  211. {
  212. initFakeSession();
  213. // client will ask for config
  214. initial_messages->add(createAnswer(0, el("{}")));
  215. EXPECT_EQ(false, haveSubscription("Spec2", "*"));
  216. ModuleCCSession mccs(ccspecfile("spec2.spec"), my_config_handler, my_command_handler);
  217. EXPECT_EQ(true, haveSubscription("Spec2", "*"));
  218. EXPECT_EQ(2, msg_queue->size());
  219. ElementPtr msg;
  220. std::string group, to;
  221. // checked above, drop em
  222. msg = getFirstMessage(group, to);
  223. msg = getFirstMessage(group, to);
  224. int result;
  225. result = mccs.checkCommand();
  226. EXPECT_EQ(0, result);
  227. // not a command, should be ignored
  228. addMessage(el("1"), "Spec2", "*");
  229. result = mccs.checkCommand();
  230. EXPECT_EQ(0, result);
  231. addMessage(el("{ \"command\": [ \"good_command\" ] }"), "Spec2", "*");
  232. result = mccs.checkCommand();
  233. EXPECT_EQ(1, msg_queue->size());
  234. msg = getFirstMessage(group, to);
  235. EXPECT_EQ("{\"result\": [ 0 ]}", msg->str());
  236. EXPECT_EQ(0, result);
  237. addMessage(el("{ \"command\": \"bad_command\" }"), "Spec2", "*");
  238. result = mccs.checkCommand();
  239. EXPECT_EQ(1, msg_queue->size());
  240. msg = getFirstMessage(group, to);
  241. EXPECT_EQ("{\"result\": [ 1, \"Command part in command message missing, empty, or not a list\" ]}", msg->str());
  242. EXPECT_EQ(0, result);
  243. addMessage(el("{ \"command\": [ \"bad_command\" ] }"), "Spec2", "*");
  244. result = mccs.checkCommand();
  245. EXPECT_EQ(1, msg_queue->size());
  246. msg = getFirstMessage(group, to);
  247. EXPECT_EQ("{\"result\": [ 1, \"bad command\" ]}", msg->str());
  248. EXPECT_EQ(0, result);
  249. addMessage(el("{ \"command\": [ \"command_with_arg\", 1 ] }"), "Spec2", "*");
  250. result = mccs.checkCommand();
  251. EXPECT_EQ(1, msg_queue->size());
  252. msg = getFirstMessage(group, to);
  253. EXPECT_EQ("{\"result\": [ 0, 2 ]}", msg->str());
  254. EXPECT_EQ(0, result);
  255. addMessage(el("{ \"command\": [ \"command_with_arg\" ] }"), "Spec2", "*");
  256. result = mccs.checkCommand();
  257. EXPECT_EQ(1, msg_queue->size());
  258. msg = getFirstMessage(group, to);
  259. EXPECT_EQ("{\"result\": [ 1, \"arg missing\" ]}", msg->str());
  260. EXPECT_EQ(0, result);
  261. addMessage(el("{ \"command\": [ \"command_with_arg\", \"asdf\" ] }"), "Spec2", "*");
  262. result = mccs.checkCommand();
  263. EXPECT_EQ(1, msg_queue->size());
  264. msg = getFirstMessage(group, to);
  265. EXPECT_EQ("{\"result\": [ 1, \"arg bad type\" ]}", msg->str());
  266. EXPECT_EQ(0, result);
  267. mccs.setCommandHandler(NULL);
  268. addMessage(el("{ \"command\": [ \"whatever\" ] }"), "Spec2", "*");
  269. result = mccs.checkCommand();
  270. EXPECT_EQ(1, msg_queue->size());
  271. msg = getFirstMessage(group, to);
  272. EXPECT_EQ("{\"result\": [ 1, \"Command given but no command handler for module\" ]}", msg->str());
  273. EXPECT_EQ(0, result);
  274. EXPECT_EQ(1, mccs.getValue("item1")->intValue());
  275. addMessage(el("{ \"command\": [ \"config_update\", { \"item1\": 2 } ] }"), "Spec2", "*");
  276. result = mccs.checkCommand();
  277. EXPECT_EQ(1, msg_queue->size());
  278. msg = getFirstMessage(group, to);
  279. EXPECT_EQ("{\"result\": [ 0 ]}", msg->str());
  280. EXPECT_EQ(0, result);
  281. EXPECT_EQ(2, mccs.getValue("item1")->intValue());
  282. addMessage(el("{ \"command\": [ \"config_update\", { \"item1\": \"asdf\" } ] }"), "Spec2", "*");
  283. result = mccs.checkCommand();
  284. EXPECT_EQ(1, msg_queue->size());
  285. msg = getFirstMessage(group, to);
  286. EXPECT_EQ("{\"result\": [ 2, \"Error in config validation: Type mismatch\" ]}", msg->str());
  287. EXPECT_EQ(0, result);
  288. EXPECT_EQ(2, mccs.getValue("item1")->intValue());
  289. addMessage(el("{ \"command\": [ \"config_update\", { \"item1\": 5 } ] }"), "Spec2", "*");
  290. result = mccs.checkCommand();
  291. EXPECT_EQ(1, msg_queue->size());
  292. msg = getFirstMessage(group, to);
  293. EXPECT_EQ("{\"result\": [ 6, \"I do not like the number 5\" ]}", msg->str());
  294. EXPECT_EQ(0, result);
  295. EXPECT_EQ(2, mccs.getValue("item1")->intValue());
  296. endFakeSession();
  297. }
  298. TEST(CCSession, remoteConfig)
  299. {
  300. std::string module_name;
  301. int item1;
  302. initFakeSession();
  303. ModuleCCSession mccs(ccspecfile("spec1.spec"), NULL, NULL);
  304. EXPECT_EQ(true, haveSubscription("Spec1", "*"));
  305. // first simply connect, with no config values, and see we get
  306. // the default
  307. initial_messages->add(createAnswer(0, el("{}")));
  308. EXPECT_EQ(false, haveSubscription("Spec2", "*"));
  309. module_name = mccs.addRemoteConfig(ccspecfile("spec2.spec"));
  310. EXPECT_EQ("Spec2", module_name);
  311. EXPECT_EQ(true, haveSubscription("Spec2", "*"));
  312. item1 = mccs.getRemoteConfigValue(module_name, "item1")->intValue();
  313. EXPECT_EQ(1, item1);
  314. // Remove it and see we get an error asking for a config value
  315. mccs.removeRemoteConfig(module_name);
  316. EXPECT_EQ(false, haveSubscription("Spec2", "*"));
  317. EXPECT_THROW(mccs.getRemoteConfigValue(module_name, "item1"), CCSessionError);
  318. // Now re-add it, with a specific config value, and see we get that
  319. initial_messages->add(createAnswer(0, el("{ \"item1\": 2 }")));
  320. module_name = mccs.addRemoteConfig(ccspecfile("spec2.spec"));
  321. item1 = mccs.getRemoteConfigValue(module_name, "item1")->intValue();
  322. EXPECT_EQ(2, item1);
  323. // Try a config_update command
  324. addMessage(el("{ \"command\": [ \"config_update\", { \"item1\": 3 } ] }"), module_name, "*");
  325. mccs.checkCommand();
  326. item1 = mccs.getRemoteConfigValue(module_name, "item1")->intValue();
  327. EXPECT_EQ(3, item1);
  328. // remove, re-add, now with a *bad* config request answer
  329. mccs.removeRemoteConfig(module_name);
  330. initial_messages->add(el("{}"));
  331. EXPECT_THROW(mccs.addRemoteConfig(ccspecfile("spec2.spec")), CCSessionError);
  332. initial_messages->add(createAnswer(1, "my_error"));
  333. EXPECT_THROW(mccs.addRemoteConfig(ccspecfile("spec2.spec")), CCSessionError);
  334. initial_messages->add(createAnswer());
  335. mccs.addRemoteConfig(ccspecfile("spec2.spec"));
  336. endFakeSession();
  337. }