• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "proxy_death_handler.h"
19 #include "privacy_manager_proxy_death_param.h"
20 #include "proxy_death_callback_stub.h"
21 #undef private
22 #include "constant.h"
23 #include "permission_record_set.h"
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 
31 class PrivacyManagerProxyDeathTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 };
38 
SetUpTestCase()39 void PrivacyManagerProxyDeathTest::SetUpTestCase()
40 {
41 }
42 
TearDownTestCase()43 void PrivacyManagerProxyDeathTest::TearDownTestCase()
44 {
45 }
46 
SetUp()47 void PrivacyManagerProxyDeathTest::SetUp()
48 {
49 }
50 
TearDown()51 void PrivacyManagerProxyDeathTest::TearDown()
52 {
53 }
54 
55 /**
56  * @tc.name: PrivacyManagerProxyDeathTest001
57  * @tc.desc: AddProxyStub test.
58  * @tc.type: FUNC
59  * @tc.require:
60  */
61 HWTEST_F(PrivacyManagerProxyDeathTest, PrivacyManagerProxyDeathTest001, TestSize.Level0)
62 {
63     auto handler = std::make_shared<ProxyDeathHandler>();
64     auto anonyStub = new (std::nothrow) ProxyDeathCallBackStub();
65     int32_t callerPid = 456; // 456 is random input
66     std::shared_ptr<ProxyDeathParam> param = std::make_shared<PrivacyManagerProxyDeathParam>(callerPid);
67     handler->AddProxyStub(anonyStub->AsObject(), param);
68     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1);
69     handler->AddProxyStub(anonyStub->AsObject(), param);
70     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1); // has inserted
71 
72     auto anonyStub2 = new (std::nothrow) ProxyDeathCallBackStub();
73     std::shared_ptr<ProxyDeathParam> param2 = std::make_shared<PrivacyManagerProxyDeathParam>(callerPid);
74     handler->AddProxyStub(anonyStub2->AsObject(), param2);
75     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 2);
76 }
77 
78 /**
79  * @tc.name: PrivacyManagerProxyDeathTest001
80  * @tc.desc: ReleaseProxyByParam test.
81  * @tc.type: FUNC
82  * @tc.require:
83  */
84 HWTEST_F(PrivacyManagerProxyDeathTest, PrivacyManagerProxyDeathTest002, TestSize.Level0)
85 {
86     auto handler = std::make_shared<ProxyDeathHandler>();
87     auto anonyStub = new (std::nothrow) ProxyDeathCallBackStub();
88     int32_t callerPid = 456; // 456 is random input
89     std::shared_ptr<ProxyDeathParam> param = std::make_shared<PrivacyManagerProxyDeathParam>(callerPid);
90     handler->AddProxyStub(anonyStub->AsObject(), param);
91     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1);
92 
93     auto anonyStub2 = new (std::nothrow) ProxyDeathCallBackStub();
94     std::shared_ptr<ProxyDeathParam> param2 = std::make_shared<PrivacyManagerProxyDeathParam>(callerPid);
95     handler->AddProxyStub(anonyStub2->AsObject(), param2);
96     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 2);
97 
98     handler->ReleaseProxyByParam(param);
99     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 0);
100 }
101 
102 class TestProxyDeathParam : public ProxyDeathParam {
103 public:
ProcessParam()104     void ProcessParam() override
105     {
106         status = true;
107     }
IsEqual(ProxyDeathParam *)108     bool IsEqual(ProxyDeathParam *) override
109     {
110         return status;
111     }
112     bool status = false;
113 };
114 
115 /**
116  * @tc.name: PrivacyManagerProxyDeathTest003
117  * @tc.desc: HandleRemoteDied test.
118  * @tc.type: FUNC
119  * @tc.require:
120  */
121 HWTEST_F(PrivacyManagerProxyDeathTest, PrivacyManagerProxyDeathTest003, TestSize.Level0)
122 {
123     auto handler = std::make_shared<ProxyDeathHandler>();
124     auto anonyStub = new (std::nothrow) ProxyDeathCallBackStub();
125     std::shared_ptr<ProxyDeathParam> param = std::make_shared<TestProxyDeathParam>();
126     handler->AddProxyStub(anonyStub->AsObject(), param);
127     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1);
128 
129     handler->HandleRemoteDied(anonyStub->AsObject());
130     EXPECT_TRUE(param->IsEqual(nullptr));
131     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 0);
132 }
133 
134 /**
135  * @tc.name: PrivacyManagerProxyDeathTest004
136  * @tc.desc: HandleRemoteDied test.
137  * @tc.type: FUNC
138  * @tc.require:
139  */
140 HWTEST_F(PrivacyManagerProxyDeathTest, PrivacyManagerProxyDeathTest004, TestSize.Level0)
141 {
142     auto handler = std::make_shared<ProxyDeathHandler>();
143     auto anonyStub = new (std::nothrow) ProxyDeathCallBackStub();
144     std::shared_ptr<ProxyDeathParam> param = std::make_shared<TestProxyDeathParam>();
145     handler->AddProxyStub(anonyStub->AsObject(), param);
146     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1);
147 
148     auto anonyStub2 = new (std::nothrow) ProxyDeathCallBackStub();
149     std::shared_ptr<ProxyDeathParam> param2 = std::make_shared<TestProxyDeathParam>();
150 
151     handler->HandleRemoteDied(anonyStub2->AsObject());
152     EXPECT_FALSE(param->IsEqual(nullptr));
153     EXPECT_EQ(handler->proxyStubAndRecipientMap_.size(), 1);
154 }
155 }
156 }
157 }