refactoring: make BusType a local type 03/202903/6
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 5 Apr 2019 10:47:19 +0000 (12:47 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Thu, 11 Apr 2019 15:16:45 +0000 (17:16 +0200)
BusType does not need to be a global type, because in fact
it is needed only to choose a correct policy checker.

In the test cases, or in the command line tools we explicitly know
which checker we need to choose.

Thus, it is only required for the API, where we still keep it
in the configuration.

This will allow further simplifications.

Change-Id: I61663d496279b47a6a0338b93434b5ae42326f33

src/dbuspolicy_serializer.cpp
src/internal/internal.h [deleted file]
src/internal/naive_policy_checker.hpp
src/internal/policy.hpp
src/internal/serializer.cpp
src/libdbuspolicy1.cpp
src/stest_performance.cpp
src/test-libdbuspolicy1-access-deny-gdi.cpp
src/test-libdbuspolicy1-access-deny.cpp
src/test-libdbuspolicy1-ownership-deny-gdi.cpp

index deb009a..7ca1c41 100644 (file)
@@ -3,7 +3,6 @@
 
 #include <iostream>
 #include <fstream>
-#include "internal/internal.h"
 #include "internal/serializer.hpp"
 #include "internal/naive_policy_checker.hpp"
 #include "dbuspolicy1/libdbuspolicy1.h"
@@ -27,10 +26,9 @@ static void print_help(const char *name) {
 
 int main(int argc, char *argv[])
 {
-       BusType bus_type = BusType::SYSTEM_BUS;
        bool need_input_filename = true;
        std::string output_filename;
-       std::string input_filename;
+       const char *input_filename = system_bus_conf_file_primary();
        int c;
 
        while (1) {
@@ -42,7 +40,7 @@ int main(int argc, char *argv[])
                switch(c) {
                case 0:
                        if (option_index == 1)
-                               bus_type = BusType::SESSION_BUS;
+                               input_filename = session_bus_conf_file_primary();
                        need_input_filename = false;
                        break;
                case 'o':
@@ -58,19 +56,10 @@ int main(int argc, char *argv[])
                        print_help(argv[0]);
                        return 1;
                }
-       } else {
-               switch (bus_type) {
-               case BusType::SYSTEM_BUS:
-                       input_filename = system_bus_conf_file_primary();
-                       break;
-               case BusType::SESSION_BUS:
-                       input_filename = session_bus_conf_file_primary();
-                       break;
-               }
        }
 
        if (output_filename.empty())
-               output_filename = input_filename + ".serialized";
+               output_filename = std::string(input_filename) + ".serialized";
 
        cout << "Read from: " << input_filename << " write to: " << output_filename << endl;
        ldp_xml_parser::Serializer serializer;
diff --git a/src/internal/internal.h b/src/internal/internal.h
deleted file mode 100644 (file)
index 9cd1516..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
-*/
-
-
-/**
-       \defgroup Implementation Implementation
-       Internal implementation of policy.
- */
-/**
- * \file
- * \ingroup Implementation
- */
-#ifndef _LIBDBUSPOLICY1_INTERNAL_H_
-#define _LIBDBUSPOLICY1_INTERNAL_H_
-
-typedef enum {
-       SYSTEM_BUS = 0,
-       SESSION_BUS = 1
-} BusType;
-
-#endif
index baf1b85..d92fba2 100644 (file)
@@ -138,10 +138,4 @@ namespace ldp_xml_parser
 DCL_NODESTRUCT_GLOBAL(ldp_xml_parser::NaivePolicyChecker, policy_checker_system)
 DCL_NODESTRUCT_GLOBAL(ldp_xml_parser::NaivePolicyChecker, policy_checker_session)
 
-inline ldp_xml_parser::NaivePolicyChecker &policy_checker(BusType bus_type) {
-       if (SESSION_BUS == bus_type)
-               return policy_checker_session();
-       return policy_checker_system();
-}
-
 #endif
index f7593b2..51a4322 100644 (file)
@@ -25,7 +25,6 @@
 
 #include "type_list.h"
 #include "bus_names_array.hpp"
-#include "internal.h"
 
 #include <boost/utility/string_ref.hpp>
 #include <string>
index 869a718..578157a 100644 (file)
@@ -15,7 +15,6 @@
 */
 #include "include/fb_generated.h"
 #include "include/flatbuffers/flatbuffers.h"
-#include "internal.h"
 #include "naive_policy_checker.hpp"
 #include "serializer.hpp"
 #include "tslog.hpp"
index a2fdde4..c15aebd 100644 (file)
@@ -44,6 +44,11 @@ using ldp_xml_parser::MatchItemSend;
 using ldp_xml_parser::MatchItemReceive;
 using ldp_xml_parser::MatchItemOwn;
 
+typedef enum {
+       SYSTEM_BUS = 0,
+       SESSION_BUS = 1
+} BusType;
+
 struct kconn {
        KdbusConnection conn;
        BusType bus_type;
@@ -59,6 +64,12 @@ static std::once_flag init_once_done;
 static std::once_flag init_once_db[2];
 static std::once_flag init_once_conn[2];
 
+inline ldp_xml_parser::NaivePolicyChecker &policy_checker(BusType bus_type) {
+       if (SESSION_BUS == bus_type)
+               return policy_checker_session();
+       return policy_checker_system();
+}
+
 extern "C" void __dbuspolicy1_change_creds(uid_t uid, gid_t gid, const char* label)
 {
        g_udesc.uid = uid;
index 9b2e7ba..2f1c643 100644 (file)
@@ -1,5 +1,4 @@
 #include "internal/include/fb_generated.h"
-#include "internal/internal.h"
 #include "internal/naive_policy_checker.hpp"
 #include "internal/policy.hpp"
 #include "internal/serializer.hpp"
@@ -7,7 +6,6 @@
 #include "internal/storage_backend_serialized.hpp"
 #include "internal/tslog.hpp"
 #include "libdbuspolicy1-private.h"
-#include <dbuspolicy1/libdbuspolicy1.h>
 #include <getopt.h>
 #include <iostream>
 #include <map>
index 5b35919..dbad42a 100644 (file)
@@ -1,5 +1,4 @@
 #include "internal/include/fb_generated.h"
-#include "internal/internal.h"
 #include "internal/naive_policy_checker.hpp"
 #include "internal/policy.hpp"
 #include "internal/serializer.hpp"
@@ -89,11 +88,8 @@ void print_test(const struct AccessTest* t, bool result) {
 }
 
 template <typename DB>
-bool run_tests_for_bus(const DB &db, const BusType bus_type, const std::vector<AccessTest>& test_setup, int& i, bool& passed) {
-
+bool run_tests_for_bus(const DB &db, NaivePolicyChecker &checker, const std::vector<AccessTest>& test_setup, int& i, bool& passed) {
        for (const auto& test : test_setup) {
-               auto &checker = policy_checker(bus_type);
-
                checker.updateGroupDb(test.user, test.group);
 
                const auto &gids = *checker.getGroups<MatchItemAccess>(test.user, test.group);
@@ -125,17 +121,17 @@ bool run_policy_db(const std::pair<TestBusSetup, TestBusSetup> access_test) {
        const auto& system_bus_setup = access_test.first;
        const auto& session_bus_setup = access_test.second;
 
-       policy_checker_system().initDb(system_bus_setup.first.c_str());
+       auto &checker_system = policy_checker_system();
+       auto &checker_session = policy_checker_session();
+
+       checker_system.initDb(system_bus_setup.first.c_str());
        if (session_bus_setup.first != "") {
-               policy_checker_session().initDb(session_bus_setup.first.c_str());
+               checker_session.initDb(session_bus_setup.first.c_str());
        }
 
-       auto *sys_db = &policy_checker_system().getPolicyDb();
-       auto *ses_db = &policy_checker_session().getPolicyDb();
-
        printf("POLICY_DB:\n");
-       return run_tests_for_bus(*sys_db, SYSTEM_BUS, system_bus_setup.second, i, passed) &&
-               run_tests_for_bus(*ses_db, SESSION_BUS, session_bus_setup.second, i, passed);
+       return run_tests_for_bus(checker_system.getPolicyDb(), checker_system, system_bus_setup.second, i, passed) &&
+               run_tests_for_bus(checker_session.getPolicyDb(), checker_session, session_bus_setup.second, i, passed);
 }
 
 bool run_fb(const std::pair<TestBusSetup, TestBusSetup> access_test) {
@@ -159,12 +155,12 @@ bool run_fb(const std::pair<TestBusSetup, TestBusSetup> access_test) {
 
        const FB::File *file_sys = FB::GetFile(buff_sys);
        storage_sys.init(file_sys);
-       bool res = run_tests_for_bus(storage_sys, SYSTEM_BUS, system_bus_setup.second, i, passed);
+       bool res = run_tests_for_bus(storage_sys, policy_checker_system(), system_bus_setup.second, i, passed);
 
        if (buff_ses) {
                const FB::File *file_ses = FB::GetFile(buff_ses);
                storage_ses.init(file_ses);
-               res &= run_tests_for_bus(storage_ses, SESSION_BUS, session_bus_setup.second, i, passed);
+               res &= run_tests_for_bus(storage_ses, policy_checker_session(), session_bus_setup.second, i, passed);
        }
        return res;
 }
index d74144e..e7e80af 100644 (file)
@@ -1,4 +1,3 @@
-#include "internal/internal.h"
 #include "internal/naive_policy_checker.hpp"
 #include "internal/tslog.hpp"
 #include <dbuspolicy1/libdbuspolicy1.h>
@@ -81,9 +80,8 @@ void print_test(const struct AccessTest* t, DecisionResult result) {
                   DECISIONS[t->expected_result], DECISIONS[result]);
 }
 
-void run_tests_for_bus(BusType bus_type, const std::vector<AccessTest>& test_setup, int& i, bool& passed) {
+void run_tests_for_bus(ldp_xml_parser::NaivePolicyChecker &checker, const std::vector<AccessTest>& test_setup, int& i, bool& passed) {
        for (const auto& test : test_setup) {
-               auto &checker = policy_checker(bus_type);
                checker.updateGroupDb(test.user, test.group);
                auto decision = checker.check(bus_owner, test.user, test.group, test.label);
                if (decision != test.expected_result) {
@@ -109,8 +107,8 @@ bool run_access_tests() {
                        policy_checker_session().initDb(session_bus_setup.first.c_str());
                }
 
-               run_tests_for_bus(SYSTEM_BUS, system_bus_setup.second, i, passed);
-               run_tests_for_bus(SESSION_BUS, session_bus_setup.second, i, passed);
+               run_tests_for_bus(policy_checker_system(), system_bus_setup.second, i, passed);
+               run_tests_for_bus(policy_checker_session(), session_bus_setup.second, i, passed);
        }
        return passed;
 }
index a4f7f67..3d86a31 100644 (file)
@@ -1,5 +1,4 @@
 #include "internal/include/fb_generated.h"
-#include "internal/internal.h"
 #include "internal/naive_policy_checker.hpp"
 #include "internal/policy.hpp"
 #include "internal/serializer.hpp"