• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "rpc_test.h"
18 
19 #include <cstring>
20 
21 #include "rpc_log.h"
22 #include "rpc_errno.h"
23 #include "ipc_skeleton.h"
24 #include "serializer.h"
25 #include "utils_list.h"
26 #include "dbinder_service.h"
27 
28 typedef struct {
29     UTILS_DL_LIST list;
30     int32_t saId;
31     SvcIdentity *sid;
32 } SvcInfo;
33 
34 namespace {
35 UTILS_DL_LIST *g_saList = nullptr;
36 
AddSystemAbility(int32_t saId,SvcIdentity * sid)37 int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid)
38 {
39     if (saId <= INVALID_SAID) {
40         return ERR_FAILED;
41     }
42     RPC_LOG_INFO("AddSystemAbility called.... handle = %d", sid->handle);
43     RPC_LOG_INFO("AddSystemAbility called.... cookie = %u", sid->cookie);
44     if (g_saList == nullptr) {
45         return ERR_FAILED;
46     }
47 
48     SvcInfo* node = (SvcInfo *)calloc(1, sizeof(SvcInfo));
49     if (node == nullptr) {
50         RPC_LOG_ERROR("AddSystemAbility node calloc failed");
51         return ERR_FAILED;
52     }
53     node->saId = saId;
54     node->sid = sid;
55     UtilsListAdd(g_saList, &node->list);
56     return ERR_NONE;
57 }
58 
GetSystemAbility(int32_t saId,const char * deviceId,SvcIdentity * sid)59 int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid)
60 {
61     SvcInfo* node = nullptr;
62     SvcInfo* next = nullptr;
63     UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, g_saList, SvcInfo, list)
64     {
65         if (node->saId == saId) {
66             sid->handle = node->sid->handle;
67             sid->token = node->sid->token;
68             sid->cookie = node->sid->cookie;
69             RPC_LOG_INFO("find sa, said = %d, handle = %d, cookie = %u", saId, sid->handle, sid->cookie);
70             return ERR_NONE;
71         }
72     }
73     return ERR_FAILED;
74 }
75 
AddRemoteSystemAbility(int32_t saId,SvcIdentity * sid)76 int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid)
77 {
78     if (AddSystemAbility(saId, sid) == ERR_FAILED) {
79         RPC_LOG_ERROR("AddSystemAbility failed");
80         return ERR_FAILED;
81     }
82 
83     const char *name = "16";
84     if (RegisterRemoteProxy(name, strlen(name), saId) != ERR_NONE) {
85         RPC_LOG_ERROR("RegisterRemoteProxy failed");
86         return ERR_FAILED;
87     }
88 
89     return ERR_NONE;
90 }
91 
GetRemoteSystemAbility(IpcIo * data,SvcIdentity * sid)92 int32_t GetRemoteSystemAbility(IpcIo *data, SvcIdentity *sid)
93 {
94     int32_t saId;
95     ReadInt32(data, &saId);
96     if (saId <= INVALID_SAID) {
97         return ERR_FAILED;
98     }
99     size_t len;
100     const char *deviceId = (const char *)ReadString(data, &len);
101 
102     const char *name = "16";
103     uint32_t idLen = (uint32_t)strlen(deviceId);
104     RPC_LOG_INFO("GetRemoteSystemAbility start");
105 
106     int32_t ret = MakeRemoteBinder(name, 2, deviceId, idLen, (uintptr_t)saId, 0, (void *)sid);
107     if (ret != ERR_NONE) {
108         RPC_LOG_ERROR("MakeRemoteBinder failed");
109     }
110     RPC_LOG_INFO("GetRemoteSystemAbility handle=%d, cookie=%u", sid->handle, sid->cookie);
111 
112     return ret;
113 }
114 
RemoteRequest(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)115 int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
116 {
117     int32_t result = ERR_NONE;
118     RPC_LOG_INFO("OnRemoteRequest called.... code = %u", code);
119     switch (code) {
120         case GET_SYSTEM_ABILITY_TRANSACTION: {
121             int32_t saId;
122             ReadInt32(data, &saId);
123             SvcIdentity sid;
124             result = GetSystemAbility(saId, "", &sid);
125             WriteRemoteObject(reply, &sid);
126             break;
127         }
128         case ADD_SYSTEM_ABILITY_TRANSACTION: {
129             int32_t saId;
130             ReadInt32(data, &saId);
131             SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
132             if (sid == nullptr) {
133                 result = ERR_FAILED;
134                 break;
135             }
136             ReadRemoteObject(data, sid);
137             result = AddSystemAbility(saId, sid);
138             break;
139         }
140         case GET_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
141             SvcIdentity sid;
142             result = GetRemoteSystemAbility(data, &sid);
143             if (result != ERR_NONE) {
144                 WriteInt32(reply, result);
145             } else {
146                 WriteRemoteObject(reply, &sid);
147             }
148             break;
149         }
150         case ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
151             int32_t saId;
152             ReadInt32(data, &saId);
153             SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
154             if (sid == nullptr) {
155                 result = ERR_FAILED;
156                 break;
157             }
158             ReadRemoteObject(data, sid);
159             result = AddRemoteSystemAbility(saId, sid);
160             if (result != ERR_NONE) {
161                 WriteInt32(reply, result);
162             }
163             break;
164         }
165         default:
166             RPC_LOG_ERROR("unknown code %u", code);
167             break;
168     }
169     return result;
170 }
171 
mainFunc(void)172 int32_t mainFunc(void)
173 {
174     RPC_LOG_INFO("Enter System Ability Manager .... ");
175 
176     g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST));
177     if (g_saList == nullptr) {
178         RPC_LOG_ERROR("g_saList calloc failed");
179         return ERR_FAILED;
180     }
181     UtilsListInit(g_saList);
182     return ERR_NONE;
183 }
184 }
185 
186 using namespace testing::ext;
187 
188 namespace OHOS {
189 class RpcSamgrTest : public testing::Test {
190 public:
SetUpTestCase()191     static void SetUpTestCase()
192     {
193         RPC_LOG_INFO("----------test case for rpc samgr start-------------\n");
194         mainFunc();
195     }
TearDownTestCase()196     static void TearDownTestCase()
197     {
198         RPC_LOG_INFO("----------test case for rpc samgr end -------------\n");
199     }
SetUp()200     void SetUp() {}
TearDown()201     void TearDown() {}
202 };
203 
204 /**
205  * @tc.name: RPC_SendRequestNum_02
206  * @tc.desc: start dbinder service failed
207  * @tc.type: FUNC
208  * @tc.require:
209  */
210 HWTEST_F(RpcSamgrTest, RpcSamgrTest001, TestSize.Level1)
211 {
212     IpcObjectStub objectStub = {
213         .func = RemoteRequest,
214         .isRemote = false
215     };
216 
217     SvcIdentity target = {
218         .handle = 0,
219         .cookie = NULL
220     };
221 
222     int32_t ret = SetContextObject(target);
223     EXPECT_EQ(ret, ERR_INVALID_PARAM);
224     RPC_LOG_ERROR("SAMGR register samgr failed");
225 
226     return;
227 }
228 
229 /**
230  * @tc.name: RPC_SendRequestNum_01
231  * @tc.desc: start dbinder service success
232  * @tc.type: FUNC
233  * @tc.require:
234  */
235 HWTEST_F(RpcSamgrTest, RpcSamgrTest002, TestSize.Level0)
236 {
237     IpcObjectStub objectStub = {
238         .func = RemoteRequest,
239         .isRemote = false
240     };
241 
242     SvcIdentity target = {
243         .handle = 0,
244         .cookie = (uintptr_t)&objectStub
245     };
246 
247     if (SetContextObject(target) != ERR_NONE) {
248         RPC_LOG_ERROR("SAMGR register samgr failed");
249         return;
250     }
251 
252     int32_t ret = StartDBinderService();
253     EXPECT_EQ(ret, ERR_NONE);
254     RPC_LOG_INFO("StartDBinderService finished");
255 
256     JoinWorkThread();
257 }
258 }  // namespace OHOS