datasrc_configurator_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (C) 2012 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 <auth/datasrc_configurator.h>
  15. #include <config/tests/fake_session.h>
  16. #include <config/ccsession.h>
  17. #include <util/threads/lock.h>
  18. #include <gtest/gtest.h>
  19. #include <boost/bind.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <memory>
  22. using namespace isc;
  23. using namespace isc::cc;
  24. using namespace isc::config;
  25. using namespace isc::data;
  26. using namespace isc::dns;
  27. using namespace std;
  28. using namespace boost;
  29. namespace {
  30. class DatasrcConfiguratorTest;
  31. class FakeList {
  32. public:
  33. FakeList(const RRClass&) :
  34. configuration_(new ListElement)
  35. {}
  36. void configure(const ConstElementPtr& configuration, bool allow_cache) {
  37. EXPECT_TRUE(allow_cache);
  38. conf_ = configuration->get(0)->get("type")->stringValue();
  39. configuration_ = configuration;
  40. }
  41. const string& getConf() const {
  42. return (conf_);
  43. }
  44. ConstElementPtr getConfiguration() const {
  45. return (configuration_);
  46. }
  47. private:
  48. string conf_;
  49. ConstElementPtr configuration_;
  50. };
  51. typedef shared_ptr<FakeList> ListPtr;
  52. // We use the test fixture as both parameters, this makes it possible
  53. // to easily fake all needed methods and look that they were called.
  54. typedef DataSourceConfiguratorGeneric<DatasrcConfiguratorTest,
  55. FakeList> Configurator;
  56. void
  57. datasrcConfigHandler(Configurator* configurator, const std::string&,
  58. isc::data::ConstElementPtr config,
  59. const isc::config::ConfigData&)
  60. {
  61. if (config->contains("classes")) {
  62. configurator->reconfigure(config->get("classes"));
  63. }
  64. }
  65. class DatasrcConfiguratorTest : public ::testing::Test {
  66. public:
  67. // These pretend to be the server
  68. ListPtr getClientList(const RRClass& rrclass) {
  69. log_ += "get " + rrclass.toText() + "\n";
  70. return (lists_[rrclass]);
  71. }
  72. void setClientList(const RRClass& rrclass, const ListPtr& list) {
  73. log_ += "set " + rrclass.toText() + " " +
  74. (list ? list->getConf() : "") + "\n";
  75. lists_[rrclass] = list;
  76. }
  77. vector<RRClass> getClientListClasses() const {
  78. vector<RRClass> result;
  79. for (std::map<RRClass, ListPtr>::const_iterator it(lists_.begin());
  80. it != lists_.end(); ++it) {
  81. result.push_back(it->first);
  82. }
  83. return (result);
  84. }
  85. isc::util::thread::Mutex& getClientListMutex() const {
  86. return (mutex_);
  87. }
  88. protected:
  89. DatasrcConfiguratorTest() :
  90. session(ElementPtr(new ListElement), ElementPtr(new ListElement),
  91. ElementPtr(new ListElement)),
  92. configurator_(this),
  93. specfile(string(TEST_OWN_DATA_DIR) + "/spec.spec")
  94. {
  95. initSession();
  96. }
  97. void initSession() {
  98. session.getMessages()->add(createAnswer());
  99. mccs.reset(new ModuleCCSession(specfile, session, NULL, NULL, false,
  100. false));
  101. }
  102. void TearDown() {
  103. // Make sure no matter what we did, it is cleaned up.
  104. mccs->removeRemoteConfig("data_sources");
  105. }
  106. void init(const ElementPtr& config = ElementPtr()) {
  107. session.getMessages()->
  108. add(createAnswer(0,
  109. moduleSpecFromFile(string(PLUGIN_DATA_PATH) +
  110. "/datasrc.spec").
  111. getFullSpec()));
  112. if (config) {
  113. session.getMessages()->add(createAnswer(0, config));
  114. } else {
  115. session.getMessages()->
  116. add(createAnswer(0, ElementPtr(new MapElement)));
  117. }
  118. mccs->addRemoteConfig("data_sources",
  119. boost::bind(datasrcConfigHandler, &configurator_,
  120. _1, _2, _3), false);
  121. }
  122. void SetUp() {
  123. init();
  124. }
  125. ElementPtr buildConfig(const string& config) const {
  126. const ElementPtr internal(Element::fromJSON(config));
  127. const ElementPtr external(Element::fromJSON("{\"version\": 1}"));
  128. external->set("classes", internal);
  129. return (external);
  130. }
  131. void initializeINList() {
  132. const ConstElementPtr
  133. config(buildConfig("{\"IN\": [{\"type\": \"xxx\"}]}"));
  134. session.addMessage(createCommand("config_update", config),
  135. "data_sources", "*");
  136. mccs->checkCommand();
  137. // Check it called the correct things (check that there's no IN yet and
  138. // set a new one.
  139. EXPECT_EQ("get IN\nset IN xxx\n", log_);
  140. EXPECT_EQ(1, lists_.size());
  141. }
  142. FakeSession session;
  143. auto_ptr<ModuleCCSession> mccs;
  144. Configurator configurator_;
  145. const string specfile;
  146. std::map<RRClass, ListPtr> lists_;
  147. string log_;
  148. mutable isc::util::thread::Mutex mutex_;
  149. };
  150. // Check the initialization (and cleanup)
  151. TEST_F(DatasrcConfiguratorTest, DISABLED_initialization) {
  152. // It can't be initialized again
  153. EXPECT_THROW(init(), InvalidOperation);
  154. EXPECT_TRUE(session.haveSubscription("data_sources", "*"));
  155. EXPECT_FALSE(session.haveSubscription("data_sources", "*"));
  156. // We can't reconfigure now (not even manually)
  157. EXPECT_THROW(configurator_.reconfigure(ElementPtr(new MapElement())),
  158. InvalidOperation);
  159. // If the server param is NULL, it does not work
  160. EXPECT_THROW(Configurator(NULL), InvalidParameter);
  161. EXPECT_FALSE(session.haveSubscription("data_sources", "*")); // TBD
  162. // But we can initialize it again now
  163. EXPECT_NO_THROW(init());
  164. EXPECT_TRUE(session.haveSubscription("data_sources", "*"));
  165. }
  166. // Push there a configuration with a single list.
  167. TEST_F(DatasrcConfiguratorTest, createList) {
  168. initializeINList();
  169. }
  170. TEST_F(DatasrcConfiguratorTest, modifyList) {
  171. // First, initialize the list
  172. initializeINList();
  173. // And now change the configuration of the list
  174. const ElementPtr
  175. config(buildConfig("{\"IN\": [{\"type\": \"yyy\"}]}"));
  176. session.addMessage(createCommand("config_update", config), "data_sources",
  177. "*");
  178. log_ = "";
  179. mccs->checkCommand();
  180. // This one does not set
  181. EXPECT_EQ("get IN\n", log_);
  182. // But this should contain the yyy configuration
  183. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  184. EXPECT_EQ(1, lists_.size());
  185. }
  186. // Check we can have multiple lists at once
  187. TEST_F(DatasrcConfiguratorTest, multiple) {
  188. const ElementPtr
  189. config(buildConfig("{\"IN\": [{\"type\": \"yyy\"}], "
  190. "\"CH\": [{\"type\": \"xxx\"}]}"));
  191. session.addMessage(createCommand("config_update", config), "data_sources",
  192. "*");
  193. mccs->checkCommand();
  194. // We have set commands for both classes.
  195. EXPECT_EQ("get CH\nset CH xxx\nget IN\nset IN yyy\n", log_);
  196. // We should have both there
  197. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  198. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  199. EXPECT_EQ(2, lists_.size());
  200. }
  201. // Check we can add another one later and the old one does not get
  202. // overwritten.
  203. //
  204. // It's almost like above, but we initialize first with single-list
  205. // config.
  206. TEST_F(DatasrcConfiguratorTest, updateAdd) {
  207. initializeINList();
  208. const ElementPtr
  209. config(buildConfig("{\"IN\": [{\"type\": \"yyy\"}], "
  210. "\"CH\": [{\"type\": \"xxx\"}]}"));
  211. session.addMessage(createCommand("config_update", config), "data_sources",
  212. "*");
  213. log_ = "";
  214. mccs->checkCommand();
  215. // The CH is set, IN not
  216. EXPECT_EQ("get CH\nset CH xxx\nget IN\n", log_);
  217. // But this should contain the yyy configuration
  218. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  219. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  220. EXPECT_EQ(2, lists_.size());
  221. }
  222. // We delete a class list in this test.
  223. TEST_F(DatasrcConfiguratorTest, updateDelete) {
  224. initializeINList();
  225. const ElementPtr
  226. config(buildConfig("{}"));
  227. session.addMessage(createCommand("config_update", config), "data_sources",
  228. "*");
  229. log_ = "";
  230. mccs->checkCommand();
  231. EXPECT_EQ("get IN\nset IN \n", log_);
  232. EXPECT_FALSE(lists_[RRClass::IN()]);
  233. // In real auth server, the NULL one would be removed. However, we just
  234. // store it, so the IN bucket is still in there. This checks there's nothing
  235. // else.
  236. EXPECT_EQ(1, lists_.size());
  237. }
  238. // Check that we can rollback an addition if something else fails
  239. TEST_F(DatasrcConfiguratorTest, rollbackAddition) {
  240. initializeINList();
  241. // The configuration is wrong. However, the CH one will get done first.
  242. const ElementPtr
  243. config(buildConfig("{\"IN\": [{\"type\": 13}], "
  244. "\"CH\": [{\"type\": \"xxx\"}]}"));
  245. session.addMessage(createCommand("config_update", config), "data_sources",
  246. "*");
  247. log_ = "";
  248. // It does not throw, as it is handled in the ModuleCCSession.
  249. // Throwing from the reconfigure is checked in other tests.
  250. EXPECT_NO_THROW(mccs->checkCommand());
  251. // Anyway, the result should not contain CH now and the original IN should
  252. // be there.
  253. EXPECT_EQ("xxx", lists_[RRClass::IN()]->getConf());
  254. EXPECT_FALSE(lists_[RRClass::CH()]);
  255. }
  256. // Check that we can rollback a deletion if something else fails
  257. TEST_F(DatasrcConfiguratorTest, rollbackDeletion) {
  258. initializeINList();
  259. // Put the CH there
  260. const ElementPtr
  261. config1(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  262. "\"CH\": [{\"type\": \"xxx\"}]}"));
  263. configurator_.reconfigure(config1);
  264. const ElementPtr
  265. config2(Element::fromJSON("{\"IN\": [{\"type\": 13}]}"));
  266. // This would delete CH. However, the IN one fails.
  267. // As the deletions happen after the additions/settings
  268. // and there's no known way to cause an exception during the
  269. // deletions, it is not a true rollback, but the result should
  270. // be the same.
  271. EXPECT_THROW(configurator_.reconfigure(config2), TypeError);
  272. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  273. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  274. }
  275. // Check that we can roll back configuration change if something
  276. // fails later on.
  277. TEST_F(DatasrcConfiguratorTest, rollbackConfiguration) {
  278. initializeINList();
  279. // Put the CH there
  280. const ElementPtr
  281. config1(Element::fromJSON("{\"IN\": [{\"type\": \"yyy\"}], "
  282. "\"CH\": [{\"type\": \"xxx\"}]}"));
  283. configurator_.reconfigure(config1);
  284. // Now, the CH happens first. But nevertheless, it should be
  285. // restored to the previoeus version.
  286. const ElementPtr
  287. config2(Element::fromJSON("{\"IN\": [{\"type\": 13}], "
  288. "\"CH\": [{\"type\": \"yyy\"}]}"));
  289. EXPECT_THROW(configurator_.reconfigure(config2), TypeError);
  290. EXPECT_EQ("yyy", lists_[RRClass::IN()]->getConf());
  291. EXPECT_EQ("xxx", lists_[RRClass::CH()]->getConf());
  292. }
  293. }