• 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 #include <cstdlib>
16 #include <string>
17 #include "gtest/gtest.h"
18 #include "message_parcel.h"
19 #include "iservice_registry.h"
20 #include "if_system_ability_manager.h"
21 #include "system_ability_definition.h"
22 #include "listen_ability_proxy.h"
23 #include "test_sa_proxy_cache_proxy.h"
24 #include "test_log.h"
25 
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace {
30     int g_mockReturn;
31 }
32 
33 class SaProxyCacheTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void SaProxyCacheTest::SetUpTestCase()
42 {}
43 
TearDownTestCase()44 void SaProxyCacheTest::TearDownTestCase()
45 {}
46 
SetUp()47 void SaProxyCacheTest::SetUp()
48 {}
49 
TearDown()50 void SaProxyCacheTest::TearDown()
51 {}
52 
CheckCallGetDoubleFuncIpcTimes(sptr<ITestSaProxyCache> & proxy,int32_t input,int32_t expectIpcTimes)53 bool CheckCallGetDoubleFuncIpcTimes(sptr<ITestSaProxyCache>& proxy, int32_t input, int32_t expectIpcTimes)
54 {
55     constexpr double pi = 3.14;
56     double retDouble;
57 
58     auto ret = proxy->GetDoubleFunc(input, retDouble);
59     if (ret != ERR_OK) {
60         EXPECT_EQ(ret, ERR_OK);
61         return false;
62     }
63     EXPECT_DOUBLE_EQ(retDouble, input * pi);
64     int times = proxy->TestGetIpcSendRequestTimes();
65     if (times != expectIpcTimes) {
66         EXPECT_EQ(times, expectIpcTimes);
67         return false;
68     }
69 
70     return true;
71 }
72 
73 /**
74  * @tc.name: SaProxyCacheTest001
75  * @tc.desc: test proxy cache
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 HWTEST_F(SaProxyCacheTest, SaProxyCacheTest001, TestSize.Level0)
80 {
81     DTEST_LOG << "SaProxyCacheTest001 start" << std::endl;
82     sptr<ISystemAbilityManager> systemAbilityManager =
83         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
84     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID);
85     sptr<ITestSaProxyCache> proxy = iface_cast<ITestSaProxyCache>(remoteObject);
86     EXPECT_NE(proxy, nullptr);
87     bool ret;
88 
89     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 3, 1), true);
90 
91     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 4, 2), true);
92 
93     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10, 3), true);
94 
95     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10, 3), true);
96 
97     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 20, 4), true);
98 
99     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 20, 4), true);
100 
101     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 100, 5), true);
102 
103     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 1000, 6), true);
104 
105     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 2000, 7), true);
106 
107     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 3000, 8), true);
108 
109     // test cache hit
110     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 4, 8), true);
111 
112     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 1000, 8), true);
113 
114     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 3000, 8), true);
115 
116     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 3000, 8), true);
117 
118     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 2000, 8), true);
119 
120     //  exceed cache map size, eliminate cache (3, 9.42)
121     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10000, 9), true);
122 
123     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 3, 10), true);
124 
125     std::string input("AABB");
126     std::string output;
127     ret = proxy->GetStringFunc(input, output);
128     EXPECT_EQ(ret, ERR_OK);
129     EXPECT_EQ(output, "AABBBBAA");
130     EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 11);
131 
132     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10, 12), true);
133 
134     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10, 12), true);
135 
136     // timeout
137     usleep(5 * 1000 * 1000);
138     EXPECT_EQ(CheckCallGetDoubleFuncIpcTimes(proxy, 10, 13), true);
139 
140     output.clear();
141     EXPECT_EQ(proxy->GetStringFunc(input, output), ERR_OK);
142     EXPECT_EQ(output, "AABBBBAA");
143     EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 14);
144     DTEST_LOG << "SaProxyCacheTest001 end" << std::endl;
145 }
146 
147 class MockIRemoteObject : public IRemoteObject {
148 public:
MockIRemoteObject()149     MockIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
150 
~MockIRemoteObject()151     ~MockIRemoteObject() {}
152 
GetObjectRefCount()153     int32_t GetObjectRefCount() override
154     {
155         return 0;
156     }
157 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)158     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
159     {
160         DTEST_LOG << "mockmockmock" << std::endl;
161         reply.WriteInt32(ERR_PERMISSION_DENIED);
162         return g_mockReturn;
163     }
164 
IsProxyObject() const165     bool IsProxyObject() const override
166     {
167         return true;
168     }
169 
CheckObjectLegality() const170     bool CheckObjectLegality() const override
171     {
172         return true;
173     }
174 
AddDeathRecipient(const sptr<DeathRecipient> & recipient)175     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
176     {
177         return true;
178     }
179 
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)180     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
181     {
182         return true;
183     }
184 
Marshalling(Parcel & parcel) const185     bool Marshalling(Parcel &parcel) const override
186     {
187         return true;
188     }
189 
AsInterface()190     sptr<IRemoteBroker> AsInterface() override
191     {
192         return nullptr;
193     }
194 
Dump(int fd,const std::vector<std::u16string> & args)195     int Dump(int fd, const std::vector<std::u16string> &args) override
196     {
197         return 0;
198     }
199 
GetObjectDescriptor() const200     std::u16string GetObjectDescriptor() const
201     {
202         std::u16string descriptor = std::u16string();
203         return descriptor;
204     }
205 };
206 
207 /**
208  * @tc.name: SaProxyCacheTest002
209  * @tc.desc: test abnormal barnch
210  * @tc.type: FUNC
211  * @tc.require:
212  */
213 HWTEST_F(SaProxyCacheTest, SaProxyCacheTest002, TestSize.Level0)
214 {
215     DTEST_LOG << "SaProxyCacheTest002 start" << std::endl;
216     {
217         g_mockReturn = ERR_TIMED_OUT;
218         sptr<MockIRemoteObject> iRemoteObject = sptr<MockIRemoteObject>(new (std::nothrow) MockIRemoteObject());
219         EXPECT_TRUE(iRemoteObject != nullptr);
220         TestSaProxyCacheProxy p(iRemoteObject);
221         std::string input("hello_test");
222         std::string output;
223         auto ret = p.GetStringFunc(input, output);
224         EXPECT_EQ(ret, ERR_TIMED_OUT);
225         EXPECT_EQ(p.TestGetIpcSendRequestTimes(), 1);
226 
227         output.clear();
228         ret = p.GetStringFunc(input, output);
229         EXPECT_EQ(p.TestGetIpcSendRequestTimes(), 2);
230     }
231 
232     {
233         g_mockReturn = ERR_OK;
234         sptr<MockIRemoteObject> iRemoteObject = sptr<MockIRemoteObject>(new (std::nothrow) MockIRemoteObject());
235         EXPECT_TRUE(iRemoteObject != nullptr);
236         TestSaProxyCacheProxy p(iRemoteObject);
237 
238         double retDouble;
239         auto ret = p.GetDoubleFunc(100, retDouble);
240         EXPECT_EQ(ret, ERR_PERMISSION_DENIED);
241         EXPECT_EQ(p.TestGetIpcSendRequestTimes(), 1);
242 
243         ret = p.GetDoubleFunc(100, retDouble);
244         EXPECT_EQ(p.TestGetIpcSendRequestTimes(), 2);
245     }
246     DTEST_LOG << "SaProxyCacheTest002 end" << std::endl;
247 }
248 
249 /**
250  * @tc.name: SaProxyCacheTest003
251  * @tc.desc: test proxy object and cache are destroyed together
252  * @tc.type: FUNC
253  * @tc.require:
254  */
255 HWTEST_F(SaProxyCacheTest, SaProxyCacheTest003, TestSize.Level2)
256 {
257     DTEST_LOG << "SaProxyCacheTest003 start" << std::endl;
258     std::vector<bool> input;
259     std::vector<int8_t> output;
260     std::vector<int8_t> expect;
261     input.push_back(true);
262     input.push_back(false);
263     input.push_back(true);
264     for (auto i:input) {
265         expect.push_back(i == true ? 1 : -1);
266     }
267     {
268         sptr<ISystemAbilityManager> systemAbilityManager =
269             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
270         sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID);
271         sptr<ITestSaProxyCache> proxy = iface_cast<ITestSaProxyCache>(remoteObject);
272         EXPECT_NE(proxy, nullptr);
273 
274         int ret = proxy->GetVectorFunc(input, output);
275         EXPECT_EQ(ret, ERR_OK);
276         EXPECT_EQ((output == expect), 1);
277         EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 1);
278 
279         output.clear();
280         ret = proxy->GetVectorFunc(input, output);
281         EXPECT_EQ(ret, ERR_OK);
282         EXPECT_EQ((output == expect), 1);
283         EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 1);
284     }
285 
286     sptr<ISystemAbilityManager> systemAbilityManager =
287         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
288     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID);
289     sptr<ITestSaProxyCache> proxy = iface_cast<ITestSaProxyCache>(remoteObject);
290     EXPECT_NE(proxy, nullptr);
291 
292     output.clear();
293     auto ret = proxy->GetVectorFunc(input, output);
294     EXPECT_EQ(ret, ERR_OK);
295     EXPECT_EQ((output == expect), 1);
296     EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 1);
297     DTEST_LOG << "SaProxyCacheTest003 end" << std::endl;
298 }
299 
300 /**
301  * @tc.name: SaProxyCacheTest004
302  * @tc.desc: test clear cache when sa stub exits
303  * @tc.type: FUNC
304  * @tc.require:
305  */
306 HWTEST_F(SaProxyCacheTest, SaProxyCacheTest004, TestSize.Level2)
307 {
308     DTEST_LOG << "SaProxyCacheTest004 start" << std::endl;
309     sptr<ISystemAbilityManager> systemAbilityManager =
310         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
311     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID);
312     sptr<ITestSaProxyCache> proxy = iface_cast<ITestSaProxyCache>(remoteObject);
313 
314     sptr<IRemoteObject> remoteObject1 = systemAbilityManager->GetSystemAbility(1494);
315     sptr<IListenAbility> listenProxy = iface_cast<IListenAbility>(remoteObject1);
316 
317     int pid;
318     double retDouble, retDouble2;
319     auto ret = proxy->GetSaPid(pid);
320 
321     std::string cmd = "kill -9 ";
322     cmd += std::to_string(pid);
323 
324     CheckCallGetDoubleFuncIpcTimes(proxy, 3, 1);
325 
326     CheckCallGetDoubleFuncIpcTimes(proxy, 3, 1);
327 
328     std::string input("AABB");
329     std::string output;
330     ret = proxy->GetStringFunc(input, output);
331     EXPECT_EQ(ret, ERR_OK);
332     EXPECT_EQ(output, "AABBBBAA");
333     EXPECT_EQ(proxy->TestGetIpcSendRequestTimes(), 2);
334 
335     EXPECT_EQ(listenProxy->AddVolume(100), 101);
336     EXPECT_EQ(listenProxy->TestSaCallSa(100, retDouble2), ERR_OK);
337 
338     system(cmd.c_str());
339     DTEST_LOG << cmd << std::endl;
340 
341     int trytime = 3;
342     ret = ERR_OK;
343     while ((ret == ERR_OK) && (trytime != 0)) {
344         ret = proxy->GetDoubleFunc(3, retDouble);
345         usleep(500000);
346         trytime--;
347     }
348     EXPECT_NE(ret, ERR_OK);
349     EXPECT_GT(proxy->TestGetIpcSendRequestTimes(), 2);
350 
351     int currIpcSendRequestTimes = proxy->TestGetIpcSendRequestTimes();
352     ret = proxy->GetStringFunc(input, output);
353     EXPECT_NE(ret, ERR_OK);
354     EXPECT_GT(proxy->TestGetIpcSendRequestTimes(), currIpcSendRequestTimes);
355 
356     ret = listenProxy->TestSaCallSa(100, retDouble2);
357     EXPECT_NE(ret, ERR_OK);
358 
359     int32_t times;
360     ret = listenProxy->TestGetIpcSendRequestTimes(times);
361     EXPECT_EQ(ret, ERR_OK);
362     EXPECT_GT(times, 1);
363 
364     EXPECT_EQ(listenProxy->AddVolume(100), 101);
365     EXPECT_EQ(listenProxy->TestClearSa1493Proxy_(), ERR_OK);
366     DTEST_LOG << "SaProxyCacheTest004 end" << std::endl;
367 }
368 
369 /**
370  * @tc.name: SaProxyCacheTest005
371  * @tc.desc: test clear cache when sa stub exits
372  * @tc.type: FUNC
373  * @tc.require:
374  */
375 HWTEST_F(SaProxyCacheTest, SaProxyCacheTest005, TestSize.Level2)
376 {
377     DTEST_LOG << "SaProxyCacheTest005 start" << std::endl;
378     sptr<ISystemAbilityManager> systemAbilityManager =
379         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
380     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID);
381     sptr<ITestSaProxyCache> proxy = iface_cast<ITestSaProxyCache>(remoteObject);
382     EXPECT_NE(proxy, nullptr);
383 
384     sptr<IRemoteObject> remoteObject1 = systemAbilityManager->GetSystemAbility(1494);
385     sptr<IListenAbility> listenProxy = iface_cast<IListenAbility>(remoteObject1);
386     EXPECT_NE(listenProxy, nullptr);
387 
388     int pid;
389     double retDouble, retDouble2;
390     auto ret = proxy->GetSaPid(pid);
391     EXPECT_EQ(ret, ERR_OK);
392     DTEST_LOG << "SaProxyCacheTest005 end" << std::endl;
393 }
394 }