Leosac  0.8.0
Open Source Access Control
SysFsGpioConfig.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 
21 #include "tools/log.hpp"
22 #include "gtest/gtest.h"
23 #include <string>
24 
25 using namespace Leosac::Module::SysFsGpio;
26 
27 namespace Leosac
28 {
29 namespace Test
30 {
31 
36 class SysFsGpioConfigTest : public ::testing::Test
37 {
38  public:
39  // starts the led module
41  {
42  ctx_ = new zmqpp::context_t();
43 
44  build_config_case1();
45  build_config_case2();
46  build_config_case3();
47  }
48 
50  {
51  delete ctx_;
52  }
53 
55  {
56  boost::property_tree::ptree aliases_cfg, module_cfg;
57 
58  module_cfg.add("export_path", "/path/to/export");
59  module_cfg.add("unexport_path", "/path/to/unexport");
60  module_cfg.add("value_path", "/path/to/gpios/__PLACEHOLDER__/value");
61  module_cfg.add("edge_path", "/path/to/gpios/__PLACEHOLDER__/edge");
62  module_cfg.add("direction_path", "/path/to/gpios/__PLACEHOLDER__/direction");
63 
64  module_cfg.add_child("aliases", aliases_cfg);
65  cfg_case_1_ = module_cfg;
66  }
67 
69  {
70  boost::property_tree::ptree aliases_cfg, module_cfg;
71 
72  module_cfg.add("export_path", "/path/to/export");
73  module_cfg.add("unexport_path", "/path/to/unexport");
74  module_cfg.add("value_path", "/random/path/to/__PLACEHOLDER__/value");
75  module_cfg.add("edge_path", "/random/path/to/__PLACEHOLDER__/edge");
76  module_cfg.add("direction_path",
77  "/random/path/to/__PLACEHOLDER__/direction");
78 
79  // we define default aliases rules.
80  aliases_cfg.add("default", "gpio__NO__");
81  module_cfg.add_child("aliases", aliases_cfg);
82  cfg_case_2_ = module_cfg;
83  }
84 
86  {
87  boost::property_tree::ptree aliases_cfg, module_cfg;
88 
89  module_cfg.add("export_path", "/path/to/export");
90  module_cfg.add("unexport_path", "/path/to/unexport");
91  module_cfg.add("value_path", "/path/to/gpios/__PLACEHOLDER__/value");
92  module_cfg.add("edge_path", "/path/to/gpios/__PLACEHOLDER__/edge");
93  module_cfg.add("direction_path", "/path/to/gpios/__PLACEHOLDER__/direction");
94 
95  // we define default aliases rules.
96  aliases_cfg.add("default", "gpio__NO__");
97  // lets say gpio with number 21 is somewhere else in the filesystem
98  aliases_cfg.add("21", "gpio_magic_number");
99  module_cfg.add_child("aliases", aliases_cfg);
100 
101  cfg_case_3_ = module_cfg;
102  }
103 
104  zmqpp::context *ctx_;
105  boost::property_tree::ptree cfg_case_1_;
106  boost::property_tree::ptree cfg_case_2_;
107  boost::property_tree::ptree cfg_case_3_;
108 };
109 
110 TEST_F(SysFsGpioConfigTest, ExportUnexportPath)
111 {
112  SysFsGpioConfig my_config(cfg_case_1_);
113 
114  ASSERT_EQ("/path/to/export", my_config.export_path());
115  ASSERT_EQ("/path/to/unexport", my_config.unexport_path());
116 }
117 
118 TEST_F(SysFsGpioConfigTest, PathAndDefaultAliases)
119 {
120  SysFsGpioConfig my_config(cfg_case_2_);
121 
122  ASSERT_EQ("/random/path/to/gpio14/value", my_config.value_path(14));
123  ASSERT_EQ("/random/path/to/gpio3/value", my_config.value_path(3));
124  ASSERT_EQ("/random/path/to/gpio17/value", my_config.value_path(17));
125 }
126 
127 TEST_F(SysFsGpioConfigTest, MoreComplexeAliases)
128 {
129  SysFsGpioConfig my_config(cfg_case_3_);
130 
131  ASSERT_EQ("/path/to/gpios/gpio14/value", my_config.value_path(14));
132  ASSERT_EQ("/path/to/gpios/gpio3/value", my_config.value_path(3));
133  ASSERT_EQ("/path/to/gpios/gpio_magic_number/value", my_config.value_path(21));
134 
135  ASSERT_EQ("/path/to/gpios/gpio3/edge", my_config.edge_path(3));
136  ASSERT_EQ("/path/to/gpios/gpio_magic_number/edge", my_config.edge_path(21));
137 
138  ASSERT_EQ("/path/to/gpios/gpio3/direction", my_config.direction_path(3));
139  ASSERT_EQ("/path/to/gpios/gpio_magic_number/direction",
140  my_config.direction_path(21));
141 }
142 }
143 }
Leosac::Test::SysFsGpioConfigTest::SysFsGpioConfigTest
SysFsGpioConfigTest()
Definition: SysFsGpioConfig.cpp:40
Leosac::Module::SysFsGpio::SysFsGpioConfig::direction_path
std::string direction_path(int pin_no) const
Compute the absolute path the "direction" file for pin_no.
Definition: SysFsGpioConfig.cpp:93
Leosac::Test::SysFsGpioConfigTest::cfg_case_1_
boost::property_tree::ptree cfg_case_1_
Definition: SysFsGpioConfig.cpp:105
Leosac::Test::SysFsGpioConfigTest::build_config_case2
void build_config_case2()
Definition: SysFsGpioConfig.cpp:68
Leosac::Test::SysFsGpioConfigTest::ctx_
zmqpp::context * ctx_
Definition: SysFsGpioConfig.cpp:104
SysFsGpioConfig.hpp
Leosac::Test::SysFsGpioConfigTest
Note this class doesnt inherits the TestHelper class.
Definition: SysFsGpioConfig.cpp:36
Leosac::Module::SysFsGpio::SysFsGpioConfig
Internal configuration helper for sysfsgpio module.
Definition: SysFsGpioConfig.hpp:40
Leosac::Test::SysFsGpioConfigTest::~SysFsGpioConfigTest
~SysFsGpioConfigTest()
Definition: SysFsGpioConfig.cpp:49
Leosac::Module::SysFsGpio::SysFsGpioConfig::value_path
std::string value_path(int pin_no) const
Compute the absolute path the "value" file for pin_no.
Definition: SysFsGpioConfig.cpp:73
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::SysFsGpio
Namespace for the module that implements GPIO support using the Linux Kernel sysfs interface.
Definition: SysFsGpioConfig.hpp:32
Leosac::Module::SysFsGpio::SysFsGpioConfig::edge_path
std::string edge_path(int pin_no) const
Compute the absolute path the "edge" file for pin_no.
Definition: SysFsGpioConfig.cpp:83
Leosac::Test::SysFsGpioConfigTest::cfg_case_3_
boost::property_tree::ptree cfg_case_3_
Definition: SysFsGpioConfig.cpp:107
Leosac::Test::SysFsGpioConfigTest::build_config_case1
void build_config_case1()
Definition: SysFsGpioConfig.cpp:54
log.hpp
Leosac::Test::SysFsGpioConfigTest::build_config_case3
void build_config_case3()
Definition: SysFsGpioConfig.cpp:85
Leosac::Test::SysFsGpioConfigTest::cfg_case_2_
boost::property_tree::ptree cfg_case_2_
Definition: SysFsGpioConfig.cpp:106
Leosac::Test::TEST_F
TEST_F(SysFsGpioConfigTest, MoreComplexeAliases)
Definition: SysFsGpioConfig.cpp:127
Leosac::Module::SysFsGpio::SysFsGpioConfig::export_path
const std::string & export_path() const
Returns the absolute path to the "export" sysfs file.
Definition: SysFsGpioConfig.cpp:63
Leosac::Module::SysFsGpio::SysFsGpioConfig::unexport_path
const std::string & unexport_path() const
Returns the absolute path to the "unexport" sysfs file.
Definition: SysFsGpioConfig.cpp:68