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 "uiservice_fuzzer.h"
17
18 #include "ui_service_mgr_client.h"
19 #include "want.h"
20
21 namespace OHOS {
22 constexpr size_t MAXBYTELEN = 65535;
23
RegisterCallBackTest(const uint8_t * data,const size_t size)24 bool RegisterCallBackTest(const uint8_t* data, const size_t size)
25 {
26 OHOS::AAFwk::Want want;
27 OHOS::Ace::UIServiceMgrClient client;
28 sptr<Ace::IUIService> uiService = nullptr;
29 std::string randomString(reinterpret_cast<const char*>(data), size);
30 int randomNumber = static_cast<int>(size % MAXBYTELEN);
31 DialogCallback callback;
32 int* id = nullptr;
33 client.RegisterCallBack(want, uiService);
34 client.ShowDialog(
35 randomString, randomString, OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW,
36 randomNumber, randomNumber, randomNumber, randomNumber, callback, id);
37 client.UpdateDialog(randomNumber, randomString);
38 return client.CancelDialog(randomNumber) == ERR_OK;
39 }
40
UnregisterCallBackTest(const uint8_t * data,const size_t size)41 bool UnregisterCallBackTest(const uint8_t* data, const size_t size)
42 {
43 OHOS::AAFwk::Want want;
44 OHOS::Ace::UIServiceMgrClient client;
45 return client.UnregisterCallBack(want) == ERR_OK;
46 }
47
PushTest(const uint8_t * data,const size_t size)48 bool PushTest(const uint8_t* data, const size_t size)
49 {
50 OHOS::AAFwk::Want want;
51 std::string randomString(reinterpret_cast<const char*>(data), size);
52 OHOS::Ace::UIServiceMgrClient client;
53 return client.Push(want, randomString, randomString, randomString, randomString) == ERR_OK;
54 }
55
RequestTest(const uint8_t * data,const size_t size)56 bool RequestTest(const uint8_t* data, const size_t size)
57 {
58 OHOS::AAFwk::Want want;
59 std::string randomString(reinterpret_cast<const char*>(data), size);
60 OHOS::Ace::UIServiceMgrClient client;
61 return client.Request(want, randomString, randomString) == ERR_OK;
62 }
63
ReturnRequestTest(const uint8_t * data,const size_t size)64 bool ReturnRequestTest(const uint8_t* data, const size_t size)
65 {
66 OHOS::AAFwk::Want want;
67 std::string randomString(reinterpret_cast<const char*>(data), size);
68 OHOS::Ace::UIServiceMgrClient client;
69 return client.ReturnRequest(want, randomString, randomString, randomString) == ERR_OK;
70 }
71 }
72
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76 /* Run your code on data */
77 OHOS::RegisterCallBackTest(data, size);
78 OHOS::PushTest(data, size);
79 OHOS::RequestTest(data, size);
80 OHOS::ReturnRequestTest(data, size);
81 OHOS::UnregisterCallBackTest(data, size);
82 return 0;
83 }
84
85