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 "datetimeformatadapterimpl_fuzzer.h"
17
18 #define private public
19 #include "common_event_data.h"
20 #include "common_event_manager.h"
21 #include "common_event_subscriber.h"
22 #include "date_time_format_adapter_impl.h"
23 #include "time_service_client.h"
24
25 using namespace OHOS::NWeb;
26
27 namespace OHOS {
28 namespace {
29 bool g_commonEvent = false;
30 bool g_unCommonEvent = false;
31 using Want = OHOS::AAFwk::Want;
32 } // namespace
33 namespace EventFwk {
SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)34 bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber>& subscriber)
35 {
36 return g_commonEvent;
37 }
38
UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)39 bool CommonEventManager::UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber>& subscriber)
40 {
41 return g_unCommonEvent;
42 }
43 } // namespace EventFwk
44 namespace NWeb {
45 class MockTimezoneEventCallbackAdapter : public TimezoneEventCallbackAdapter {
46 public:
47 MockTimezoneEventCallbackAdapter() = default;
TimezoneChanged(std::shared_ptr<WebTimezoneInfo> info)48 void TimezoneChanged(std::shared_ptr<WebTimezoneInfo> info) {}
49 };
50
DateTimeFormatAdapterFuzzTest(const uint8_t * data,size_t size)51 bool DateTimeFormatAdapterFuzzTest(const uint8_t* data, size_t size)
52 {
53 EventFwk::CommonEventSubscribeInfo in;
54 std::shared_ptr<TimezoneEventCallbackAdapter> cb = std::make_shared<MockTimezoneEventCallbackAdapter>();
55 auto adapter = std::make_shared<NWebTimeZoneEventSubscriber>(in, cb);
56 Want want;
57 want.SetAction("test");
58 EventFwk::CommonEventData newdata(want);
59 adapter->OnReceiveEvent(newdata);
60 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
61 newdata.SetWant(want);
62 adapter->OnReceiveEvent(newdata);
63 adapter->eventCallback_ = nullptr;
64 adapter->OnReceiveEvent(newdata);
65
66 auto newadapter = std::make_shared<DateTimeFormatAdapterImpl>();
67 std::shared_ptr<TimezoneEventCallbackAdapter> newcb = std::make_shared<MockTimezoneEventCallbackAdapter>();
68 newadapter->RegTimezoneEvent(std::move(newcb));
69 bool newresult = newadapter->StartListen();
70 g_commonEvent = true;
71 newresult = newadapter->StartListen();
72 newadapter->StopListen();
73 g_unCommonEvent = true;
74 newadapter->StopListen();
75 newadapter->StopListen();
76 std::string timeZoneNicosia("");
77 OHOS::MiscServices::TimeServiceClient::GetInstance()->SetTimeZone(timeZoneNicosia);
78 auto timeStr = newadapter->GetTimezone();
79 return true;
80 }
81 } // namespace NWeb
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 OHOS::NWeb::DateTimeFormatAdapterFuzzTest(data, size);
87 return 0;
88 }
89 } // namespace OHOS