Coverage for /pythoncovmergedfiles/medio/medio/src/python_phonenumbers_fuzzer.py: 44%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

146 statements  

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 

13 

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 """ 

28 

29import sys 

30import struct 

31import atheris 

32import phonenumbers 

33from phonenumbers import Leniency, NumberParseException 

34 

35def helper_parse(inp): 

36 """ Parsing phonenumbers """ 

37 try: 

38 return phonenumbers.parse(inp, None) 

39 except NumberParseException: 

40 return None 

41 

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) 

47 

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) 

53 

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) 

59 

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) 

65 

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) 

71 

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) 

77 

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) 

83 

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) 

89 

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) 

95 

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) 

101 

102def test_connects_to_emergency_number(inp): 

103 """ Testing phonenumbers connects_to_emergency_number method """ 

104 try: 

105 phonenumbers.connects_to_emergency_number(inp, "MA") 

106 except ModuleNotFoundError: 

107 return 

108 

109def test_is_emergency_number(inp): 

110 """ Testing phonenumbers is_emergency_number method """ 

111 try: 

112 phonenumbers.is_emergency_number(inp, "MA") 

113 except ModuleNotFoundError: 

114 return 

115 

116def test_is_carrier_specific(inp): 

117 """ Testing phonenumbers is_carrier_specific method """ 

118 phone_number = helper_parse(inp) 

119 if phone_number is not None: 

120 phonenumbers.is_carrier_specific(phone_number) 

121 

122def helper_matcher(inp, leniency): 

123 """ Helper for phonenumbers PhoneNumberMatcher method """ 

124 matcher = phonenumbers.PhoneNumberMatcher(inp, "US", leniency=leniency) 

125 if matcher.has_next(): 

126 matcher.next() 

127 

128def test_matcher_possible(inp): 

129 """ Testing phonenumbers PhoneNumberMatcher method, leniency: possible """ 

130 helper_matcher(inp, Leniency.VALID) 

131 

132def test_matcher_valid(inp): 

133 """ Testing phonenumbers PhoneNumberMatcher method, leniency: valid """ 

134 helper_matcher(inp, Leniency.VALID) 

135 

136def test_matcher_strict_grouping(inp): 

137 """ Testing phonenumbers PhoneNumberMatcher method, leniency: strict grouping """ 

138 helper_matcher(inp, Leniency.STRICT_GROUPING) 

139 

140def test_matcher_exact_grouping(inp): 

141 """ Testing phonenumbers PhoneNumberMatcher method, leniency: exact grouping """ 

142 helper_matcher(inp, Leniency.EXACT_GROUPING) 

143 

144def test_input_digit(inp): 

145 """ Testing phonenumbers AsYouTypeFormatter / input_digit method """ 

146 try: 

147 formatter = phonenumbers.AsYouTypeFormatter("US") 

148 formatter.input_digit(inp) 

149 except TypeError: 

150 return 

151 

152def test_input_digit_per_digit(inp): 

153 """ Testing phonenumbers AsYouTypeFormatter / input_digit method, digit by digit input """ 

154 try: 

155 formatter = phonenumbers.AsYouTypeFormatter("US") 

156 for digit in inp: 

157 formatter.input_digit(digit) 

158 formatter.get_remembered_position() 

159 except TypeError: 

160 return 

161 except UnicodeEncodeError: 

162 return 

163 

164def test_is_mobile_number_portable_region(inp): 

165 """ Testing phonenumbers is_mobile_number_portable_region method """ 

166 phonenumbers.is_mobile_number_portable_region(inp) 

167 

168def test_convert_alpha_characters_in_number(inp): 

169 """ Testing phonenumbers convert_alpha_characters_in_number method """ 

170 phonenumbers.convert_alpha_characters_in_number(inp) 

171 

172def test_normalize_digits_only(inp): 

173 """ Testing phonenumbers normalize_digits_only method """ 

174 phonenumbers.normalize_digits_only(inp) 

175 

176def test_normalize_diallable_chars_only(inp): 

177 """ Testing phonenumbers normalize_diallable_chars_only method """ 

178 phonenumbers.normalize_diallable_chars_only(inp) 

179 

180def test_country_mobile_token(inp): 

181 """ Testing phonenumbers country_mobile_token method """ 

182 phonenumbers.country_mobile_token(inp) 

183 

184def test_supported_types_for_region(inp): 

185 """ Testing phonenumbers supported_types_for_region method """ 

186 phonenumbers.supported_types_for_region(inp) 

187 

188def test_supported_types_for_non_geo_entity(inp): 

189 """ Testing phonenumbers supported_types_for_non_geo_entity method """ 

190 phonenumbers.supported_types_for_non_geo_entity(inp) 

191 

192LONGSTR = 1 

193MEDIUMSTR = 2 

194SHORTSTR = 3 

195SSHORTSTR = 4 

196 

197TESTS = [ 

198 (test_is_possible_number, str), 

199 (test_is_valid_number, str), 

200 (test_can_be_internationally_dialled, str), 

201 (test_length_of_geographical_area_code, str), 

202 (test_length_of_national_destination_code, str), 

203 (test_is_number_geographical, str), 

204 (test_national_significant_number, str), 

205 (test_is_possible_short_number, str), 

206 (test_is_valid_short_number, str), 

207 (test_expected_cost, str), 

208 (test_connects_to_emergency_number, LONGSTR), 

209 (test_is_emergency_number, LONGSTR), 

210 (test_is_carrier_specific, str), 

211 (test_matcher_possible, SHORTSTR), 

212 (test_matcher_valid, SHORTSTR), 

213 (test_matcher_strict_grouping, SHORTSTR), 

214 (test_matcher_exact_grouping, SHORTSTR), 

215 (test_input_digit, str), 

216 (test_input_digit_per_digit, SSHORTSTR), 

217 (test_is_mobile_number_portable_region, str), 

218 (test_convert_alpha_characters_in_number, LONGSTR), 

219 (test_normalize_digits_only, LONGSTR), 

220 (test_normalize_diallable_chars_only, LONGSTR), 

221 (test_country_mobile_token, str), 

222 (test_supported_types_for_region, str), 

223 (test_supported_types_for_non_geo_entity, str), 

224] 

225 

226def get_input(input_bytes, idx): 

227 """ Get input of the right type/size """ 

228 fdp = atheris.FuzzedDataProvider(input_bytes) 

229 if TESTS[idx][1] == str: 

230 return fdp.ConsumeUnicode(sys.maxsize) 

231 if TESTS[idx][1] == LONGSTR: 

232 return fdp.ConsumeUnicode(100000) 

233 if TESTS[idx][1] == MEDIUMSTR: 

234 return fdp.ConsumeUnicode(10000) 

235 if TESTS[idx][1] == SHORTSTR: 

236 return fdp.ConsumeUnicode(1000) 

237 if TESTS[idx][1] == SSHORTSTR: 

238 return fdp.ConsumeUnicode(100) 

239 return None 

240 

241def test_one_input(input_bytes): 

242 """ Fuzzer's entry point """ 

243 if len(input_bytes) < 1: 

244 return 

245 idx = struct.unpack('>B', input_bytes[:1])[0] 

246 if idx >= len(TESTS): 

247 return 

248 TESTS[idx][0](get_input(input_bytes[1:], idx)) 

249 

250def main(): 

251 """ main function """ 

252 atheris.Setup(sys.argv, test_one_input, enable_python_coverage=False) 

253 atheris.Fuzz() 

254 

255 

256if __name__ == "__main__": 

257 atheris.instrument_all() 

258 main()