# See the License for the specific language governing permissions and
# limitations under the License
-SET(TARGET_OSQUERY_LIB osquery)
SET(TARGET_APIX_LIB apix)
+SET(TARGET_OSQUERY_LIB osquery)
+SET(TARGET_POLICYD_LIB policyd)
-ADD_SUBDIRECTORY(osquery)
ADD_SUBDIRECTORY(apix)
-
-IF(DEFINED GBS_BUILD)
- SET(TARGET_POLICYD_LIB policyd)
- ADD_SUBDIRECTORY(policyd)
-ENDIF(DEFINED GBS_BUILD)
+ADD_SUBDIRECTORY(osquery)
+ADD_SUBDIRECTORY(policyd)
SET(${TARGET_POLICYD_LIB}_SRCS "")
SET(${TARGET_POLICYD_LIB}_TESTS "")
-SET(DEPENDENCY appsvc
- aul
- bundle
- capi-base-common
- capi-system-info
- capi-system-system-settings
- cynara-client
- cynara-session
- deviced
- gio-2.0
- glib-2.0
- klay
- libtzplatform-config
- notification
- pkgmgr
- pkgmgr-info
- security-privilege-manager
- sqlite3
- syspopup-caller
- vconf)
+IF(DEFINED GBS_BUILD)
+ SET(DEPENDENCY appsvc
+ aul
+ bundle
+ capi-base-common
+ capi-system-info
+ capi-system-system-settings
+ cynara-client
+ cynara-session
+ deviced
+ gio-2.0
+ glib-2.0
+ klay
+ libtzplatform-config
+ notification
+ pkgmgr
+ pkgmgr-info
+ security-privilege-manager
+ sqlite3
+ syspopup-caller
+ vconf)
-PKG_CHECK_MODULES(POLICYD_DEPS REQUIRED ${DEPENDENCY})
+ PKG_CHECK_MODULES(POLICYD_DEPS REQUIRED ${DEPENDENCY})
-INCLUDE_DIRECTORIES(SYSTEM . ${POLICYD_DEPS_INCLUDE_DIRS})
+ INCLUDE_DIRECTORIES(SYSTEM . ${POLICYD_DEPS_INCLUDE_DIRS})
-ADD_DEFINITIONS(-DDATA_PATH="${DATA_INSTALL_DIR}"
- -DRUN_PATH="${RUN_INSTALL_DIR}"
- -DDB_PATH="${DB_INSTALL_DIR}"
- -DPLUGIN_INSTALL_DIR="${PLUGIN_INSTALL_DIR}"
- -DEVENT_CONFIGURE_DIR="${EVENT_CONFIGURE_DIR}")
+ ADD_DEFINITIONS(-DDATA_PATH="${DATA_INSTALL_DIR}"
+ -DRUN_PATH="${RUN_INSTALL_DIR}"
+ -DDB_PATH="${DB_INSTALL_DIR}"
+ -DPLUGIN_INSTALL_DIR="${PLUGIN_INSTALL_DIR}"
+ -DEVENT_CONFIGURE_DIR="${EVENT_CONFIGURE_DIR}")
+ ADD_DEFINITIONS(-DUG_WAYLAND)
-ADD_SUBDIRECTORY(client)
-ADD_SUBDIRECTORY(conf)
-ADD_SUBDIRECTORY(pil)
-ADD_SUBDIRECTORY(server)
-ADD_SUBDIRECTORY(tools)
+ ADD_SUBDIRECTORY(client)
+ ADD_SUBDIRECTORY(conf)
+ ADD_SUBDIRECTORY(pil)
+ ADD_SUBDIRECTORY(server)
+ ADD_SUBDIRECTORY(tools)
+ ADD_SUBDIRECTORY(ui)
-ADD_DEFINITIONS(-DUG_WAYLAND)
-ADD_SUBDIRECTORY(ui)
+ ADD_LIBRARY(${TARGET_POLICYD_LIB} STATIC ${${TARGET_POLICYD_LIB}_SRCS})
-ADD_LIBRARY(${TARGET_POLICYD_LIB} STATIC ${${TARGET_POLICYD_LIB}_SRCS})
+ TARGET_LINK_LIBRARIES(${TARGET_POLICYD_LIB} ${POLICYD_DEPS_LIBRARIES} pthread dl)
-TARGET_LINK_LIBRARIES(${TARGET_POLICYD_LIB} ${POLICYD_DEPS_LIBRARIES} pthread dl)
+ SET_TARGET_PROPERTIES(${TARGET_POLICYD_LIB} PROPERTIES COMPILE_FLAGS "-fPIE")
+ SET_TARGET_PROPERTIES(${TARGET_POLICYD_LIB} PROPERTIES LINK_FLAGS "-pie")
+ENDIF(DEFINED GBS_BUILD)
-SET_TARGET_PROPERTIES(${TARGET_POLICYD_LIB} PROPERTIES COMPILE_FLAGS "-fPIE")
-SET_TARGET_PROPERTIES(${TARGET_POLICYD_LIB} PROPERTIES LINK_FLAGS "-pie")
+ADD_SUBDIRECTORY(sdk)
ADD_EXECUTABLE(${TARGET_POLICYD_TEST} ../apix/main/tests.cpp
${${TARGET_POLICYD_LIB}_TESTS})
-TARGET_LINK_LIBRARIES(${TARGET_POLICYD_TEST} ${TARGET_POLICYD_LIB}
- gtest)
+TARGET_LINK_LIBRARIES(${TARGET_POLICYD_TEST} gtest
+ pthread)
ADD_TEST(${TARGET_POLICYD_TEST} ${TARGET_POLICYD_TEST})
INSTALL(TARGETS ${TARGET_POLICYD_TEST}
DESTINATION ${CMAKE_INSTALL_BINDIR}
+++ /dev/null
-/*
- * Copyright (c) 2019 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
- */
-
-#pragma once
-
-#include <string>
-#include <unordered_map>
-
-#include <klay/rmi/service.h>
-
-#include "policy-model.h"
-
-class DomainPolicy : public AbstractPolicy {
-public:
- DomainPolicy(const std::string& name) : AbstractPolicy(name) {}
- virtual ~DomainPolicy() = default;
-
- DomainPolicy(const DomainPolicy&) = delete;
- DomainPolicy& operator=(const DomainPolicy&) = delete;
-
- DomainPolicy(DomainPolicy&&) = default;
- DomainPolicy& operator=(DomainPolicy&&) = default;
-
- inline void set(const DataSetInt& val) override
- {
- PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
- PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
-
- std::lock_guard<std::mutex> mtxGuard(mtx);
- this->enforce(admin.getUid());
- }
-
- inline DataSetInt get() override
- {
- uid_t domain = rmi::Service::getPeerUid();
-
- std::lock_guard<std::mutex> mtxGuard(mtx);
- if (!current.count(domain))
- enforce(domain);
-
- return current[domain].value;
- }
-
-private:
- inline void enforce(uid_t domain) override
- {
- auto value = initial;
- if (!ready[domain])
- current[domain] = initial;
-
- PolicyStorage::strictize(id, value, domain);
- if (current[domain] != value) {
- apply(value, domain);
- current[domain] = value;
- }
-
- ready[domain] = true;
- }
-
-private:
- std::unordered_map<uid_t, DataSetInt> current;
- std::unordered_map<uid_t, bool> ready;
-};
+++ /dev/null
-/*
- * Copyright (c) 2019 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
- */
-
-#pragma once
-
-#include <string>
-#include <unordered_map>
-
-#include <klay/rmi/service.h>
-
-#include "policy-model.h"
-
-class GlobalPolicy : public AbstractPolicy {
-public:
- GlobalPolicy(const std::string& name) : AbstractPolicy(name) {}
- virtual ~GlobalPolicy() = default;
-
- GlobalPolicy(const GlobalPolicy&) = delete;
- GlobalPolicy& operator=(const GlobalPolicy&) = delete;
-
- GlobalPolicy(GlobalPolicy&&) = default;
- GlobalPolicy& operator=(GlobalPolicy&&) = default;
-
- inline void set(const DataSetInt& val) override
- {
- PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
- PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
-
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce(-1);
- }
-
- inline DataSetInt get() override
- {
- std::lock_guard<std::mutex> mtxGuard(mtx);
- if (!ready) {
- enforce(-1);
- ready = true;
- }
- return current.value;
- }
-
-private:
- inline void enforce(uid_t) override
- {
- auto value = initial;
- PolicyStorage::strictize(id, value);
- if (current != value) {
- apply(value);
- current = value;
- }
- }
-
-private:
- DataSetInt current;
- bool ready = false;
-};
+++ /dev/null
-/*
- * Copyright (c) 2019 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
- */
-
-#pragma once
-
-#include <string>
-#include <vector>
-#include <mutex>
-#include <memory>
-
-#include <klay/exception.h>
-
-#include "policy-storage.h"
-#include "policy-admin.h"
-#include "observer.h"
-
-class AbstractPolicy;
-
-struct AbstractPolicyProvider {
- AbstractPolicyProvider() {}
- virtual ~AbstractPolicyProvider() {}
-
- std::vector<std::shared_ptr<AbstractPolicy>> policies;
-};
-
-class AbstractPolicy : public Observer {
-public:
- explicit AbstractPolicy(const std::string& name) : name(name)
- {
- this->id = PolicyStorage::define(name, initial);
- if (id < 0)
- throw runtime::Exception("Failed to define policy");
-
- PolicyStorage::subscribeEvent(this);
- }
-
- ~AbstractPolicy() = default;
-
- AbstractPolicy(const AbstractPolicy&) = delete;
- AbstractPolicy& operator=(const AbstractPolicy&) = delete;
-
- AbstractPolicy(AbstractPolicy&&) = default;
- AbstractPolicy& operator=(AbstractPolicy&&) = default;
-
- virtual void set(const DataSetInt& value) = 0;
- virtual DataSetInt get() = 0;
-
- virtual bool apply(const DataSetInt& value, uid_t domain = 0) = 0;
-
- inline void onEvent(uid_t domain)
- {
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce(domain);
- }
-
- std::string name;
-
-protected:
- int id = -1;
- std::mutex mtx;
- DataSetInt initial;
-
-private:
- virtual void enforce(uid_t domain = 0) = 0;
-};
+++ /dev/null
-/*
- * Copyright (c) 2019 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
- */
-
-#include <gtest/gtest.h>
-
-#include "../global-policy.h"
-#include "../domain-policy.h"
-#include "../policy-storage.h"
-#include "../policy-event.h"
-
-/// TODO(Sangwan): Refactor policy-model
-/// => Let client know only global-policy or domain-policy
-
-class PolicyModelTests : public testing::Test {};
-
-class TestGlobalPolicy : public GlobalPolicy {
-public:
- TestGlobalPolicy() : GlobalPolicy("test_policy")
- {
- PolicyEventNotifier::create("test_policy");
- }
-
- bool apply(const DataSetInt&, uid_t)
- {
- PolicyEventNotifier::emit("test_policy", "allowed");
- return true;
- }
-};
-
-TEST_F(PolicyModelTests, global_policy) {
- AbstractPolicyProvider provider;
- provider.policies.emplace_back(std::make_shared<TestGlobalPolicy>());
-
- EXPECT_EQ(provider.policies.size(), 1);
-}
--- /dev/null
+# Copyright (c) 2019 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.
+#
+
+FILE(GLOB SDK_TESTS "tests/*.cpp")
+ADD_POLICYD_TEST(${SDK_TESTS})
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include "policy-model.h"
+
+#include <exception>
+#include <string>
+#include <unordered_map>
+
+#include <sys/types.h>
+
+namespace policyd {
+
+class DomainPolicy : public PolicyModel {
+public:
+ explicit DomainPolicy(std::string name, PolicyValue initial) noexcept :
+ PolicyModel(std::move(name), std::move(initial)) {}
+ virtual ~DomainPolicy() = default;
+
+ DomainPolicy(DomainPolicy&&) = default;
+ DomainPolicy& operator=(DomainPolicy&&) = default;
+
+ inline void set(uid_t domain, const PolicyValue& value) {
+ current[domain] = value;
+
+ try {
+ this->onChanged(domain, value);
+ } catch (const std::exception& e) {
+ current.erase(domain);
+ std::rethrow_exception(std::current_exception());
+ }
+ }
+
+ inline const PolicyValue& get(uid_t domain) const {
+ if (!current.count(domain))
+ throw std::runtime_error("Policy value should be set once before use.");
+
+ return current.at(domain);
+ }
+
+ virtual void onChanged(uid_t domain, const PolicyValue& value) = 0;
+
+private:
+ std::unordered_map<uid_t, PolicyValue> current;
+};
+
+} // namespace policyd
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include "policy-model.h"
+
+#include <exception>
+#include <string>
+
+namespace policyd {
+
+class GlobalPolicy : public PolicyModel {
+public:
+ explicit GlobalPolicy(std::string name, PolicyValue initial) noexcept :
+ PolicyModel(std::move(name), std::move(initial)) {}
+ virtual ~GlobalPolicy() = default;
+
+ GlobalPolicy(GlobalPolicy&&) = default;
+ GlobalPolicy& operator=(GlobalPolicy&&) = default;
+
+ inline void set(const PolicyValue& value) {
+ current = value;
+ ready = true;
+
+ try {
+ this->onChanged(value);
+ } catch (const std::exception& e) {
+ ready = false;
+ std::rethrow_exception(std::current_exception());
+ }
+ }
+
+ inline const PolicyValue& get() const {
+ if (!ready)
+ throw std::runtime_error("Policy value should be set once before use.");
+
+ return current;
+ }
+
+ virtual void onChanged(const PolicyValue& value) = 0;
+
+private:
+ PolicyValue current;
+ bool ready = false;
+};
+
+} // namespace policyd
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include"policy-value.h"
+
+#include <string>
+
+namespace policyd {
+
+class PolicyModel {
+public:
+ explicit PolicyModel(std::string name, PolicyValue initial) noexcept :
+ name(std::move(name)), initial(std::move(initial)) {}
+ virtual ~PolicyModel() = default;
+
+ PolicyModel(const PolicyModel&) = delete;
+ PolicyModel& operator=(const PolicyModel&) = delete;
+
+ PolicyModel(PolicyModel&&) = default;
+ PolicyModel& operator=(PolicyModel&&) = default;
+
+ const std::string& getName() const noexcept { return name; }
+ const PolicyValue& getInitial() const noexcept { return initial; }
+
+protected:
+ std::string name;
+ PolicyValue initial;
+};
+
+} // namespace policyd
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include "domain-policy.h"
+#include "global-policy.h"
+
+#include <cstddef>
+#include <unordered_map>
+
+namespace policyd {
+
+class PolicyProvider final {
+public:
+ explicit PolicyProvider(std::string name) noexcept : name(std::move(name)) {}
+
+ inline void add(std::shared_ptr<GlobalPolicy>&& policy) {
+ global.emplace(policy->getName(), std::move(policy));
+ }
+
+ inline void add(std::shared_ptr<DomainPolicy>&& policy) {
+ domain.emplace(policy->getName(), std::move(policy));
+ }
+
+ inline const std::string& getName() const noexcept { return name; }
+
+ std::size_t gsize() { return global.size(); }
+ std::size_t dsize() { return domain.size(); }
+
+private:
+ std::string name;
+ std::unordered_map<std::string, std::shared_ptr<GlobalPolicy>> global;
+ std::unordered_map<std::string, std::shared_ptr<DomainPolicy>> domain;
+};
+
+using PolicyFactory = PolicyProvider* (*)();
+
+} // namespace policyd
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#pragma once
+
+namespace policyd {
+
+// TODO: Support various value type
+struct PolicyValue final {
+ explicit PolicyValue(int value) noexcept : value(value) {}
+ explicit PolicyValue() noexcept = default;
+ ~PolicyValue() = default;
+
+ PolicyValue(const PolicyValue&) noexcept = default;
+ PolicyValue& operator=(const PolicyValue&) noexcept = default;
+
+ PolicyValue(PolicyValue&&) noexcept = default;
+ PolicyValue& operator=(PolicyValue&&) noexcept = default;
+
+ PolicyValue& operator=(int val) {
+ value = val;
+ return *this;
+ }
+
+ operator int() const { return value; }
+ bool operator==(const PolicyValue& rhs) const { return value == rhs.value; }
+ bool operator!=(const PolicyValue& rhs) const { return value != rhs.value; }
+
+ int value = -1;
+};
+
+} // namespace policyd
--- /dev/null
+/*
+ * Copyright (c) 2019 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
+ */
+
+#include <gtest/gtest.h>
+
+#include "../domain-policy.h"
+#include "../global-policy.h"
+#include "../policy-provider.h"
+
+#include <exception>
+
+namespace {
+ int g_value = -1;
+ int d_value = -1;
+ uid_t d_uid = 0;
+} // anonymous namespace
+
+using namespace policyd;
+
+class PolicyModelTests : public testing::Test {};
+
+class TestGlobalPolicy : public GlobalPolicy {
+public:
+ TestGlobalPolicy() : GlobalPolicy("test_policy", PolicyValue(1)) {}
+
+ virtual void onChanged(const PolicyValue& value) {
+ g_value = value;
+ }
+};
+
+TEST_F(PolicyModelTests, global_policy) {
+ TestGlobalPolicy policy;
+
+ EXPECT_EQ(policy.getName(), "test_policy");
+ EXPECT_EQ(policy.getInitial(), 1);
+
+ // Policy value should be set once before use
+ bool isRaised = false;
+ try {
+ auto value = policy.get();
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ EXPECT_TRUE(isRaised);
+
+ policy.set(PolicyValue(3));
+ EXPECT_EQ(3, g_value);
+ EXPECT_EQ(3, policy.get());
+}
+
+class TestDomainPolicy : public DomainPolicy {
+public:
+ TestDomainPolicy() : DomainPolicy("test_policy", PolicyValue(1)) {}
+
+ virtual void onChanged(uid_t domain, const PolicyValue& value) {
+ d_uid = domain;
+ d_value = value;
+ }
+};
+
+TEST_F(PolicyModelTests, domain_policy) {
+ TestDomainPolicy policy;
+ uid_t domain = 5001;
+
+ EXPECT_EQ(policy.getName(), "test_policy");
+ EXPECT_EQ(policy.getInitial(), 1);
+
+ // Policy value should be set once before use
+ bool isRaised = false;
+ try {
+ auto value = policy.get(domain);
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ EXPECT_TRUE(isRaised);
+
+ policy.set(domain, PolicyValue(3));
+ EXPECT_EQ(d_uid, domain);
+ EXPECT_EQ(3, policy.get(domain));
+}
+
+TEST_F(PolicyModelTests, policy_provider) {
+ PolicyProvider provider("testProvider");
+ provider.add(std::make_shared<TestGlobalPolicy>());
+ provider.add(std::make_shared<TestDomainPolicy>());
+
+ EXPECT_EQ(1, provider.gsize());
+ EXPECT_EQ(1, provider.dsize());
+}