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 <cstddef>
17 #include <cstdint>
18 #include "locale_config.h"
19 #include "phone_number_matched.h"
20 #include "regex_rule.h"
21 #include "regexrule_fuzzer.h"
22
23 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)24 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
25 {
26 using namespace Global::I18n;
27 using namespace i18n::phonenumbers;
28 if (size < 1) {
29 return false;
30 }
31 std::string input(reinterpret_cast<const char*>(data), size);
32 icu::UnicodeString regex(input.c_str());
33 RegexRule* regexRule = new RegexRule(regex, input, input, input, input);
34
35 regexRule->GetType();
36 icu::RegexPattern* pattern = regexRule->GetPattern();
37 i18n::phonenumbers::PhoneNumber phoneNumber;
38 PhoneNumberUtil* phoneNumberUtil = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
39 std::string country = LocaleConfig::GetSystemRegion();
40 phoneNumberUtil->Parse(input, country, &phoneNumber);
41
42 PhoneNumberMatch* possibleNumber = new PhoneNumberMatch(size, input, phoneNumber);
43 regexRule->Handle(possibleNumber, regex);
44 regexRule->IsValid(possibleNumber, regex);
45 regexRule->CountDigits(regex);
46 delete possibleNumber;
47 delete pattern;
48 delete regexRule;
49 return true;
50 }
51 }
52
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56 /* Run your code on data */
57 OHOS::DoSomethingInterestingWithMyAPI(data, size);
58 return 0;
59 }
60
61