1 /*
2 * Copyright (C) 2022 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 #include "system_ability_definition.h"
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "softbus_bus_center.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_object_proxy.h"
23 #include "rpc_test.h"
24 #include "access_token_adapter.h"
25
26 namespace OHOS {
27 using namespace testing::ext;
28 using namespace OHOS;
29
30 #define TEST_NUMS 1000
31 static std::string g_deviceId;
32 static sptr<IRpcFooTest> g_rpcTestProxy;
33 static sptr<IRpcFooTest> g_ipcTestProxy;
34 static IPCObjectProxy *g_proxy;
35 class RpcClientTest : public testing::Test {
36 public:
37 static constexpr char DBINDER_PKG_NAME[] = "DBinderService";
38 static constexpr int NODE_NUM = 4;
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 };
42
SetUpTestCase()43 void RpcClientTest::SetUpTestCase()
44 {
45 NodeBasicInfo *nodeInfo[NODE_NUM];
46 int32_t infoNum = NODE_NUM;
47 int32_t ret = GetAllNodeDeviceInfo(DBINDER_PKG_NAME, nodeInfo, &infoNum);
48 ASSERT_EQ(ret, 0);
49 ASSERT_NE(infoNum, 0);
50
51 g_deviceId = nodeInfo[0]->networkId;
52 }
53
TearDownTestCase()54 void RpcClientTest::TearDownTestCase() {}
55
56 HWTEST_F(RpcClientTest, function_test_001, TestSize.Level1)
57 {
58 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59 ASSERT_FALSE(saMgr == nullptr);
60
61 sptr<IRemoteObject> object = saMgr->GetSystemAbility(RPC_TEST_SERVICE, g_deviceId);
62 ASSERT_TRUE(object != nullptr) << "deviceid is " << g_deviceId;
63
64 g_rpcTestProxy = iface_cast<IRpcFooTest>(object);
65 ASSERT_TRUE(g_rpcTestProxy != nullptr);
66
67 g_proxy = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
68 ASSERT_EQ(g_proxy->GetProto(), IRemoteObject::IF_PROT_DATABUS);
69 }
70
71 HWTEST_F(RpcClientTest, function_test_002, TestSize.Level1)
72 {
73 std::string fooName = g_rpcTestProxy->TestGetFooName();
74 std::string selfFooName = g_rpcTestProxy->GetFooName();
75 ASSERT_TRUE(fooName == selfFooName) << "fooName: " << fooName << " fooName_: " << selfFooName;
76 }
77
78 HWTEST_F(RpcClientTest, function_test_003, TestSize.Level1)
79 {
80 uint32_t tokenId = RpcGetSelfTokenID();
81
82 MessageParcel dataParcel, replyParcel;
83 int32_t err = g_rpcTestProxy->TestAccessToken(dataParcel, replyParcel);
84 ASSERT_EQ(err, ERR_NONE);
85
86 uint32_t featureSet = replyParcel.ReadUint32();
87 uint32_t getTokenId = replyParcel.ReadUint32();
88 if ((featureSet & RPC_ACCESS_TOKEN_FLAG) > 0) {
89 ASSERT_EQ(tokenId, getTokenId) << "deviceid is " << g_deviceId;
90 } else {
91 ASSERT_EQ(getTokenId, 0) << "deviceid is " << g_deviceId;
92 }
93 }
94
95 HWTEST_F(RpcClientTest, function_test_004, TestSize.Level1)
96 {
97 uint32_t tokenId = RpcGetSelfTokenID();
98
99 MessageParcel dataParcel, replyParcel;
100 for (int i = 0; i < TEST_NUMS; i++) {
101 int32_t err = g_rpcTestProxy->TestAccessToken(dataParcel, replyParcel);
102 ASSERT_EQ(err, ERR_NONE);
103
104 uint32_t featureSet = replyParcel.ReadUint32();
105 uint32_t getTokenId = replyParcel.ReadUint32();
106 if ((featureSet & RPC_ACCESS_TOKEN_FLAG) > 0) {
107 ASSERT_EQ(tokenId, getTokenId) << "deviceid is " << g_deviceId;
108 } else {
109 ASSERT_EQ(getTokenId, 0) << "deviceid is " << g_deviceId;
110 }
111 }
112 }
113
114 HWTEST_F(RpcClientTest, function_test_005, TestSize.Level1)
115 {
116 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 ASSERT_FALSE(saMgr == nullptr);
118
119 sptr<IRemoteObject> object = saMgr->GetSystemAbility(RPC_TEST_SERVICE2);
120 ASSERT_TRUE(object != nullptr);
121
122 MessageParcel dataParcel, replyParcel;
123 dataParcel.WriteRemoteObject(object);
124 dataParcel.WriteInt32(1);
125 dataParcel.WriteInt32(1);
126 dataParcel.WriteInt32(1);
127 dataParcel.WriteInt32(1);
128 dataParcel.WriteInt32(1);
129 int32_t err = g_rpcTestProxy->TestRemoteObject(dataParcel, replyParcel);
130 ASSERT_EQ(err, ERR_NONE);
131 err = replyParcel.ReadInt32();
132 ASSERT_EQ(err, ERR_NONE);
133 }
134
135 HWTEST_F(RpcClientTest, function_test_006, TestSize.Level1)
136 {
137 sptr<IRemoteObject::DeathRecipient> death(new RpcDeathRecipient());
138 ASSERT_EQ(g_proxy->GetProto(), IRemoteObject::IF_PROT_DATABUS);
139 g_proxy->AddDeathRecipient(death.GetRefPtr());
140 }
141 }