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 <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 virtual 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 szData(reinterpret_cast<const char*>(data), size);
38 std::shared_ptr<CredentialCallbackFuzzTest> callback = std::make_shared<CredentialCallbackFuzzTest>();
39 std::string returnJsonStr;
40
41 DeviceManager::GetInstance().RequestCredential(szData, szData, returnJsonStr);
42 DeviceManager::GetInstance().ImportCredential(szData, szData);
43 DeviceManager::GetInstance().DeleteCredential(szData, szData);
44 DeviceManager::GetInstance().RegisterCredentialCallback(szData, callback);
45 DeviceManager::GetInstance().UnRegisterCredentialCallback(szData);
46 }
47 }
48 }
49
50 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
52 {
53 /* Run your code on data */
54 OHOS::DistributedHardware::DeviceManagerCredentialFuzzTest(data, size);
55
56 return 0;
57 }
58