• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #define private public
18 #include "active_status_callback_manager.h"
19 #undef private
20 #include "perm_active_status_change_callback_stub.h"
21 #include "privacy_error.h"
22 #include "token_setproc.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 namespace {
30 static bool g_isAddSucc = true;
31 }
32 
33 class ActiveStatusCallbackManagerTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void ActiveStatusCallbackManagerTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void ActiveStatusCallbackManagerTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void ActiveStatusCallbackManagerTest::SetUp()
50 {
51 }
52 
TearDown()53 void ActiveStatusCallbackManagerTest::TearDown()
54 {
55 }
56 
57 class PermActiveStatusChangeCallbackTest : public PermActiveStatusChangeCallbackStub {
58 public:
59     PermActiveStatusChangeCallbackTest() = default;
60     virtual ~PermActiveStatusChangeCallbackTest() = default;
61 
62     void ActiveStatusChangeCallback(ActiveChangeResponse& result) override;
63     bool AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& deathRecipient) override;
IsProxyObject() const64     bool IsProxyObject() const override
65     {
66         return true;
67     };
68 };
69 
ActiveStatusChangeCallback(ActiveChangeResponse & result)70 void PermActiveStatusChangeCallbackTest::ActiveStatusChangeCallback(ActiveChangeResponse& result)
71 {
72 }
73 
AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)74 bool PermActiveStatusChangeCallbackTest::AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& deathRecipient)
75 {
76     return g_isAddSucc == true ? true : false;
77 }
78 
79 class PermActiveStatusChangeCallbackTest1 : public PermActiveStatusChangeCallbackStub {
80 public:
81     PermActiveStatusChangeCallbackTest1() = default;
82     virtual ~PermActiveStatusChangeCallbackTest1() = default;
83 
84     void ActiveStatusChangeCallback(ActiveChangeResponse& result) override;
85 };
86 
ActiveStatusChangeCallback(ActiveChangeResponse & result)87 void PermActiveStatusChangeCallbackTest1::ActiveStatusChangeCallback(ActiveChangeResponse& result)
88 {
89 }
90 
91 /*
92  * @tc.name: AddCallback001
93  * @tc.desc: AddCallback mock function test
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(ActiveStatusCallbackManagerTest, AddCallback001, TestSize.Level0)
98 {
99     // backup
100     bool temp = g_isAddSucc;
101     std::vector<CallbackData> callbackDataList = ActiveStatusCallbackManager::GetInstance().callbackDataList_;
102     ActiveStatusCallbackManager::GetInstance().callbackDataList_.clear();
103 
104     sptr<IRemoteObject> callback;
105     std::vector<std::string> permList;
106     permList.emplace_back("ohos.permission.CAMERA");
107 
108     int32_t ret = ActiveStatusCallbackManager::GetInstance().AddCallback(GetSelfTokenID(), permList, callback);
109     EXPECT_EQ(ret, ERR_PARAM_INVALID);
110 
111     wptr<IRemoteObject> remote = new (std::nothrow) PermActiveStatusChangeCallbackTest();
112     callback = remote.promote();
113     ret = ActiveStatusCallbackManager::GetInstance().AddCallback(GetSelfTokenID(), permList, callback);
114     EXPECT_EQ(ret, RET_SUCCESS);
115 
116     g_isAddSucc = false;
117     ret = ActiveStatusCallbackManager::GetInstance().AddCallback(GetSelfTokenID(), permList, callback);
118     EXPECT_EQ(ret, ERR_ADD_DEATH_RECIPIENT_FAILED);
119 
120     // recovery
121     g_isAddSucc = temp;
122     ActiveStatusCallbackManager::GetInstance().callbackDataList_ = callbackDataList;
123 }
124 
125 /*
126  * @tc.name: AddCallback002
127  * @tc.desc: AddCallback function test
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(ActiveStatusCallbackManagerTest, AddCallback002, TestSize.Level0)
132 {
133     // backup
134     std::vector<CallbackData> callbackDataList = ActiveStatusCallbackManager::GetInstance().callbackDataList_;
135     ActiveStatusCallbackManager::GetInstance().callbackDataList_.clear();
136 
137     sptr<IRemoteObject> callback;
138     std::vector<std::string> permList;
139     permList.emplace_back("ohos.permission.CAMERA");
140 
141     wptr<IRemoteObject> remote = new (std::nothrow) PermActiveStatusChangeCallbackTest1();
142     callback = remote.promote();
143     int32_t ret = ActiveStatusCallbackManager::GetInstance().AddCallback(GetSelfTokenID(), permList, callback);
144     EXPECT_EQ(ret, RET_SUCCESS);
145 
146     // recovery
147     ActiveStatusCallbackManager::GetInstance().callbackDataList_ = callbackDataList;
148 }
149 } // namespace AccessToken
150 } // namespace Security
151 } // namespace OHOS
152