1 /*
2  * Copyright (c) 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 <string>
17 #include <cstdlib>
18 #include <random>
19 
20 #include "deviceprofile_connector.h"
21 #include "device_profile_connector_fuzzer.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
DeviceProfileConnectorFuzzTest(const uint8_t * data,size_t size)25 void DeviceProfileConnectorFuzzTest(const uint8_t* data, size_t size)
26 {
27     if ((data == nullptr) || (size < sizeof(DmAccesser))) {
28         return;
29     }
30 
31     std::string trustDeviceId(reinterpret_cast<const char*>(data), size);
32     std::string requestDeviceId(reinterpret_cast<const char*>(data), size);
33     std::string pkgName(reinterpret_cast<const char*>(data), size);
34     std::string trustUdid(reinterpret_cast<const char*>(data), size);
35     std::string localDeviceId(reinterpret_cast<const char*>(data), size);
36     std::string targetDeviceId(reinterpret_cast<const char*>(data), size);
37     std::string requestAccountId(reinterpret_cast<const char*>(data), size);
38     std::string deviceIdHash(reinterpret_cast<const char*>(data), size);
39     std::string trustBundleName(reinterpret_cast<const char*>(data), size);
40     DmAclInfo aclInfo;
41     aclInfo.bindType = *(reinterpret_cast<const int32_t*>(data));
42     aclInfo.bindLevel = *(reinterpret_cast<const int32_t*>(data));
43     aclInfo.trustDeviceId = trustDeviceId;
44     aclInfo.deviceIdHash = deviceIdHash;
45     aclInfo.authenticationType = *(reinterpret_cast<const int32_t*>(data));
46     DmAccesser dmAccesser;
47     dmAccesser.requestDeviceId = requestDeviceId;
48     dmAccesser.requestUserId = *(reinterpret_cast<const int32_t*>(data));
49     dmAccesser.requestAccountId = requestAccountId;
50     DmAccessee dmAccessee;
51     dmAccessee.trustDeviceId = trustDeviceId;
52     dmAccessee.trustUserId = *(reinterpret_cast<const int32_t*>(data));
53     dmAccessee.trustBundleName = trustBundleName;
54     int32_t userId = *(reinterpret_cast<const int32_t*>(data));
55     std::string accountId(reinterpret_cast<const char*>(data), size);
56 
57     DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId);
58     DeviceProfileConnector::GetInstance().GetBindTypeByPkgName(pkgName, requestDeviceId, trustUdid);
59     DeviceProfileConnector::GetInstance().GetPkgNameFromAcl(localDeviceId, targetDeviceId);
60     DeviceProfileConnector::GetInstance().GetOfflineParamFromAcl(trustDeviceId, requestDeviceId);
61     DeviceProfileConnector::GetInstance().PutAccessControlList(aclInfo, dmAccesser, dmAccessee);
62     DeviceProfileConnector::GetInstance().DeleteAccessControlList(userId, accountId);
63     DeviceProfileConnector::GetInstance().DeleteAccessControlList(pkgName, localDeviceId, requestDeviceId);
64     DeviceProfileConnector::GetInstance().UpdateAccessControlList(userId, accountId, accountId);
65     DeviceProfileConnector::GetInstance().CheckIdenticalAccount(userId, accountId);
66     DeviceProfileConnector::GetInstance().DeleteP2PAccessControlList(userId, accountId);
67     DeviceProfileConnector::GetInstance().CheckDevIdInAclForDevBind(pkgName, localDeviceId);
68     DeviceProfileConnector::GetInstance().DeleteTimeOutAcl(localDeviceId);
69     DeviceProfileConnector::GetInstance().GetTrustNumber(localDeviceId);
70 }
71 }
72 }
73 
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77     /* Run your code on data */
78     OHOS::DistributedHardware::DeviceProfileConnectorFuzzTest(data, size);
79 
80     return 0;
81 }