1 /*
2  * Copyright (c) 2022-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 <cstddef>
17 #include <cstdint>
18 #include <string>
19 #include "device_manager_impl.h"
20 #include "device_manager.h"
21 #include "device_manager_callback.h"
22 #include "dm_credential_impl_fuzzer.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 class CredentialCallbackFuzzTest : public CredentialCallback {
27 public:
~CredentialCallbackFuzzTest()28     virtual ~CredentialCallbackFuzzTest() {}
OnCredentialResult(int32_t & action,const std::string & credentialResult)29     void OnCredentialResult(int32_t &action, const std::string &credentialResult) override {}
30 };
31 
DeviceManagerCredentialFuzzTest(const uint8_t * data,size_t size)32 void DeviceManagerCredentialFuzzTest(const uint8_t* data, size_t size)
33 {
34     if ((data == nullptr) || (size == 0)) {
35         return;
36     }
37     std::string pkgName(reinterpret_cast<const char*>(data), size);
38     std::string reqJsonStr;
39     std::string returnJsonStr;
40     std::shared_ptr<CredentialCallbackFuzzTest> callback = std::make_shared<CredentialCallbackFuzzTest>();
41 
42     DeviceManager::GetInstance().RequestCredential(pkgName, returnJsonStr);
43     DeviceManager::GetInstance().ImportCredential(pkgName, reqJsonStr, returnJsonStr);
44     DeviceManager::GetInstance().DeleteCredential(pkgName, reqJsonStr, returnJsonStr);
45     DeviceManager::GetInstance().RegisterCredentialCallback(pkgName, callback);
46     DeviceManager::GetInstance().UnRegisterCredentialCallback(pkgName);
47 }
48 }
49 }
50 
51 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
53 {
54     /* Run your code on data */
55     OHOS::DistributedHardware::DeviceManagerCredentialFuzzTest(data, size);
56 
57     return 0;
58 }
59