1 /*
2 * Copyright (c) 2025 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 "widget_callback_stub_fuzzer.h"
17
18 #include "parcel.h"
19
20 #include "iam_fuzz_test.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "widget_callback_service.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 using namespace std;
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 namespace {
33 constexpr uint32_t WIDGET_CALLBACK_CODE_MIN = 0;
34 constexpr uint32_t WIDGET_CALLBACK_CODE_MAX = 100;
35 const std::u16string WIDGET_CALLBACK_INTERFACE_TOKEN = u"ohos.UserIam.UserAuth.WidgetCallback";
36
37 class DummyIUserAuthWidgetCallback final : public IUserAuthWidgetCallback {
38 public:
SendCommand(const std::string & cmdData)39 void SendCommand(const std::string &cmdData)
40 {
41 IAM_LOGI("start");
42 static_cast<void>(cmdData);
43 }
44 };
45
WidgetCallbackStubFuzzTest(const uint8_t * rawData,size_t size)46 bool WidgetCallbackStubFuzzTest(const uint8_t *rawData, size_t size)
47 {
48 IAM_LOGI("start");
49 if (rawData == nullptr) {
50 return false;
51 }
52
53 auto service =
54 Common::MakeShared<WidgetCallbackService>(Common::MakeShared<DummyIUserAuthWidgetCallback>());
55 for (uint32_t code = WIDGET_CALLBACK_CODE_MIN; code < WIDGET_CALLBACK_CODE_MAX; code++) {
56 MessageParcel data;
57 MessageParcel reply;
58 MessageOption optionSync = MessageOption::TF_SYNC;
59 MessageOption optionAsync = MessageOption::TF_ASYNC;
60 // Sync
61 data.WriteInterfaceToken(WIDGET_CALLBACK_INTERFACE_TOKEN);
62 data.WriteBuffer(rawData, size);
63 data.RewindRead(0);
64 (void)service->OnRemoteRequest(code, data, reply, optionSync);
65 // Async
66 (void)service->OnRemoteRequest(code, data, reply, optionAsync);
67 }
68 return true;
69 }
70 } // namespace
71 } // namespace UserAuth
72 } // namespace UserIam
73 } // namespace OHOS
74
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
77 {
78 OHOS::UserIam::UserAuth::WidgetCallbackStubFuzzTest(data, size);
79 return 0;
80 }
81