1 // Copyright (C) 2009 The Libphonenumber Authors
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 // Author: David Yonge-Mallo
16
17 // Note that these tests use the test metadata, not the normal metadata file, so
18 // should not be used for regression test purposes - these tests are
19 // illustrative only and test functionality.
20
21 #include "phonenumbers/shortnumberinfo.h"
22
23 #include <gtest/gtest.h>
24
25 #include "phonenumbers/base/logging.h"
26 #include "phonenumbers/default_logger.h"
27 #include "phonenumbers/phonenumberutil.h"
28 #include "phonenumbers/stringutil.h"
29 #include "phonenumbers/test_util.h"
30
31 namespace i18n {
32 namespace phonenumbers {
33 class ShortNumberInfoTest : public testing::Test {
34 protected:
ParseNumberForTesting(const string & number,const string & region_code)35 PhoneNumber ParseNumberForTesting(const string& number,
36 const string& region_code) {
37 PhoneNumber phone_number;
38 PhoneNumberUtil::ErrorType error_type = phone_util_.Parse(
39 number, region_code, &phone_number);
40 CHECK_EQ(error_type, PhoneNumberUtil::NO_PARSING_ERROR);
41 IGNORE_UNUSED(error_type);
42 return phone_number;
43 }
44
ShortNumberInfoTest()45 ShortNumberInfoTest() : short_info_() {
46 PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
47 }
48
49 const PhoneNumberUtil phone_util_;
50 const ShortNumberInfo short_info_;
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(ShortNumberInfoTest);
54 };
55
TEST_F(ShortNumberInfoTest,IsPossibleShortNumber)56 TEST_F(ShortNumberInfoTest, IsPossibleShortNumber) {
57 PhoneNumber possible_number;
58 possible_number.set_country_code(33);
59 possible_number.set_national_number(uint64{123456});
60 EXPECT_TRUE(short_info_.IsPossibleShortNumber(possible_number));
61 EXPECT_TRUE(short_info_.IsPossibleShortNumberForRegion(
62 ParseNumberForTesting("123456", RegionCode::FR()), RegionCode::FR()));
63
64 PhoneNumber impossible_number;
65 impossible_number.set_country_code(33);
66 impossible_number.set_national_number(uint64{9});
67 EXPECT_FALSE(short_info_.IsPossibleShortNumber(impossible_number));
68
69 // Note that GB and GG share the country calling code 44, and that this
70 // number is possible but not valid.
71 PhoneNumber shared_number;
72 shared_number.set_country_code(44);
73 shared_number.set_national_number(uint64{11001});
74 EXPECT_TRUE(short_info_.IsPossibleShortNumber(shared_number));
75 }
76
TEST_F(ShortNumberInfoTest,IsValidShortNumber)77 TEST_F(ShortNumberInfoTest, IsValidShortNumber) {
78 PhoneNumber valid_number;
79 valid_number.set_country_code(33);
80 valid_number.set_national_number(uint64{1010});
81 EXPECT_TRUE(short_info_.IsValidShortNumber(valid_number));
82 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
83 ParseNumberForTesting("1010", RegionCode::FR()), RegionCode::FR()));
84
85 PhoneNumber invalid_number;
86 invalid_number.set_country_code(33);
87 invalid_number.set_national_number(uint64{123456});
88 EXPECT_FALSE(short_info_.IsValidShortNumber(invalid_number));
89 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
90 ParseNumberForTesting("123456", RegionCode::FR()), RegionCode::FR()));
91
92 // Note that GB and GG share the country calling code 44.
93 PhoneNumber shared_number;
94 shared_number.set_country_code(44);
95 shared_number.set_national_number(uint64{18001});
96 EXPECT_TRUE(short_info_.IsValidShortNumber(shared_number));
97 }
98
TEST_F(ShortNumberInfoTest,IsCarrierSpecific)99 TEST_F(ShortNumberInfoTest, IsCarrierSpecific) {
100 PhoneNumber carrier_specific_number;
101 carrier_specific_number.set_country_code(1);
102 carrier_specific_number.set_national_number(uint64{33669});
103 EXPECT_TRUE(short_info_.IsCarrierSpecific(carrier_specific_number));
104 EXPECT_TRUE(short_info_.IsCarrierSpecificForRegion(
105 ParseNumberForTesting("33669", RegionCode::US()), RegionCode::US()));
106
107 PhoneNumber not_carrier_specific_number;
108 not_carrier_specific_number.set_country_code(1);
109 not_carrier_specific_number.set_national_number(uint64{911});
110 EXPECT_FALSE(short_info_.IsCarrierSpecific(not_carrier_specific_number));
111 EXPECT_FALSE(short_info_.IsCarrierSpecificForRegion(
112 ParseNumberForTesting("911", RegionCode::US()), RegionCode::US()));
113
114 PhoneNumber carrier_specific_number_for_some_region;
115 carrier_specific_number_for_some_region.set_country_code(1);
116 carrier_specific_number_for_some_region.set_national_number(uint64{211});
117 EXPECT_TRUE(short_info_.IsCarrierSpecific(
118 carrier_specific_number_for_some_region));
119 EXPECT_TRUE(short_info_.IsCarrierSpecificForRegion(
120 carrier_specific_number_for_some_region, RegionCode::US()));
121 EXPECT_FALSE(short_info_.IsCarrierSpecificForRegion(
122 carrier_specific_number_for_some_region, RegionCode::BB()));
123 }
124
TEST_F(ShortNumberInfoTest,IsSmsService)125 TEST_F(ShortNumberInfoTest, IsSmsService) {
126 PhoneNumber sms_service_number_for_some_region;
127 sms_service_number_for_some_region.set_country_code(1);
128 sms_service_number_for_some_region.set_national_number(uint64{21234});
129 EXPECT_TRUE(short_info_.IsSmsServiceForRegion(
130 sms_service_number_for_some_region, RegionCode::US()));
131 EXPECT_FALSE(short_info_.IsSmsServiceForRegion(
132 sms_service_number_for_some_region, RegionCode::BB()));
133 }
134
TEST_F(ShortNumberInfoTest,GetExpectedCost)135 TEST_F(ShortNumberInfoTest, GetExpectedCost) {
136 uint64 national_number;
137 const string& premium_rate_example =
138 short_info_.GetExampleShortNumberForCost(
139 RegionCode::FR(), ShortNumberInfo::PREMIUM_RATE);
140 EXPECT_EQ(ShortNumberInfo::PREMIUM_RATE,
141 short_info_.GetExpectedCostForRegion(
142 ParseNumberForTesting(premium_rate_example, RegionCode::FR()),
143 RegionCode::FR()));
144 PhoneNumber premium_rate_number;
145 premium_rate_number.set_country_code(33);
146 safe_strtou64(premium_rate_example, &national_number);
147 premium_rate_number.set_national_number(national_number);
148 EXPECT_EQ(ShortNumberInfo::PREMIUM_RATE,
149 short_info_.GetExpectedCost(premium_rate_number));
150
151 const string& standard_rate_example =
152 short_info_.GetExampleShortNumberForCost(
153 RegionCode::FR(), ShortNumberInfo::STANDARD_RATE);
154 EXPECT_EQ(ShortNumberInfo::STANDARD_RATE,
155 short_info_.GetExpectedCostForRegion(
156 ParseNumberForTesting(standard_rate_example, RegionCode::FR()),
157 RegionCode::FR()));
158 PhoneNumber standard_rate_number;
159 standard_rate_number.set_country_code(33);
160 safe_strtou64(standard_rate_example, &national_number);
161 standard_rate_number.set_national_number(national_number);
162 EXPECT_EQ(ShortNumberInfo::STANDARD_RATE,
163 short_info_.GetExpectedCost(standard_rate_number));
164
165 const string& toll_free_example =
166 short_info_.GetExampleShortNumberForCost(
167 RegionCode::FR(), ShortNumberInfo::TOLL_FREE);
168 EXPECT_EQ(ShortNumberInfo::TOLL_FREE,
169 short_info_.GetExpectedCostForRegion(
170 ParseNumberForTesting(toll_free_example, RegionCode::FR()),
171 RegionCode::FR()));
172 PhoneNumber toll_free_number;
173 toll_free_number.set_country_code(33);
174 safe_strtou64(toll_free_example, &national_number);
175 toll_free_number.set_national_number(national_number);
176 EXPECT_EQ(ShortNumberInfo::TOLL_FREE,
177 short_info_.GetExpectedCost(toll_free_number));
178
179 EXPECT_EQ(
180 ShortNumberInfo::UNKNOWN_COST,
181 short_info_.GetExpectedCostForRegion(
182 ParseNumberForTesting("12345", RegionCode::FR()), RegionCode::FR()));
183 PhoneNumber unknown_cost_number;
184 unknown_cost_number.set_country_code(33);
185 unknown_cost_number.set_national_number(uint64{12345});
186 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
187 short_info_.GetExpectedCost(unknown_cost_number));
188
189 // Test that an invalid number may nevertheless have a cost other than
190 // UNKNOWN_COST.
191 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
192 ParseNumberForTesting("116123", RegionCode::FR()), RegionCode::FR()));
193 EXPECT_EQ(
194 ShortNumberInfo::TOLL_FREE,
195 short_info_.GetExpectedCostForRegion(
196 ParseNumberForTesting("116123", RegionCode::FR()), RegionCode::FR()));
197 PhoneNumber invalid_number;
198 invalid_number.set_country_code(33);
199 invalid_number.set_national_number(uint64{116123});
200 EXPECT_FALSE(short_info_.IsValidShortNumber(invalid_number));
201 EXPECT_EQ(ShortNumberInfo::TOLL_FREE,
202 short_info_.GetExpectedCost(invalid_number));
203
204 // Test a nonexistent country code.
205 EXPECT_EQ(
206 ShortNumberInfo::UNKNOWN_COST,
207 short_info_.GetExpectedCostForRegion(
208 ParseNumberForTesting("911", RegionCode::US()), RegionCode::ZZ()));
209 unknown_cost_number.Clear();
210 unknown_cost_number.set_country_code(123);
211 unknown_cost_number.set_national_number(uint64{911});
212 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
213 short_info_.GetExpectedCost(unknown_cost_number));
214 }
215
TEST_F(ShortNumberInfoTest,GetExpectedCostForSharedCountryCallingCode)216 TEST_F(ShortNumberInfoTest, GetExpectedCostForSharedCountryCallingCode) {
217 // Test some numbers which have different costs in countries sharing the same
218 // country calling code. In Australia, 1234 is premium-rate, 1194 is
219 // standard-rate, and 733 is toll-free. These are not known to be valid
220 // numbers in the Christmas Islands.
221 string ambiguous_premium_rate_string("1234");
222 PhoneNumber ambiguous_premium_rate_number;
223 ambiguous_premium_rate_number.set_country_code(61);
224 ambiguous_premium_rate_number.set_national_number(uint64{1234});
225 string ambiguous_standard_rate_string("1194");
226 PhoneNumber ambiguous_standard_rate_number;
227 ambiguous_standard_rate_number.set_country_code(61);
228 ambiguous_standard_rate_number.set_national_number(uint64{1194});
229 string ambiguous_toll_free_string("733");
230 PhoneNumber ambiguous_toll_free_number;
231 ambiguous_toll_free_number.set_country_code(61);
232 ambiguous_toll_free_number.set_national_number(uint64{733});
233
234 EXPECT_TRUE(short_info_.IsValidShortNumber(ambiguous_premium_rate_number));
235 EXPECT_TRUE(short_info_.IsValidShortNumber(ambiguous_standard_rate_number));
236 EXPECT_TRUE(short_info_.IsValidShortNumber(ambiguous_toll_free_number));
237
238 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
239 ParseNumberForTesting(ambiguous_premium_rate_string, RegionCode::AU()),
240 RegionCode::AU()));
241 EXPECT_EQ(ShortNumberInfo::PREMIUM_RATE,
242 short_info_.GetExpectedCostForRegion(
243 ParseNumberForTesting(ambiguous_premium_rate_string,
244 RegionCode::AU()),
245 RegionCode::AU()));
246
247 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
248 ParseNumberForTesting(ambiguous_premium_rate_string, RegionCode::CX()),
249 RegionCode::CX()));
250 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
251 short_info_.GetExpectedCostForRegion(
252 ParseNumberForTesting(ambiguous_premium_rate_string,
253 RegionCode::CX()),
254 RegionCode::CX()));
255 // PREMIUM_RATE takes precedence over UNKNOWN_COST.
256 EXPECT_EQ(ShortNumberInfo::PREMIUM_RATE,
257 short_info_.GetExpectedCost(ambiguous_premium_rate_number));
258
259 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
260 ParseNumberForTesting(ambiguous_standard_rate_string, RegionCode::AU()),
261 RegionCode::AU()));
262 EXPECT_EQ(ShortNumberInfo::STANDARD_RATE,
263 short_info_.GetExpectedCostForRegion(
264 ParseNumberForTesting(ambiguous_standard_rate_string,
265 RegionCode::AU()),
266 RegionCode::AU()));
267
268 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
269 ParseNumberForTesting(ambiguous_standard_rate_string, RegionCode::CX()),
270 RegionCode::CX()));
271 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
272 short_info_.GetExpectedCostForRegion(
273 ParseNumberForTesting(ambiguous_standard_rate_string,
274 RegionCode::CX()),
275 RegionCode::CX()));
276 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
277 short_info_.GetExpectedCost(ambiguous_standard_rate_number));
278
279 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
280 ParseNumberForTesting(ambiguous_toll_free_string, RegionCode::AU()),
281 RegionCode::AU()));
282 EXPECT_EQ(
283 ShortNumberInfo::TOLL_FREE,
284 short_info_.GetExpectedCostForRegion(
285 ParseNumberForTesting(ambiguous_toll_free_string, RegionCode::AU()),
286 RegionCode::AU()));
287
288 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
289 ParseNumberForTesting(ambiguous_toll_free_string, RegionCode::CX()),
290 RegionCode::CX()));
291 EXPECT_EQ(
292 ShortNumberInfo::UNKNOWN_COST,
293 short_info_.GetExpectedCostForRegion(
294 ParseNumberForTesting(ambiguous_toll_free_string, RegionCode::CX()),
295 RegionCode::CX()));
296 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
297 short_info_.GetExpectedCost(ambiguous_toll_free_number));
298 }
299
TEST_F(ShortNumberInfoTest,GetExampleShortNumber)300 TEST_F(ShortNumberInfoTest, GetExampleShortNumber) {
301 EXPECT_FALSE(short_info_.GetExampleShortNumber(RegionCode::AD()).empty());
302 EXPECT_FALSE(short_info_.GetExampleShortNumber(RegionCode::FR()).empty());
303 EXPECT_TRUE(short_info_.GetExampleShortNumber(RegionCode::UN001()).empty());
304 EXPECT_TRUE(
305 short_info_.GetExampleShortNumber(RegionCode::GetUnknown()).empty());
306 }
307
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumber_US)308 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumber_US) {
309 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("911", RegionCode::US()));
310 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("112", RegionCode::US()));
311 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("999", RegionCode::US()));
312 }
313
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumberLongNumber_US)314 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumberLongNumber_US) {
315 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("9116666666",
316 RegionCode::US()));
317 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("1126666666",
318 RegionCode::US()));
319 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("9996666666",
320 RegionCode::US()));
321 }
322
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumberWithFormatting_US)323 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumberWithFormatting_US) {
324 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("9-1-1", RegionCode::US()));
325 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("1-1-2", RegionCode::US()));
326 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("9-9-9",
327 RegionCode::US()));
328 }
329
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumberWithPlusSign_US)330 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumberWithPlusSign_US) {
331 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("+911", RegionCode::US()));
332 // This hex sequence is the full-width plus sign U+FF0B.
333 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("\xEF\xBC\x8B" "911",
334 RegionCode::US()));
335 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber(" +911",
336 RegionCode::US()));
337 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("+112", RegionCode::US()));
338 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("+999", RegionCode::US()));
339 }
340
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumber_BR)341 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumber_BR) {
342 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("911", RegionCode::BR()));
343 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("190", RegionCode::BR()));
344 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("999", RegionCode::BR()));
345 }
346
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumberLongNumber_BR)347 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumberLongNumber_BR) {
348 // Brazilian emergency numbers don't work when additional digits are appended.
349 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("9111", RegionCode::BR()));
350 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("1900", RegionCode::BR()));
351 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("9996", RegionCode::BR()));
352 }
353
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumber_CL)354 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumber_CL) {
355 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("131", RegionCode::CL()));
356 EXPECT_TRUE(short_info_.ConnectsToEmergencyNumber("133", RegionCode::CL()));
357 }
358
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumberLongNumber_CL)359 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumberLongNumber_CL) {
360 // Chilean emergency numbers don't work when additional digits are appended.
361 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("1313", RegionCode::CL()));
362 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("1330", RegionCode::CL()));
363 }
364
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumber_AO)365 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumber_AO) {
366 // Angola doesn't have any metadata for emergency numbers in the test
367 // metadata.
368 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("911", RegionCode::AO()));
369 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("222123456",
370 RegionCode::AO()));
371 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("923123456",
372 RegionCode::AO()));
373 }
374
TEST_F(ShortNumberInfoTest,ConnectsToEmergencyNumber_ZW)375 TEST_F(ShortNumberInfoTest, ConnectsToEmergencyNumber_ZW) {
376 // Zimbabwe doesn't have any metadata in the test metadata.
377 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("911", RegionCode::ZW()));
378 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("01312345",
379 RegionCode::ZW()));
380 EXPECT_FALSE(short_info_.ConnectsToEmergencyNumber("0711234567",
381 RegionCode::ZW()));
382 }
383
TEST_F(ShortNumberInfoTest,IsEmergencyNumber_US)384 TEST_F(ShortNumberInfoTest, IsEmergencyNumber_US) {
385 EXPECT_TRUE(short_info_.IsEmergencyNumber("911", RegionCode::US()));
386 EXPECT_TRUE(short_info_.IsEmergencyNumber("112", RegionCode::US()));
387 EXPECT_FALSE(short_info_.IsEmergencyNumber("999", RegionCode::US()));
388 }
389
TEST_F(ShortNumberInfoTest,IsEmergencyNumberLongNumber_US)390 TEST_F(ShortNumberInfoTest, IsEmergencyNumberLongNumber_US) {
391 EXPECT_FALSE(short_info_.IsEmergencyNumber("9116666666", RegionCode::US()));
392 EXPECT_FALSE(short_info_.IsEmergencyNumber("1126666666", RegionCode::US()));
393 EXPECT_FALSE(short_info_.IsEmergencyNumber("9996666666", RegionCode::US()));
394 }
395
TEST_F(ShortNumberInfoTest,IsEmergencyNumberWithFormatting_US)396 TEST_F(ShortNumberInfoTest, IsEmergencyNumberWithFormatting_US) {
397 EXPECT_TRUE(short_info_.IsEmergencyNumber("9-1-1", RegionCode::US()));
398 EXPECT_TRUE(short_info_.IsEmergencyNumber("*911", RegionCode::US()));
399 EXPECT_TRUE(short_info_.IsEmergencyNumber("1-1-2", RegionCode::US()));
400 EXPECT_TRUE(short_info_.IsEmergencyNumber("*112", RegionCode::US()));
401 EXPECT_FALSE(short_info_.IsEmergencyNumber("9-9-9", RegionCode::US()));
402 EXPECT_FALSE(short_info_.IsEmergencyNumber("*999", RegionCode::US()));
403 }
404
TEST_F(ShortNumberInfoTest,IsEmergencyNumberWithPlusSign_US)405 TEST_F(ShortNumberInfoTest, IsEmergencyNumberWithPlusSign_US) {
406 EXPECT_FALSE(short_info_.IsEmergencyNumber("+911", RegionCode::US()));
407 // This hex sequence is the full-width plus sign U+FF0B.
408 EXPECT_FALSE(short_info_.IsEmergencyNumber("\xEF\xBC\x8B" "911",
409 RegionCode::US()));
410 EXPECT_FALSE(short_info_.IsEmergencyNumber(" +911", RegionCode::US()));
411 EXPECT_FALSE(short_info_.IsEmergencyNumber("+112", RegionCode::US()));
412 EXPECT_FALSE(short_info_.IsEmergencyNumber("+999", RegionCode::US()));
413 }
414
TEST_F(ShortNumberInfoTest,IsEmergencyNumber_BR)415 TEST_F(ShortNumberInfoTest, IsEmergencyNumber_BR) {
416 EXPECT_TRUE(short_info_.IsEmergencyNumber("911", RegionCode::BR()));
417 EXPECT_TRUE(short_info_.IsEmergencyNumber("190", RegionCode::BR()));
418 EXPECT_FALSE(short_info_.IsEmergencyNumber("999", RegionCode::BR()));
419 }
420
TEST_F(ShortNumberInfoTest,EmergencyNumberLongNumber_BR)421 TEST_F(ShortNumberInfoTest, EmergencyNumberLongNumber_BR) {
422 EXPECT_FALSE(short_info_.IsEmergencyNumber("9111", RegionCode::BR()));
423 EXPECT_FALSE(short_info_.IsEmergencyNumber("1900", RegionCode::BR()));
424 EXPECT_FALSE(short_info_.IsEmergencyNumber("9996", RegionCode::BR()));
425 }
426
TEST_F(ShortNumberInfoTest,IsEmergencyNumber_AO)427 TEST_F(ShortNumberInfoTest, IsEmergencyNumber_AO) {
428 // Angola doesn't have any metadata for emergency numbers in the test
429 // metadata.
430 EXPECT_FALSE(short_info_.IsEmergencyNumber("911", RegionCode::AO()));
431 EXPECT_FALSE(short_info_.IsEmergencyNumber("222123456", RegionCode::AO()));
432 EXPECT_FALSE(short_info_.IsEmergencyNumber("923123456", RegionCode::AO()));
433 }
434
TEST_F(ShortNumberInfoTest,IsEmergencyNumber_ZW)435 TEST_F(ShortNumberInfoTest, IsEmergencyNumber_ZW) {
436 // Zimbabwe doesn't have any metadata in the test metadata.
437 EXPECT_FALSE(short_info_.IsEmergencyNumber("911", RegionCode::ZW()));
438 EXPECT_FALSE(short_info_.IsEmergencyNumber("01312345", RegionCode::ZW()));
439 EXPECT_FALSE(short_info_.IsEmergencyNumber("0711234567", RegionCode::ZW()));
440 }
441
TEST_F(ShortNumberInfoTest,EmergencyNumberForSharedCountryCallingCode)442 TEST_F(ShortNumberInfoTest, EmergencyNumberForSharedCountryCallingCode) {
443 // Test the emergency number 112, which is valid in both Australia and the
444 // Christmas Islands.
445 EXPECT_TRUE(short_info_.IsEmergencyNumber("112", RegionCode::AU()));
446 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
447 ParseNumberForTesting("112", RegionCode::AU()), RegionCode::AU()));
448 EXPECT_EQ(
449 ShortNumberInfo::TOLL_FREE,
450 short_info_.GetExpectedCostForRegion(
451 ParseNumberForTesting("112", RegionCode::AU()), RegionCode::AU()));
452
453 EXPECT_TRUE(short_info_.IsEmergencyNumber("112", RegionCode::CX()));
454 EXPECT_TRUE(short_info_.IsValidShortNumberForRegion(
455 ParseNumberForTesting("112", RegionCode::CX()), RegionCode::CX()));
456 EXPECT_EQ(
457 ShortNumberInfo::TOLL_FREE,
458 short_info_.GetExpectedCostForRegion(
459 ParseNumberForTesting("112", RegionCode::CX()), RegionCode::CX()));
460
461 PhoneNumber shared_emergency_number;
462 shared_emergency_number.set_country_code(61);
463 shared_emergency_number.set_national_number(uint64{112});
464 EXPECT_TRUE(short_info_.IsValidShortNumber(shared_emergency_number));
465 EXPECT_EQ(ShortNumberInfo::TOLL_FREE,
466 short_info_.GetExpectedCost(shared_emergency_number));
467 }
468
TEST_F(ShortNumberInfoTest,OverlappingNANPANumber)469 TEST_F(ShortNumberInfoTest, OverlappingNANPANumber) {
470 // 211 is an emergency number in Barbados, while it is a toll-free
471 // information line in Canada and the USA.
472 EXPECT_TRUE(short_info_.IsEmergencyNumber("211", RegionCode::BB()));
473 EXPECT_EQ(
474 ShortNumberInfo::TOLL_FREE,
475 short_info_.GetExpectedCostForRegion(
476 ParseNumberForTesting("211", RegionCode::BB()), RegionCode::BB()));
477
478 EXPECT_FALSE(short_info_.IsEmergencyNumber("211", RegionCode::US()));
479 EXPECT_EQ(
480 ShortNumberInfo::UNKNOWN_COST,
481 short_info_.GetExpectedCostForRegion(
482 ParseNumberForTesting("211", RegionCode::US()), RegionCode::US()));
483
484 EXPECT_FALSE(short_info_.IsEmergencyNumber("211", RegionCode::CA()));
485 EXPECT_EQ(
486 ShortNumberInfo::TOLL_FREE,
487 short_info_.GetExpectedCostForRegion(
488 ParseNumberForTesting("211", RegionCode::CA()), RegionCode::CA()));
489 }
490
TEST_F(ShortNumberInfoTest,CountryCallingCodeIsNotIgnored)491 TEST_F(ShortNumberInfoTest, CountryCallingCodeIsNotIgnored) {
492 // +46 is the country calling code for Sweden (SE), and 40404 is a valid short
493 // number in the US.
494 EXPECT_FALSE(short_info_.IsPossibleShortNumberForRegion(
495 ParseNumberForTesting("+4640404", RegionCode::SE()), RegionCode::US()));
496 EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
497 ParseNumberForTesting("+4640404", RegionCode::SE()), RegionCode::US()));
498 EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
499 short_info_.GetExpectedCostForRegion(
500 ParseNumberForTesting("+4640404", RegionCode::SE()),
501 RegionCode::US()));
502 }
503
504 } // namespace phonenumbers
505 } // namespace i18n
506