blob: 8e4692c9e1d1206896a54ef5b737258b4b4682af [file] [log] [blame]
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +09001/*
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 Hughes153c1022016-09-13 17:13:29 -070016
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 Miyakawaf06f5fa2009-07-07 11:11:34 +090025
26#include "PhoneNumberUtils.h"
27
28#include <stdio.h>
29#include <string.h>
30
Elliott Hughes153c1022016-09-13 17:13:29 -070031#include <gtest/gtest.h>
32
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090033using namespace android;
34
Francisco Pimenta5f46e582017-07-12 09:52:58 -070035
36TEST(PhoneNumberUtils, compareStrictNullOrEmpty) {
Elliott Hughes153c1022016-09-13 17:13:29 -070037 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 Pimenta5f46e582017-07-12 09:52:58 -070041}
Dmitri Plotnikov37214cd2011-01-06 09:01:51 -080042
Francisco Pimenta5f46e582017-07-12 09:52:58 -070043TEST(PhoneNumberUtils, compareStrictDigitsSame) {
Elliott Hughes153c1022016-09-13 17:13:29 -070044 EXPECT_TRUE(phone_number_compare_strict("999", "999"));
45 EXPECT_TRUE(phone_number_compare_strict("119", "119"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -070046}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090047
Francisco Pimenta5f46e582017-07-12 09:52:58 -070048TEST(PhoneNumberUtils, compareStrictDigitsDifferent) {
Elliott Hughes153c1022016-09-13 17:13:29 -070049 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 Pimenta5f46e582017-07-12 09:52:58 -070053}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090054
Francisco Pimenta5f46e582017-07-12 09:52:58 -070055TEST(PhoneNumberUtils, compareStrictGoogle) {
Elliott Hughes153c1022016-09-13 17:13:29 -070056 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 Pimenta5f46e582017-07-12 09:52:58 -070059}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090060
Francisco Pimenta5f46e582017-07-12 09:52:58 -070061TEST(PhoneNumberUtils, compareStrictTrunkPrefixUs) {
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090062 // trunk (NDD) prefix must be properly handled in US
Elliott Hughes153c1022016-09-13 17:13:29 -070063 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 Miyakawaf06f5fa2009-07-07 11:11:34 +090068
Elliott Hughes153c1022016-09-13 17:13:29 -070069 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 Pimenta5f46e582017-07-12 09:52:58 -070072}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090073
Francisco Pimenta5f46e582017-07-12 09:52:58 -070074TEST(PhoneNumberUtils, compareStrictDifferentCountryCode) {
Elliott Hughes153c1022016-09-13 17:13:29 -070075 EXPECT_FALSE(phone_number_compare_strict("+19012345678", "+819012345678"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -070076}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +090077
Francisco Pimenta5f46e582017-07-12 09:52:58 -070078TEST(PhoneNumberUtils, compareStrictTrunkJapan) {
Elliott Hughes153c1022016-09-13 17:13:29 -070079 EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567"));
Elliott Hughes153c1022016-09-13 17:13:29 -070080 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 Miyakawaf06f5fa2009-07-07 11:11:34 +090083
Francisco Pimenta5f46e582017-07-12 09:52:58 -070084 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
89TEST(PhoneNumberUtils, compareStrictTrunkRussia) {
90 EXPECT_TRUE(phone_number_compare_strict("+79161234567", "89161234567"));
91}
92
93TEST(PhoneNumberUtils, compareStrictTrunkFrance) {
94 EXPECT_TRUE(phone_number_compare_strict("+33123456789", "0123456789"));
95}
96
97TEST(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
117TEST(PhoneNumberUtils, compareStrictTrunkIgnoreJapan) {
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900118 // Trunk prefix must not be ignored in Japan
Elliott Hughes153c1022016-09-13 17:13:29 -0700119 EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "90-1234-5678"));
Elliott Hughes153c1022016-09-13 17:13:29 -0700120 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 Pimenta5f46e582017-07-12 09:52:58 -0700123 EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678"));
Elliott Hughes153c1022016-09-13 17:13:29 -0700124 EXPECT_FALSE(phone_number_compare_strict("+81-90-1234-5678", "+81-090-1234-5678"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700125}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900126
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700127TEST(PhoneNumberUtils, compareStrictInternationalNational) {
Elliott Hughes153c1022016-09-13 17:13:29 -0700128 EXPECT_TRUE(phone_number_compare_strict("+593(800)123-1234", "8001231234"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700129}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900130
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700131TEST(PhoneNumberUtils, compareStrictTwoContinuousZeros) {
132 // Two continuous 0 at the begining of the phone string should not be
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900133 // treated as trunk prefix.
Elliott Hughes153c1022016-09-13 17:13:29 -0700134 EXPECT_FALSE(phone_number_compare_strict("008001231234", "8001231234"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700135}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900136
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700137TEST(PhoneNumberUtils, compareStrictCallerIdThailandUs) {
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900138 // Test broken caller ID seen on call from Thailand to the US
Elliott Hughes153c1022016-09-13 17:13:29 -0700139 EXPECT_TRUE(phone_number_compare_strict("+66811234567", "166811234567"));
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900140 // Confirm that the bug found before does not re-appear.
Elliott Hughes153c1022016-09-13 17:13:29 -0700141 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 Pimenta5f46e582017-07-12 09:52:58 -0700146}
147
148TEST(PhoneNumberUtils, compareStrictNamp1661) {
Daisuke Miyakawab3b8a9d2009-09-01 22:07:18 +0900149 // This is not related to Thailand case. NAMP "1" + region code "661".
Elliott Hughes153c1022016-09-13 17:13:29 -0700150 EXPECT_TRUE(phone_number_compare_strict("16610001234", "6610001234"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700151}
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900152
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700153TEST(PhoneNumberUtils, compareStrictAlphaDifferent) {
Wei Huang0d04c4c2009-08-31 19:18:23 -0700154 // 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 Hughes153c1022016-09-13 17:13:29 -0700157 EXPECT_FALSE(phone_number_compare_strict("abcd", "bcde"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700158}
Wei Huang0d04c4c2009-08-31 19:18:23 -0700159
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700160TEST(PhoneNumberUtils, compareStrictAlphaNumericSame) {
Wei Huang0d04c4c2009-08-31 19:18:23 -0700161 // 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 Hughes153c1022016-09-13 17:13:29 -0700165 EXPECT_TRUE(phone_number_compare_strict("1-800-flowers", "800-flowers"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700166}
Wei Huang0d04c4c2009-08-31 19:18:23 -0700167
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700168TEST(PhoneNumberUtils, compareStrictAlphaNumericDifferent) {
Elliott Hughes153c1022016-09-13 17:13:29 -0700169 EXPECT_FALSE(phone_number_compare_strict("1-800-flowers", "1-800-abcdefg"));
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700170}
Wei Huang0d04c4c2009-08-31 19:18:23 -0700171
Francisco Pimenta5f46e582017-07-12 09:52:58 -0700172// 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
178TEST(PhoneNumberUtils, compareStrictWrongPrefix) {
Daisuke Miyakawaf06f5fa2009-07-07 11:11:34 +0900179 // 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 Pimenta5f46e582017-07-12 09:52:58 -0700181 // 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 Hughes153c1022016-09-13 17:13:29 -0700195}
196
Hui Yuf2a22be2019-04-29 18:19:45 -0700197TEST(PhoneNumberUtils, compareStrict_phone_number_stripped_reversed_inter) {
Elliott Hughes153c1022016-09-13 17:13:29 -0700198 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 Miyakawaf06f5fa2009-07-07 11:11:34 +0900205
Dmitri Plotnikov37214cd2011-01-06 09:01:51 -0800206 ASSERT_STRIPPED_REVERSE("", "");
Elliott Hughes153c1022016-09-13 17:13:29 -0700207
Dmitri Plotnikov37214cd2011-01-06 09:01:51 -0800208 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 Miyakawaf06f5fa2009-07-07 11:11:34 +0900222}
Hui Yuf2a22be2019-04-29 18:19:45 -0700223
224TEST(PhoneNumberUtils, compareStrictRussianNumbers) {
225 EXPECT_FALSE(phone_number_compare_strict("84951234567", "+84951234567"));
226
227 EXPECT_FALSE(phone_number_compare_strict("88001234567", "+88001234567"));
228}