| 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 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 35 | |
| 36 | TEST(PhoneNumberUtils, compareStrictNullOrEmpty) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 37 | EXPECT_TRUE(phone_number_compare_strict(NULL, NULL)); |
| 38 | EXPECT_TRUE(phone_number_compare_strict("", NULL)); |
| 39 | EXPECT_TRUE(phone_number_compare_strict(NULL, "")); |
| 40 | EXPECT_TRUE(phone_number_compare_strict("", "")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 41 | } |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 42 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 43 | TEST(PhoneNumberUtils, compareStrictDigitsSame) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 44 | EXPECT_TRUE(phone_number_compare_strict("999", "999")); |
| 45 | EXPECT_TRUE(phone_number_compare_strict("119", "119")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 46 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 47 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 48 | TEST(PhoneNumberUtils, compareStrictDigitsDifferent) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 49 | EXPECT_FALSE(phone_number_compare_strict("123456789", "923456789")); |
| 50 | EXPECT_FALSE(phone_number_compare_strict("123456789", "123456781")); |
| 51 | EXPECT_FALSE(phone_number_compare_strict("123456789", "1234567890")); |
| 52 | EXPECT_FALSE(phone_number_compare_strict("123456789", "0123456789")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 53 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 54 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 55 | TEST(PhoneNumberUtils, compareStrictGoogle) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 56 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "6502530000")); |
| 57 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "650 253 0000")); |
| 58 | EXPECT_TRUE(phone_number_compare_strict("650 253 0000", "6502530000")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 59 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 60 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 61 | TEST(PhoneNumberUtils, compareStrictTrunkPrefixUs) { |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 62 | // trunk (NDD) prefix must be properly handled in US |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 63 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "1-650-253-0000")); |
| 64 | EXPECT_TRUE(phone_number_compare_strict("650-253-0000", " 1-650-253-0000")); |
| 65 | EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "11-650-253-0000")); |
| 66 | EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "0-650-253-0000")); |
| 67 | EXPECT_FALSE(phone_number_compare_strict("555-4141", "+1-700-555-4141")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 68 | |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(phone_number_compare_strict("+1 650-253-0000", "6502530000")); |
| 70 | EXPECT_TRUE(phone_number_compare_strict("001 650-253-0000", "6502530000")); |
| 71 | EXPECT_TRUE(phone_number_compare_strict("0111 650-253-0000", "6502530000")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 72 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 73 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 74 | TEST(PhoneNumberUtils, compareStrictDifferentCountryCode) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(phone_number_compare_strict("+19012345678", "+819012345678")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 76 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 77 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 78 | TEST(PhoneNumberUtils, compareStrictTrunkJapan) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567")); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+819012345678")); |
| 81 | EXPECT_TRUE(phone_number_compare_strict("090(1234)5678", "+819012345678")); |
| 82 | 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] | 83 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(phone_number_compare_strict("+819012345678", "090-1234-5678")); |
| 85 | EXPECT_TRUE(phone_number_compare_strict("+819012345678", "090(1234)5678")); |
| 86 | EXPECT_TRUE(phone_number_compare_strict("+81-90-1234-5678", "090-1234-5678")); |
| 87 | } |
| 88 | |
| 89 | TEST(PhoneNumberUtils, compareStrictTrunkRussia) { |
| 90 | EXPECT_TRUE(phone_number_compare_strict("+79161234567", "89161234567")); |
| 91 | } |
| 92 | |
| 93 | TEST(PhoneNumberUtils, compareStrictTrunkFrance) { |
| 94 | EXPECT_TRUE(phone_number_compare_strict("+33123456789", "0123456789")); |
| 95 | } |
| 96 | |
| 97 | TEST(PhoneNumberUtils, compareStrictTrunkNetherlandsCities) { |
| 98 | EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567")); |
| 99 | } |
| 100 | |
| 101 | // TODO: Two digit trunk prefixes are not handled |
| 102 | //TEST(PhoneNumberUtils, compareStrictTrunkHungary) { |
| 103 | // EXPECT_TRUE(phone_number_compare_strict("+36 1 234 5678", "06 1234-5678")); |
| 104 | //} |
| 105 | |
| 106 | // TODO: Two digit trunk prefixes are not handled |
| 107 | //TEST(PhoneNumberUtils, compareStrictTrunkMexico) { |
| 108 | // EXPECT_TRUE(phone_number_compare_strict("+52 55 1234 5678", "01 55 1234 5678")); |
| 109 | //} |
| 110 | |
| 111 | // TODO: Two digit trunk prefixes are not handled |
| 112 | //TEST(PhoneNumberUtils, compareStrictTrunkMongolia) { |
| 113 | // EXPECT_TRUE(phone_number_compare_strict("+976 1 123 4567", "01 1 23 4567")); |
| 114 | // EXPECT_TRUE(phone_number_compare_strict("+976 2 234 5678", "02 2 34 5678")); |
| 115 | //} |
| 116 | |
| 117 | TEST(PhoneNumberUtils, compareStrictTrunkIgnoreJapan) { |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 118 | // Trunk prefix must not be ignored in Japan |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 119 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "90-1234-5678")); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 120 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "080-1234-5678")); |
| 121 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "190-1234-5678")); |
| 122 | EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "890-1234-5678")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 123 | EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678")); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(phone_number_compare_strict("+81-90-1234-5678", "+81-090-1234-5678")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 125 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 126 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 127 | TEST(PhoneNumberUtils, compareStrictInternationalNational) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(phone_number_compare_strict("+593(800)123-1234", "8001231234")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 129 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 130 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 131 | TEST(PhoneNumberUtils, compareStrictTwoContinuousZeros) { |
| 132 | // Two continuous 0 at the begining of the phone string should not be |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 133 | // treated as trunk prefix. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 134 | EXPECT_FALSE(phone_number_compare_strict("008001231234", "8001231234")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 135 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 136 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 137 | TEST(PhoneNumberUtils, compareStrictCallerIdThailandUs) { |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 138 | // Test broken caller ID seen on call from Thailand to the US |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(phone_number_compare_strict("+66811234567", "166811234567")); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 140 | // Confirm that the bug found before does not re-appear. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 141 | EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678")); |
| 142 | EXPECT_TRUE(phone_number_compare_strict("650-000-3456", "16500003456")); |
| 143 | EXPECT_TRUE(phone_number_compare_strict("011 1 7005554141", "+17005554141")); |
| 144 | EXPECT_FALSE(phone_number_compare_strict("011 11 7005554141", "+17005554141")); |
| 145 | EXPECT_FALSE(phone_number_compare_strict("+44 207 792 3490", "00 207 792 3490")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(PhoneNumberUtils, compareStrictNamp1661) { |
| Daisuke Miyakawa | b3b8a9d | 2009-09-01 22:07:18 +0900 | [diff] [blame] | 149 | // This is not related to Thailand case. NAMP "1" + region code "661". |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 150 | EXPECT_TRUE(phone_number_compare_strict("16610001234", "6610001234")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 151 | } |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 152 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 153 | TEST(PhoneNumberUtils, compareStrictAlphaDifferent) { |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 154 | // We also need to compare two alpha addresses to make sure two different strings |
| 155 | // aren't treated as the same addresses. This is relevant to SMS as SMS sender may |
| 156 | // contain all alpha chars. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 157 | EXPECT_FALSE(phone_number_compare_strict("abcd", "bcde")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 158 | } |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 159 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 160 | TEST(PhoneNumberUtils, compareStrictAlphaNumericSame) { |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 161 | // in the U.S. people often use alpha in the phone number to easily remember it |
| 162 | // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of |
| 163 | // phone number in Contacts and others, we should make sure the comparison method |
| 164 | // handle them. |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(phone_number_compare_strict("1-800-flowers", "800-flowers")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 166 | } |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 167 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 168 | TEST(PhoneNumberUtils, compareStrictAlphaNumericDifferent) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 169 | EXPECT_FALSE(phone_number_compare_strict("1-800-flowers", "1-800-abcdefg")); |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 170 | } |
| Wei Huang | 0d04c4c | 2009-08-31 19:18:23 -0700 | [diff] [blame] | 171 | |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 172 | // TODO: we currently do not support this comparison. |
| 173 | // TODO: It maybe be nice to support this in the future. |
| 174 | //TEST(PhoneNumberUtils, compareStrictLettersToDigits) { |
| 175 | // EXPECT_TRUE("1-800-flowers", "1-800-356-9377") |
| 176 | //} |
| 177 | |
| 178 | TEST(PhoneNumberUtils, compareStrictWrongPrefix) { |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 179 | // Currently we cannot get this test through (Japanese trunk prefix is 0, |
| 180 | // but there is no sensible way to know it now (as of 2009-6-12)... |
| Francisco Pimenta | 5f46e58 | 2017-07-12 09:52:58 -0700 | [diff] [blame] | 181 | // EXPECT_FALSE(phone_number_compare_strict("290-1234-5678", "+819012345678")); |
| 182 | // EXPECT_FALSE(phone_number_compare_strict("+819012345678", "290-1234-5678")); |
| 183 | |
| 184 | // USA |
| 185 | EXPECT_FALSE(phone_number_compare_strict("550-450-3605", "+14504503605")); |
| 186 | |
| 187 | EXPECT_FALSE(phone_number_compare_strict("550-450-3605", "+15404503605")); |
| 188 | EXPECT_FALSE(phone_number_compare_strict("550-450-3605", "+15514503605")); |
| 189 | EXPECT_FALSE(phone_number_compare_strict("5504503605", "+14504503605")); |
| 190 | |
| 191 | EXPECT_FALSE(phone_number_compare_strict("+14504503605", "550-450-3605")); |
| 192 | EXPECT_FALSE(phone_number_compare_strict("+15404503605", "550-450-3605")); |
| 193 | EXPECT_FALSE(phone_number_compare_strict("+15514503605", "550-450-3605")); |
| 194 | EXPECT_FALSE(phone_number_compare_strict("+14504503605", "5504503605")); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| Hui Yu | f2a22be | 2019-04-29 18:19:45 -0700 | [diff] [blame] | 197 | TEST(PhoneNumberUtils, compareStrict_phone_number_stripped_reversed_inter) { |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 198 | char out[6]; |
| 199 | int outlen; |
| 200 | |
| 201 | #define ASSERT_STRIPPED_REVERSE(input, expected) \ |
| 202 | phone_number_stripped_reversed_inter((input), out, sizeof(out), &outlen); \ |
| 203 | out[outlen] = 0; \ |
| 204 | ASSERT_STREQ((expected), (out)); \ |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 205 | |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 206 | ASSERT_STRIPPED_REVERSE("", ""); |
| Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 207 | |
| Dmitri Plotnikov | 37214cd | 2011-01-06 09:01:51 -0800 | [diff] [blame] | 208 | ASSERT_STRIPPED_REVERSE("123", "321"); |
| 209 | ASSERT_STRIPPED_REVERSE("123*N#", "#N*321"); |
| 210 | |
| 211 | // Buffer overflow |
| 212 | ASSERT_STRIPPED_REVERSE("1234567890", "098765"); |
| 213 | |
| 214 | // Only one plus is copied |
| 215 | ASSERT_STRIPPED_REVERSE("1+2+", "+21"); |
| 216 | |
| 217 | // Pause/wait in the phone number |
| 218 | ASSERT_STRIPPED_REVERSE("12;34", "21"); |
| 219 | |
| 220 | // Ignoring non-dialable |
| 221 | ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321"); |
| Daisuke Miyakawa | f06f5fa | 2009-07-07 11:11:34 +0900 | [diff] [blame] | 222 | } |
| Hui Yu | f2a22be | 2019-04-29 18:19:45 -0700 | [diff] [blame] | 223 | |
| 224 | TEST(PhoneNumberUtils, compareStrictRussianNumbers) { |
| 225 | EXPECT_FALSE(phone_number_compare_strict("84951234567", "+84951234567")); |
| 226 | |
| 227 | EXPECT_FALSE(phone_number_compare_strict("88001234567", "+88001234567")); |
| 228 | } |