1f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa/*
2f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Copyright (C) 2009 The Android Open Source Project
3f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa *
4f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Licensed under the Apache License, Version 2.0 (the "License");
5f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * you may not use this file except in compliance with the License.
6f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * You may obtain a copy of the License at
7f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa *
8f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa *      http://www.apache.org/licenses/LICENSE-2.0
9f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa *
10f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Unless required by applicable law or agreed to in writing, software
11f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * distributed under the License is distributed on an "AS IS" BASIS,
12f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * See the License for the specific language governing permissions and
14f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * limitations under the License.
15f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa */
16153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
17153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes//
18153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes// Note that similar (or almost same) tests exist in Java side (See
19153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes// DatabaseGeneralTest.java in AndroidTests). The differences are:
20153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes// - this test is quite easy to do (You can do it in your Unix PC)
21153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes// - this test is not automatically executed by build servers
22153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes//
23153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes// You should also execute the test before submitting this.
24153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes//
25f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
26f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include "PhoneNumberUtils.h"
27f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
28f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include <stdio.h>
29f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include <string.h>
30f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
31153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes#include <gtest/gtest.h>
32153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
33f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawausing namespace android;
34f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
35153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott HughesTEST(PhoneNumberUtils, phone_number_compare_strict) {
36153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict(NULL, NULL));
37153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("", NULL));
38153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict(NULL, ""));
39153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("", ""));
40153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
41153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("999", "999"));
42153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("119", "119"));
43153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
44153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("123456789", "923456789"));
45153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("123456789", "123456781"));
46153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("123456789", "1234567890"));
47153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("123456789", "0123456789"));
48f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
49f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Google, Inc.
50153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "6502530000"));
51153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "650 253 0000"));
52153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650 253 0000", "6502530000"));
53f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
54f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // trunk (NDD) prefix must be properly handled in US
55153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "1-650-253-0000"));
56153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "   1-650-253-0000"));
57153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "11-650-253-0000"));
58153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "0-650-253-0000"));
59153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("555-4141", "+1-700-555-4141"));
60f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
61153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+1 650-253-0000", "6502530000"));
62153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("001 650-253-0000", "6502530000"));
63153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("0111 650-253-0000", "6502530000"));
64f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
65f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Country code is different.
66153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("+19012345678", "+819012345678"));
67f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
68f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Russian trunk digit
69153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+79161234567", "89161234567"));
70f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
71f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // French trunk digit
72153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+33123456789", "0123456789"));
73f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
74f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Trunk digit for city codes in the Netherlands
75153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567"));
76f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
77f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Japanese dial
78153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+819012345678"));
79153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("090(1234)5678", "+819012345678"));
80153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+81-90-1234-5678"));
81f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
82f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Trunk prefix must not be ignored in Japan
83153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "90-1234-5678"));
84f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
85153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "080-1234-5678"));
86153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "190-1234-5678"));
87153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "890-1234-5678"));
88153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("+81-90-1234-5678", "+81-090-1234-5678"));
89f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
90153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+593(800)123-1234", "8001231234"));
91f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
92f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Two continuous 0 at the beginieng of the phone string should not be
93f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // treated as trunk prefix.
94153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("008001231234", "8001231234"));
95f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
96f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Test broken caller ID seen on call from Thailand to the US
97153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("+66811234567", "166811234567"));
98f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
99f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Confirm that the bug found before does not re-appear.
100153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678"));
101153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("650-000-3456", "16500003456"));
102153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("011 1 7005554141", "+17005554141"));
103153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("011 11 7005554141", "+17005554141"));
104153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("+44 207 792 3490", "00 207 792 3490"));
105b3b8a9dac508afdbac8de93a31a752c60e34e254Daisuke Miyakawa    // This is not related to Thailand case. NAMP "1" + region code "661".
106153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("16610001234", "6610001234"));
107f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
1080d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // We also need to compare two alpha addresses to make sure two different strings
1090d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // aren't treated as the same addresses. This is relevant to SMS as SMS sender may
1100d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // contain all alpha chars.
111153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("abcd", "bcde"));
1120d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang
1130d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // in the U.S. people often use alpha in the phone number to easily remember it
1140d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of
1150d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // phone number in Contacts and others, we should make sure the comparison method
1160d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // handle them.
117153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_TRUE(phone_number_compare_strict("1-800-flowers", "800-flowers"));
1180d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang
1190d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // TODO: we currently do not support this comparison. It maybe nice to support this
1200d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang    // TODO: in the future.
121153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    // EXPECT_TRUE("1-800-flowers", "1-800-356-9377")
1220d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang
123153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    EXPECT_FALSE(phone_number_compare_strict("1-800-flowers", "1-800-abcdefg"));
1240d04c4caa9c0464f555eed458abf2ac7d997ad7dWei Huang
125f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // Currently we cannot get this test through (Japanese trunk prefix is 0,
126f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa    // but there is no sensible way to know it now (as of 2009-6-12)...
127153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    // EXPECT_FALSE("290-1234-5678", "+819012345678");
128153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes}
129153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
130153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott HughesTEST(PhoneNumberUtils, phone_number_stripped_reversed_inter) {
131153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    char out[6];
132153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    int outlen;
133153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
134153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes#define ASSERT_STRIPPED_REVERSE(input, expected) \
135153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    phone_number_stripped_reversed_inter((input), out, sizeof(out), &outlen); \
136153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    out[outlen] = 0; \
137153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes    ASSERT_STREQ((expected), (out)); \
138f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa
13937214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("", "");
140153c102e5ba0ff0e8df7a862c8f4e449007d04adElliott Hughes
14137214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("123", "321");
14237214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("123*N#", "#N*321");
14337214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov
14437214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    // Buffer overflow
14537214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("1234567890", "098765");
14637214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov
14737214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    // Only one plus is copied
14837214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("1+2+", "+21");
14937214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov
15037214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    // Pause/wait in the phone number
15137214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("12;34", "21");
15237214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov
15337214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    // Ignoring non-dialable
15437214cd8af79110b7c8bd82855625287237d30ecDmitri Plotnikov    ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321");
155f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa}
156