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
16 #include "cmdgetosaccountservicestub_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include "src/callbacks.h"
21
22 #define private public
23 #include "account_mgr_service.h"
24 #undef private
25 #include "iaccount.h"
26
27 using namespace std;
28 using namespace OHOS::AccountSA;
29
30 namespace OHOS {
31 namespace {
32 const std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IAccount";
33 static pthread_once_t g_fcOnce = PTHREAD_ONCE_INIT;
34 }
35
SelinuxLog(int logLevel,const char * fmt,...)36 static int SelinuxLog(int logLevel, const char *fmt, ...)
37 {
38 (void)logLevel;
39 (void)fmt;
40 return 0;
41 }
42
SelinuxSetCallback()43 static void SelinuxSetCallback()
44 {
45 union selinux_callback cb;
46 cb.func_log = SelinuxLog;
47 selinux_set_callback(SELINUX_CB_LOG, cb);
48 }
49
CmdGetOsAccountServiceStubFuzzTest(const uint8_t * data,size_t size)50 bool CmdGetOsAccountServiceStubFuzzTest(const uint8_t* data, size_t size)
51 {
52 __selinux_once(g_fcOnce, SelinuxSetCallback);
53 if ((data == nullptr) || (size == 0)) {
54 return false;
55 }
56
57 MessageParcel dataTemp;
58 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
59 return false;
60 }
61
62 MessageParcel reply;
63 MessageOption option;
64
65 uint32_t code = static_cast<uint32_t>(AccountMgrInterfaceCode::GET_OS_ACCOUNT_SERVICE);
66 DelayedRefSingleton<AccountMgrService>::GetInstance().state_ = ServiceRunningState::STATE_RUNNING;
67 DelayedRefSingleton<AccountMgrService>::GetInstance().OnRemoteRequest(code, dataTemp, reply, option);
68
69 return true;
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::CmdGetOsAccountServiceStubFuzzTest(data, size);
78 return 0;
79 }
80
81