• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modal_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 "modal_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 MODAL_CALLBACK_CODE_MIN = 0;
34 constexpr uint32_t MODAL_CALLBACK_CODE_MAX = 100;
35 const std::u16string MODAL_CALLBACK_INTERFACE_TOKEN = u"ohos.UserIam.UserAuth.ModalCallback";
36 
37 class DummyUserAuthModalClientCallback final : public UserAuthModalClientCallback {
38 public:
SendCommand(uint64_t contextId,const std::string & cmdData)39     void SendCommand(uint64_t contextId, const std::string &cmdData)
40     {
41         IAM_LOGI("start");
42         static_cast<void>(contextId);
43         static_cast<void>(cmdData);
44     }
IsModalInit()45     bool IsModalInit()
46     {
47         IAM_LOGI("start");
48         return false;
49     }
IsModalDestroy()50     bool IsModalDestroy()
51     {
52         IAM_LOGI("start");
53         return false;
54     }
55 
56 private:
CancelAuthentication(uint64_t contextId,int32_t cancelReason)57     void CancelAuthentication(uint64_t contextId, int32_t cancelReason)
58     {
59         IAM_LOGI("start");
60         static_cast<void>(contextId);
61         static_cast<void>(cancelReason);
62     }
63 };
64 
ModalCallbackStubFuzzTest(const uint8_t * rawData,size_t size)65 bool ModalCallbackStubFuzzTest(const uint8_t *rawData, size_t size)
66 {
67     IAM_LOGI("start");
68     if (rawData == nullptr) {
69         return false;
70     }
71 
72     auto service =
73         Common::MakeShared<ModalCallbackService>(Common::MakeShared<DummyUserAuthModalClientCallback>());
74     for (uint32_t code = MODAL_CALLBACK_CODE_MIN; code < MODAL_CALLBACK_CODE_MAX; code++) {
75         MessageParcel data;
76         MessageParcel reply;
77         MessageOption optionSync = MessageOption::TF_SYNC;
78         MessageOption optionAsync = MessageOption::TF_ASYNC;
79         // Sync
80         data.WriteInterfaceToken(MODAL_CALLBACK_INTERFACE_TOKEN);
81         data.WriteBuffer(rawData, size);
82         data.RewindRead(0);
83         (void)service->OnRemoteRequest(code, data, reply, optionSync);
84         // Async
85         (void)service->OnRemoteRequest(code, data, reply, optionAsync);
86     }
87     return true;
88 }
89 } // namespace
90 } // namespace UserAuth
91 } // namespace UserIam
92 } // namespace OHOS
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
96 {
97     OHOS::UserIam::UserAuth::ModalCallbackStubFuzzTest(data, size);
98     return 0;
99 }
100