tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_phoneNumber.js (9913B)


      1 /**
      2 * Tests PhoneNumber.sys.mjs and PhoneNumberNormalizer.sys.mjs.
      3 */
      4 
      5 "use strict";
      6 
      7 var PhoneNumber, PhoneNumberNormalizer;
      8 add_setup(async () => {
      9  ({ PhoneNumber } = ChromeUtils.importESModule(
     10    "resource://gre/modules/shared/PhoneNumber.sys.mjs"
     11  ));
     12  ({ PhoneNumberNormalizer } = ChromeUtils.importESModule(
     13    "resource://gre/modules/shared/PhoneNumberNormalizer.sys.mjs"
     14  ));
     15 });
     16 
     17 function IsPlain(dial, expected) {
     18  let result = PhoneNumber.IsPlain(dial);
     19  Assert.equal(result, expected);
     20 }
     21 
     22 function Normalize(dial, expected) {
     23  let result = PhoneNumberNormalizer.Normalize(dial);
     24  Assert.equal(result, expected);
     25 }
     26 
     27 function CantParse(dial, currentRegion) {
     28  let result = PhoneNumber.Parse(dial, currentRegion);
     29  Assert.equal(null, result);
     30 }
     31 
     32 function Parse(dial, currentRegion) {
     33  let result = PhoneNumber.Parse(dial, currentRegion);
     34  Assert.notEqual(result, null);
     35  return result;
     36 }
     37 
     38 function Test(dial, currentRegion, nationalNumber, region) {
     39  let result = Parse(dial, currentRegion);
     40  Assert.equal(result.nationalNumber, nationalNumber);
     41  Assert.equal(result.region, region);
     42  return result;
     43 }
     44 
     45 function TestProperties(dial, currentRegion) {
     46  let result = Parse(dial, currentRegion);
     47  Assert.ok(result.internationalFormat);
     48  Assert.ok(result.internationalNumber);
     49  Assert.ok(result.nationalFormat);
     50  Assert.ok(result.nationalNumber);
     51  Assert.ok(result.countryName);
     52  Assert.ok(result.countryCode);
     53 }
     54 
     55 function Format(
     56  dial,
     57  currentRegion,
     58  nationalNumber,
     59  region,
     60  nationalFormat,
     61  internationalFormat
     62 ) {
     63  let result = Test(dial, currentRegion, nationalNumber, region);
     64  Assert.equal(result.nationalFormat, nationalFormat);
     65  Assert.equal(result.internationalFormat, internationalFormat);
     66  return result;
     67 }
     68 
     69 function AllEqual(list, currentRegion) {
     70  let parsedList = list.map(item => Parse(item, currentRegion));
     71  let firstItem = parsedList.shift();
     72  for (let item of parsedList) {
     73    Assert.deepEqual(item, firstItem);
     74  }
     75 }
     76 
     77 add_task(async function test_phoneNumber() {
     78  // Test whether could a string be a phone number.
     79  IsPlain(null, false);
     80  IsPlain("", false);
     81  IsPlain("1", true);
     82  IsPlain("*2", true); // Real number used in Venezuela
     83  IsPlain("*8", true); // Real number used in Venezuela
     84  IsPlain("12", true);
     85  IsPlain("123", true);
     86  IsPlain("1a2", false);
     87  IsPlain("12a", false);
     88  IsPlain("1234", true);
     89  IsPlain("123a", false);
     90  IsPlain("+", true);
     91  IsPlain("+1", true);
     92  IsPlain("+12", true);
     93  IsPlain("+123", true);
     94  IsPlain("()123", false);
     95  IsPlain("(1)23", false);
     96  IsPlain("(12)3", false);
     97  IsPlain("(123)", false);
     98  IsPlain("(123)4", false);
     99  IsPlain("(123)4", false);
    100  IsPlain("123;ext=", false);
    101  IsPlain("123;ext=1", false);
    102  IsPlain("123;ext=1234567", false);
    103  IsPlain("123;ext=12345678", false);
    104  IsPlain("123 ext:1", false);
    105  IsPlain("123 ext:1#", false);
    106  IsPlain("123-1#", false);
    107  IsPlain("123 1#", false);
    108  IsPlain("123 12345#", false);
    109  IsPlain("123 +123456#", false);
    110 
    111  // Getting international number back from intl number.
    112  TestProperties("+19497262896");
    113 
    114  // Test parsing national numbers.
    115  Parse("033316005", "NZ");
    116  Parse("03-331 6005", "NZ");
    117  Parse("03 331 6005", "NZ");
    118  // Testing international prefixes.
    119  // Should strip country code.
    120  Parse("0064 3 331 6005", "NZ");
    121 
    122  // Test CA before US because CA has to import meta-information for US.
    123  Parse("4031234567", "CA");
    124  Parse("(416) 585-4319", "CA");
    125  Parse("647-967-4357", "CA");
    126  Parse("416-716-8768", "CA");
    127  Parse("18002684646", "CA");
    128  Parse("416-445-9119", "CA");
    129  Parse("1-800-668-6866", "CA");
    130  Parse("(416) 453-6486", "CA");
    131  Parse("(647) 268-4778", "CA");
    132  Parse("647-218-1313", "CA");
    133  Parse("+1 647-209-4642", "CA");
    134  Parse("416-559-0133", "CA");
    135  Parse("+1 647-639-4118", "CA");
    136  Parse("+12898803664", "CA");
    137  Parse("780-901-4687", "CA");
    138  Parse("+14167070550", "CA");
    139  Parse("+1-647-522-6487", "CA");
    140  Parse("(416) 877-0880", "CA");
    141 
    142  // Try again, but this time we have an international number with region rode US. It should
    143  // recognize the country code and parse accordingly.
    144  Parse("01164 3 331 6005", "US");
    145  Parse("+64 3 331 6005", "US");
    146  Parse("64(0)64123456", "NZ");
    147  // Check that using a "/" is fine in a phone number.
    148  Parse("123/45678", "DE");
    149  Parse("123-456-7890", "US");
    150 
    151  // Test parsing international numbers.
    152  Parse("+1 (650) 333-6000", "NZ");
    153  Parse("1-650-333-6000", "US");
    154  // Calling the US number from Singapore by using different service providers
    155  // 1st test: calling using SingTel IDD service (IDD is 001)
    156  Parse("0011-650-333-6000", "SG");
    157  // 2nd test: calling using StarHub IDD service (IDD is 008)
    158  Parse("0081-650-333-6000", "SG");
    159  // 3rd test: calling using SingTel V019 service (IDD is 019)
    160  Parse("0191-650-333-6000", "SG");
    161  // Calling the US number from Poland
    162  Parse("0~01-650-333-6000", "PL");
    163  // Using "++" at the start.
    164  Parse("++1 (650) 333-6000", "PL");
    165  // Using a full-width plus sign.
    166  Parse("\uFF0B1 (650) 333-6000", "SG");
    167  // The whole number, including punctuation, is here represented in full-width form.
    168  Parse(
    169    "\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
    170      "\u3000\uFF13\uFF13\uFF13\uFF0D\uFF16\uFF10\uFF10\uFF10",
    171    "SG"
    172  );
    173 
    174  // Test parsing with leading zeros.
    175  Parse("+39 02-36618 300", "NZ");
    176  Parse("02-36618 300", "IT");
    177  Parse("312 345 678", "IT");
    178 
    179  // Test parsing numbers in Argentina.
    180  Parse("+54 9 343 555 1212", "AR");
    181  Parse("0343 15 555 1212", "AR");
    182  Parse("+54 9 3715 65 4320", "AR");
    183  Parse("03715 15 65 4320", "AR");
    184  Parse("+54 11 3797 0000", "AR");
    185  Parse("011 3797 0000", "AR");
    186  Parse("+54 3715 65 4321", "AR");
    187  Parse("03715 65 4321", "AR");
    188  Parse("+54 23 1234 0000", "AR");
    189  Parse("023 1234 0000", "AR");
    190 
    191  // Test numbers in Mexico
    192  Parse("+52 (449)978-0001", "MX");
    193  Parse("01 (449)978-0001", "MX");
    194  Parse("(449)978-0001", "MX");
    195  Parse("+52 1 33 1234-5678", "MX");
    196  Parse("044 (33) 1234-5678", "MX");
    197  Parse("045 33 1234-5678", "MX");
    198 
    199  // Test that lots of spaces are ok.
    200  Parse("0 3   3 3 1   6 0 0 5", "NZ");
    201 
    202  // Test omitting the current region. This is only valid when the number starts
    203  // with a '+'.
    204  Parse("+64 3 331 6005");
    205  Parse("+64 3 331 6005", null);
    206 
    207  // US numbers
    208  Format(
    209    "19497261234",
    210    "US",
    211    "9497261234",
    212    "US",
    213    "(949) 726-1234",
    214    "+1 949-726-1234"
    215  );
    216 
    217  // Try a couple german numbers from the US with various access codes.
    218  Format(
    219    "49451491934",
    220    "US",
    221    "0451491934",
    222    "DE",
    223    "0451 491934",
    224    "+49 451 491934"
    225  );
    226  Format(
    227    "+49451491934",
    228    "US",
    229    "0451491934",
    230    "DE",
    231    "0451 491934",
    232    "+49 451 491934"
    233  );
    234  Format(
    235    "01149451491934",
    236    "US",
    237    "0451491934",
    238    "DE",
    239    "0451 491934",
    240    "+49 451 491934"
    241  );
    242 
    243  // Now try dialing the same number from within the German region.
    244  Format(
    245    "451491934",
    246    "DE",
    247    "0451491934",
    248    "DE",
    249    "0451 491934",
    250    "+49 451 491934"
    251  );
    252  Format(
    253    "0451491934",
    254    "DE",
    255    "0451491934",
    256    "DE",
    257    "0451 491934",
    258    "+49 451 491934"
    259  );
    260 
    261  // Numbers in italy keep the leading 0 in the city code when dialing internationally.
    262  Format(
    263    "0577-555-555",
    264    "IT",
    265    "0577555555",
    266    "IT",
    267    "05 7755 5555",
    268    "+39 05 7755 5555"
    269  );
    270 
    271  // Colombian international number without the leading "+"
    272  Format("5712234567", "CO", "12234567", "CO", "(1) 2234567", "+57 1 2234567");
    273 
    274  // Telefonica tests
    275  Format(
    276    "612123123",
    277    "ES",
    278    "612123123",
    279    "ES",
    280    "612 12 31 23",
    281    "+34 612 12 31 23"
    282  );
    283 
    284  // Chile mobile number from a landline
    285  Format(
    286    "0997654321",
    287    "CL",
    288    "997654321",
    289    "CL",
    290    "(99) 765 4321",
    291    "+56 99 765 4321"
    292  );
    293 
    294  // Chile mobile number from another mobile number
    295  Format(
    296    "997654321",
    297    "CL",
    298    "997654321",
    299    "CL",
    300    "(99) 765 4321",
    301    "+56 99 765 4321"
    302  );
    303 
    304  // Dialing 911 in the US. This is not a national number.
    305  CantParse("911", "US");
    306 
    307  // China mobile number with a 0 in it
    308  Format(
    309    "15955042864",
    310    "CN",
    311    "015955042864",
    312    "CN",
    313    "0159 5504 2864",
    314    "+86 159 5504 2864"
    315  );
    316 
    317  // Testing international region numbers.
    318  CantParse("883510000000091", "001");
    319  Format(
    320    "+883510000000092",
    321    "001",
    322    "510000000092",
    323    "001",
    324    "510 000 000 092",
    325    "+883 510 000 000 092"
    326  );
    327  Format(
    328    "883510000000093",
    329    "FR",
    330    "510000000093",
    331    "001",
    332    "510 000 000 093",
    333    "+883 510 000 000 093"
    334  );
    335  Format(
    336    "+883510000000094",
    337    "FR",
    338    "510000000094",
    339    "001",
    340    "510 000 000 094",
    341    "+883 510 000 000 094"
    342  );
    343  Format(
    344    "883510000000095",
    345    "US",
    346    "510000000095",
    347    "001",
    348    "510 000 000 095",
    349    "+883 510 000 000 095"
    350  );
    351  Format(
    352    "+883510000000096",
    353    "US",
    354    "510000000096",
    355    "001",
    356    "510 000 000 096",
    357    "+883 510 000 000 096"
    358  );
    359  CantParse("979510000012", "001");
    360  Format(
    361    "+979510000012",
    362    "001",
    363    "510000012",
    364    "001",
    365    "5 1000 0012",
    366    "+979 5 1000 0012"
    367  );
    368 
    369  // Test normalizing numbers. Only 0-9,#* are valid in a phone number.
    370  Normalize("+ABC # * , 9 _ 1 _0", "+222#*,910");
    371  Normalize("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999");
    372  Normalize("abcdefghijklmnopqrstuvwxyz", "22233344455566677778889999");
    373 
    374  // 8 and 9 digit numbers with area code in Brazil with collect call prefix (90)
    375  AllEqual(
    376    [
    377      "01187654321",
    378      "0411187654321",
    379      "551187654321",
    380      "90411187654321",
    381      "+551187654321",
    382    ],
    383    "BR"
    384  );
    385  AllEqual(
    386    [
    387      "011987654321",
    388      "04111987654321",
    389      "5511987654321",
    390      "904111987654321",
    391      "+5511987654321",
    392    ],
    393    "BR"
    394  );
    395 
    396  Assert.equal(PhoneNumberNormalizer.Normalize("123abc", true), "123");
    397  Assert.equal(PhoneNumberNormalizer.Normalize("12345", true), "12345");
    398  Assert.equal(PhoneNumberNormalizer.Normalize("1abcd", false), "12223");
    399 });