• 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 <algorithm>
17 #include <gtest/gtest.h>
18 
19 #include <cstring>
20 #include <securec.h>
21 #include "ipc_cparcel.h"
22 #include "ipc_cremote_object.h"
23 #include "ipc_cskeleton.h"
24 
25 #include "if_system_ability_manager.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "ipc_inner_object.h"
29 #include "ipc_skeleton.h"
30 #include "ipc_error_code.h"
31 #define private public
32 #define protected public
33 #include "comm_auth_info.h"
34 #include "dbinder_databus_invoker.h"
35 #include "dbinder_session_object.h"
36 #include "binder_invoker.h"
37 #include "ipc_debug.h"
38 #include "ipc_skeleton.h"
39 #include "ipc_object_proxy.h"
40 #include "ipc_object_stub.h"
41 #include "ipc_process_skeleton.h"
42 #include "ipc_thread_skeleton.h"
43 #include "dbinder_session_object.h"
44 #include "message_option.h"
45 #include "mock_iremote_invoker.h"
46 #undef protected
47 #undef private
48 #include <iostream>
49 #include <thread>
50 
51 using namespace std;
52 using namespace testing::ext;
53 using namespace OHOS;
54 using namespace OHOS::HiviewDFX;
55 using namespace testing::ext;
56 
57 namespace OHOS {
58 static constexpr int MAX_MEMORY_SIZE = 204800;
59 
60 class IpcCApiSkeletonUnitTest : public testing::Test {
61 public:
62     static void SetUpTestCase(void);
63     static void TearDownTestCase(void);
64     void SetUp();
65     void TearDown();
66     static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_CAPI, "IpcCApiUnitTest" };
67 };
68 
SetUpTestCase()69 void IpcCApiSkeletonUnitTest::SetUpTestCase()
70 {}
71 
TearDownTestCase()72 void IpcCApiSkeletonUnitTest::TearDownTestCase()
73 {}
74 
SetUp()75 void IpcCApiSkeletonUnitTest::SetUp()
76 {}
77 
TearDown()78 void IpcCApiSkeletonUnitTest::TearDown()
79 {}
80 
LocalMemAllocator(int32_t len)81 static void* LocalMemAllocator(int32_t len)
82 {
83     if (len < 0 || len > MAX_MEMORY_SIZE) {
84         return nullptr;
85     }
86     void *buffer = malloc(len);
87     if (buffer == nullptr) {
88         return nullptr;
89     }
90     (void)memset_s(buffer, len, 0, len);
91     return buffer;
92 }
93 
LocalMemAllocatorErr(int32_t len)94 static void* LocalMemAllocatorErr(int32_t len)
95 {
96     return nullptr;
97 }
98 
99 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_JoinWorkThread_001, TestSize.Level1)
100 {
101     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
102     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
103     current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker;
104 
105     EXPECT_CALL(*invoker, GetStatus())
106         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
107     OH_IPCSkeleton_JoinWorkThread();
108     ASSERT_TRUE(IPCThreadSkeleton::GetCurrent() != nullptr);
109     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
110     delete invoker;
111 }
112 
113 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_StopWorkThread_001, TestSize.Level1)
114 {
115     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
116     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
117     current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker;
118 
119     EXPECT_CALL(*invoker, GetStatus())
120         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
121 
122     OH_IPCSkeleton_StopWorkThread();
123     ASSERT_TRUE(IPCThreadSkeleton::GetCurrent() != nullptr);
124     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
125     delete invoker;
126 }
127 
128 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingTokenId_001, TestSize.Level1)
129 {
130     uint64_t tokenId = 1213;
131     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
132     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
133     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
134 
135     EXPECT_CALL(*invoker, GetStatus())
136         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
137 
138     EXPECT_CALL(*invoker, GetCallerTokenID())
139         .WillRepeatedly(testing::Return(tokenId));
140     auto result = OH_IPCSkeleton_GetCallingTokenId();
141 
142     EXPECT_EQ(result, tokenId);
143 
144     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
145     delete invoker;
146 }
147 
148 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetFirstTokenId_001, TestSize.Level1)
149 {
150     auto id = OH_IPCSkeleton_GetFirstTokenId();
151     EXPECT_EQ(id, 0);
152 }
153 
154 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetSelfTokenId_001, TestSize.Level1)
155 {
156     auto id = OH_IPCSkeleton_GetSelfTokenId();
157     EXPECT_GT(id, 0);
158 }
159 
160 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingPid_001, TestSize.Level1)
161 {
162     auto id = OH_IPCSkeleton_GetCallingPid();
163     EXPECT_NE(id, 0);
164 }
165 
166 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingUid_001, TestSize.Level1)
167 {
168     auto uid = OH_IPCSkeleton_GetCallingUid();
169     EXPECT_EQ(uid, 0);
170 }
171 
172 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsLocalCalling_001, TestSize.Level1)
173 {
174     EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), true);
175     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
176     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
177     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
178 
179     EXPECT_CALL(*invoker, GetStatus())
180         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
181 
182     EXPECT_CALL(*invoker, IsLocalCalling())
183         .WillRepeatedly(testing::Return(false));
184 
185     EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), false);
186     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
187     delete invoker;
188 }
189 
190 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetCallingIdentity_001, TestSize.Level1)
191 {
192     EXPECT_NE(OH_IPCSkeleton_SetCallingIdentity(nullptr), OH_IPC_SUCCESS);
193 
194     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
195     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
196     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
197 
198     EXPECT_CALL(*invoker, GetStatus())
199         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
200 
201     std::string testStr = "hello, world.";
202     EXPECT_CALL(*invoker, SetCallingIdentity(testStr, false))
203         .WillRepeatedly(testing::Return(true));
204 
205     auto ret = OH_IPCSkeleton_SetCallingIdentity(testStr.c_str());
206     EXPECT_EQ(ret, OH_IPC_SUCCESS);
207     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
208     delete invoker;
209 }
210 
211 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_ResetCallingIdentity_001, TestSize.Level1)
212 {
213     char *identity = nullptr;
214     int32_t len = sizeof(identity);
215     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(nullptr, &len, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
216     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, nullptr, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
217     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, nullptr), OH_IPC_CHECK_PARAM_ERROR);
218     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocatorErr), OH_IPC_MEM_ALLOCATOR_ERROR);
219 
220     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
221     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
222     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
223 
224     EXPECT_CALL(*invoker, GetStatus())
225         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
226 
227     std::string testStr = "testStr";
228     EXPECT_CALL(*invoker, ResetCallingIdentity())
229         .WillRepeatedly(testing::Return(testStr));
230 
231     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocator), OH_IPC_SUCCESS);
232     ZLOGE(LOG_LABEL, "identity is %{public}s, len is %{public}d", identity, len);
233     EXPECT_STREQ(identity, testStr.c_str());
234     EXPECT_EQ(len, strlen(testStr.c_str()) + 1);
235     if (identity != nullptr) {
236         delete identity;
237     }
238     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
239     delete invoker;
240 }
241 
242 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsHandlingTransaction_001, TestSize.Level1)
243 {
244     EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), false);
245 
246     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
247     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
248     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
249 
250     EXPECT_CALL(*invoker, GetStatus())
251         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
252 
253     EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), true);
254     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
255     delete invoker;
256 }
257 
258 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetMaxWorkThreadNum_001, TestSize.Level1)
259 {
260     int normalCount = 4;
261     int negativeCount = -1;
262     int maxCount = 33;
263     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(normalCount), OH_IPC_SUCCESS);
264     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(negativeCount), OH_IPC_CHECK_PARAM_ERROR);
265     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(maxCount), OH_IPC_CHECK_PARAM_ERROR);
266 }
267 } // namespace OHOS