File indexing completed on 2024-07-21 12:06:18

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2004 David Faure <faure@kde.org>
0005     SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 // krazy:excludeall=contractions
0010 
0011 #include "kemailaddresstest.h"
0012 
0013 #include "../src/kemailaddress.h"
0014 
0015 #include <QDebug>
0016 #include <QUrl>
0017 
0018 #include <QTest>
0019 
0020 QTEST_MAIN(KEmailAddressTest)
0021 
0022 using namespace KEmailAddress;
0023 
0024 Q_DECLARE_METATYPE(EmailParseResult)
0025 
0026 void KEmailAddressTest::testGetNameAndEmail()
0027 {
0028     QFETCH(QString, input);
0029     QFETCH(QString, expName);
0030     QFETCH(QString, expEmail);
0031     QFETCH(bool, expRetVal);
0032 
0033     QString name;
0034     QString email;
0035     bool retVal = extractEmailAddressAndName(input, email, name);
0036     QCOMPARE(retVal, expRetVal);
0037     QCOMPARE(name, expName);
0038     QCOMPARE(email, expEmail);
0039 }
0040 
0041 void KEmailAddressTest::testGetNameAndEmail_data()
0042 {
0043     QTest::addColumn<QString>("input");
0044     QTest::addColumn<QString>("expName");
0045     QTest::addColumn<QString>("expEmail");
0046     QTest::addColumn<bool>("expRetVal");
0047 
0048     QTest::newRow("Empty input") << QString() << QString() << QString() << false;
0049     QTest::newRow("Email only") << "faure@kde.org" << QString() << "faure@kde.org" << false;
0050     QTest::newRow("Normal case") << "David Faure <faure@kde.org>"
0051                                  << "David Faure"
0052                                  << "faure@kde.org" << true;
0053     QTest::newRow("Double-quotes 1") << "\"Faure, David\" <faure@kde.org>"
0054                                      << "Faure, David"
0055                                      << "faure@kde.org" << true;
0056     QTest::newRow("Double-quotes 2") << "<faure@kde.org> \"David Faure\""
0057                                      << "David Faure"
0058                                      << "faure@kde.org" << true;
0059     QTest::newRow("Parenthesis 1") << "faure@kde.org (David Faure)"
0060                                    << "David Faure"
0061                                    << "faure@kde.org" << true;
0062     QTest::newRow("Parenthesis 2") << "(David Faure) faure@kde.org"
0063                                    << "David Faure"
0064                                    << "faure@kde.org" << true;
0065     QTest::newRow("Parenthesis 3") << "My Name (me) <me@home.net>"
0066                                    << "My Name (me)"
0067                                    << "me@home.net" << true; // #93513
0068 
0069     // As per https://intevation.de/roundup/kolab/issue858
0070     QTest::newRow("Nested parenthesis") << "faure@kde.org (David (The Man) Faure)"
0071                                         << "David (The Man) Faure"
0072                                         << "faure@kde.org" << true;
0073     QTest::newRow("Double-quotes inside parenthesis 1") << "faure@kde.org (David \"Crazy\" Faure)"
0074                                                         << "David \"Crazy\" Faure"
0075                                                         << "faure@kde.org" << true;
0076     QTest::newRow("Double-quotes inside parenthesis 2") << "(David \"Crazy\" Faure) faure@kde.org"
0077                                                         << "David \"Crazy\" Faure"
0078                                                         << "faure@kde.org" << true;
0079     QTest::newRow("Parenthesis inside double-quotes 1") << "\"Faure (David)\" <faure@kde.org>"
0080                                                         << "Faure (David)"
0081                                                         << "faure@kde.org" << true;
0082     QTest::newRow("Parenthesis inside double-quotes 2") << "<faure@kde.org> \"Faure (David)\""
0083                                                         << "Faure (David)"
0084                                                         << "faure@kde.org" << true;
0085     QTest::newRow("Space in email") << "David Faure < faure@kde.org >"
0086                                     << "David Faure"
0087                                     << "faure@kde.org" << true;
0088     QTest::newRow("'@' in name 1") << "faure@kde.org (a@b)"
0089                                    << "a@b"
0090                                    << "faure@kde.org" << true;
0091     QTest::newRow("'@' in name 2") << "\"a@b\" <faure@kde.org>"
0092                                    << "a@b"
0093                                    << "faure@kde.org" << true;
0094 
0095     // While typing, when there's no '@' yet
0096     QTest::newRow("while typing 1") << "foo"
0097                                     << "foo" << QString() << false;
0098     QTest::newRow("while typing 2") << "foo <"
0099                                     << "foo" << QString() << false;
0100     QTest::newRow("while typing 3") << "foo <b"
0101                                     << "foo"
0102                                     << "b" << true;
0103 
0104     // If multiple emails are there, only return the first one
0105     QTest::newRow("multiple emails") << "\"Faure, David\" <faure@kde.org>, KHZ <khz@khz.khz>"
0106                                      << "Faure, David"
0107                                      << "faure@kde.org" << true;
0108 
0109     QTest::newRow("domain literals") << "Matt Douhan <matt@[123.123.123.123]>"
0110                                      << "Matt Douhan"
0111                                      << "matt@[123.123.123.123]" << true;
0112     QTest::newRow("@ inside the comment") << "\"Matt@Douhan\" <matt@fruitsalad.org>"
0113                                           << "Matt@Douhan"
0114                                           << "matt@fruitsalad.org" << true;
0115     QTest::newRow("No '@'") << "foo <distlist>"
0116                             << "foo"
0117                             << "distlist" << true;
0118     QTest::newRow("Backslash in display name") << "\"Lastname\\, Firstname\""
0119                                                   " <firstname@lastname.com>"
0120                                                << "Lastname, Firstname"
0121                                                << "firstname@lastname.com" << true;
0122     QTest::newRow("# in domain") << "Matt Douhan <dm3tt@db0zdf.#rpl.deu.eu>"
0123                                  << "Matt Douhan"
0124                                  << "dm3tt@db0zdf.#rpl.deu.eu" << true;
0125 }
0126 
0127 void KEmailAddressTest::testIsValidEmailAddress()
0128 {
0129     QFETCH(QString, input);
0130     QFETCH(EmailParseResult, expErrorCode);
0131 
0132     QCOMPARE(isValidAddress(input), expErrorCode);
0133 }
0134 
0135 void KEmailAddressTest::testIsValidEmailAddress_data()
0136 {
0137     QTest::addColumn<QString>("input");
0138     QTest::addColumn<EmailParseResult>("expErrorCode");
0139 
0140     // Too many @'s
0141     QTest::newRow("1") << "matt@@fruitsalad.org" << TooManyAts;
0142 
0143     // Too few @'s
0144     QTest::newRow("2") << "mattfruitsalad.org" << TooFewAts;
0145 
0146     // An empty string
0147     QTest::newRow("3") << QString() << AddressEmpty;
0148 
0149     // email address starting with a @
0150     QTest::newRow("4") << "@mattfruitsalad.org" << MissingLocalPart;
0151 
0152     // make sure that starting @ and an additional @ in the same email address don't conflict
0153     // trap the starting @ first and break
0154     QTest::newRow("5") << "@matt@fruitsalad.org" << MissingLocalPart;
0155 
0156     // email address ending with a @
0157     QTest::newRow("6") << "mattfruitsalad.org@" << MissingDomainPart;
0158 
0159     // make sure that ending with@ and an additional @ in the email address don't conflict
0160     QTest::newRow("7") << "matt@fruitsalad.org@" << MissingDomainPart;
0161 
0162     // unbalanced Parens
0163     QTest::newRow("8") << "mattjongel)@fruitsalad.org" << UnbalancedParens;
0164 
0165     // unbalanced Parens the other way around
0166     QTest::newRow("9") << "mattjongel(@fruitsalad.org" << UnbalancedParens;
0167 
0168     // Correct parens just to make sure it works
0169     QTest::newRow("10") << "matt(jongel)@fruitsalad.org" << AddressOk;
0170 
0171     // Check that anglebrackets are closed
0172     QTest::newRow("11") << "matt douhan<matt@fruitsalad.org" << UnclosedAngleAddr;
0173 
0174     // Check that angle brackets are closed the other way around
0175     QTest::newRow("12") << "matt douhan>matt@fruitsalad.org" << UnopenedAngleAddr;
0176 
0177     // Check that angle brackets are closed the other way around, and anglebrackets in domainpart
0178     // instead of local part
0179     QTest::newRow("13") << "matt douhan matt@<fruitsalad.org" << UnclosedAngleAddr;
0180 
0181     // check that a properly formatted anglebrackets situation is OK
0182     QTest::newRow("14") << "matt douhan<matt@fruitsalad.org>" << AddressOk;
0183 
0184     // a full email address with comments angle brackets and the works should be valid too
0185     QTest::newRow("15") << "Matt (jongel) Douhan <matt@fruitsalad.org>" << AddressOk;
0186 
0187     // Double quotes
0188     QTest::newRow("16") << "\"Matt Douhan\" <matt@fruitsalad.org>" << AddressOk;
0189 
0190     // Double quotes inside parens
0191     QTest::newRow("17") << "Matt (\"jongel\") Douhan <matt@fruitsalad.org>" << AddressOk;
0192 
0193     // DOuble quotes not closed
0194     QTest::newRow("18") << "Matt \"jongel Douhan <matt@fruitsalad.org>" << UnbalancedQuote;
0195 
0196     // Parens inside double quotes
0197     QTest::newRow("19") << "Matt \"(jongel)\" Douhan <matt@fruitsalad.org>" << AddressOk;
0198 
0199     // Space in email
0200     QTest::newRow("20") << "Matt Douhan < matt@fruitsalad.org >" << AddressOk;
0201 
0202     // @ is allowed inisde doublequotes
0203     QTest::newRow("21") << "\"matt@jongel\" <matt@fruitsalad.org>" << AddressOk;
0204 
0205     // anglebrackets inside dbl quotes
0206     QTest::newRow("22") << "\"matt<blah blah>\" <matt@fruitsalad.org>" << AddressOk;
0207 
0208     // a , inside a double quoted string is OK, how do I know this? well Ingo says so
0209     // and it makes sense since it is also a separator of email addresses
0210     QTest::newRow("23") << "\"Douhan, Matt\" <matt@fruitsalad.org>" << AddressOk;
0211 
0212     // Domains literals also need to work
0213     QTest::newRow("24") << "Matt Douhan <matt@[123.123.123.123]>" << AddressOk;
0214 
0215     // Typo in domain literal address
0216     QTest::newRow("25") << "Matt Douhan <matt@[123.123.123,123]>" << UnexpectedComma;
0217 
0218     // Some more insane tests but still valid so they must work
0219     QTest::newRow("26") << "Matt Douhan <\"m@att\"@jongel.com>" << AddressOk;
0220 
0221     // BUG 99657
0222     QTest::newRow("27") << "matt@jongel.fibbel.com" << AddressOk;
0223 
0224     // BUG 98720
0225     QTest::newRow("28") << "mailto:@mydomain" << DisallowedChar;
0226 
0227     // correct error msg when a comma is inside <>
0228     QTest::newRow("29") << "Matt Douhan <matt@fruitsalad,org>" << UnexpectedComma;
0229 
0230     // several commentlevels
0231     QTest::newRow("30") << "Matt Douhan (hey(jongel)fibbel) <matt@fruitsalad.org>" << AddressOk;
0232 
0233     // several comment levels and one (the outer) being unbalanced
0234     QTest::newRow("31") << "Matt Douhan (hey(jongel)fibbel <matt@fruitsalad.org>" << UnbalancedParens;
0235 
0236     // several comment levels and one (the inner) being unbalanced
0237     QTest::newRow("32") << "Matt Douhan (hey(jongelfibbel) <matt@fruitsalad.org>" << UnbalancedParens;
0238 
0239     // an error inside a double quote is no error
0240     QTest::newRow("33") << "Matt Douhan \"(jongel\" <matt@fruitsalad.org>" << AddressOk;
0241 
0242     // inside a quoted string double quotes are only allowed in pairs as per rfc2822
0243     QTest::newRow("34") << "Matt Douhan \"jongel\"fibbel\" <matt@fruitsalad.org>" << UnbalancedQuote;
0244 
0245     // a questionmark is valid in an atom
0246     QTest::newRow("35") << "Matt? <matt@fruitsalad.org>" << AddressOk;
0247 
0248     // weird but OK
0249     QTest::newRow("36") << "\"testing, \\\"testing\" <matt@fruitsalad.org>" << AddressOk;
0250 
0251     // escape a quote to many to see if it makes it invalid
0252     QTest::newRow("37") << "\"testing, \\\"testing\\\" <matt@fruitsalad.org>" << UnbalancedQuote;
0253 
0254     // escape a parens and thus make a comma appear
0255     QTest::newRow("38") << "Matt (jongel, fibbel\\) <matt@fruitsalad.org>" << UnbalancedParens;
0256 
0257     // several errors inside doublequotes
0258     QTest::newRow("39") << "Matt \"(jongel,\\\" < fibbel\\)\" <matt@fruitsalad.org>" << AddressOk;
0259 
0260     // BUG 105705
0261     QTest::newRow("40") << "matt-@fruitsalad.org" << AddressOk;
0262 
0263     // underscore at the end of local part
0264     QTest::newRow("41") << "matt_@fruitsalad.org" << AddressOk;
0265 
0266     // how about ( comment ) in the domain part
0267     QTest::newRow("42") << "matt_@(this is a cool host)fruitsalad.org" << AddressOk;
0268 
0269     // To quote rfc2822 the test below is aesthetically displeasing, but perfectly legal.
0270     QTest::newRow("43") << "Pete(A wonderful \\) chap) <pete(his account)@silly.test(his host)>" << AddressOk;
0271 
0272     // quoted pair or not quoted pair
0273     QTest::newRow("44") << "\"jongel '\\\" fibbel\" <matt@fruitsalad.org>" << AddressOk;
0274     QTest::newRow("45") << "\"jongel '\" fibbel\" <matt@fruitsalad.org>" << UnbalancedQuote;
0275 
0276     // full atext support according to rfc2822
0277     QTest::newRow("46") << "!matt@fruitsalad.org" << AddressOk;
0278     QTest::newRow("47") << "#matt@fruitsalad.org" << AddressOk;
0279     QTest::newRow("48") << "$matt@fruitsalad.org" << AddressOk;
0280     QTest::newRow("49") << "%matt@fruitsalad.org" << AddressOk;
0281     QTest::newRow("50") << "&matt@fruitsalad.org" << AddressOk;
0282     QTest::newRow("51") << "'matt@fruitsalad.org" << AddressOk;
0283     QTest::newRow("52") << "*matt@fruitsalad.org" << AddressOk;
0284     QTest::newRow("53") << "+matt@fruitsalad.org" << AddressOk;
0285     QTest::newRow("54") << "/matt@fruitsalad.org" << AddressOk;
0286     QTest::newRow("55") << "=matt@fruitsalad.org" << AddressOk;
0287     QTest::newRow("56") << "?matt@fruitsalad.org" << AddressOk;
0288     QTest::newRow("57") << "^matt@fruitsalad.org" << AddressOk;
0289     QTest::newRow("58") << "_matt@fruitsalad.org" << AddressOk;
0290     QTest::newRow("59") << "-matt@fruitsalad.org" << AddressOk;
0291     QTest::newRow("60") << "`matt@fruitsalad.org" << AddressOk;
0292     QTest::newRow("61") << "{matt@fruitsalad.org" << AddressOk;
0293     QTest::newRow("62") << "|matt@fruitsalad.org" << AddressOk;
0294     QTest::newRow("63") << "}matt@fruitsalad.org" << AddressOk;
0295     QTest::newRow("64") << "~matt@fruitsalad.org" << AddressOk;
0296     QTest::newRow("65") << "matt%matt@fruitsalad.org" << AddressOk;
0297 
0298     // bug 105405
0299     QTest::newRow("66") << "[foobar] <matt@fruitsalad.org>" << InvalidDisplayName;
0300     QTest::newRow("67") << "matt \"[foobar]\" Douhan <matt@fruitsalad.org>" << AddressOk;
0301 
0302     QTest::newRow("68") << "Matt Douhan <matt\"@@\"fruitsalad.org>" << TooFewAts;
0303 
0304     QTest::newRow("# in domain") << "dm3tt@db0zdf.#rpl.deu.eu" << AddressOk;
0305     QTest::newRow("dot at the end") << "msadmin@guug.de." << AddressOk;
0306     QTest::newRow("dot at the end with brackets") << "Martin Schulte <martin.schulte@guug.de.>" << AddressOk;
0307 
0308     // TODO this should be a valid email address, but the checking for missing dots broke it.
0309     // QTest::newRow( "valid email address without dots" ) << "user@localhost" << AddressOk;
0310 }
0311 
0312 void KEmailAddressTest::testIsValidAddressList()
0313 {
0314     QFETCH(QString, list);
0315     QFETCH(EmailParseResult, expErrorCode);
0316 
0317     QString badAddress;
0318     QCOMPARE(isValidAddressList(list, badAddress), expErrorCode);
0319 }
0320 
0321 void KEmailAddressTest::testIsValidAddressList_data()
0322 {
0323     QTest::addColumn<QString>("list");
0324     QTest::addColumn<EmailParseResult>("expErrorCode");
0325 
0326     // bug  139477
0327     QTest::newRow("1") << "martin.schulte@guug.de, msadmin@guug.de,"
0328                           " msnewsletter@guug.de"
0329                        << AddressOk;
0330     QTest::newRow("2") << "martin.schulte@guug.de; msadmin@guug.de;"
0331                           " msnewsletter@guug.de"
0332                        << AddressOk;
0333     QTest::newRow("3") << "martin.schulte@guug.de, msadmin@guug.de.,"
0334                           " msnewsletter@guug.de"
0335                        << AddressOk;
0336     QTest::newRow("4") << "Martin Schulte <martin.schulte@guug.de>,"
0337                           " MS Admin <msadmin@guug.de>, MS News <msnewsletter@guug.de>"
0338                        << AddressOk;
0339     QTest::newRow("5") << "Martin Schulte <martin.schulte@guug.de>;"
0340                           " MS Admin <msadmin@guug.de>; MS News <msnewsletter@guug.de>"
0341                        << AddressOk;
0342     QTest::newRow("6") << "Martin Schulte <martin.schulte@guug.de.>,"
0343                           " MS Admin <msadmin@guug.de>, MS News <msnewsletter@guug.de>"
0344                        << AddressOk;
0345 }
0346 
0347 void KEmailAddressTest::testIsValidSimpleEmailAddress()
0348 {
0349     QFETCH(QString, input);
0350     QFETCH(bool, expResult);
0351 
0352     QCOMPARE(isValidSimpleAddress(input), expResult);
0353 }
0354 
0355 void KEmailAddressTest::testIsValidSimpleEmailAddress_data()
0356 {
0357     QTest::addColumn<QString>("input");
0358     QTest::addColumn<bool>("expResult");
0359 
0360     // checks for "pure" email addresses in the form of xxx@yyy.tld
0361     QTest::newRow("") << "matt@fruitsalad.org" << true;
0362     QTest::newRow("") << QStringLiteral("test@täst.invalid") << true;
0363     // non-ASCII char as first char of IDN
0364     QTest::newRow("") << QStringLiteral("i_want@øl.invalid") << true;
0365     QTest::newRow("") << "matt@[123.123.123.123]" << true;
0366     QTest::newRow("") << "matt@[3.3.3.3]" << true;
0367     QTest::newRow("") << "matt@[4.4.4.4]" << true;
0368     QTest::newRow("") << "matt@[192.168.254.254]" << true;
0369     QTest::newRow("") << "\"matt\"@fruitsalad.org" << true;
0370     QTest::newRow("") << "-matt@fruitsalad.org" << true;
0371     QTest::newRow("") << "\"-matt\"@fruitsalad.org" << true;
0372     QTest::newRow("") << "matt@jongel.fibbel.com" << true;
0373     QTest::newRow("") << "Matt Douhan <matt@fruitsalad.org>" << false;
0374     // BUG 105705
0375     QTest::newRow("") << "matt-@fibbel.com" << true;
0376     QTest::newRow("") << "matt@fibbel-is-a-geek.com" << true;
0377     QTest::newRow("") << "matt_@fibbel.com" << true;
0378     // Check the defined chars for atext according to rfc2822
0379     QTest::newRow("") << "!matt@fruitsalad.org" << true;
0380     QTest::newRow("") << "#matt@fruitsalad.org" << true;
0381     QTest::newRow("") << "$matt@fruitsalad.org" << true;
0382     QTest::newRow("") << "%matt@fruitsalad.org" << true;
0383     QTest::newRow("") << "&matt@fruitsalad.org" << true;
0384     QTest::newRow("") << "'matt@fruitsalad.org" << true;
0385     QTest::newRow("") << "*matt@fruitsalad.org" << true;
0386     QTest::newRow("") << "+matt@fruitsalad.org" << true;
0387     QTest::newRow("") << "/matt@fruitsalad.org" << true;
0388     QTest::newRow("") << "=matt@fruitsalad.org" << true;
0389     QTest::newRow("") << "?matt@fruitsalad.org" << true;
0390     QTest::newRow("") << "^matt@fruitsalad.org" << true;
0391     QTest::newRow("") << "_matt@fruitsalad.org" << true;
0392     QTest::newRow("") << "-matt@fruitsalad.org" << true;
0393     QTest::newRow("") << "`matt@fruitsalad.org" << true;
0394     QTest::newRow("") << "{matt@fruitsalad.org" << true;
0395     QTest::newRow("") << "|matt@fruitsalad.org" << true;
0396     QTest::newRow("") << "}matt@fruitsalad.org" << true;
0397     QTest::newRow("") << "~matt@fruitsalad.org" << true;
0398     // BUG 108476
0399     QTest::newRow("") << "foo+matt@fruitsalad.org" << true;
0400     QTest::newRow("") << "bar=matt@fruitsalad.org" << true;
0401     QTest::newRow("") << "jongel-matt@fruitsalad.org" << true;
0402     QTest::newRow("") << "matt-@fruitsalad.org" << true;
0403 
0404     // check if the pure email address is wrong
0405     QTest::newRow("") << "mattfruitsalad.org" << false;
0406     QTest::newRow("") << "matt@[123.123.123.123" << false;
0407     QTest::newRow("") << "matt@123.123.123.123]" << false;
0408     QTest::newRow("") << "matt@[.123.123.123]" << false;
0409     QTest::newRow("") << "matt@[123.123.123]" << false;
0410     QTest::newRow("") << "\"matt@fruitsalad.org" << false;
0411     QTest::newRow("") << "matt\"@fruitsalad.org" << false;
0412     QTest::newRow("") << QString() << false;
0413 
0414     // BUG 203881
0415     QTest::newRow("") << "2advance@my-site.com" << true;
0416 
0417     // and here some insane but still valid cases
0418     QTest::newRow("") << "\"m@tt\"@fruitsalad.org" << true;
0419 
0420     QTest::newRow("") << "matt\"@@\"fruitsalad.org" << false;
0421     QTest::newRow("# in domain") << "dm3tt@db0zdf.#rpl.deu.eu" << true;
0422 
0423     // add tests for missing local/domain parts
0424     QTest::newRow("") << "@mattfruitsalad.org" << false;
0425     QTest::newRow("") << "matt@" << false;
0426     QTest::newRow("") << "@" << false;
0427 }
0428 
0429 void KEmailAddressTest::testGetEmailAddress()
0430 {
0431     QFETCH(QString, input);
0432     QFETCH(QString, expResult);
0433 
0434     QCOMPARE(extractEmailAddress(input), expResult);
0435 }
0436 
0437 void KEmailAddressTest::testGetEmailAddress_data()
0438 {
0439     QTest::addColumn<QString>("input");
0440     QTest::addColumn<QString>("expResult");
0441 
0442     QTest::newRow("1") << "matt@fruitsalad.org"
0443                        << "matt@fruitsalad.org";
0444     QTest::newRow("2") << "Matt Douhan <matt@fruitsalad.org>"
0445                        << "matt@fruitsalad.org";
0446     QTest::newRow("3") << "\"Matt Douhan <blah blah>\" <matt@fruitsalad.org>"
0447                        << "matt@fruitsalad.org";
0448     QTest::newRow("4") << "\"Matt <blah blah>\" <matt@fruitsalad.org>"
0449                        << "matt@fruitsalad.org";
0450     QTest::newRow("5") << "Matt Douhan (jongel) <matt@fruitsalad.org" << QString();
0451     QTest::newRow("6") << "Matt Douhan (m@tt) <matt@fruitsalad.org>"
0452                        << "matt@fruitsalad.org";
0453     QTest::newRow("7") << "\"Douhan, Matt\" <matt@fruitsalad.org>"
0454                        << "matt@fruitsalad.org";
0455     QTest::newRow("8") << "\"Matt Douhan (m@tt)\" <matt@fruitsalad.org>"
0456                        << "matt@fruitsalad.org";
0457     QTest::newRow("9") << "\"Matt Douhan\" (matt <matt@fruitsalad.org>" << QString();
0458     QTest::newRow("10") << "Matt Douhan <matt@[123.123.123.123]>"
0459                         << "matt@[123.123.123.123]";
0460     QTest::newRow("11") << "dm3tt@db0zdf.#rpl.deu.eu"
0461                         << "dm3tt@db0zdf.#rpl.deu.eu";
0462 }
0463 
0464 void KEmailAddressTest::testCheckSplitEmailAddrList()
0465 {
0466     QFETCH(QString, input);
0467     QFETCH(QStringList, expResult);
0468 
0469     QCOMPARE(splitAddressList(input), expResult);
0470 }
0471 
0472 void KEmailAddressTest::testCheckSplitEmailAddrList_data()
0473 {
0474     QTest::addColumn<QString>("input");
0475     QTest::addColumn<QStringList>("expResult");
0476 
0477     QTest::newRow("") << "kloecker@kde.org (Kloecker, Ingo)" << (QStringList() << QStringLiteral("kloecker@kde.org (Kloecker, Ingo)"));
0478     QTest::newRow("") << "Matt Douhan <matt@fruitsalad.org>, Foo Bar <foo@bar.com>"
0479                       << (QStringList() << QStringLiteral("Matt Douhan <matt@fruitsalad.org>") << QStringLiteral("Foo Bar <foo@bar.com>"));
0480     QTest::newRow("") << "\"Matt, Douhan\" <matt@fruitsalad.org>, Foo Bar <foo@bar.com>"
0481                       << (QStringList() << QStringLiteral("\"Matt, Douhan\" <matt@fruitsalad.org>") << QStringLiteral("Foo Bar <foo@bar.com>"));
0482     QTest::newRow("") << "\"Lastname\\, Firstname\" <firstname.lastname@example.com>"
0483                       << (QStringList() << QStringLiteral("\"Lastname\\, Firstname\" <firstname.lastname@example.com>"));
0484     QTest::newRow("") << "First Collective Last <collectivename.lastname@example.com>" // Uses non-ASCII spacing
0485                       << (QStringList() << QStringLiteral("First Collective Last <collectivename.lastname@example.com>"));
0486     QTest::newRow("") << "Foo Bar <foo@bar.com>, First Collective Last <collectivename.lastname@example.com>" // Uses non-ASCII spacing
0487                       << (QStringList() << QStringLiteral("Foo Bar <foo@bar.com>")
0488                                         << QStringLiteral("First Collective Last <collectivename.lastname@example.com>"));
0489     QTest::newRow("")
0490         << "Foo  Bar <foo@bar.com>,  \"   First  Collective Last  \"  <collectivename.lastname@example.com>" // Uses multiple and quoted surrounding spaces
0491         << (QStringList() << QStringLiteral("Foo  Bar <foo@bar.com>")
0492                           << QStringLiteral("\"   First  Collective Last  \"  <collectivename.lastname@example.com>"));
0493 }
0494 
0495 void KEmailAddressTest::testNormalizeAddressesAndEncodeIDNs()
0496 {
0497     QFETCH(QString, input);
0498     QFETCH(QString, expResult);
0499 
0500     QCOMPARE(normalizeAddressesAndEncodeIdn(input), expResult);
0501 }
0502 
0503 void KEmailAddressTest::testNormalizeAddressesAndEncodeIDNs_data()
0504 {
0505     QTest::addColumn<QString>("input");
0506     QTest::addColumn<QString>("expResult");
0507 
0508     QTest::newRow("") << "matt@fruitsalad.org"
0509                       << "matt@fruitsalad.org";
0510     QTest::newRow("") << "Matt Douhan <matt@fruitsalad.org>"
0511                       << "Matt Douhan <matt@fruitsalad.org>";
0512     QTest::newRow("") << "Matt Douhan (jongel) <matt@fruitsalad.org>"
0513                       << "Matt Douhan (jongel) <matt@fruitsalad.org>";
0514     QTest::newRow("") << "Matt Douhan (jongel,fibbel) <matt@fruitsalad.org>"
0515                       << "Matt Douhan (jongel,fibbel) <matt@fruitsalad.org>";
0516     QTest::newRow("") << "matt@fruitsalad.org (jongel,fibbel)"
0517                       << "\"jongel,fibbel\" <matt@fruitsalad.org>";
0518     QTest::newRow("") << "matt@fruitsalad.org (\"jongel,fibbel\")"
0519                       << "\"jongel,fibbel\" <matt@fruitsalad.org>";
0520 
0521     QTest::newRow("") << "First Collective Last <collectivename.lastname@example.com>" // Uses non-ASCII spacing
0522                       << "First Collective Last <collectivename.lastname@example.com>";
0523     QTest::newRow("") << "Foo Bar <foo@bar.com> , First Collective Last <collectivename.lastname@example.com>" // Uses non-ASCII spacing
0524                       << "Foo Bar <foo@bar.com>, First Collective Last <collectivename.lastname@example.com>";
0525     QTest::newRow("")
0526         << "Foo  Bar  <foo@bar.com>,  \"   First  Collective Last  \"  <collectivename.lastname@example.com>" // Uses multiple and quoted surrounding spaces
0527         << "Foo  Bar <foo@bar.com>, \"   First  Collective Last  \" <collectivename.lastname@example.com>";
0528 }
0529 
0530 void KEmailAddressTest::testNormalizeAddressesAndDecodeIDNs()
0531 {
0532     QFETCH(QString, input);
0533     QFETCH(QString, expResult);
0534 
0535     QCOMPARE(normalizeAddressesAndDecodeIdn(input), expResult);
0536 }
0537 
0538 void KEmailAddressTest::testNormalizeAddressesAndDecodeIDNs_data()
0539 {
0540     QTest::addColumn<QString>("input");
0541     QTest::addColumn<QString>("expResult");
0542 
0543     QTest::newRow("") << "=?us-ascii?Q?Surname=2C=20Name?= <nobody@example.org>"
0544                       << "\"Surname, Name\" <nobody@example.org>";
0545 
0546     QTest::newRow("") << "=?iso-8859-1?B?5Hf8b2xmLPZBbmRyZWFz?= <nobody@example.org>" << QStringLiteral("\"äwüolf,öAndreas\" <nobody@example.org>");
0547 
0548     QTest::newRow("") << QStringLiteral("\"Andreas Straß\" <nobody@example.org>") << QStringLiteral("\"Andreas Straß\" <nobody@example.org>");
0549 
0550     QTest::newRow("") << QStringLiteral("\"András\" \"Manţia\" <amantia@kde.org>") << QStringLiteral("\"András\" \"Manţia\" <amantia@kde.org>");
0551 }
0552 
0553 void KEmailAddressTest::testQuoteIfNecessary()
0554 {
0555     QFETCH(QString, input);
0556     QFETCH(QString, expResult);
0557     QBENCHMARK {
0558         QCOMPARE(quoteNameIfNecessary(input), expResult);
0559     }
0560 }
0561 
0562 void KEmailAddressTest::testQuoteIfNecessary_data()
0563 {
0564     QTest::addColumn<QString>("input");
0565     QTest::addColumn<QString>("expResult");
0566 
0567     QTest::newRow("") << "Matt Douhan"
0568                       << "Matt Douhan";
0569     QTest::newRow("") << "Douhan, Matt"
0570                       << "\"Douhan, Matt\"";
0571     QTest::newRow("") << "Matt \"jongel\" Douhan"
0572                       << "\"Matt \\\"jongel\\\" Douhan\"";
0573     QTest::newRow("") << "Matt \\\"jongel\\\" Douhan"
0574                       << "\"Matt \\\"jongel\\\" Douhan\"";
0575     QTest::newRow("") << "trailing '\\\\' should never occur \\"
0576                       << "\"trailing '\\\\' should never occur \\\"";
0577     QTest::newRow("") << "\"don't quote again\""
0578                       << "\"don't quote again\"";
0579     QTest::newRow("") << "\"leading double quote"
0580                       << "\"\\\"leading double quote\"";
0581     QTest::newRow("") << "trailing double quote\""
0582                       << "\"trailing double quote\\\"\"";
0583 #if 0
0584     BEFORE: static QRegularExpression
0585       RESULT : KEmailAddressTest::testQuoteIfNecessary():
0586       0.015 msecs per iteration (total: 64, iterations: 4096)
0587       0.019 msecs per iteration (total: 80, iterations: 4096)
0588       0.019 msecs per iteration (total: 81, iterations: 4096)
0589       0.0244 msecs per iteration (total: 100, iterations: 4096)
0590       0.0079 msecs per iteration (total: 65, iterations: 8192)
0591       0.020 msecs per iteration (total: 82, iterations: 4096)
0592       0.020 msecs per iteration (total: 82, iterations: 4096)
0593 
0594       AFTER:
0595       RESULT : KEmailAddressTest::testQuoteIfNecessary():
0596       0.0097 msecs per iteration (total: 80, iterations: 8192)
0597       0.010 msecs per iteration (total: 82, iterations: 8192)
0598       0.015 msecs per iteration (total: 63, iterations: 4096)
0599       0.0076 msecs per iteration (total: 63, iterations: 8192)
0600       0.0098 msecs per iteration (total: 81, iterations: 8192)
0601       0.010 msecs per iteration (total: 87, iterations: 8192)
0602 #endif
0603 }
0604 
0605 void KEmailAddressTest::testMailtoUrls()
0606 {
0607     QFETCH(QString, input);
0608     const QUrl url = encodeMailtoUrl(input);
0609     qDebug() << url;
0610     QCOMPARE(url.scheme().toLatin1().data(), "mailto");
0611     QCOMPARE(decodeMailtoUrl(url), input);
0612     qDebug() << decodeMailtoUrl(url);
0613 }
0614 
0615 void KEmailAddressTest::testMailtoUrls_data()
0616 {
0617     QTest::addColumn<QString>("input");
0618 
0619     QTest::newRow("") << "tokoe@domain.com";
0620     QTest::newRow("") << QStringLiteral("\"Tobias König\" <tokoe@domain.com>");
0621     QTest::newRow("") << QStringLiteral("\"Alberto Simões\" <alberto@example.com");
0622     QTest::newRow("") << QStringLiteral("Alberto Simões <alberto@example.com");
0623 }
0624 
0625 #include "moc_kemailaddresstest.cpp"