Block matching empty phone numbers

This commit is contained in:
Simon Redman 2019-06-07 16:39:05 -06:00
parent d4508cd6df
commit 3c346b9409
2 changed files with 19 additions and 0 deletions

View file

@ -28,6 +28,11 @@ Q_LOGGING_CATEGORY(KDECONNECT_SMS_SMSHELPER, "kdeconnect.sms.smshelper")
bool SmsHelper::isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2)
{
if (canonicalPhone1.isEmpty() || canonicalPhone2.isEmpty()) {
// The empty string is not a valid phone number so does not match anything
return false;
}
// To decide if a phone number matches:
// 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored
// 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string,

View file

@ -48,6 +48,7 @@ private Q_SLOTS:
void testDifferentPhoneNumbers1();
void testDifferentPhoneNumbers2();
void testAllZeros();
void testEmptyInput();
};
/**
@ -254,5 +255,18 @@ void SmsHelperTest::testAllZeros()
QCOMPARE(canonicalized.length(), zeros.length());
}
/**
* An empty string is not a valid phone number and should not match anything
*/
void SmsHelperTest::testEmptyInput()
{
const QString& empty = QLatin1String("");
const QString& shortCode = QLatin1String("44455");
const QString& realNumber = QLatin1String("12223334444");
QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, shortCode), "The empty string matched a shortcode phone number");
QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, realNumber), "The empty string matched a real phone number");
}
QTEST_MAIN(SmsHelperTest);
#include "testsmshelper.moc"