/src/libphonenumber/cpp/test/phonenumbers/fuzz_shortnumberinfo.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2025 Google Inc. |
2 | | |
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 | | #include "phonenumbers/phonenumbermatcher.h" |
16 | | #include <string> |
17 | | #include <vector> |
18 | | #include <limits> |
19 | | #include <unicode/unistr.h> |
20 | | |
21 | | #include "phonenumbers/base/basictypes.h" |
22 | | #include "phonenumbers/base/memory/scoped_ptr.h" |
23 | | #include "phonenumbers/base/memory/singleton.h" |
24 | | #include "phonenumbers/default_logger.h" |
25 | | #include "phonenumbers/phonenumber.h" |
26 | | #include "phonenumbers/phonenumbermatch.h" |
27 | | #include "phonenumbers/phonenumberutil.h" |
28 | | #include "phonenumbers/stringutil.h" |
29 | | #include "phonenumbers/asyoutypeformatter.h" |
30 | | #include "phonenumbers/shortnumberinfo.h" |
31 | | #include <fuzzer/FuzzedDataProvider.h> |
32 | | |
33 | | // returns a short number cost based on the data we got from libfuzzer |
34 | | i18n::phonenumbers::ShortNumberInfo::ShortNumberCost ConsumeShortNumberCost( |
35 | 3.11k | FuzzedDataProvider& fuzzed_data) { |
36 | 3.11k | switch (fuzzed_data.ConsumeIntegralInRange(0, 4)) { |
37 | 3.05k | case 0: return i18n::phonenumbers::ShortNumberInfo::TOLL_FREE; |
38 | 6 | case 1: return i18n::phonenumbers::ShortNumberInfo::STANDARD_RATE; |
39 | 22 | case 2: return i18n::phonenumbers::ShortNumberInfo::PREMIUM_RATE; |
40 | 24 | default: return i18n::phonenumbers::ShortNumberInfo::UNKNOWN_COST; |
41 | 3.11k | } |
42 | 3.11k | } |
43 | | |
44 | 3.11k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
45 | | // setup the data provider and util |
46 | 3.11k | FuzzedDataProvider fuzzed_data(data, size); |
47 | 3.11k | i18n::phonenumbers::PhoneNumberUtil* phone_util = |
48 | 3.11k | i18n::phonenumbers::PhoneNumberUtil::GetInstance(); |
49 | | |
50 | | // setup all the data we need to pass to the target methods |
51 | 3.11k | i18n::phonenumbers::PhoneNumber phone_number; |
52 | 3.11k | std::string number = fuzzed_data.ConsumeRandomLengthString(32); |
53 | 3.11k | bool region_is_2_bytes = fuzzed_data.ConsumeBool(); |
54 | 3.11k | std::string region = fuzzed_data.ConsumeBytesAsString(region_is_2_bytes ? 2 : 3); |
55 | 3.11k | if (fuzzed_data.ConsumeBool()) { |
56 | 52 | phone_util->ParseAndKeepRawInput(number, region, &phone_number); |
57 | 3.05k | } else { |
58 | 3.05k | phone_util->Parse(number, region, &phone_number); |
59 | 3.05k | } |
60 | | |
61 | | // fuzz the public methods |
62 | 3.11k | i18n::phonenumbers::ShortNumberInfo short_info; |
63 | 3.11k | short_info.IsPossibleShortNumberForRegion(phone_number, region); |
64 | 3.11k | short_info.IsPossibleShortNumber(phone_number); |
65 | 3.11k | short_info.IsValidShortNumber(phone_number); |
66 | 3.11k | short_info.GetExpectedCostForRegion(phone_number, region); |
67 | 3.11k | short_info.GetExpectedCost(phone_number); |
68 | 3.11k | short_info.GetExampleShortNumber(region); |
69 | 3.11k | i18n::phonenumbers::ShortNumberInfo::ShortNumberCost cost = |
70 | 3.11k | ConsumeShortNumberCost(fuzzed_data); |
71 | 3.11k | short_info.GetExampleShortNumberForCost(region, cost); |
72 | 3.11k | short_info.ConnectsToEmergencyNumber(number, region); |
73 | 3.11k | short_info.IsEmergencyNumber(number, region); |
74 | 3.11k | short_info.IsCarrierSpecific(phone_number); |
75 | 3.11k | short_info.IsCarrierSpecificForRegion(phone_number, region); |
76 | 3.11k | short_info.IsSmsServiceForRegion(phone_number, region); |
77 | | |
78 | 3.11k | return 0; |
79 | 3.11k | } |