1 /*
2  * Copyright (c) 2023 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 "bundleactiveobserver_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "app_mgr_interface.h"
20 
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundle_active_service.h"
24 #include "iapp_group_callback.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28     constexpr uint32_t U32_AT_SIZE = 4;
29     constexpr uint8_t TWENTYFOUR = 24;
30     constexpr uint8_t SIXTEEN = 16;
31     constexpr uint8_t EIGHT = 8;
32     constexpr uint8_t TWO = 2;
33     constexpr uint8_t THREE = 3;
34     bool g_isInited = false;
35 
GetU32Data(const char * ptr)36     uint32_t GetU32Data(const char* ptr)
37     {
38         return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
39     }
40 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)41     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
42     {
43         DelayedSingleton<FuzztestHelper>::GetInstance()->NativeTokenGet();
44         if (!g_isInited) {
45             DelayedSingleton<BundleActiveService>::GetInstance()->InitService();
46             g_isInited = true;
47         }
48         sptr<IAppGroupCallback> appGroupCallback = nullptr;
49         DelayedSingleton<BundleActiveService>::GetInstance()->RegisterAppGroupCallBack(appGroupCallback);
50         DelayedSingleton<BundleActiveService>::GetInstance()->UnRegisterAppGroupCallBack(appGroupCallback);
51         bool result = false;
52         int32_t userId = static_cast<int32_t>(GetU32Data(data));
53         std::string inputBundlName(data);
54         DelayedSingleton<BundleActiveService>::GetInstance()->IsBundleIdle(result, inputBundlName, userId);
55         DelayedSingleton<BundleActiveService>::GetInstance()->IsBundleUsePeriod(result, inputBundlName, userId);
56         return true;
57     }
58 } // namespace DeviceUsageStats
59 } // namespace OHOS
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     if (data == nullptr) {
66         return 0;
67     }
68 
69     if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
70         return 0;
71     }
72     char* ch = (char *)malloc(size + 1);
73     if (ch == nullptr) {
74         return 0;
75     }
76 
77     (void)memset_s(ch, size + 1, 0x00, size + 1);
78     if (memcpy_s(ch, size, data, size) != EOK) {
79         free(ch);
80         ch = nullptr;
81         return 0;
82     }
83 
84     OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(ch, size);
85     free(ch);
86     ch = nullptr;
87     return 0;
88 }
89 
90