• 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 <iostream>
17 #include <thread>
18 #include <gtest/gtest.h>
19 #include <nativetoken_kit.h>
20 #include <token_setproc.h>
21 
22 #include "if_system_ability_manager.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "softbus_bus_center.h"
26 
27 #include "ipc_object_proxy.h"
28 #include "ipc_skeleton.h"
29 #include "rpc_test_service_death_recipient.h"
30 #include "rpc_test_service_proxy.h"
31 #include "rpc_test_service_stub.h"
32 
33 namespace OHOS {
34 using namespace testing::ext;
35 
36 static std::string g_deviceId;
37 
InitTokenId(void)38 static void InitTokenId(void)
39 {
40     uint64_t tokenId;
41     const char *perms[] = {
42         OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER,
43         OHOS_PERMISSION_DISTRIBUTED_DATASYNC,
44     };
45     uint32_t permsSize = sizeof(perms) / sizeof(perms[0]);
46     NativeTokenInfoParams infoInstance = {
47         .dcapsNum = 0,
48         .permsNum = permsSize,
49         .aclsNum = 0,
50         .dcaps = NULL,
51         .perms = perms,
52         .acls = NULL,
53         .processName = "com.rpc.test",
54         .aplStr = "system_core",
55     };
56     tokenId = GetAccessTokenId(&infoInstance);
57     SetSelfTokenID(tokenId);
58 }
59 
60 class RpcClientTest : public testing::Test {
61 public:
62     static void SetUpTestCase(void);
63     static void TearDownTestCase(void);
64 };
65 
SetUpTestCase()66 void RpcClientTest::SetUpTestCase()
67 {
68     InitTokenId();
69     std::string pkgName = "DBinderService";
70     NodeBasicInfo* nodeBasicInfo = nullptr;
71     int32_t devicesNumber = 0;
72     if (GetAllNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo, &devicesNumber) != 0) {
73         std::cout << "GetAllNodeDeviceInfo failed" << std::endl;
74         return;
75     }
76     if (devicesNumber == 0) {
77         std::cout << "The number of devices in the current device network is zero." << std::endl;
78         return;
79     }
80     g_deviceId = nodeBasicInfo[0].networkId;
81     FreeNodeInfo(nodeBasicInfo);
82 }
83 
TearDownTestCase()84 void RpcClientTest::TearDownTestCase()
85 {
86 }
87 
GetRemoteProxyObject()88 sptr<RpcTestServiceProxy> GetRemoteProxyObject()
89 {
90     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91     if (samgrProxy == nullptr) {
92         std::cout << "Failed to obtain samgr proxy object." << std::endl;
93         return nullptr;
94     }
95 
96     sptr<IRemoteObject> remoteProxy = samgrProxy->GetSystemAbility(RPC_TEST_SERVICE, g_deviceId);
97     if (remoteProxy == nullptr) {
98         std::cout << "Failed to obtain remote proxy object." << std::endl;
99         return nullptr;
100     }
101 
102     sptr<RpcTestServiceProxy> proxy = iface_cast<RpcTestServiceProxy>(remoteProxy);
103     if (proxy == nullptr) {
104         std::cout << "Iface_cast is failed." << std::endl;
105         return nullptr;
106     }
107     return proxy;
108 }
109 
110 /**
111  * @tc.name: TestGetProto001
112  * @tc.desc: verify whether the obtained object is a remote proxy object across devices.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(RpcClientTest, TestGetProto001, TestSize.Level1)
116 {
117     sptr<RpcTestServiceProxy> proxyObject = GetRemoteProxyObject();
118     ASSERT_TRUE(proxyObject != nullptr) << "GetRemoteProxyObject is failed";
119     ASSERT_EQ(proxyObject->TestGetProto(), IRemoteObject::IF_PROT_DATABUS);
120 }
121 
122 /**
123  * @tc.name: TestGetServiceName001
124  * @tc.desc: Verify if the remote service name and local end are the same.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(RpcClientTest, TestGetServiceName001, TestSize.Level1)
128 {
129     sptr<RpcTestServiceProxy> proxyObject = GetRemoteProxyObject();
130     ASSERT_TRUE(proxyObject != nullptr) << "GetRemoteProxyObject is failed";
131 
132     MessageOption option;
133     MessageParcel data, reply;
134     int32_t ret = proxyObject->TestGetServiceName(data, reply, option);
135     EXPECT_EQ(ret, ERR_NONE);
136 
137     std::string remoteName = reply.ReadString();
138     std::string localName = proxyObject->GetServiceName();
139     EXPECT_TRUE(remoteName == localName) << "remoteName: " << remoteName << " localName: " << localName;
140 }
141 
142 /**
143  * @tc.name: TestAccessToken001
144  * @tc.desc: Verify if the remote service name and local end are the same.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(RpcClientTest, TestAccessToken001, TestSize.Level1)
148 {
149     sptr<RpcTestServiceProxy> proxyObject = GetRemoteProxyObject();
150     ASSERT_TRUE(proxyObject != nullptr) << "GetRemoteProxyObject is failed";
151 
152     MessageOption option;
153     MessageParcel data, reply;
154     int32_t ret = proxyObject->TestAccessToken(data, reply, option);
155     ASSERT_EQ(ret, ERR_NONE);
156 
157     uint32_t tokenId = IPCSkeleton::GetSelfTokenID();
158     uint32_t getTokenId = reply.ReadUint32();
159     EXPECT_EQ(tokenId, getTokenId);
160 }
161 
162 /**
163  * @tc.name: TestAddDeathRecipient001
164  * @tc.desc: Verify if the remote service name and local end are the same.
165  * @tc.type: FUNC
166  */
167 HWTEST_F(RpcClientTest, TestAddDeathRecipient001, TestSize.Level1)
168 {
169     sptr<RpcTestServiceProxy> proxyObject = GetRemoteProxyObject();
170     ASSERT_TRUE(proxyObject != nullptr) << "GetRemoteProxyObject is failed";
171 
172     sptr<IRemoteObject::DeathRecipient> death(new RpcTestServiceDeathRecipient());
173     EXPECT_TRUE(proxyObject->TestAddDeathRecipient(death.GetRefPtr()));
174 }
175 
176 /**
177  * @tc.name: ConcurrentGetSystemAbility001
178  * @tc.desc: Multi threaded concurrent retrieval of remote proxy objects
179  * @tc.type: FUNC
180  */
181 HWTEST_F(RpcClientTest, TestSyncAdd001, TestSize.Level1)
182 {
183     sptr<RpcTestServiceProxy> proxyObject = GetRemoteProxyObject();
184     ASSERT_TRUE(proxyObject != nullptr) << "GetRemoteProxyObject is failed";
185 
186     MessageOption option;
187     MessageParcel data, reply;
188     int32_t value1 = 100;
189     int32_t value2 = 200;
190 
191     data.WriteInt32(value1);
192     data.WriteInt32(value2);
193     int32_t ret = proxyObject->TestSyncAdd(data, reply, option);
194     ASSERT_EQ(ret, ERR_NONE);
195     int32_t sum = reply.ReadInt32();
196     EXPECT_EQ(sum, (value1 + value2));
197 }
198 
199 /**
200  * @tc.name: TestGetSystemAbility001
201  * @tc.desc: Multi threaded concurrent retrieval of remote proxy objects
202  * @tc.type: FUNC
203  */
204 HWTEST_F(RpcClientTest, TestGetSystemAbility001, TestSize.Level1)
205 {
206     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
207     ASSERT_TRUE(samgrProxy != nullptr) << "Failed to obtain samgr proxy object.";
208 
209     std::set<sptr<IRemoteObject>> objectSet;
210     std::mutex objectVecMutex;
211     int32_t expectSize = 1;
212 
213     for (int32_t i = 0; i < 50; i++) {
__anon12e08de10102null214         std::thread t([&samgrProxy, &objectSet, &objectVecMutex] {
215             sptr<IRemoteObject> object = samgrProxy->GetSystemAbility(RPC_TEST_SERVICE, g_deviceId);
216             EXPECT_TRUE(object != nullptr) << "Failed to obtain remote proxy object.";
217             std::unique_lock<std::mutex> lockGuard(objectVecMutex);
218             objectSet.insert(object);
219         });
220         t.detach();
221     }
222     sleep(1);
223     EXPECT_EQ(objectSet.size(), expectSize);
224 }
225 }