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