|
17 | 17 | # See the License for the specific language governing permissions and |
18 | 18 | # limitations under the License. |
19 | 19 | import sys |
20 | | -import unittest |
21 | 20 |
|
22 | 21 | import phonenumbers |
23 | 22 | from phonenumbers import PhoneNumber, PhoneMetadata |
|
26 | 25 | from phonenumbers import ValidationResult, NumberFormat, CountryCodeSource |
27 | 26 | # Access internal functions of phonenumberutil.py |
28 | 27 | from phonenumbers import phonenumberutil |
| 28 | +from .testmetadatatest import TestMetadataTestCase |
29 | 29 |
|
30 | | -# Override library metadata with the test metadata. |
31 | | -REAL_REGION_METADATA = PhoneMetadata.region_metadata |
32 | | -REAL_CC_TO_RC = phonenumberutil.COUNTRY_CODE_TO_REGION_CODE |
33 | | - |
34 | | -PhoneMetadata.region_metadata = {} |
35 | | -phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = {} |
36 | | - |
37 | | -# Import the test data; this will re-populate the |
38 | | -# PhoneMetadata.region_metadata map |
39 | | -from .testdata import _COUNTRY_CODE_TO_REGION_CODE as TEST_CC_TO_RC |
40 | | -TEST_REGION_METADATA = PhoneMetadata.region_metadata |
41 | 30 |
|
42 | 31 | if sys.version_info >= (3, 0): |
43 | 32 | # Python 3 has unlimited-precision int, so no 'L' suffix |
|
49 | 38 | _UP = 'u' |
50 | 39 |
|
51 | 40 |
|
52 | | -def reinstate_real_metadata(): |
53 | | - """Reinstate real phone number metadata""" |
54 | | - phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = REAL_CC_TO_RC |
55 | | - PhoneMetadata.region_metadata = REAL_REGION_METADATA |
56 | | - |
57 | | - |
58 | | -def insert_test_metadata(): |
59 | | - """Insert test metadata into library""" |
60 | | - phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = TEST_CC_TO_RC |
61 | | - PhoneMetadata.region_metadata = TEST_REGION_METADATA |
62 | | - |
63 | | -# Reinstate the real metadata so any importers of this module are not affected |
64 | | -reinstate_real_metadata() |
65 | | - |
66 | 41 | # Set up some test numbers to re-use. |
67 | 42 | ALPHA_NUMERIC_NUMBER = FrozenPhoneNumber(country_code=1, national_number=80074935247L) |
68 | 43 | AR_MOBILE = FrozenPhoneNumber(country_code=54, national_number=91187654321L) |
@@ -101,20 +76,13 @@ def insert_test_metadata(): |
101 | 76 | XY_NUMBER = FrozenPhoneNumber(country_code=999, national_number=1234567890L) |
102 | 77 |
|
103 | 78 |
|
104 | | -class PhoneNumberUtilTest(unittest.TestCase): |
| 79 | +class PhoneNumberUtilTest(TestMetadataTestCase): |
105 | 80 | """Unit tests for phonenumbers/__init__.py |
106 | 81 |
|
107 | | - Note that these tests use the metadata contained in the files in |
108 | | - tests/data, not the normal metadata files, so should not be used for |
109 | | - regression test purposes - these tests are illustrative only and test |
110 | | - functionality. |
| 82 | + Note that these tests use the test metadata, not the normal metadata file, |
| 83 | + so should not be used for regression test purposes - these tests are |
| 84 | + illustrative only and test functionality. |
111 | 85 | """ |
112 | | - def setUp(self): |
113 | | - insert_test_metadata() |
114 | | - |
115 | | - def tearDown(self): |
116 | | - reinstate_real_metadata() |
117 | | - |
118 | 86 | def testSupportedRegions(self): |
119 | 87 | self.assertTrue(len(phonenumbers.SUPPORTED_REGIONS) > 0) |
120 | 88 | # Python version extra test |
@@ -1015,13 +983,9 @@ def testIsValidForRegion(self): |
1015 | 983 | invalidNumber.country_code = 3923 |
1016 | 984 | invalidNumber.national_number = 2366L |
1017 | 985 | self.assertFalse(phonenumbers.is_valid_number_for_region(invalidNumber, "ZZ")) |
1018 | | - invalidNumber.country_code = 3923 |
1019 | | - invalidNumber.national_number = 2366L |
1020 | 986 | self.assertFalse(phonenumbers.is_valid_number_for_region(invalidNumber, "001")) |
1021 | 987 | invalidNumber.country_code = 0 |
1022 | | - invalidNumber.national_number = 2366L |
1023 | 988 | self.assertFalse(phonenumbers.is_valid_number_for_region(invalidNumber, "001")) |
1024 | | - invalidNumber.country_code = 0 |
1025 | 989 | self.assertFalse(phonenumbers.is_valid_number_for_region(invalidNumber, "ZZ")) |
1026 | 990 |
|
1027 | 991 | # Python version extra test |
@@ -1550,6 +1514,28 @@ def testParseNumberWithAlphaCharacters(self): |
1550 | 1514 | self.assertEqual(premiumNumber, phonenumbers.parse("0900 332 600A5", "NZ")) |
1551 | 1515 | self.assertEqual(premiumNumber, phonenumbers.parse("0900 a332 600A5", "NZ")) |
1552 | 1516 |
|
| 1517 | + def testParseMaliciousInput(self): |
| 1518 | + # Lots of leading + signs before the possible number. |
| 1519 | + maliciousNumber = '+' * 6000 + "12222-33-244 extensioB 343+" |
| 1520 | + try: |
| 1521 | + phonenumbers.parse(maliciousNumber, "US") |
| 1522 | + self.fail("This should not parse without throwing an exception %s" % maliciousNumber) |
| 1523 | + except NumberParseException, e: |
| 1524 | + # Expected this exception. |
| 1525 | + self.assertEqual(e.error_type, |
| 1526 | + NumberParseException.TOO_LONG, |
| 1527 | + msg="Wrong error type stored in exception.") |
| 1528 | + |
| 1529 | + maliciousNumberWithAlmostExt = "200" * 350 + " extensiOB 345" |
| 1530 | + try: |
| 1531 | + phonenumbers.parse(maliciousNumberWithAlmostExt, "US") |
| 1532 | + self.fail("This should not parse without throwing an exception %s" % maliciousNumberWithAlmostExt) |
| 1533 | + except NumberParseException, e: |
| 1534 | + # Expected this exception. |
| 1535 | + self.assertEqual(e.error_type, |
| 1536 | + NumberParseException.TOO_LONG, |
| 1537 | + msg="Wrong error type stored in exception.") |
| 1538 | + |
1553 | 1539 | def testParseWithInternationalPrefixes(self): |
1554 | 1540 | self.assertEqual(US_NUMBER, phonenumbers.parse("+1 (650) 253-0000", "NZ")) |
1555 | 1541 | self.assertEqual(INTERNATIONAL_TOLL_FREE, phonenumbers.parse("011 800 1234 5678", "US")) |
|
0 commit comments