File indexing completed on 2024-05-05 04:58:59

0001 /**
0002  * SPDX-FileCopyrightText: 2019 Simon Redman <simon@ergotech.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "smsapp/smshelper.h"
0008 
0009 #include <QtTest>
0010 
0011 /**
0012  * This class tests the working of device class
0013  */
0014 class SmsHelperTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private Q_SLOTS:
0019 
0020     void testSimplePhoneMatch();
0021     void testMessyPhoneMatch();
0022     void testMissingCountryCode();
0023     void testLeadingZeros();
0024     void testLongDistancePhoneNumber();
0025     void testShortCodePhoneNumberNonMatch();
0026     void testShortCodeMatch();
0027     void testShortCodeNonMatch();
0028     void testAustralianShortCodeNumberNonMatch();
0029     void testAustralianShortCodeMatch();
0030     void testAustralianShortCodeNonMatch();
0031     void testCzechRepublicShortCodeNumberNonMatch();
0032     void testCzechRepublicShortCodeMatch();
0033     void testCzechRepublicShortCodeNonMatch();
0034     void testDifferentPhoneNumbers1();
0035     void testDifferentPhoneNumbers2();
0036     void testAllZeros();
0037     void testEmptyInput();
0038 };
0039 
0040 /**
0041  * Two phone numbers which are exactly the same should match
0042  */
0043 void SmsHelperTest::testSimplePhoneMatch()
0044 {
0045     const QString &phoneNumber = QLatin1String("+1 (222) 333-4444");
0046 
0047     QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, phoneNumber), "Failed to match a phone number with itself");
0048 }
0049 
0050 /**
0051  * Two phone numbers which have been entered with different formatting should match
0052  */
0053 void SmsHelperTest::testMessyPhoneMatch()
0054 {
0055     const QString &phoneNumber = QLatin1String("12223334444");
0056     const QString &messyPhoneNumber = QLatin1String("+1 (222) 333-4444");
0057 
0058     QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, messyPhoneNumber), "Failed to match same number with different formatting characters");
0059 }
0060 
0061 /**
0062  * I don't think most people in the US even know what a country code *is*, and I know lots of people
0063  * who don't enter it into their contacts book here. Make sure that kind of match works.
0064  */
0065 void SmsHelperTest::testMissingCountryCode()
0066 {
0067     const QString &shortForm = QLatin1String("(222) 333-4444");
0068     const QString &longForm = QLatin1String("+1 (222) 333-4444");
0069 
0070     QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm), "Failed to match two same numbers with missing country code");
0071 }
0072 
0073 /**
0074  * I don't quite understand which cases this applies, but sometimes you see a message where the
0075  * country code has been prefixed with a few zeros. Make sure that works too.
0076  */
0077 void SmsHelperTest::testLeadingZeros()
0078 {
0079     const QString &shortForm = QLatin1String("+1 (222) 333-4444");
0080     const QString &zeroForm = QLatin1String("001 (222) 333-4444");
0081 
0082     QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, zeroForm), "Failed to match two same numbers with padded zeros");
0083 }
0084 
0085 /**
0086  * At least on my phone, it is possible to leave the area code and country code off but still have
0087  * the phone call or text message go through. Some people's contacts books might be this way, so make
0088  * sure we support matching that too
0089  */
0090 void SmsHelperTest::testLongDistancePhoneNumber()
0091 {
0092     const QString &shortForm = QLatin1String("333-4444");
0093     const QString &longForm = QLatin1String("+1 (222) 333-4444");
0094 
0095     QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm), "Failed to suffix match two phone numbers");
0096 }
0097 
0098 /**
0099  * Phone operators define short codes for a variety of reasons. Even if they have the same suffix,
0100  * they should not match a regular phone number
0101  */
0102 void SmsHelperTest::testShortCodePhoneNumberNonMatch()
0103 {
0104     const QString &shortCode = QLatin1String("44455");
0105     const QString &normalNumber = QLatin1String("222-334-4455");
0106 
0107     QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode, normalNumber), "Short code matched with normal number");
0108 }
0109 
0110 /**
0111  * Two of the same short code should be able to match
0112  */
0113 void SmsHelperTest::testShortCodeMatch()
0114 {
0115     const QString &shortCode = QLatin1String("44455");
0116     QVERIFY2(SmsHelper::isPhoneNumberMatch(shortCode, shortCode), "Did not match two of the same short code");
0117 }
0118 
0119 /**
0120  * Two different short codes should not match
0121  */
0122 void SmsHelperTest::testShortCodeNonMatch()
0123 {
0124     const QString &shortCode1 = QLatin1String("44455");
0125     const QString &shortCode2 = QLatin1String("66677");
0126     QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode1, shortCode2), "Incorrectly matched two different short codes");
0127 }
0128 
0129 /**
0130  * Even in the land down under, a short code phone number should not match a regular phone number
0131  */
0132 void SmsHelperTest::testAustralianShortCodeNumberNonMatch()
0133 {
0134     const QString &australianShortCode = QLatin1String("19333444");
0135     // I'm not sure if this is even a valid Australian phone number, but whatever...
0136     const QString &australianPhoneNumber = QLatin1String("+41 02 1233 3444");
0137 
0138     QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode, australianPhoneNumber), "Matched Australian short code with regular phone number");
0139 }
0140 
0141 /**
0142  * Two of the same Australian short code numbers should be able to match
0143  */
0144 void SmsHelperTest::testAustralianShortCodeMatch()
0145 {
0146     const QString &australianShortCode = QLatin1String("12333444");
0147 
0148     QVERIFY2(SmsHelper::isPhoneNumberMatch(australianShortCode, australianShortCode), "Failed to match Australian short code number");
0149 }
0150 
0151 /**
0152  * Two different Australian short code numbers should be able to match
0153  */
0154 void SmsHelperTest::testAustralianShortCodeNonMatch()
0155 {
0156     const QString &australianShortCode1 = QLatin1String("12333444");
0157     const QString &australianShortCode2 = QLatin1String("12555666");
0158 
0159     QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode1, australianShortCode2), "Matched two different Australian short code numbers");
0160 }
0161 
0162 /**
0163  * A Czech Republic short code phone number should not match a regular phone number
0164  */
0165 void SmsHelperTest::testCzechRepublicShortCodeNumberNonMatch()
0166 {
0167     const QString &czechRepShortCode = QLatin1String("9090930");
0168     // I'm not sure if this is even a valid Czech Republic phone number, but whatever...
0169     const QString &czechRepPhoneNumber = QLatin1String("+420 809 090 930");
0170 
0171     QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepPhoneNumber), "Matched Czech Republic short code with regular phone number");
0172 }
0173 
0174 /**
0175  * Two of the same Czech Republic short code numbers should be able to match
0176  */
0177 void SmsHelperTest::testCzechRepublicShortCodeMatch()
0178 {
0179     const QString &czechRepShortCode = QLatin1String("9090930");
0180 
0181     QVERIFY2(SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepShortCode), "Failed to match Czech Republic short code number");
0182 }
0183 
0184 /**
0185  * Two different Czech Republic short code numbers should be able to match
0186  */
0187 void SmsHelperTest::testCzechRepublicShortCodeNonMatch()
0188 {
0189     const QString &czechRepShortCode1 = QLatin1String("9090930");
0190     const QString &czechRepShortCode2 = QLatin1String("9080990");
0191 
0192     QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode1, czechRepShortCode2), "Matched two different Czech Republic short code numbers");
0193 }
0194 
0195 /**
0196  * Two phone numbers which are different should not be reported as the same
0197  */
0198 void SmsHelperTest::testDifferentPhoneNumbers1()
0199 {
0200     const QString &phone1 = QLatin1String("+1 (222) 333-4444");
0201     const QString &phone2 = QLatin1String("+1 (333) 222-4444");
0202 
0203     QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2), "Incorrectly marked two different numbers as same");
0204 }
0205 
0206 /**
0207  * Two phone numbers which are different should not be reported as the same
0208  */
0209 void SmsHelperTest::testDifferentPhoneNumbers2()
0210 {
0211     const QString &phone1 = QLatin1String("+1 (222) 333-4444");
0212     const QString &phone2 = QLatin1String("122-2333");
0213 
0214     QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2), "Incorrectly *prefix* matched two phone numbers");
0215 }
0216 
0217 /**
0218  * Some places allow a message with all zeros to be a short code. We should allow that too.
0219  */
0220 void SmsHelperTest::testAllZeros()
0221 {
0222     const QString &zeros = QLatin1String("00000");
0223     const QString &canonicalized = SmsHelper::canonicalizePhoneNumber(zeros);
0224 
0225     QCOMPARE(canonicalized.length(), zeros.length());
0226 }
0227 
0228 /**
0229  * An empty string is not a valid phone number and should not match anything
0230  */
0231 void SmsHelperTest::testEmptyInput()
0232 {
0233     const QString &empty = QLatin1String("");
0234     const QString &shortCode = QLatin1String("44455");
0235     const QString &realNumber = QLatin1String("12223334444");
0236 
0237     QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, shortCode), "The empty string matched a shortcode phone number");
0238     QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, realNumber), "The empty string matched a real phone number");
0239 }
0240 
0241 QTEST_GUILESS_MAIN(SmsHelperTest);
0242 #include "smshelpertest.moc"