refactoring: move Serializer to ldp_serializer namespace
[platform/core/system/libdbuspolicy.git] / src / test-libdbuspolicy1-signal-gdi.cpp
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"
8 #include <map>
9
10 using namespace ldp_xml_parser;
11
12 struct SignalTest {
13         Decision expected_result;
14         uid_t user;
15         gid_t group;
16         const char* label;
17         const char* dest;
18         const char* interface;
19 };
20
21 std::map<Decision, const char*> DECISIONS {
22         { Decision::ANY,   "ANY"   },
23         { Decision::ALLOW, "ALLOW" },
24         { Decision::DENY,  "DENY"  },
25         { Decision::CHECK, "CHECK" }
26 };
27
28 /**
29  * This test set tests ability to parse xml db
30  * and check signal call allowance in many use cases
31  */
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"},
35 };
36
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]);
40 }
41
42 template <typename DB>
43 bool signal_test(const DB &db) {
44         unsigned  i = 0;
45         bool flag = true;
46         for (const auto &test : signal_tests) {
47                 KdbusBusNames names;
48                 MatchItemSend m_item(test.interface, NULL, NULL, ldp_xml_parser::MessageType::SIGNAL,
49                                 names.addSpaceSeparatedNames(test.dest));
50
51                 auto ret = db.getDecisionItemContextMandatory(m_item);
52
53                 if (ret.getDecision() == Decision::ANY)
54                         ret = db.getDecisionItemUser(test.user, m_item);
55
56                 if (ret.getDecision() == Decision::ANY)
57                         ret = db.getDecisionItemGroup(test.group, m_item);
58
59                 if (ret.getDecision() == Decision::ANY)
60                         ret = db.getDecisionItemContextDefault(m_item);
61
62                 auto decision = ret.getDecision();
63
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);
67                         printf("\n");
68                         flag = false;
69                 }
70         }
71         return flag;
72 }
73
74 bool run_policy_db() {
75         policy_checker_system().initDb("tests/default_allow/system.conf");
76         auto &db = policy_checker_system().getPolicyDb();
77
78         printf("POLICY_DB:\n");
79         return signal_test(db);
80 }
81
82 bool run_fb() {
83         ldp_serializer::Serializer serializer;
84         size_t size;
85         uint8_t *buff = serializer.serialize("tests/default_allow/system.conf", size);
86
87         ldp_serialized::StorageBackendSerialized storage;
88         storage.initFromData(buff);
89
90         printf("FLATBUFFERS:\n");
91         return signal_test(storage);
92 }
93
94 bool run_xml() {
95         return true;
96 }
97
98 bool run_tests() {
99         return run_policy_db() && run_fb() && run_xml();
100 }
101
102 int main() {
103         tslog::init();
104         if (!run_tests())
105                 return -1;
106 return 0;
107 }