Coverage for /pythoncovmergedfiles/medio/medio/src/python_phonenumbers_fuzzer.py: 59%
139 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:30 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:30 +0000
1###### Coverage stub
2import atexit
3import coverage
4cov = coverage.coverage(data_file='.coverage', cover_pylib=True)
5cov.start()
6# Register an exist handler that will print coverage
7def exit_handler():
8 cov.stop()
9 cov.save()
10atexit.register(exit_handler)
11####### End of coverage stub
12#!/usr/bin/python3
14# Copyright 2020 Google LLC
15#
16# Licensed under the Apache License, Version 2.0 (the "License");
17# you may not use this file except in compliance with the License.
18# You may obtain a copy of the License at
19#
20# http://www.apache.org/licenses/LICENSE-2.0
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS,
24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25# See the License for the specific language governing permissions and
26# limitations under the License.
27""" Harnass for fuzzing https://github.com/daviddrysdale/python-phonenumbers.git """
29import sys
30import struct
31import atheris
32import phonenumbers
33from phonenumbers import Leniency, NumberParseException
35def helper_parse(inp):
36 """ Parsing phonenumbers """
37 try:
38 return phonenumbers.parse(inp, None)
39 except NumberParseException:
40 return None
42def test_is_possible_number(inp):
43 """ Testing phonenumbers is_possible_number method """
44 phone_number = helper_parse(inp)
45 if phone_number is not None:
46 phonenumbers.is_possible_number(phone_number)
48def test_is_valid_number(inp):
49 """ Testing phonenumbers is_valid_number method """
50 phone_number = helper_parse(inp)
51 if phone_number is not None:
52 phonenumbers.is_valid_number(phone_number)
54def test_can_be_internationally_dialled(inp):
55 """ Testing phonenumbers can_be_internationally_dialled method """
56 phone_number = helper_parse(inp)
57 if phone_number is not None:
58 phonenumbers.can_be_internationally_dialled(phone_number)
60def test_length_of_geographical_area_code(inp):
61 """ Testing phonenumbers length_of_geographical_area_code method """
62 phone_number = helper_parse(inp)
63 if phone_number is not None:
64 phonenumbers.length_of_geographical_area_code(phone_number)
66def test_length_of_national_destination_code(inp):
67 """ Testing phonenumbers length_of_national_destination_code method """
68 phone_number = helper_parse(inp)
69 if phone_number is not None:
70 phonenumbers.length_of_national_destination_code(phone_number)
72def test_is_number_geographical(inp):
73 """ Testing phonenumbers is_number_geographical method """
74 phone_number = helper_parse(inp)
75 if phone_number is not None:
76 phonenumbers.is_number_geographical(phone_number)
78def test_national_significant_number(inp):
79 """ Testing phonenumbers national_significant_number method """
80 phone_number = helper_parse(inp)
81 if phone_number is not None:
82 phonenumbers.national_significant_number(phone_number)
84def test_is_possible_short_number(inp):
85 """ Testing phonenumbers is_possible_short_number method """
86 phone_number = helper_parse(inp)
87 if phone_number is not None:
88 phonenumbers.is_possible_short_number(phone_number)
90def test_is_valid_short_number(inp):
91 """ Testing phonenumbers is_valid_short_number method """
92 phone_number = helper_parse(inp)
93 if phone_number is not None:
94 phonenumbers.is_valid_short_number(phone_number)
96def test_expected_cost(inp):
97 """ Testing phonenumbers expected_cost method """
98 phone_number = helper_parse(inp)
99 if phone_number is not None:
100 phonenumbers.expected_cost(phone_number)
102def test_connects_to_emergency_number(inp):
103 """ Testing phonenumbers connects_to_emergency_number method """
104 phonenumbers.connects_to_emergency_number(inp, "MA")
106def test_is_emergency_number(inp):
107 """ Testing phonenumbers is_emergency_number method """
108 phonenumbers.is_emergency_number(inp, "MA")
110def test_is_carrier_specific(inp):
111 """ Testing phonenumbers is_carrier_specific method """
112 phone_number = helper_parse(inp)
113 if phone_number is not None:
114 phonenumbers.is_carrier_specific(phone_number)
116def helper_matcher(inp, leniency):
117 """ Helper for phonenumbers PhoneNumberMatcher method """
118 matcher = phonenumbers.PhoneNumberMatcher(inp, "US", leniency=leniency)
119 if matcher.has_next():
120 matcher.next()
122def test_matcher_possible(inp):
123 """ Testing phonenumbers PhoneNumberMatcher method, leniency: possible """
124 helper_matcher(inp, Leniency.VALID)
126def test_matcher_valid(inp):
127 """ Testing phonenumbers PhoneNumberMatcher method, leniency: valid """
128 helper_matcher(inp, Leniency.VALID)
130def test_matcher_strict_grouping(inp):
131 """ Testing phonenumbers PhoneNumberMatcher method, leniency: strict grouping """
132 helper_matcher(inp, Leniency.STRICT_GROUPING)
134def test_matcher_exact_grouping(inp):
135 """ Testing phonenumbers PhoneNumberMatcher method, leniency: exact grouping """
136 helper_matcher(inp, Leniency.EXACT_GROUPING)
138def test_input_digit(inp):
139 """ Testing phonenumbers AsYouTypeFormatter / input_digit method """
140 try:
141 formatter = phonenumbers.AsYouTypeFormatter("US")
142 formatter.input_digit(inp)
143 except TypeError:
144 return
146def test_input_digit_per_digit(inp):
147 """ Testing phonenumbers AsYouTypeFormatter / input_digit method, digit by digit input """
148 try:
149 formatter = phonenumbers.AsYouTypeFormatter("US")
150 for digit in inp:
151 formatter.input_digit(digit)
152 formatter.get_remembered_position()
153 except TypeError:
154 return
155 except UnicodeEncodeError:
156 return
158def test_is_mobile_number_portable_region(inp):
159 """ Testing phonenumbers is_mobile_number_portable_region method """
160 phonenumbers.is_mobile_number_portable_region(inp)
162def test_convert_alpha_characters_in_number(inp):
163 """ Testing phonenumbers convert_alpha_characters_in_number method """
164 phonenumbers.convert_alpha_characters_in_number(inp)
166def test_normalize_digits_only(inp):
167 """ Testing phonenumbers normalize_digits_only method """
168 phonenumbers.normalize_digits_only(inp)
170def test_normalize_diallable_chars_only(inp):
171 """ Testing phonenumbers normalize_diallable_chars_only method """
172 phonenumbers.normalize_diallable_chars_only(inp)
174def test_country_mobile_token(inp):
175 """ Testing phonenumbers country_mobile_token method """
176 phonenumbers.country_mobile_token(inp)
178def test_supported_types_for_region(inp):
179 """ Testing phonenumbers supported_types_for_region method """
180 phonenumbers.supported_types_for_region(inp)
182def test_supported_types_for_non_geo_entity(inp):
183 """ Testing phonenumbers supported_types_for_non_geo_entity method """
184 phonenumbers.supported_types_for_non_geo_entity(inp)
186LONGSTR = 1
187MEDIUMSTR = 2
188SHORTSTR = 3
189SSHORTSTR = 4
191TESTS = [
192 (test_is_possible_number, str),
193 (test_is_valid_number, str),
194 (test_can_be_internationally_dialled, str),
195 (test_length_of_geographical_area_code, str),
196 (test_length_of_national_destination_code, str),
197 (test_is_number_geographical, str),
198 (test_national_significant_number, str),
199 (test_is_possible_short_number, str),
200 (test_is_valid_short_number, str),
201 (test_expected_cost, str),
202 (test_connects_to_emergency_number, LONGSTR),
203 (test_is_emergency_number, LONGSTR),
204 (test_is_carrier_specific, str),
205 (test_matcher_possible, SHORTSTR),
206 (test_matcher_valid, SHORTSTR),
207 (test_matcher_strict_grouping, SHORTSTR),
208 (test_matcher_exact_grouping, SHORTSTR),
209 (test_input_digit, str),
210 (test_input_digit_per_digit, SSHORTSTR),
211 (test_is_mobile_number_portable_region, str),
212 (test_convert_alpha_characters_in_number, LONGSTR),
213 (test_normalize_digits_only, LONGSTR),
214 (test_normalize_diallable_chars_only, LONGSTR),
215 (test_country_mobile_token, str),
216 (test_supported_types_for_region, str),
217 (test_supported_types_for_non_geo_entity, str),
218]
220def get_input(input_bytes, idx):
221 """ Get input of the right type/size """
222 fdp = atheris.FuzzedDataProvider(input_bytes)
223 if TESTS[idx][1] == str:
224 return fdp.ConsumeUnicode(sys.maxsize)
225 if TESTS[idx][1] == LONGSTR:
226 return fdp.ConsumeUnicode(100000)
227 if TESTS[idx][1] == MEDIUMSTR:
228 return fdp.ConsumeUnicode(10000)
229 if TESTS[idx][1] == SHORTSTR:
230 return fdp.ConsumeUnicode(1000)
231 if TESTS[idx][1] == SSHORTSTR:
232 return fdp.ConsumeUnicode(100)
233 return None
235def test_one_input(input_bytes):
236 """ Fuzzer's entry point """
237 if len(input_bytes) < 1:
238 return
239 idx = struct.unpack('>B', input_bytes[:1])[0]
240 if idx >= len(TESTS):
241 return
242 TESTS[idx][0](get_input(input_bytes[1:], idx))
244def main():
245 """ main function """
246 atheris.Setup(sys.argv, test_one_input, enable_python_coverage=False)
247 atheris.Fuzz()
250if __name__ == "__main__":
251 atheris.instrument_all()
252 main()