• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "devauthservgetregisterinfo_fuzzer.h"
17 #include "common_defs.h"
18 #include "device_auth.h"
19 #include "device_auth_defines.h"
20 #include "hc_dev_info.h"
21 #include "hc_log.h"
22 #include "json_utils.h"
23 
24 namespace OHOS {
GenerateReqJson(CJson * json,const char * deviceId)25     static void GenerateReqJson(CJson *json, const char *deviceId)
26     {
27         AddStringToJson(json, FIELD_VERSION, "1");
28         AddStringToJson(json, FIELD_DEVICE_ID, deviceId);
29         AddStringToJson(json, FIELD_USER_ID, "123");
30     }
31 
FuzzDoGetRegisterInfo(const uint8_t * data,size_t size)32     bool FuzzDoGetRegisterInfo(const uint8_t* data, size_t size)
33     {
34         if (data == nullptr) {
35             return false;
36         }
37         InitDeviceAuthService();
38         CJson *reqJson = CreateJson();
39         std::string deviceId(reinterpret_cast<const char *>(data), size);
40         GenerateReqJson(reqJson, deviceId.c_str());
41         char *reqJsonStr = PackJsonToString(reqJson);
42         FreeJson(reqJson);
43         const DeviceGroupManager *gmInstance = GetGmInstance();
44         char *returnJsonStr = nullptr;
45         LOGI("reqJsonStr: %" LOG_PUB "s", reqJsonStr);
46         gmInstance->getRegisterInfo(reqJsonStr, &returnJsonStr);
47         if (returnJsonStr != nullptr) {
48             gmInstance->destroyInfo(&returnJsonStr);
49         }
50         ClearAndFreeJsonString(reqJsonStr);
51         DestroyDeviceAuthService();
52         return true;
53     }
54 }
55 
56 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59     /* Run your code on data */
60     OHOS::FuzzDoGetRegisterInfo(data, size);
61     return 0;
62 }
63 
64