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 #include "storagemanagerproxy_fuzzer.h"
16 #include "storage_manager_proxy.h"
17 #include "storagemanagerproxymock.h"
18 #include <vector>
19 #include <memory>
20 
21 using namespace OHOS::StorageManager;
22 
23 namespace OHOS {
24 namespace StorageManager {
StorageManagerProxyFuzzTest(const uint8_t * data,size_t size)25 bool StorageManagerProxyFuzzTest(const uint8_t *data, size_t size)
26 {
27     if ((data == nullptr) || (size <= sizeof(uint64_t))) {
28         return false;
29     }
30     auto impl = new StorageManagerProxyMock();
31     auto proxy = std::make_shared<StorageManagerProxy>(impl);
32     if (proxy == nullptr || impl == nullptr) {
33         return 0;
34     }
35 
36     VolumeCore vc;
37     Disk disk;
38     MessageParcel reply;
39     MessageOption option;
40     std::vector<uint8_t> token;
41     std::vector<uint8_t> secret;
42 
43     std::string metaData(reinterpret_cast<const char *>(data), size);
44     int32_t metaData2 = *(reinterpret_cast<const int32_t *>(data));
45     uint32_t metaData3 = *(reinterpret_cast<const uint32_t *>(data));
46     uint64_t metaData4 = *(reinterpret_cast<const uint64_t *>(data));
47     token.push_back(*data);
48     secret.push_back(*data);
49     proxy->StopUser(metaData2);
50     proxy->Mount(metaData);
51     proxy->Unmount(metaData);
52     proxy->DeleteUserKeys(metaData3);
53     proxy->NotifyDiskCreated(disk);
54     proxy->InactiveUserKey(metaData3);
55     proxy->UpdateKeyContext(metaData3);
56     proxy->NotifyVolumeCreated(vc);
57     proxy->RemoveUser(metaData2, metaData3);
58     proxy->PrepareStartUser(metaData2);
59     proxy->Format(metaData, metaData);
60     proxy->GetDiskById(metaData, disk);
61     proxy->Partition(metaData, metaData2);
62     proxy->PrepareAddUser(metaData2, metaData3);
63     proxy->GenerateUserKeys(metaData2, metaData3);
64     proxy->NotifyDiskDestroyed(metaData);
65     proxy->ActiveUserKey(metaData2, token, secret);
66     proxy->SetVolumeDescription(metaData, metaData);
67     proxy->UpdateUserAuth(metaData2, metaData4, token, secret, secret);
68     proxy->NotifyVolumeMounted(metaData, metaData2, metaData, metaData, metaData);
69     return true;
70 }
71 
StorageManagerProxyGetFuzzTest(const uint8_t * data,size_t size)72 bool StorageManagerProxyGetFuzzTest(const uint8_t *data, size_t size)
73 {
74     if ((data == nullptr) || (size <= sizeof(int64_t))) {
75         return false;
76     }
77     auto impl = new StorageManagerProxyMock();
78     auto proxy = std::make_shared<StorageManagerProxy>(impl);
79     if (proxy == nullptr || impl == nullptr) {
80         return 0;
81     }
82     VolumeExternal vc1;
83     std::string metaData(reinterpret_cast<const char *>(data), size);
84     int32_t userId = *(reinterpret_cast<const int32_t *>(data));
85     int64_t metaData4 = *(reinterpret_cast<const int64_t *>(data));
86 
87     BundleStats bundleStats;
88     StorageStats storageStats;
89     std::vector<VolumeExternal> vecOfVol;
90     std::vector<Disk> vecOfDisk;
91     std::vector<int64_t> incrementalBackTimes;
92     std::vector<int64_t> pkgFileSizes;
93     std::vector<int64_t> incPkgFileSizes;
94     std::vector<std::string> bundleName;
95     incrementalBackTimes.push_back(metaData4);
96     pkgFileSizes.push_back(metaData4);
97     incPkgFileSizes.push_back(metaData4);
98     bundleName.push_back(metaData);
99     proxy->GetAllVolumes(vecOfVol);
100     proxy->GetAllDisks(vecOfDisk);
101     proxy->GetSystemSize(metaData4);
102     proxy->GetTotalSize(metaData4);
103     proxy->GetFreeSize(metaData4);
104     proxy->GetUserStorageStats(storageStats);
105     proxy->GetBundleStats(metaData, bundleStats, 0, 0);
106     proxy->GetCurrentBundleStats(bundleStats, 0);
107     proxy->GetUserStorageStats(userId, storageStats);
108     proxy->GetUserStorageStatsByType(userId, storageStats, metaData);
109     proxy->GetVolumeByUuid(metaData, vc1);
110     proxy->GetVolumeById(metaData, vc1);
111     proxy->GetFreeSizeOfVolume(metaData, metaData4);
112     proxy->GetTotalSizeOfVolume(metaData, metaData4);
113     proxy->GetBundleStatsForIncrease(userId, bundleName, incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
114     return true;
115 }
116 } // namespace StorageManager
117 } // namespace OHOS
118 
119 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)120 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
121 {
122     /* Run your code on data */
123 
124     OHOS::StorageManager::StorageManagerProxyFuzzTest(data, size);
125     OHOS::StorageManager::StorageManagerProxyGetFuzzTest(data, size);
126     return 0;
127 }
128