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 <base/hdi_smq.h>
17 #include <functional>
18 #include <gtest/gtest.h>
19 #include <hdf_io_service_if.h>
20 #include <hdf_log.h>
21 #include <hdf_service_status_inner.h>
22 #include <idevmgr_hdi.h>
23 #include <iostream>
24 #include <ipc_object_stub.h>
25 #include <iservice_registry.h>
26 #include <iservmgr_hdi.h>
27 #include <iservstat_listener_hdi.h>
28 #include <osal_time.h>
29 #include <string>
30
31 #include "sample_hdi.h"
32
33 #define HDF_LOG_TAG service_manager_test_cpp
34
35 namespace OHOS {
36 using namespace testing::ext;
37 using OHOS::IRemoteObject;
38 using OHOS::sptr;
39 using OHOS::HDI::Base::SharedMemQueue;
40 using OHOS::HDI::Base::SharedMemQueueMeta;
41 using OHOS::HDI::Base::SmqType;
42 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
43 using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
44 using OHOS::HDI::ServiceManager::V1_0::IServStatListener;
45 using OHOS::HDI::ServiceManager::V1_0::ServiceStatus;
46 using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub;
47 using OHOS::HDI::DeviceManager::V1_0::HdiDevHostInfo;
48 using OHOS::HDI::ServiceManager::V1_0::HdiServiceInfo;
49 static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service";
50 static constexpr const char *TEST1_SERVICE_NAME = "sample1_driver_service";
51 static constexpr const char16_t *TEST_SERVICE_INTERFACE_DESC = u"hdf.test.sampele_service";
52 static constexpr const char *TEST_SERVICE_INTERFACE_DESC_N = "hdf.test.sampele_service";
53 static constexpr const char *TEST_SERVICE_INTERFACE_DESC_INVALID = "___";
54 static constexpr const char *TEST_SERVICE_INTERFACE_DESC_VOID = "";
55 static constexpr const char *TEST_SERVICE_INTERFACE_DESC_NULL = nullptr;
56 static constexpr int PAYLOAD_NUM = 1234;
57 static constexpr int SMQ_TEST_QUEUE_SIZE = 10;
58 static constexpr int SMQ_TEST_WAIT_TIME = 100;
59 static constexpr int WAIT_LOAD_UNLOAD_TIME = 300;
60 static constexpr int DEVICE_SERVICE_MANAGER_SA_ID = 5100;
61 static constexpr int INVALID_CODE = 100;
62 static constexpr int ERROR_CODE_WITH_INVALID_CODE = 305;
63
64 class HdfServiceMangerHdiTest : public testing::Test {
65 public:
SetUpTestCase()66 static void SetUpTestCase()
67 {
68 auto devmgr = IDeviceManager::Get();
69 if (devmgr != nullptr) {
70 HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__);
71 devmgr->LoadDevice(TEST_SERVICE_NAME);
72 }
73 }
TearDownTestCase()74 static void TearDownTestCase()
75 {
76 auto devmgr = IDeviceManager::Get();
77 if (devmgr != nullptr) {
78 HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__);
79 devmgr->UnloadDevice(TEST_SERVICE_NAME);
80 }
81 }
82 void TestServiceListenerStop(const sptr<IDeviceManager>& devmgr, const sptr<IServiceManager>& servmgr);
83 void TestSampleService(sptr<IRemoteObject>& sampleService, const sptr<IDeviceManager>& devmgr,
84 const sptr<IServiceManager>& servmgr);
SetUp()85 void SetUp() {};
TearDown()86 void TearDown() {};
87 };
88
89 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest001, TestSize.Level1)
90 {
91 auto servmgr = IServiceManager::Get();
92 ASSERT_TRUE(servmgr != nullptr);
93 }
94
95 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest002, TestSize.Level1)
96 {
97 auto servmgr = IServiceManager::Get();
98 ASSERT_TRUE(servmgr != nullptr);
99
100 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
101
102 ASSERT_TRUE(sampleService != nullptr);
103
104 OHOS::MessageParcel data;
105 OHOS::MessageParcel reply;
106 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
107 ASSERT_EQ(ret, true);
108 data.WriteCString("sample_service test call");
109
110 OHOS::MessageOption option;
111 int status = sampleService->SendRequest(SAMPLE_SERVICE_PING, data, reply, option);
112 ASSERT_EQ(status, 0);
113 }
114
115 class IPCObjectStubTest : public OHOS::IPCObjectStub {
116 public:
IPCObjectStubTest()117 explicit IPCObjectStubTest() : OHOS::IPCObjectStub(u"") {};
118 virtual ~IPCObjectStubTest() = default;
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)119 int OnRemoteRequest(
120 uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) override
121 {
122 HDF_LOGI("IPCObjectStubTest::OnRemoteRequest called, code = %{public}d", code);
123 payload = data.ReadInt32();
124
125 return HDF_SUCCESS;
126 }
127
128 static int32_t payload;
129 };
130
131 int32_t IPCObjectStubTest::payload = 0;
132
133 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest003, TestSize.Level1)
134 {
135 auto servmgr = IServiceManager::Get();
136 ASSERT_TRUE(servmgr != nullptr);
137
138 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
139 ASSERT_TRUE(sampleService != nullptr);
140
141 sptr<IRemoteObject> callback = new IPCObjectStubTest();
142 OHOS::MessageParcel data;
143 OHOS::MessageParcel reply;
144 int32_t payload = PAYLOAD_NUM;
145 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
146 ASSERT_EQ(ret, true);
147 data.WriteInt32(payload);
148 data.WriteRemoteObject(callback);
149
150 OHOS::MessageOption option;
151 int status = sampleService->SendRequest(SAMPLE_SERVICE_CALLBACK, data, reply, option);
152 ASSERT_EQ(status, 0);
153 ASSERT_EQ(IPCObjectStubTest::payload, payload);
154 }
155
156 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest004, TestSize.Level1)
157 {
158 auto servmgr = IServiceManager::Get();
159 ASSERT_TRUE(servmgr != nullptr);
160
161 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
162 ASSERT_TRUE(sampleService != nullptr);
163
164 OHOS::MessageParcel data;
165 OHOS::MessageParcel reply;
166 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
167 ASSERT_EQ(ret, true);
168 data.WriteInt32(PAYLOAD_NUM);
169 data.WriteInt32(PAYLOAD_NUM);
170
171 OHOS::MessageOption option;
172 int status = sampleService->SendRequest(SAMPLE_SERVICE_SUM, data, reply, option);
173 ASSERT_EQ(status, 0);
174 int32_t result = reply.ReadInt32();
175 int32_t expRes = PAYLOAD_NUM + PAYLOAD_NUM;
176 ASSERT_EQ(result, expRes);
177 }
178
179 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest006, TestSize.Level1)
180 {
181 auto servmgr = IServiceManager::Get();
182 ASSERT_TRUE(servmgr != nullptr);
183
184 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
185 ASSERT_TRUE(sampleService != nullptr);
186
187 OHOS::MessageParcel data;
188 OHOS::MessageParcel reply;
189
190 constexpr int buffersize = 10;
191 uint8_t dataBuffer[buffersize];
192 for (int i = 0; i < buffersize; i++) {
193 dataBuffer[i] = i;
194 }
195 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
196 ASSERT_EQ(ret, true);
197 ret = data.WriteUnpadBuffer(dataBuffer, sizeof(dataBuffer));
198 ASSERT_TRUE(ret);
199
200 OHOS::MessageOption option;
201 int status = sampleService->SendRequest(SAMPLE_BUFFER_TRANS, data, reply, option);
202 ASSERT_EQ(status, 0);
203
204 const uint8_t *retBuffer = reply.ReadUnpadBuffer(buffersize);
205 ASSERT_TRUE(retBuffer != nullptr);
206
207 for (int i = 0; i < buffersize; i++) {
208 ASSERT_EQ(retBuffer[i], i);
209 }
210 }
211
212 /*
213 * Test device manager Load/UnLoad device and driver dynamic register device
214 */
215 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1)
216 {
217 auto devmgr = IDeviceManager::Get();
218 ASSERT_TRUE(devmgr != nullptr);
219 devmgr->UnloadDevice(TEST_SERVICE_NAME);
220
221 auto servmgr = IServiceManager::Get();
222 ASSERT_TRUE(servmgr != nullptr);
223 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
224 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
225 ASSERT_TRUE(sampleService == nullptr);
226
227 int ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
228 ASSERT_EQ(ret, HDF_SUCCESS);
229 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
230 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
231 ASSERT_TRUE(sampleService != nullptr);
232
233 OHOS::MessageParcel data;
234 OHOS::MessageParcel reply;
235 OHOS::MessageOption option;
236 ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
237 ASSERT_EQ(ret, true);
238 const char *newServName = "sample_driver_service2";
239 ret = data.WriteCString(newServName);
240 ASSERT_TRUE(ret);
241
242 int status = sampleService->SendRequest(SAMPLE_REGISTER_DEVICE, data, reply, option);
243 ASSERT_EQ(status, HDF_SUCCESS);
244
245 auto sampleService2 = servmgr->GetService(newServName);
246 ASSERT_TRUE(sampleService != nullptr);
247
248 data.FlushBuffer();
249 reply.FlushBuffer();
250 ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
251 ASSERT_EQ(ret, true);
252 data.WriteInt32(PAYLOAD_NUM);
253 data.WriteInt32(PAYLOAD_NUM);
254
255 status = sampleService2->SendRequest(SAMPLE_SERVICE_SUM, data, reply, option);
256 ASSERT_EQ(status, 0);
257 int32_t result = reply.ReadInt32();
258
259 int32_t expRes = PAYLOAD_NUM + PAYLOAD_NUM;
260 ASSERT_EQ(result, expRes);
261 sampleService2 = nullptr;
262
263 data.FlushBuffer();
264 reply.FlushBuffer();
265 ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
266 ASSERT_EQ(ret, true);
267 data.WriteCString(newServName);
268
269 status = sampleService->SendRequest(SAMPLE_UNREGISTER_DEVICE, data, reply, option);
270 ASSERT_EQ(status, HDF_SUCCESS);
271
272 sampleService2 = servmgr->GetService(newServName);
273 ASSERT_TRUE(sampleService2 == nullptr);
274
275 ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
276 ASSERT_EQ(ret, HDF_SUCCESS);
277 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
278 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
279 ASSERT_TRUE(sampleService == nullptr);
280 }
281
282 class ServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub {
283 public:
284 using StatusCallback = std::function<void(const ServiceStatus &)>;
ServStatListener(StatusCallback callback)285 explicit ServStatListener(StatusCallback callback) : callback_(std::move(callback)) {}
286 ~ServStatListener() = default;
OnReceive(const ServiceStatus & status)287 void OnReceive(const ServiceStatus &status) override
288 {
289 callback_(status);
290 }
291
292 private:
293 StatusCallback callback_;
294 };
295
296 /*
297 * Test service start status listener
298 */
TestServiceListenerStop(const sptr<IDeviceManager> & devmgr,const sptr<IServiceManager> & servmgr)299 void HdfServiceMangerHdiTest::TestServiceListenerStop(const sptr<IDeviceManager>& devmgr,
300 const sptr<IServiceManager>& servmgr)
301 {
302 std::string servInfo;
303 uint16_t devClass;
304 uint16_t servStatus;
305 bool callbacked = false;
306 ::OHOS::sptr<IServStatListener> listener
307 = new ServStatListener(ServStatListener::StatusCallback([&](const ServiceStatus &status) {
308 HDF_LOGI("service status callback");
309 if (status.serviceName == std::string(TEST_SERVICE_NAME)) {
310 servInfo = status.info;
311 devClass = status.deviceClass;
312 servStatus = status.status;
313 callbacked = true;
314 }
315 }));
316
317 int status = servmgr->RegisterServiceStatusListener(listener, DEVICE_CLASS_DEFAULT);
318 ASSERT_EQ(status, HDF_SUCCESS);
319
320 int ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
321 ASSERT_EQ(ret, HDF_SUCCESS);
322 constexpr int waitCount = 300;
323 int count = waitCount;
324 while (!callbacked && count > 0) {
325 OsalMSleep(1);
326 count--;
327 }
328 ASSERT_TRUE(callbacked);
329 ASSERT_EQ(devClass, DEVICE_CLASS_DEFAULT);
330 ASSERT_EQ(servInfo, std::string(TEST_SERVICE_NAME));
331 ASSERT_EQ(servStatus, OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START);
332
333 callbacked = false;
334 ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
335 ASSERT_EQ(ret, HDF_SUCCESS);
336
337 count = waitCount;
338 while (!callbacked && count > 0) {
339 OsalMSleep(1);
340 count--;
341 }
342 ASSERT_TRUE(callbacked);
343 ASSERT_EQ(devClass, DEVICE_CLASS_DEFAULT);
344 ASSERT_EQ(servInfo, std::string(TEST_SERVICE_NAME));
345 ASSERT_EQ(servStatus, OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_STOP);
346
347 status = servmgr->UnregisterServiceStatusListener(listener);
348 ASSERT_EQ(status, HDF_SUCCESS);
349 }
350
351 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest008, TestSize.Level1)
352 {
353 auto devmgr = IDeviceManager::Get();
354 ASSERT_TRUE(devmgr != nullptr);
355 devmgr->UnloadDevice(TEST_SERVICE_NAME);
356
357 auto servmgr = IServiceManager::Get();
358 ASSERT_TRUE(servmgr != nullptr);
359 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
360 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
361 ASSERT_TRUE(sampleService == nullptr);
362
363 TestServiceListenerStop(devmgr, servmgr);
364 }
365
366 /*
367 * Test service status listener update service info
368 */
TestSampleService(sptr<IRemoteObject> & sampleService,const sptr<IDeviceManager> & devmgr,const sptr<IServiceManager> & servmgr)369 void HdfServiceMangerHdiTest::TestSampleService(sptr<IRemoteObject>& sampleService,
370 const sptr<IDeviceManager>& devmgr, const sptr<IServiceManager>& servmgr)
371 {
372 std::string servInfo;
373 uint16_t devClass;
374 uint16_t servStatus;
375 bool callbacked = false;
376 ::OHOS::sptr<IServStatListener> listener
377 = new ServStatListener(
378 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
379 if (status.serviceName == std::string(TEST_SERVICE_NAME) &&
380 status.status == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_CHANGE) {
381 HDF_LOGI("sample service status callback");
382 servInfo = status.info;
383 devClass = status.deviceClass;
384 servStatus = status.status;
385 callbacked = true;
386 }
387 }));
388
389 int status = servmgr->RegisterServiceStatusListener(listener, DEVICE_CLASS_DEFAULT);
390 ASSERT_EQ(status, HDF_SUCCESS);
391 constexpr int FIRST_WAIT = 20;
392 OsalMSleep(FIRST_WAIT); // skip callback on register
393 OHOS::MessageParcel data;
394 OHOS::MessageParcel reply;
395 OHOS::MessageOption option;
396 bool res = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
397 ASSERT_EQ(res, true);
398 std::string info = "foo";
399 data.WriteCString(info.data());
400 callbacked = false;
401 status = sampleService->SendRequest(SAMPLE_UPDATE_SERVIE, data, reply, option);
402 ASSERT_EQ(status, HDF_SUCCESS);
403 constexpr int waitCount = 300;
404 int count = waitCount;
405 while (!callbacked && count > 0) {
406 OsalMSleep(1);
407 count--;
408 }
409 ASSERT_TRUE(callbacked);
410 ASSERT_EQ(devClass, DEVICE_CLASS_DEFAULT);
411 ASSERT_EQ(servInfo, info);
412 int ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
413 ASSERT_EQ(ret, HDF_SUCCESS);
414
415 status = servmgr->UnregisterServiceStatusListener(listener);
416 ASSERT_EQ(status, HDF_SUCCESS);
417 }
418
419 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest009, TestSize.Level1)
420 {
421 auto devmgr = IDeviceManager::Get();
422 ASSERT_TRUE(devmgr != nullptr);
423 devmgr->UnloadDevice(TEST_SERVICE_NAME);
424
425 auto servmgr = IServiceManager::Get();
426 ASSERT_TRUE(servmgr != nullptr);
427 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
428 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
429 ASSERT_TRUE(sampleService == nullptr);
430
431 int ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
432 ASSERT_EQ(ret, HDF_SUCCESS);
433 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
434 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
435 ASSERT_TRUE(sampleService != nullptr);
436
437 TestSampleService(sampleService, devmgr, servmgr);
438 }
439
440 /*
441 * Test service status listener unregister
442 */
443 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest010, TestSize.Level1)
444 {
445 auto devmgr = IDeviceManager::Get();
446 ASSERT_TRUE(devmgr != nullptr);
447 devmgr->UnloadDevice(TEST_SERVICE_NAME);
448
449 auto servmgr = IServiceManager::Get();
450 ASSERT_TRUE(servmgr != nullptr);
451 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
452 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
453 ASSERT_TRUE(sampleService == nullptr);
454
455 std::string servInfo;
456 uint16_t devClass;
457 uint16_t servStatus;
458 bool callbacked = false;
459 ::OHOS::sptr<IServStatListener> listener
460 = new ServStatListener(
__anon4c4667ae0302(const ServiceStatus &status) 461 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
462 HDF_LOGI("service status callback");
463 if (status.serviceName == std::string(TEST_SERVICE_NAME) &&
464 status.status == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) {
465 servInfo = status.info;
466 devClass = status.deviceClass;
467 servStatus = status.status;
468 callbacked = true;
469 }
470 }));
471
472 int status = servmgr->RegisterServiceStatusListener(listener, DEVICE_CLASS_DEFAULT);
473 ASSERT_EQ(status, HDF_SUCCESS);
474
475 int ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
476 ASSERT_EQ(ret, HDF_SUCCESS);
477
478 constexpr int waitCount = 300;
479 int count = waitCount;
480 while (!callbacked && count > 0) {
481 OsalMSleep(1);
482 count--;
483 }
484 ASSERT_TRUE(callbacked);
485 ASSERT_EQ(devClass, DEVICE_CLASS_DEFAULT);
486 ASSERT_EQ(servInfo, std::string(TEST_SERVICE_NAME));
487
488 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
489 ASSERT_TRUE(sampleService != nullptr);
490
491 status = servmgr->UnregisterServiceStatusListener(listener);
492 ASSERT_EQ(status, HDF_SUCCESS);
493
494 callbacked = false;
495 ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
496 ASSERT_EQ(ret, HDF_SUCCESS);
497
498 OsalMSleep(10);
499 ASSERT_FALSE(callbacked);
500
501 ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
502 ASSERT_EQ(ret, HDF_SUCCESS);
503 }
504
505 /*
506 * smq test normal read/write
507 */
508 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest011, TestSize.Level1)
509 {
510 HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__);
511 auto servmgr = IServiceManager::Get();
512 ASSERT_TRUE(servmgr != nullptr);
513
514 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
515 ASSERT_TRUE(sampleService != nullptr);
516
517 OHOS::MessageParcel data;
518 OHOS::MessageParcel reply;
519 OHOS::MessageOption option;
520 std::unique_ptr<SharedMemQueue<SampleSmqElement>> smq =
521 std::make_unique<SharedMemQueue<SampleSmqElement>>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ);
522 ASSERT_TRUE(smq->IsGood());
523 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
524 ASSERT_EQ(ret, true);
525 ret = smq->GetMeta()->Marshalling(data);
526 ASSERT_TRUE(ret);
527 data.WriteUint32(1);
528
529 int status = sampleService->SendRequest(SAMPLE_TRANS_SMQ, data, reply, option);
530 ASSERT_EQ(status, 0);
531
532 constexpr int SEND_TIMES = 20;
533 for (size_t i = 0; i < SEND_TIMES; i++) {
534 SampleSmqElement t = {0};
535 t.data32 = i;
536 t.data64 = i + 1;
537
538 HDF_LOGI("%{public}s:write smq message %{public}zu", __func__, i);
539 auto status = smq->Write(&t, 1, OHOS::MillisecToNanosec(SMQ_TEST_WAIT_TIME));
540 ASSERT_EQ(status, 0);
541 }
542 }
543
544 /*
545 * smq test with overflow wait
546 */
547 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest012, TestSize.Level1)
548 {
549 auto servmgr = IServiceManager::Get();
550 ASSERT_TRUE(servmgr != nullptr);
551
552 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
553 ASSERT_TRUE(sampleService != nullptr);
554
555 OHOS::MessageParcel data;
556 OHOS::MessageParcel reply;
557 OHOS::MessageOption option;
558 std::unique_ptr<SharedMemQueue<SampleSmqElement>> smq =
559 std::make_unique<SharedMemQueue<SampleSmqElement>>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ);
560 ASSERT_TRUE(smq->IsGood());
561 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
562 ASSERT_EQ(ret, true);
563 constexpr uint32_t ELEMENT_SIZE = 2;
564 ret = smq->GetMeta()->Marshalling(data);
565 ASSERT_TRUE(ret);
566
567 data.WriteUint32(ELEMENT_SIZE);
568 int status = sampleService->SendRequest(SAMPLE_TRANS_SMQ, data, reply, option);
569 ASSERT_EQ(status, 0);
570
571 constexpr int SEND_TIMES = 20;
572 for (int i = 0; i < SEND_TIMES; i++) {
573 SampleSmqElement t[ELEMENT_SIZE] = {};
574 t[0].data32 = i;
575 t[0].data64 = i + 1;
576 t[1].data32 = i + 1;
577 t[1].data64 = i + 1;
578 HDF_LOGI("%{public}s:write smq message %{public}d", __func__, i);
579 auto status = smq->Write(&t[0], ELEMENT_SIZE, OHOS::MillisecToNanosec(SMQ_TEST_WAIT_TIME));
580 ASSERT_EQ(status, 0);
581 }
582 }
583
584 /*
585 * smq test UNSYNC_SMQ
586 */
587 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest013, TestSize.Level1)
588 {
589 auto servmgr = IServiceManager::Get();
590 ASSERT_TRUE(servmgr != nullptr);
591
592 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
593 ASSERT_TRUE(sampleService != nullptr);
594
595 OHOS::MessageParcel data;
596 OHOS::MessageParcel reply;
597 OHOS::MessageOption option;
598
599 std::unique_ptr<SharedMemQueue<SampleSmqElement>> smq =
600 std::make_unique<SharedMemQueue<SampleSmqElement>>(SMQ_TEST_QUEUE_SIZE, SmqType::UNSYNC_SMQ);
601 ASSERT_TRUE(smq->IsGood());
602 bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
603 ASSERT_EQ(ret, true);
604 constexpr uint32_t ELEMENT_SIZE = 2;
605 ret = smq->GetMeta()->Marshalling(data);
606 ASSERT_TRUE(ret);
607 data.WriteUint32(ELEMENT_SIZE);
608 auto status = sampleService->SendRequest(SAMPLE_TRANS_SMQ, data, reply, option);
609 ASSERT_EQ(status, 0);
610
611 SampleSmqElement t[ELEMENT_SIZE] = {};
612 status = smq->Write(&t[0], ELEMENT_SIZE);
613 EXPECT_NE(status, 0);
614 constexpr int SEND_TIMES = 20;
615 for (int i = 0; i < SEND_TIMES; i++) {
616 t[0].data32 = i;
617 t[0].data64 = i + 1;
618 t[1].data32 = i + 1;
619 t[1].data64 = i + 1;
620 HDF_LOGI("%{public}s:write smq message %{public}d", __func__, i);
621 status = smq->WriteNonBlocking(&t[0], ELEMENT_SIZE);
622 ASSERT_EQ(status, 0);
623 }
624 }
625
626 /*
627 * Test service status listener get serviec callback on register
628 */
629 HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest014, TestSize.Level1)
630 {
631 auto servmgr = IServiceManager::Get();
632 ASSERT_TRUE(servmgr != nullptr);
633
634 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
635 ASSERT_NE(sampleService, nullptr);
636
637 bool callbacked = false;
638 bool sampleServiceStarted = false;
639 uint16_t servStatus = 0;
640 ::OHOS::sptr<IServStatListener> listener
641 = new ServStatListener(
__anon4c4667ae0402(const ServiceStatus &status) 642 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
643 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
644 callbacked = true;
645 if (status.serviceName == std::string(TEST_SERVICE_NAME)) {
646 sampleServiceStarted = true;
647 servStatus = status.status;
648 }
649 }));
650
651 int status = servmgr->RegisterServiceStatusListener(listener, DEVICE_CLASS_DEFAULT);
652 ASSERT_EQ(status, HDF_SUCCESS);
653 constexpr int waitCount = 100;
654 int count = waitCount;
655 while (!sampleServiceStarted && count > 0) {
656 OsalMSleep(1);
657 count--;
658 }
659 ASSERT_TRUE(callbacked);
660 ASSERT_TRUE(sampleServiceStarted);
661 ASSERT_EQ(servStatus, OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START);
662 status = servmgr->UnregisterServiceStatusListener(listener);
663 ASSERT_EQ(status, HDF_SUCCESS);
664 }
665
666 /*
667 * Test get service set by interfacedesc
668 */
669 HWTEST_F(HdfServiceMangerHdiTest, ListServiceByInterfaceDescTest001, TestSize.Level1)
670 {
671 auto servmgr = IServiceManager::Get();
672 ASSERT_TRUE(servmgr != nullptr);
673 std::vector<std::string> serviceNames;
674 int ret = servmgr->ListServiceByInterfaceDesc(serviceNames, TEST_SERVICE_INTERFACE_DESC_N);
675 ASSERT_TRUE(ret == HDF_SUCCESS);
676 ASSERT_FALSE(serviceNames.empty());
677 ASSERT_TRUE(serviceNames.front().compare(TEST_SERVICE_NAME) == 0);
678 }
679
680 HWTEST_F(HdfServiceMangerHdiTest, ListServiceByInterfaceDescTest002, TestSize.Level1)
681 {
682 auto servmgr = IServiceManager::Get();
683 ASSERT_TRUE(servmgr != nullptr);
684 std::vector<std::string> serviceNames;
685 int ret = servmgr->ListServiceByInterfaceDesc(serviceNames, TEST_SERVICE_INTERFACE_DESC_INVALID);
686 ASSERT_TRUE(ret == HDF_SUCCESS);
687 ASSERT_TRUE(serviceNames.empty());
688 }
689
690 HWTEST_F(HdfServiceMangerHdiTest, ListServiceByInterfaceDescTest003, TestSize.Level1)
691 {
692 auto servmgr = IServiceManager::Get();
693 ASSERT_TRUE(servmgr != nullptr);
694 std::vector<std::string> serviceNames;
695 int ret = servmgr->ListServiceByInterfaceDesc(serviceNames, TEST_SERVICE_INTERFACE_DESC_VOID);
696 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM);
697 ASSERT_TRUE(serviceNames.empty());
698 }
699
700 HWTEST_F(HdfServiceMangerHdiTest, ListServiceByInterfaceDescTest004, TestSize.Level1)
701 {
702 auto servmgr = IServiceManager::Get();
703 ASSERT_TRUE(servmgr != nullptr);
704 std::vector<std::string> serviceNames;
705 int ret = servmgr->ListServiceByInterfaceDesc(serviceNames, TEST_SERVICE_INTERFACE_DESC_NULL);
706 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM);
707 ASSERT_TRUE(serviceNames.empty());
708 }
709
710 HWTEST_F(HdfServiceMangerHdiTest, ListServiceByInterfaceDescTest005, TestSize.Level1)
711 {
712 auto devmgr = IDeviceManager::Get();
713 ASSERT_TRUE(devmgr != nullptr);
714 int ret = devmgr->LoadDevice(TEST1_SERVICE_NAME);
715 ASSERT_TRUE(ret == HDF_SUCCESS);
716 auto servmgr = IServiceManager::Get();
717 ASSERT_TRUE(servmgr != nullptr);
718 OsalMSleep(WAIT_LOAD_UNLOAD_TIME);
719 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
720 ASSERT_TRUE(sampleService != nullptr);
721 auto sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
722 ASSERT_TRUE(sample1Service != nullptr);
723
724 std::vector<std::string> serviceNames;
725 ret = servmgr->ListServiceByInterfaceDesc(serviceNames, TEST_SERVICE_INTERFACE_DESC_N);
726 ASSERT_TRUE(ret == HDF_SUCCESS);
727 constexpr int sampleServiceCount = 2;
728 ASSERT_TRUE(serviceNames.size() == sampleServiceCount);
729 ASSERT_TRUE(serviceNames[0].compare(TEST_SERVICE_NAME) == 0);
730 ASSERT_TRUE(serviceNames[1].compare(TEST1_SERVICE_NAME) == 0);
731 ret = devmgr->UnloadDevice(TEST1_SERVICE_NAME);
732 ASSERT_EQ(ret, HDF_SUCCESS);
733 }
734
735 HWTEST_F(HdfServiceMangerHdiTest, ListAllServiceTest, TestSize.Level1)
736 {
737 auto servmgr = IServiceManager::Get();
738 ASSERT_TRUE(servmgr != nullptr);
739
740 std::vector<HdiServiceInfo> serviceInfos;
741 int ret = servmgr->ListAllService(serviceInfos);
742 ASSERT_TRUE(ret == HDF_SUCCESS);
743 ASSERT_TRUE(serviceInfos.size() != 0);
744 }
745
746 HWTEST_F(HdfServiceMangerHdiTest, ListAllDeviceTest, TestSize.Level1)
747 {
748 auto devmgr = IDeviceManager::Get();
749 ASSERT_TRUE(devmgr != nullptr);
750
751 std::vector<HdiDevHostInfo> deviceInfos;
752 int ret = devmgr->ListAllDevice(deviceInfos);
753 ASSERT_TRUE(ret == HDF_SUCCESS);
754 ASSERT_TRUE(deviceInfos.size() != 0);
755 }
756
757 HWTEST_F(HdfServiceMangerHdiTest, EndSampleHostTest, TestSize.Level1)
758 {
759 auto servmgr = IServiceManager::Get();
760 ASSERT_TRUE(servmgr != nullptr);
761
762 auto devmgr = IDeviceManager::Get();
763 ASSERT_TRUE(devmgr != nullptr);
764
765 int ret = devmgr->LoadDevice(TEST1_SERVICE_NAME);
766 ASSERT_TRUE(ret == HDF_SUCCESS);
767 auto sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
768 constexpr int waitCount = 1000;
769 constexpr int msleepTime = 10;
770 constexpr int waitHostStart = 100;
771 uint32_t cnt = 0;
772 while (sample1Service == nullptr && cnt < waitCount) {
773 OsalMSleep(msleepTime);
774 sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
775 cnt++;
776 }
777 ASSERT_TRUE(sample1Service != nullptr);
778 ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
779 ASSERT_TRUE(ret == HDF_SUCCESS);
780 cnt = 0;
781 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
782 while (sampleService == nullptr && cnt < waitCount) {
783 OsalMSleep(msleepTime);
784 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
785 cnt++;
786 }
787 ASSERT_TRUE(sampleService != nullptr);
788
789 ret = devmgr->UnloadDevice(TEST1_SERVICE_NAME);
790 ASSERT_TRUE(ret == HDF_SUCCESS);
791 sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
792 OsalMSleep(msleepTime);
793
794 OHOS::MessageParcel data;
795 OHOS::MessageParcel reply;
796 OHOS::MessageOption option;
797 ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
798 ASSERT_EQ(ret, true);
799
800 ret = sampleService->SendRequest(SAMPLE_END_HOST, data, reply, option);
801 ASSERT_TRUE(ret == HDF_SUCCESS);
802 OsalMSleep(waitHostStart);
803
804 ret = devmgr->UnloadDevice(TEST1_SERVICE_NAME);
805 ASSERT_TRUE(ret == HDF_SUCCESS);
806 OsalMSleep(msleepTime);
807 ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
808 ASSERT_TRUE(ret == HDF_SUCCESS);
809 }
810
811 HWTEST_F(HdfServiceMangerHdiTest, InjectPmTest, TestSize.Level1)
812 {
813 auto servmgr = IServiceManager::Get();
814 ASSERT_TRUE(servmgr != nullptr);
815
816 auto devmgr = IDeviceManager::Get();
817 ASSERT_TRUE(devmgr != nullptr);
818
819 int ret = devmgr->LoadDevice(TEST1_SERVICE_NAME);
820 ASSERT_TRUE(ret == HDF_SUCCESS);
821 auto sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
822 constexpr int waitCount = 1000;
823 constexpr int waitHostStart = 100;
824 constexpr int msleepTime = 10;
825 uint32_t cnt = 0;
826 while (sample1Service == nullptr && cnt < waitCount) {
827 OsalMSleep(msleepTime);
828 sample1Service = servmgr->GetService(TEST1_SERVICE_NAME);
829 cnt++;
830 }
831 ASSERT_TRUE(sample1Service != nullptr);
832 ret = devmgr->LoadDevice(TEST_SERVICE_NAME);
833 ASSERT_TRUE(ret == HDF_SUCCESS);
834 auto sampleService = servmgr->GetService(TEST_SERVICE_NAME);
835 cnt = 0;
836 while (sampleService == nullptr && cnt < waitCount) {
837 OsalMSleep(msleepTime);
838 sampleService = servmgr->GetService(TEST_SERVICE_NAME);
839 cnt++;
840 }
841 ASSERT_TRUE(sampleService != nullptr);
842
843 OHOS::MessageParcel data;
844 OHOS::MessageParcel reply;
845 OHOS::MessageOption option;
846 ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC);
847 ASSERT_EQ(ret, true);
848
849 ret = sampleService->SendRequest(SAMPLE_INJECT_PM, data, reply, option);
850 ASSERT_TRUE(ret == HDF_SUCCESS);
851 OsalMSleep(msleepTime);
852 ret = sampleService->SendRequest(SAMPLE_END_HOST, data, reply, option);
853 ASSERT_TRUE(ret == HDF_SUCCESS);
854 OsalMSleep(waitHostStart);
855
856 ret = devmgr->UnloadDevice(TEST1_SERVICE_NAME);
857 ASSERT_TRUE(ret == HDF_SUCCESS);
858 OsalMSleep(msleepTime);
859 ret = devmgr->UnloadDevice(TEST_SERVICE_NAME);
860 ASSERT_TRUE(ret == HDF_SUCCESS);
861 }
862
863 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest001, TestSize.Level1)
864 {
865 ::OHOS::sptr<ServStatListener> listener
866 = new ServStatListener(
__anon4c4667ae0502(const ServiceStatus &status) 867 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
868 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
869 }));
870
871 MessageParcel data;
872 MessageParcel reply;
873 MessageOption option;
874
875 // invalid code
876 int32_t ret = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_MAX, data, reply, option);
877 ASSERT_NE(ret, HDF_SUCCESS);
878
879 // empty data
880 ret = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
881 ASSERT_NE(ret, HDF_SUCCESS);
882 }
883
884 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest002, TestSize.Level1)
885 {
886 ::OHOS::sptr<ServStatListener> listener
887 = new ServStatListener(
__anon4c4667ae0602(const ServiceStatus &status) 888 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
889 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
890 }));
891
892 MessageParcel data;
893 MessageParcel reply;
894 MessageOption option;
895
896 bool ret = data.WriteInterfaceToken(IServStatListener::GetDescriptor());
897 ASSERT_TRUE(ret);
898 int32_t reqRet = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
899 ASSERT_NE(reqRet, HDF_SUCCESS);
900 }
901
902 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest003, TestSize.Level1)
903 {
904 ::OHOS::sptr<ServStatListener> listener
905 = new ServStatListener(
__anon4c4667ae0702(const ServiceStatus &status) 906 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
907 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
908 }));
909
910 MessageParcel data;
911 MessageParcel reply;
912 MessageOption option;
913
914 bool ret = data.WriteInterfaceToken(IServStatListener::GetDescriptor());
915 ASSERT_TRUE(ret);
916 ret = data.WriteCString("");
917 ASSERT_TRUE(ret);
918
919 int32_t reqRet = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
920 ASSERT_NE(reqRet, HDF_SUCCESS);
921 }
922
923 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest004, TestSize.Level1)
924 {
925 ::OHOS::sptr<ServStatListener> listener
926 = new ServStatListener(
__anon4c4667ae0802(const ServiceStatus &status) 927 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
928 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
929 }));
930
931 MessageParcel data;
932 MessageParcel reply;
933 MessageOption option;
934
935 bool ret = data.WriteInterfaceToken(IServStatListener::GetDescriptor());
936 ASSERT_TRUE(ret);
937 ret = data.WriteCString("test_service");
938 ASSERT_TRUE(ret);
939
940 int32_t reqRet = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
941 ASSERT_NE(reqRet, HDF_SUCCESS);
942 }
943
944 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest005, TestSize.Level1)
945 {
946 ::OHOS::sptr<ServStatListener> listener
947 = new ServStatListener(
__anon4c4667ae0902(const ServiceStatus &status) 948 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
949 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
950 }));
951
952 MessageParcel data;
953 MessageParcel reply;
954 MessageOption option;
955
956 bool ret = data.WriteInterfaceToken(IServStatListener::GetDescriptor());
957 ASSERT_TRUE(ret);
958 ret = data.WriteCString("test_service");
959 ASSERT_TRUE(ret);
960 // write deviceClass
961 ret = data.WriteUint16(DeviceClass::DEVICE_CLASS_DEFAULT);
962 ASSERT_TRUE(ret);
963
964 int32_t reqRet = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
965 ASSERT_NE(reqRet, HDF_SUCCESS);
966 }
967
968 HWTEST_F(HdfServiceMangerHdiTest, ListenerTest006, TestSize.Level1)
969 {
970 ::OHOS::sptr<ServStatListener> listener
971 = new ServStatListener(
__anon4c4667ae0a02(const ServiceStatus &status) 972 ServStatListener::StatusCallback([&](const ServiceStatus &status) {
973 HDF_LOGI("service status callback, service is %{public}s", status.serviceName.data());
974 }));
975
976 MessageParcel data;
977 MessageParcel reply;
978 MessageOption option;
979
980 bool ret = data.WriteInterfaceToken(IServStatListener::GetDescriptor());
981 ASSERT_TRUE(ret);
982 ret = data.WriteCString("test_service");
983 ASSERT_TRUE(ret);
984 // write deviceClass
985 ret = data.WriteUint16(DeviceClass::DEVICE_CLASS_DEFAULT);
986 ASSERT_TRUE(ret);
987 // write status
988 ret = data.WriteUint16(ServiceStatusType::SERVIE_STATUS_START);
989 ASSERT_TRUE(ret);
990
991 int32_t reqRet = listener->OnRemoteRequest(SERVIE_STATUS_LISTENER_NOTIFY, data, reply, option);
992 ASSERT_EQ(reqRet, HDF_SUCCESS);
993 }
994
995 HWTEST_F(HdfServiceMangerHdiTest, InvalidCodeTest001, TestSize.Level1)
996 {
997 auto servmgr = OHOS::HDI::ServiceManager::V1_0::IServiceManager::Get();
998 ASSERT_NE(servmgr, nullptr);
999
1000 sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
1001 ASSERT_NE(remote, nullptr);
1002
1003 MessageParcel data;
1004 MessageParcel reply;
1005 MessageOption option;
1006
1007 ASSERT_TRUE(data.WriteInterfaceToken(IDeviceManager::GetDescriptor()));
1008 int ret = remote->SendRequest(INVALID_CODE, data, reply, option);
1009 EXPECT_EQ(ret, ERROR_CODE_WITH_INVALID_CODE);
1010 }
1011
1012 HWTEST_F(HdfServiceMangerHdiTest, InvalidCodeTest002, TestSize.Level1)
1013 {
1014 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1015 ASSERT_NE(saManager, nullptr);
1016 sptr<IRemoteObject> remote = saManager->GetSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID);
1017 ASSERT_NE(remote, nullptr);
1018
1019 MessageParcel data;
1020 MessageParcel reply;
1021 MessageOption option;
1022
1023 ASSERT_TRUE(data.WriteInterfaceToken(OHOS::HDI::ServiceManager::V1_0::IServiceManager::GetDescriptor()));
1024 int ret = remote->SendRequest(INVALID_CODE, data, reply, option);
1025 ASSERT_EQ(ret, ERROR_CODE_WITH_INVALID_CODE);
1026 }
1027 } // namespace OHOS
1028