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 "fileaccessextconnection_fuzzer.h"
16
17 #include <string>
18 #include <memory>
19
20 #include "hilog_wrapper.h"
21 #include "file_access_ext_connection.h"
22
23 namespace OHOS {
24 using namespace std;
25 using namespace FileAccessFwk;
26
OnAbilityConnectDoneFuzzTest(sptr<FileAccessExtConnection> conn,const uint8_t * data,size_t size)27 bool OnAbilityConnectDoneFuzzTest(sptr<FileAccessExtConnection> conn, const uint8_t *data, size_t size)
28 {
29 AppExecFwk::ElementName element;
30 std::string name(reinterpret_cast<const char*>(data), size);
31 element.SetBundleName(name);
32 sptr<IRemoteObject> remoteObject = nullptr;
33 conn->OnAbilityConnectDone(element, remoteObject, 0);
34 return true;
35 }
36
OnAbilityDisconnectDoneFuzzTest(sptr<FileAccessExtConnection> conn,const uint8_t * data,size_t size)37 bool OnAbilityDisconnectDoneFuzzTest(sptr<FileAccessExtConnection> conn, const uint8_t *data, size_t size)
38 {
39 AppExecFwk::ElementName element;
40 std::string name(reinterpret_cast<const char*>(data), size);
41 element.SetBundleName(name);
42 conn->OnAbilityDisconnectDone(element, 0);
43 return true;
44 }
45
IsExtAbilityConnectedFuzzTest(sptr<FileAccessExtConnection> conn)46 bool IsExtAbilityConnectedFuzzTest(sptr<FileAccessExtConnection> conn)
47 {
48 conn->IsExtAbilityConnected();
49 return true;
50 }
51
GetFileExtProxyFuzzTest(sptr<FileAccessExtConnection> conn)52 bool GetFileExtProxyFuzzTest(sptr<FileAccessExtConnection> conn)
53 {
54 conn->GetFileExtProxy();
55 return true;
56 }
57
ConnectFileExtAbility(sptr<FileAccessExtConnection> conn,const uint8_t * data,size_t size)58 bool ConnectFileExtAbility(sptr<FileAccessExtConnection> conn, const uint8_t *data, size_t size)
59 {
60 int len = size >> 1;
61 AAFwk::Want want;
62 want.SetElementName(std::string(reinterpret_cast<const char*>(data), len),
63 std::string(reinterpret_cast<const char*>(data + len), size - len));
64 sptr<IRemoteObject> remoteObject = nullptr;
65 conn->ConnectFileExtAbility(want, remoteObject);
66 return true;
67 }
68
DisconnectFileExtAbility(sptr<FileAccessExtConnection> conn)69 bool DisconnectFileExtAbility(sptr<FileAccessExtConnection> conn)
70 {
71 conn->DisconnectFileExtAbility();
72 return true;
73 }
74 } // namespace OHOS
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
78 {
79 auto conn = OHOS::sptr<OHOS::FileAccessFwk::FileAccessExtConnection>();
80 if (conn == nullptr) {
81 return 0;
82 }
83
84 OHOS::OnAbilityConnectDoneFuzzTest(conn, data, size);
85 OHOS::OnAbilityDisconnectDoneFuzzTest(conn, data, size);
86 OHOS::IsExtAbilityConnectedFuzzTest(conn);
87 OHOS::GetFileExtProxyFuzzTest(conn);
88 OHOS::ConnectFileExtAbility(conn, data, size);
89 OHOS::DisconnectFileExtAbility(conn);
90
91 return 0;
92 }
93