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 #include "fileaccessserviceproxy_fuzzer.h"
16 
17 #include <cstring>
18 #include <memory>
19 
20 #include "file_access_service_proxy.h"
21 #include "fileaccessservicemock.h"
22 #include "iservice_registry.h"
23 #include "refbase.h"
24 
25 namespace OHOS {
26 using namespace std;
27 using namespace FileAccessFwk;
28 
GetInstance()29 SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance()
30 {
31     static auto instance = std::make_shared<SystemAbilityManagerClient>();
32     return *instance;
33 }
34 
GetSystemAbilityManager()35 sptr<ISystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
36 {
37     return nullptr;
38 }
39 
DestroySystemAbilityManagerObject()40 void SystemAbilityManagerClient::DestroySystemAbilityManagerObject()
41 {}
42 
43 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)44 T TypeCast(const uint8_t *data, int *pos = nullptr)
45 {
46     if (pos) {
47         *pos += sizeof(T);
48     }
49     return *(reinterpret_cast<const T*>(data));
50 }
51 
OnChangeFuzzTest(shared_ptr<FileAccessServiceProxy> proxy,const uint8_t * data,size_t size)52 bool OnChangeFuzzTest(shared_ptr<FileAccessServiceProxy> proxy, const uint8_t *data, size_t size)
53 {
54     if (data == nullptr || size < sizeof(NotifyType)) {
55         return true;
56     }
57 
58     int pos = 0;
59     NotifyType notifyType = TypeCast<NotifyType>(data, &pos);
60     Uri uri(string(reinterpret_cast<const char*>(data + pos), size - pos));
61 
62     proxy->OnChange(uri, notifyType);
63     return true;
64 }
65 
RegisterNotifyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy,const uint8_t * data,size_t size)66 bool RegisterNotifyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy, const uint8_t *data, size_t size)
67 {
68     if (data == nullptr || size < sizeof(bool)) {
69         return true;
70     }
71 
72     int pos = 0;
73     auto info = make_shared<ConnectExtensionInfo>();
74     bool notifyForDescendants = TypeCast<bool>(data, &pos);
75     Uri uri(string(reinterpret_cast<const char*>(data + pos), size - pos));
76 
77     proxy->RegisterNotify(uri, notifyForDescendants, nullptr, info);
78     return true;
79 }
80 
UnregisterNotifyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy,const uint8_t * data,size_t size)81 bool UnregisterNotifyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy, const uint8_t *data, size_t size)
82 {
83     auto info = make_shared<ConnectExtensionInfo>();
84     Uri uri(string(reinterpret_cast<const char*>(data), size));
85 
86     proxy->UnregisterNotify(uri, nullptr, info);
87     return true;
88 }
89 
GetExensionProxyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy,const uint8_t * data,size_t size)90 bool GetExensionProxyFuzzTest(shared_ptr<FileAccessServiceProxy> proxy, const uint8_t *data, size_t size)
91 {
92     auto info = make_shared<ConnectExtensionInfo>();
93     sptr<IFileAccessExtBase> extensionProxy = nullptr;
94 
95     proxy->GetExtensionProxy(info, extensionProxy);
96     return true;
97 }
98 } // namespace OHOS
99 
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
102 {
103     auto impl = std::make_unique<OHOS::FileAccessFwk::FileAccessServiceMock>();
104     auto proxy = std::make_shared<OHOS::FileAccessFwk::FileAccessServiceProxy>(OHOS::sptr(impl.release()));
105     if (proxy == nullptr || impl == nullptr) {
106         return 0;
107     }
108 
109     OHOS::OnChangeFuzzTest(proxy, data, size);
110     OHOS::RegisterNotifyFuzzTest(proxy, data, size);
111     OHOS::UnregisterNotifyFuzzTest(proxy, data, size);
112     OHOS::GetExensionProxyFuzzTest(proxy, data, size);
113 
114     return 0;
115 }