Add unit tests for cion module
[platform/core/appfw/event-system.git] / tests / dbus_event_unit_tests / src / test_privilege_checker.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <eventsystem_internal.h>
20 #include <security-manager.h>
21
22 #include "cynara_mock.h"
23 #include "security_manager_mock.h"
24 #include "test_fixture.h"
25 #include "dbus_event/privilege_checker.hh"
26
27
28 // using namespace tizen_base;
29 using ::testing::_;
30 using ::testing::DoAll;
31 using ::testing::Invoke;
32 using ::testing::Return;
33 using ::testing::SetArgPointee;
34
35 namespace {
36
37 class Mocks : public ::testing::NiceMock<CynaraMock>,
38               public ::testing::NiceMock<SecurityManagerMock>{};
39
40 }  //namespace
41
42 class PrivilegeCheckerTest : public TestFixture {
43  public:
44   PrivilegeCheckerTest() : TestFixture(std::make_unique<Mocks>()) {}
45   virtual ~PrivilegeCheckerTest() {}
46
47   virtual void SetUp() {
48     EXPECT_CALL(GetMock<CynaraMock>(),
49       cynara_initialize(_, _)).WillRepeatedly(
50           Invoke([this](cynara** cynara, const cynara_configuration* conf) {
51             *cynara = this->cynara_;
52             return CYNARA_API_SUCCESS;
53           }));
54
55     cynara_ = (cynara*)malloc(1);
56     checker_ = std::make_unique<esd::module::PrivilegeChecker>();
57   }
58
59   virtual void TearDown() {
60     EXPECT_CALL(GetMock<CynaraMock>(),
61       cynara_finish(_)).WillRepeatedly(Return(CYNARA_API_SUCCESS));
62     checker_.reset();
63     free(cynara_);
64   }
65
66   cynara* cynara_;
67   std::unique_ptr<esd::module::PrivilegeChecker> checker_;
68 };
69
70 TEST_F(PrivilegeCheckerTest, CheckPrivilegeName) {
71   std::string priv = checker_->CheckPrivilegeName(std::string(SYS_EVENT_WIFI_STATE));
72   EXPECT_NE(priv, "");
73 }
74
75 TEST_F(PrivilegeCheckerTest, CheckPrivilegeName_N) {
76   std::string event_name("wifi_state");
77   std::string priv = checker_->CheckPrivilegeName(event_name);
78   EXPECT_EQ(priv, "");
79 }
80
81 TEST_F(PrivilegeCheckerTest, AppHasPrivilege) {
82   EXPECT_CALL(GetMock<SecurityManagerMock>(),
83       security_manager_app_has_privilege(_, _, _, _))
84           .WillOnce(DoAll(SetArgPointee<3>(1), Return(SECURITY_MANAGER_SUCCESS)));
85   uid_t uid = 5001;
86   std::string app_id("app_id");
87   std::string pkg_id("pkg_id");
88   bool ret = checker_->AppHasPrivilege(uid, app_id, SYS_EVENT_WIFI_STATE);
89   EXPECT_EQ(ret, true);
90 }
91
92 TEST_F(PrivilegeCheckerTest, Check) {
93   char* session = strdup("session");
94   EXPECT_CALL(GetMock<CynaraMock>(),
95       cynara_creds_gdbus_get_client(_, _, _, _))
96           .WillOnce(Return(CYNARA_API_SUCCESS));
97   EXPECT_CALL(GetMock<CynaraMock>(),
98       cynara_creds_gdbus_get_user(_, _, _, _))
99           .WillOnce(Return(CYNARA_API_SUCCESS));
100   EXPECT_CALL(GetMock<CynaraMock>(),
101       cynara_session_from_pid(_))
102           .WillOnce(Return(session));
103   EXPECT_CALL(GetMock<CynaraMock>(),
104       cynara_check(_, _, _, _, _))
105           .WillOnce(Return(CYNARA_API_ACCESS_ALLOWED));
106
107   std::string privilege("test");
108   bool ret = checker_->Check(nullptr, nullptr, 10, privilege);
109   EXPECT_EQ(ret, true);
110 }