2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <eventsystem_internal.h>
20 #include <security-manager.h>
22 #include "mock/cynara_mock.h"
23 #include "mock/security_manager_mock.h"
24 #include "mock/test_fixture.h"
25 #include "dbus_event/privilege_checker.hh"
28 // using namespace tizen_base;
30 using ::testing::DoAll;
31 using ::testing::Invoke;
32 using ::testing::Return;
33 using ::testing::SetArgPointee;
37 class Mocks : public ::testing::NiceMock<CynaraMock>,
38 public ::testing::NiceMock<SecurityManagerMock>{};
42 class PrivilegeCheckerTest : public TestFixture {
44 PrivilegeCheckerTest() : TestFixture(std::make_unique<Mocks>()) {}
45 virtual ~PrivilegeCheckerTest() {}
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;
55 cynara_ = (cynara*)malloc(1);
56 checker_ = std::make_unique<esd::module::PrivilegeChecker>();
59 virtual void TearDown() {
60 EXPECT_CALL(GetMock<CynaraMock>(),
61 cynara_finish(_)).WillRepeatedly(Return(CYNARA_API_SUCCESS));
67 std::unique_ptr<esd::module::PrivilegeChecker> checker_;
70 TEST_F(PrivilegeCheckerTest, CheckPrivilegeName) {
71 std::string priv = checker_->CheckPrivilegeName(std::string(SYS_EVENT_WIFI_STATE));
75 TEST_F(PrivilegeCheckerTest, CheckPrivilegeName_N) {
76 std::string event_name("wifi_state");
77 std::string priv = checker_->CheckPrivilegeName(event_name);
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)));
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);
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));
107 std::string privilege("test");
108 bool ret = checker_->Check(nullptr, nullptr, 10, privilege);
109 EXPECT_EQ(ret, true);