• 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 "enableappaccessstub_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "account_log_wrapper.h"
22 #include "app_account_manager_service.h"
23 #include "fuzz_data.h"
24 #include "iapp_account.h"
25 
26 using namespace std;
27 using namespace OHOS::AccountSA;
28 namespace OHOS {
29 const std::u16string APPACCOUNT_TOKEN = u"OHOS.AccountSA.IAppAccount";
30 const int CONST_NUMBER_ONE = 1;
EnableAppAccessStubFuzzTest(const uint8_t * data,size_t size)31 bool EnableAppAccessStubFuzzTest(const uint8_t *data, size_t size)
32 {
33     if ((data == nullptr) || (size == 0)) {
34         return false;
35     }
36     MessageParcel dataTemp;
37     FuzzData fuzzData(data, size);
38     auto token = APPACCOUNT_TOKEN;
39     auto isWriteCorrectToken = fuzzData.GetData<bool>();
40     if (!isWriteCorrectToken) {
41         token.append(CONST_NUMBER_ONE, fuzzData.GetData<char16_t>());
42     }
43     if (!dataTemp.WriteInterfaceToken(token)) {
44         return false;
45     }
46     std::string name = fuzzData.GenerateString();
47     if (!dataTemp.WriteString(name)) {
48         return false;
49     }
50     std::string authorizedApp = fuzzData.GenerateString();
51     if (!dataTemp.WriteString(authorizedApp)) {
52         return false;
53     }
54     MessageParcel reply;
55     MessageOption option;
56     uint32_t code = static_cast<uint32_t>(IAppAccountIpcCode::COMMAND_ENABLE_APP_ACCESS);
57     auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
58     appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
59     return true;
60 }
61 
DisableAppAccessStubFuzzTest(const uint8_t * data,size_t size)62 bool DisableAppAccessStubFuzzTest(const uint8_t *data, size_t size)
63 {
64     if ((data == nullptr) || (size == 0)) {
65         return false;
66     }
67     MessageParcel dataTemp;
68     if (!dataTemp.WriteInterfaceToken(APPACCOUNT_TOKEN)) {
69         return false;
70     }
71     FuzzData fuzzData(data, size);
72     std::string name = fuzzData.GenerateString();
73     if (!dataTemp.WriteString(name)) {
74         return false;
75     }
76     std::string authorizedApp = fuzzData.GenerateString();
77     if (!dataTemp.WriteString(authorizedApp)) {
78         return false;
79     }
80     MessageParcel reply;
81     MessageOption option;
82     uint32_t code = static_cast<uint32_t>(IAppAccountIpcCode::COMMAND_DISABLE_APP_ACCESS);
83     auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
84     appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
85     return true;
86 }
87 } // namespace OHOS
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92     /* Run your code on data */
93     OHOS::EnableAppAccessStubFuzzTest(data, size);
94     OHOS::DisableAppAccessStubFuzzTest(data, size);
95     return 0;
96 }
97