1 #include "internal/include/fb_generated.h"
2 #include "internal/naive_policy_checker.hpp"
3 #include "internal/policy.hpp"
4 #include "internal/serializer.hpp"
5 #include "internal/storage_backend_serialized.hpp"
6 #include "internal/storage_backend_serialized.hpp"
7 #include "internal/tslog.hpp"
10 using namespace ldp_xml_parser;
13 Decision expected_result;
18 const char* interface;
21 std::map<Decision, const char*> DECISIONS {
22 { Decision::ANY, "ANY" },
23 { Decision::ALLOW, "ALLOW" },
24 { Decision::DENY, "DENY" },
25 { Decision::CHECK, "CHECK" }
29 * This test set tests ability to parse xml db
30 * and check signal call allowance in many use cases
32 struct SignalTest signal_tests[]={
33 (struct SignalTest){Decision::ALLOW, 0, 0, "test", "bli.bla.blubb test.test1 test.tes3", "/an/object/path"},
34 (struct SignalTest){Decision::DENY, 5010, 0, "test", "bli.bla.blubb", "/an/object/path"},
37 void signalTest_print(const struct SignalTest* t, Decision result) {
38 printf("uid = %lu, gid = %lu, label = %s, dest = %s, interface = %s, expected = %s, result = %s",
39 (unsigned long)t->user, (unsigned long)t->group, t->label, t->dest, t->interface, DECISIONS[t->expected_result], DECISIONS[result]);
42 template <typename DB>
43 bool signal_test(const DB &db) {
46 for (const auto &test : signal_tests) {
48 MatchItemSend m_item(test.interface, NULL, NULL, ldp_xml_parser::MessageType::SIGNAL,
49 names.addSpaceSeparatedNames(test.dest));
51 auto ret = db.getDecisionItemContextMandatory(m_item);
53 if (ret.getDecision() == Decision::ANY)
54 ret = db.getDecisionItemUser(test.user, m_item);
56 if (ret.getDecision() == Decision::ANY)
57 ret = db.getDecisionItemGroup(test.group, m_item);
59 if (ret.getDecision() == Decision::ANY)
60 ret = db.getDecisionItemContextDefault(m_item);
62 auto decision = ret.getDecision();
64 if (test.expected_result != decision) {
65 printf("[ERROR][%d] signal test failed: %s %s ", i, DECISIONS[test.expected_result], DECISIONS[decision]);
66 signalTest_print(&test, decision);
74 bool run_policy_db() {
75 policy_checker_system().initDb("tests/default_allow/system.conf");
76 auto &db = policy_checker_system().getPolicyDb();
78 printf("POLICY_DB:\n");
79 return signal_test(db);
83 ldp_serializer::Serializer serializer;
85 uint8_t *buff = serializer.serialize("tests/default_allow/system.conf", size);
87 ldp_serialized::StorageBackendSerialized storage;
88 storage.initFromData(buff);
90 printf("FLATBUFFERS:\n");
91 return signal_test(storage);
99 return run_policy_db() && run_fb() && run_xml();