• 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, IsLocalCalling())
139         .WillRepeatedly(testing::Return(true));
140 
141     EXPECT_CALL(*invoker, GetCallerTokenID())
142         .WillRepeatedly(testing::Return(tokenId));
143     auto result = OH_IPCSkeleton_GetCallingTokenId();
144 
145     EXPECT_EQ(result, tokenId);
146 
147     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
148     delete invoker;
149 }
150 
151 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetFirstTokenId_001, TestSize.Level1)
152 {
153     auto id = OH_IPCSkeleton_GetFirstTokenId();
154     EXPECT_EQ(id, 0);
155 }
156 
157 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetSelfTokenId_001, TestSize.Level1)
158 {
159     auto id = OH_IPCSkeleton_GetSelfTokenId();
160     EXPECT_GT(id, 0);
161 }
162 
163 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingPid_001, TestSize.Level1)
164 {
165     auto id = OH_IPCSkeleton_GetCallingPid();
166     EXPECT_NE(id, 0);
167 }
168 
169 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingUid_001, TestSize.Level1)
170 {
171     auto uid = OH_IPCSkeleton_GetCallingUid();
172     EXPECT_EQ(uid, 0);
173 }
174 
175 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsLocalCalling_001, TestSize.Level1)
176 {
177     EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), true);
178     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
179     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
180     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
181 
182     EXPECT_CALL(*invoker, GetStatus())
183         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
184 
185     EXPECT_CALL(*invoker, IsLocalCalling())
186         .WillRepeatedly(testing::Return(false));
187 
188     EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), false);
189     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
190     delete invoker;
191 }
192 
193 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetCallingIdentity_001, TestSize.Level1)
194 {
195     EXPECT_NE(OH_IPCSkeleton_SetCallingIdentity(nullptr), OH_IPC_SUCCESS);
196 
197     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
198     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
199     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
200 
201     EXPECT_CALL(*invoker, GetStatus())
202         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
203 
204     std::string testStr = "hello, world.";
205     EXPECT_CALL(*invoker, SetCallingIdentity(testStr, false))
206         .WillRepeatedly(testing::Return(true));
207 
208     auto ret = OH_IPCSkeleton_SetCallingIdentity(testStr.c_str());
209     EXPECT_EQ(ret, OH_IPC_SUCCESS);
210     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
211     delete invoker;
212 }
213 
214 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_ResetCallingIdentity_001, TestSize.Level1)
215 {
216     char *identity = nullptr;
217     int32_t len = sizeof(identity);
218     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(nullptr, &len, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
219     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, nullptr, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
220     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, nullptr), OH_IPC_CHECK_PARAM_ERROR);
221     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocatorErr), OH_IPC_MEM_ALLOCATOR_ERROR);
222 
223     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
224     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
225     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
226 
227     EXPECT_CALL(*invoker, GetStatus())
228         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
229 
230     std::string testStr = "testStr";
231     EXPECT_CALL(*invoker, ResetCallingIdentity())
232         .WillRepeatedly(testing::Return(testStr));
233 
234     EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocator), OH_IPC_SUCCESS);
235     ZLOGE(LOG_LABEL, "identity is %{public}s, len is %{public}d", identity, len);
236     EXPECT_STREQ(identity, testStr.c_str());
237     EXPECT_EQ(len, strlen(testStr.c_str()) + 1);
238     if (identity != nullptr) {
239         delete identity;
240     }
241     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
242     delete invoker;
243 }
244 
245 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsHandlingTransaction_001, TestSize.Level1)
246 {
247     EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), false);
248 
249     MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
250     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
251     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
252 
253     EXPECT_CALL(*invoker, GetStatus())
254         .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
255 
256     EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), true);
257     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
258     delete invoker;
259 }
260 
261 HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetMaxWorkThreadNum_001, TestSize.Level1)
262 {
263     int normalCount = 4;
264     int negativeCount = -1;
265     int maxCount = 33;
266     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(normalCount), OH_IPC_SUCCESS);
267     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(negativeCount), OH_IPC_CHECK_PARAM_ERROR);
268     EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(maxCount), OH_IPC_CHECK_PARAM_ERROR);
269 }
270 } // namespace OHOS