Leosac  0.8.0
Open Source Access Control
ConfigManager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 #include <boost/property_tree/ptree.hpp>
23 #include <gtest/gtest.h>
24 #include <string>
25 
30 static std::string gl_data_path;
31 
32 namespace Leosac
33 {
34 namespace Test
35 {
36 class ConfigManagerTest : public ::testing::Test
37 {
38  public:
40  {
41  boost::property_tree::ptree empty_cfg;
42  empty_cfg.add<std::string>("instance_name", "OoOoO");
43  cfg0 = std::make_shared<ConfigManager>(empty_cfg);
44 
45  auto cfg_tree =
46  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager1.xml");
47  cfg1 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
48 
49  cfg_tree =
50  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager2.xml");
51  cfg2 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
52 
53  cfg_tree =
54  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager3.xml");
55  cfg3 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
56 
57  cfg_tree =
58  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager4.xml");
59  cfg4 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
60 
61  cfg_tree =
62  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager5.xml");
63  cfg5 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
64 
65  cfg_tree =
66  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager6.xml");
67  cfg6 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
68 
69  cfg_tree =
70  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager7.xml");
71  cfg7 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
72 
73  cfg_tree =
74  Tools::propertyTreeFromXmlFile(gl_data_path + "ConfigManager8.xml");
75  cfg8 = std::make_shared<ConfigManager>(cfg_tree.get_child("kernel"));
76  }
77 
78  protected:
79  std::shared_ptr<ConfigManager> cfg0;
80  std::shared_ptr<ConfigManager> cfg1;
81  std::shared_ptr<ConfigManager> cfg2;
82  std::shared_ptr<ConfigManager> cfg3;
83  std::shared_ptr<ConfigManager> cfg4;
84  std::shared_ptr<ConfigManager> cfg5;
85  std::shared_ptr<ConfigManager> cfg6;
86  std::shared_ptr<ConfigManager> cfg7;
87  std::shared_ptr<ConfigManager> cfg8;
88 };
89 
90 
92 {
93  boost::property_tree::ptree my_module_cfg;
94 
95  ASSERT_FALSE(cfg0->store_config("my", my_module_cfg));
96  // no we overwrite, so should return true.
97  ASSERT_TRUE(cfg0->store_config("my", my_module_cfg));
98 
99  auto cfg = cfg0->load_config("my");
100  ASSERT_EQ(my_module_cfg, cfg);
101 }
102 
104 {
105  auto network_cfg = cfg1->kconfig().get_child("network");
106 
107  bool enabled = network_cfg.get<bool>("enabled");
108  ASSERT_EQ(false, enabled);
109 }
110 
112 {
113  auto cfg = cfg1->get_general_config();
114 
115  auto c1 = cfg.get_child("plugin_directories");
116  auto c2 = cfg.get_child("log");
117  auto c3 = cfg.get_child("network");
118 }
119 
124 TEST_F(ConfigManagerTest, build_safe_cfg)
125 {
126  auto cfg = cfg1->get_exportable_general_config();
127 
128  auto c1 = cfg.get_child_optional("plugin_directories");
129  auto c2 = cfg.get_child_optional("log");
130  auto c3 = cfg.get_child_optional("network");
131 
132  ASSERT_FALSE(!!c1);
133  ASSERT_TRUE(!!c2);
134  ASSERT_FALSE(!!c3);
135 }
136 
142 TEST_F(ConfigManagerTest, build_safe_cfg2)
143 {
144  auto cfg = cfg2->get_exportable_general_config();
145 
146  auto c1 = cfg.get_child_optional("plugin_directories");
147  auto c2 = cfg.get_child_optional("log");
148  auto c3 = cfg.get_child_optional("network");
149 
150  ASSERT_TRUE(!!c1);
151  ASSERT_TRUE(!!c2);
152  ASSERT_TRUE(!!c3);
153 }
154 
155 TEST_F(ConfigManagerTest, build_safe_cfg3)
156 {
157  auto cfg = cfg3->get_exportable_general_config();
158 
159  auto c1 = cfg.get_child_optional("plugin_directories");
160  auto c2 = cfg.get_child_optional("log");
161  auto c3 = cfg.get_child_optional("network");
162 
163  ASSERT_FALSE(c1);
164  ASSERT_FALSE(c2);
165  ASSERT_FALSE(c3);
166 }
167 
174 TEST_F(ConfigManagerTest, test_sync_dest)
175 {
176  auto cfg = cfg4->get_exportable_general_config();
177 
178  // "merge" config.
179  cfg5->set_kconfig(cfg);
180 
181  // make sure sync_dest was honored.
182  auto merged_cfg = cfg5->get_general_config();
183 
184  auto c1 = merged_cfg.get_child_optional("log");
185  auto c2 = merged_cfg.get_child_optional("remote");
186  auto c3 = merged_cfg.get_child_optional("plugin_directories");
187 
188  ASSERT_TRUE(!!c1);
189  ASSERT_FALSE(!!c2);
190  ASSERT_TRUE(!!c3);
191 }
192 
199 TEST_F(ConfigManagerTest, test_sync_dest2)
200 {
201  auto cfg = cfg6->get_exportable_general_config();
202 
203  auto port_before = cfg.get<int>("remote.port");
204 
205  ASSERT_EQ(12345, port_before);
206  // "merge" config.
207  cfg7->set_kconfig(cfg);
208 
209  // make sure sync_dest was honored.
210  auto merged_cfg = cfg7->get_general_config();
211 
212  auto c1 = merged_cfg.get_child_optional("log");
213  auto c2 = merged_cfg.get_child_optional("remote");
214  auto c3 = merged_cfg.get_child_optional("plugin_directories");
215 
216  ASSERT_TRUE(!!c1);
217  ASSERT_TRUE(!!c2);
218  ASSERT_TRUE(!!c3);
219 
220  auto port = (*c2).get<int>("port");
221  ASSERT_EQ(12347, port);
222 }
223 
227 TEST_F(ConfigManagerTest, tes_no_import)
228 {
229  auto cfg = cfg8->get_non_importable_modules();
230  ASSERT_EQ(cfg, std::list<std::string>({"WIEGAND_READER", "TEST_AND_RESET"}));
231  ASSERT_FALSE(cfg8->is_module_importable("WIEGAND_READER"));
232  ASSERT_FALSE(cfg8->is_module_importable("TEST_AND_RESET"));
233 }
234 }
235 }
236 
237 int main(int argc, char **argv)
238 {
239  ::testing::InitGoogleTest(&argc, argv);
240 
241  // gtest shall leave us with our arguments.
242  // argv[1] shall be the path to test data file
243  assert(argc == 2);
244  gl_data_path = std::string(argv[1]) + '/';
245  return RUN_ALL_TESTS();
246 }
Leosac::Test::ConfigManagerTest::cfg2
std::shared_ptr< ConfigManager > cfg2
Definition: ConfigManager.cpp:81
Leosac::Test::ConfigManagerTest
Definition: ConfigManager.cpp:36
Leosac::Tools::propertyTreeFromXmlFile
boost::property_tree::ptree propertyTreeFromXmlFile(const std::string &path)
Build a property tree from a xml file.
Definition: XmlPropertyTree.cpp:39
main
int main(int argc, char **argv)
Definition: ConfigManager.cpp:237
Leosac::Test::ConfigManagerTest::cfg4
std::shared_ptr< ConfigManager > cfg4
Definition: ConfigManager.cpp:83
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Test::ConfigManagerTest::cfg8
std::shared_ptr< ConfigManager > cfg8
Definition: ConfigManager.cpp:87
XmlPropertyTree.hpp
Leosac::Test::ConfigManagerTest::cfg0
std::shared_ptr< ConfigManager > cfg0
Definition: ConfigManager.cpp:79
Leosac::Test::ConfigManagerTest::cfg1
std::shared_ptr< ConfigManager > cfg1
Definition: ConfigManager.cpp:80
Leosac::Test::ConfigManagerTest::cfg6
std::shared_ptr< ConfigManager > cfg6
Definition: ConfigManager.cpp:85
Leosac::Test::ConfigManagerTest::cfg7
std::shared_ptr< ConfigManager > cfg7
Definition: ConfigManager.cpp:86
Leosac::Test::TEST_F
TEST_F(AuthFileMapperTest, SimpleMapping)
Successful mapping.
Definition: AuthFile.cpp:197
Leosac::Test::ConfigManagerTest::cfg5
std::shared_ptr< ConfigManager > cfg5
Definition: ConfigManager.cpp:84
ConfigManager.hpp
Leosac::Test::ConfigManagerTest::ConfigManagerTest
ConfigManagerTest()
Definition: ConfigManager.cpp:39
Leosac::Test::ConfigManagerTest::cfg3
std::shared_ptr< ConfigManager > cfg3
Definition: ConfigManager.cpp:82