1 /*
2 * Copyright (c) 2023 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 "smspdubuffer_fuzzer.h"
17
18 #include "addsmstoken_fuzzer.h"
19 #include "sms_pdu_buffer.h"
20
21 using namespace OHOS::Telephony;
22 namespace OHOS {
23 static constexpr uint8_t TEST_BITS = 16;
24
ReadBuffer(const uint8_t * data,size_t size)25 void ReadBuffer(const uint8_t *data, size_t size)
26 {
27 std::string strValue(reinterpret_cast<const char *>(data), size);
28 auto buffer = std::make_unique<SmsReadBuffer>(strValue);
29 if (buffer == nullptr) {
30 return;
31 }
32 buffer->IsEmpty();
33
34 uint16_t desLen = static_cast<uint16_t>(size);
35 desLen += desLen;
36 for (uint16_t i = 0; i < desLen; i++) {
37 buffer->SetIndex(i);
38 buffer->GetIndex();
39 }
40 for (uint16_t i = 0; i < desLen; i++) {
41 buffer->MoveBack(i);
42 }
43 for (uint16_t i = 0; i < desLen; i++) {
44 buffer->MoveForward(i);
45 }
46
47 buffer->SetIndex(0);
48 uint8_t v1 = 0;
49 uint16_t v2 = 0;
50 for (uint16_t i = 0; i < desLen; i++) {
51 buffer->ReadByte(v1);
52 buffer->ReadWord(v2);
53 }
54
55 buffer->SetIndex(0);
56 uint8_t v3 = 0;
57 for (uint16_t i = 0; i < desLen; i++) {
58 for (uint8_t j = 0; j < TEST_BITS; j++) {
59 buffer->ReadBits(v3, j);
60 }
61 }
62 buffer->SkipBits();
63 }
64
WriteBuffer(const uint8_t * data,size_t size)65 void WriteBuffer(const uint8_t *data, size_t size)
66 {
67 auto buffer = std::make_unique<SmsWriteBuffer>();
68 if (buffer == nullptr) {
69 return;
70 }
71 buffer->IsEmpty();
72
73 for (uint16_t i = 0; i < size; i++) {
74 buffer->SetIndex(i);
75 buffer->GetIndex();
76 }
77 for (uint16_t i = 0; i < size; i++) {
78 buffer->MoveBack(i);
79 }
80 for (uint16_t i = 0; i < size; i++) {
81 buffer->MoveForward(i);
82 }
83
84 buffer->SetIndex(0);
85 for (uint16_t i = 0; i < size; i++) {
86 buffer->WriteByte(data[i]);
87 }
88 auto pduBuffer = buffer->GetPduBuffer();
89
90 buffer->SetIndex(0);
91 for (uint16_t i = 0; i < size; i++) {
92 buffer->WriteWord(static_cast<uint16_t>(data[i]));
93 }
94 auto pduBuffer2 = buffer->GetPduBuffer();
95
96 buffer->SetIndex(0);
97 uint8_t v3 = 0;
98 for (uint16_t i = 0; i < size; i++) {
99 for (uint8_t j = 0; j < TEST_BITS; j++) {
100 buffer->WriteBits(v3, j);
101 }
102 }
103 buffer->SkipBits();
104 auto pduBuffer3 = buffer->GetPduBuffer();
105
106 buffer->SetIndex(0);
107 for (uint16_t i = 0; i < size; i++) {
108 buffer->InsertByte(data[i], i);
109 }
110 auto pduBuffer4 = buffer->GetPduBuffer();
111 }
112
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)113 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
114 {
115 if (data == nullptr || size == 0) {
116 return;
117 }
118
119 ReadBuffer(data, size);
120 WriteBuffer(data, size);
121 }
122
123 } // namespace OHOS
124
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127 {
128 /* Run your code on data */
129 OHOS::AddSmsTokenFuzzer token;
130 OHOS::DoSomethingInterestingWithMyAPI(data, size);
131 return 0;
132 }
133