| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 16 | |
| 17 | // |
| 18 | // Note that similar (or almost same) tests exist in Java side (See |
| 19 | // DatabaseGeneralTest.java in AndroidTests). The differences are: |
| 20 | // - this test is quite easy to do (You can do it in your Unix PC) |
| 21 | // - this test is not automatically executed by build servers |
| 22 | // |
| 23 | // You should also execute the test before submitting this. |
| 24 | // |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 25 | |
| 26 | #include "PhoneNumberUtils.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
| 32 | |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 33 | using namespace android; |
| 34 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 35 | TEST(PhoneNumberUtils, phone_number_compare_strict) { |
| 36 | EXPECT_TRUE(phone_number_compare_strict(NULL, NULL)); |
| 37 | EXPECT_TRUE(phone_number_compare_strict("", NULL)); |
| 38 | EXPECT_TRUE(phone_number_compare_strict(NULL, "")); |
| 39 | EXPECT_TRUE(phone_number_compare_strict("", "")); |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 40 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 41 | EXPECT_TRUE(phone_number_compare_strict("999", "999")); |
| 42 | EXPECT_TRUE(phone_number_compare_strict("119", "119")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 43 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 44 | EXPECT_FALSE(phone_number_compare_strict("123456789", "923456789")); |
| 45 | EXPECT_FALSE(phone_number_compare_strict("123456789", "123456781")); |
| 46 | EXPECT_FALSE(phone_number_compare_strict("123456789", "1234567890")); |
| 47 | EXPECT_FALSE(phone_number_compare_strict("123456789", "0123456789")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 48 | |
| 49 | // Google, Inc. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "6502530000")); |
| 51 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "650 253 0000")); |
| 52 | EXPECT_TRUE(phone_number_compare_strict("650 253 0000", "6502530000")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 53 | |
| 54 | // trunk (NDD) prefix must be properly handled in US |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 55 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "1-650-253-0000")); |
| 56 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", " 1-650-253-0000")); |
| 57 | EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "11-650-253-0000")); |
| 58 | EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "0-650-253-0000")); |
| 59 | EXPECT_FALSE(phone_number_compare_strict("555-4141", "+1-700-555-4141")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 60 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 61 | EXPECT_TRUE(phone_number_compare_strict("+1 650-253-0000", "6502530000")); |
| 62 | EXPECT_TRUE(phone_number_compare_strict("001 650-253-0000", "6502530000")); |
| 63 | EXPECT_TRUE(phone_number_compare_strict("0111 650-253-0000", "6502530000")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 64 | |
| 65 | // Country code is different. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 66 | EXPECT_FALSE(phone_number_compare_strict("+19012345678", "+819012345678")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 67 | |
| 68 | // Russian trunk digit |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(phone_number_compare_strict("+79161234567", "89161234567")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 70 | |
| 71 | // French trunk digit |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 72 | EXPECT_TRUE(phone_number_compare_strict("+33123456789", "0123456789")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 73 | |
| 74 | // Trunk digit for city codes in the Netherlands |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 76 | |
| 77 | // Japanese dial |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+819012345678")); |
| 79 | EXPECT_TRUE(phone_number_compare_strict("090(1234)5678", "+819012345678")); |
| 80 | EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+81-90-1234-5678")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 81 | |
| 82 | // Trunk prefix must not be ignored in Japan |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "90-1234-5678")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 84 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 85 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "080-1234-5678")); |
| 86 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "190-1234-5678")); |
| 87 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "890-1234-5678")); |
| 88 | EXPECT_FALSE(phone_number_compare_strict("+81-90-1234-5678", "+81-090-1234-5678")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 89 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(phone_number_compare_strict("+593(800)123-1234", "8001231234")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 91 | |
| 92 | // Two continuous 0 at the beginieng of the phone string should not be |
| 93 | // treated as trunk prefix. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 94 | EXPECT_FALSE(phone_number_compare_strict("008001231234", "8001231234")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 95 | |
| 96 | // Test broken caller ID seen on call from Thailand to the US |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(phone_number_compare_strict("+66811234567", "166811234567")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 98 | |
| 99 | // Confirm that the bug found before does not re-appear. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 100 | EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678")); |
| 101 | EXPECT_TRUE(phone_number_compare_strict("650-000-3456", "16500003456")); |
| 102 | EXPECT_TRUE(phone_number_compare_strict("011 1 7005554141", "+17005554141")); |
| 103 | EXPECT_FALSE(phone_number_compare_strict("011 11 7005554141", "+17005554141")); |
| 104 | EXPECT_FALSE(phone_number_compare_strict("+44 207 792 3490", "00 207 792 3490")); |
| Daisuke Miyakawa | b3b8a9d | 2009-09-01 22:07:18 +0900 | [diff] [blame] | 105 | // This is not related to Thailand case. NAMP "1" + region code "661". |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(phone_number_compare_strict("16610001234", "6610001234")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 107 | |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 108 | // We also need to compare two alpha addresses to make sure two different strings |
| 109 | // aren't treated as the same addresses. This is relevant to SMS as SMS sender may |
| 110 | // contain all alpha chars. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 111 | EXPECT_FALSE(phone_number_compare_strict("abcd", "bcde")); |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 112 | |
| 113 | // in the U.S. people often use alpha in the phone number to easily remember it |
| 114 | // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of |
| 115 | // phone number in Contacts and others, we should make sure the comparison method |
| 116 | // handle them. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(phone_number_compare_strict("1-800-flowers", "800-flowers")); |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 118 | |
| 119 | // TODO: we currently do not support this comparison. It maybe nice to support this |
| 120 | // TODO: in the future. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 121 | // EXPECT_TRUE("1-800-flowers", "1-800-356-9377") |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 122 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 123 | EXPECT_FALSE(phone_number_compare_strict("1-800-flowers", "1-800-abcdefg")); |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 124 | |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 125 | // Currently we cannot get this test through (Japanese trunk prefix is 0, |
| 126 | // but there is no sensible way to know it now (as of 2009-6-12)... |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 127 | // EXPECT_FALSE("290-1234-5678", "+819012345678"); |
| 128 | } |
| 129 | |
| 130 | TEST(PhoneNumberUtils, phone_number_stripped_reversed_inter) { |
| 131 | char out[6]; |
| 132 | int outlen; |
| 133 | |
| 134 | #define ASSERT_STRIPPED_REVERSE(input, expected) \ |
| 135 | phone_number_stripped_reversed_inter((input), out, sizeof(out), &outlen); \ |
| 136 | out[outlen] = 0; \ |
| 137 | ASSERT_STREQ((expected), (out)); \ |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 138 | |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 139 | ASSERT_STRIPPED_REVERSE("", ""); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 140 | |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 141 | ASSERT_STRIPPED_REVERSE("123", "321"); |
| 142 | ASSERT_STRIPPED_REVERSE("123*N#", "#N*321"); |
| 143 | |
| 144 | // Buffer overflow |
| 145 | ASSERT_STRIPPED_REVERSE("1234567890", "098765"); |
| 146 | |
| 147 | // Only one plus is copied |
| 148 | ASSERT_STRIPPED_REVERSE("1+2+", "+21"); |
| 149 | |
| 150 | // Pause/wait in the phone number |
| 151 | ASSERT_STRIPPED_REVERSE("12;34", "21"); |
| 152 | |
| 153 | // Ignoring non-dialable |
| 154 | ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321"); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 155 | } |