• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "connectionobserverclientimpl_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #define protected public
24 #include "connection_observer_client_impl.h"
25 #include "service_proxy_adapter.h"
26 #undef protected
27 #undef private
28 
29 #include "ability_record.h"
30 #include "continuous_task_callback_info.h"
31 #include "connection_observer.h"
32 
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35 using namespace OHOS::AbilityRuntime;
36 
37 namespace OHOS {
38 namespace {
39 class MyConnectionObserver : public ConnectionObserver {
40 public:
41     MyConnectionObserver() = default;
42     virtual ~MyConnectionObserver() = default;
OnExtensionConnected(const ConnectionData & data)43     void OnExtensionConnected(const ConnectionData& data) override
44     {}
OnExtensionDisconnected(const ConnectionData & data)45     void OnExtensionDisconnected(const ConnectionData& data) override
46     {}
OnDlpAbilityOpened(const DlpStateData & data)47     void OnDlpAbilityOpened(const DlpStateData& data) override
48     {}
OnDlpAbilityClosed(const DlpStateData & data)49     void OnDlpAbilityClosed(const DlpStateData& data) override
50     {}
OnServiceDied()51     void OnServiceDied() override
52     {}
53 };
54 }
55 
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)56 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
57 {
58     std::shared_ptr<ConnectionObserver> observer = std::make_shared<MyConnectionObserver>();
59     // fuzz for connectionObserverClientImpl
60     auto connectionObserverClientImpl = std::make_shared<ConnectionObserverClientImpl>();
61     connectionObserverClientImpl->UnregisterObserver(observer);
62 
63     Parcel parcel;
64     parcel.WriteString(fdp->ConsumeRandomLengthString());
65     sptr<AbilityRuntime::ConnectionData> connData = AbilityRuntime::ConnectionData::Unmarshalling(parcel);
66     AbilityRuntime::ConnectionData connectionData = *connData;
67 
68     connectionObserverClientImpl->HandleExtensionConnected(connectionData);
69     connectionObserverClientImpl->HandleExtensionDisconnected(connectionData);
70 #ifdef WITH_DLP
71     AbilityRuntime::DlpStateData dlpStateData;
72     connectionObserverClientImpl->HandleDlpAbilityOpened(dlpStateData);
73     connectionObserverClientImpl->HandleDlpAbilityClosed(dlpStateData);
74 #endif // WITH_DLP
75     sptr<IRemoteObject> remoteObj;
76     auto serviceProxyAdapter = std::make_shared<ServiceProxyAdapter>(remoteObj);
77     connectionObserverClientImpl->UnregisterFromServiceLocked(serviceProxyAdapter);
78     connectionObserverClientImpl->RemoveObserversLocked(observer);
79     wptr<IRemoteObject> remote;
80     connectionObserverClientImpl->HandleRemoteDied(remote);
81     connectionObserverClientImpl->ResetProxy(remote);
82     connectionObserverClientImpl->ResetStatus();
83     connectionObserverClientImpl->NotifyServiceDiedToObservers();
84     connectionObserverClientImpl->GetObservers();
85     return true;
86 }
87 }
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92     /* Run your code on data */
93     FuzzedDataProvider fdp(data, size);
94     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
95     return 0;
96 }
97 
98