• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "updateactivemachine_fuzzer.h"
17 
18 #define private public
19 #define protected public
20 
21 #include "active.h"
22 #include "adddatatoken_fuzzer.h"
23 #include "statemachine_fuzzer.h"
24 
25 namespace OHOS {
26 using namespace OHOS::Telephony;
27 using namespace AppExecFwk;
28 using namespace OHOS::EventFwk;
29 static int32_t SIM_COUNT = 2;
30 bool g_flag = false;
31 
UpdateActiveMachineFuzz(const uint8_t * data,size_t size)32 void UpdateActiveMachineFuzz(const uint8_t *data, size_t size)
33 {
34     std::shared_ptr<StateMachineFuzzer> machine = std::make_shared<StateMachineFuzzer>();
35     if (machine == nullptr) {
36         return;
37     }
38     int32_t slotId = static_cast<int32_t>(size % SIM_COUNT);
39     std::shared_ptr<CellularDataStateMachine> cellularMachine = machine->CreateCellularDataConnect(slotId);
40     if (cellularMachine == nullptr) {
41         return;
42     }
43     sptr<Active> active =
44         std::make_unique<Active>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Active").release();
45     if (active == nullptr) {
46         return;
47     }
48     cellularMachine->Init();
49 
50     std::int32_t intValue = static_cast<int32_t>(size);
51     std::unique_ptr<uint8_t> object = std::make_unique<uint8_t>(*data);
52     if (object == nullptr) {
53         return;
54     }
55 
56     InnerEvent::Pointer event = InnerEvent::Get(intValue, object);
57     active->ProcessConnectDone(event);
58     active->ProcessDisconnectDone(event);
59     active->ProcessLostConnection(event);
60     active->ProcessDataConnectionRoamOn(event);
61     active->ProcessDataConnectionRoamOff(event);
62     active->ProcessDataConnectionVoiceCallStartedOrEnded(event);
63     active->ProcessDataConnectionComplete(event);
64     active->RefreshTcpBufferSizes();
65     active->RefreshConnectionBandwidths();
66 
67     if (machine->stateMachineEventLoop_ != nullptr) {
68         machine->stateMachineEventLoop_->Stop();
69     }
70 }
71 
UpdateActiveMachineWithMyAPI(const uint8_t * data,size_t size)72 void UpdateActiveMachineWithMyAPI(const uint8_t *data, size_t size)
73 {
74     if (data == nullptr || size == 0) {
75         return;
76     }
77 
78     if (!g_flag) {
79         UpdateActiveMachineFuzz(data, size);
80         g_flag = true;
81     }
82 }
83 } // namespace OHOS
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
87 {
88     OHOS::AddDataTokenFuzzer token;
89     /* Run your code on data */
90     OHOS::UpdateActiveMachineWithMyAPI(data, size);
91     return 0;
92 }