1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* Copyright 2014 Canonical Ltd.
*
* This file is part of webbrowser-app.
*
* webbrowser-app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* webbrowser-app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Qt
#include <QtCore/QString>
#include <QtTest/QtTest>
// local
#include "url-pattern-utils.h"
class ContainerUrlPatternsTests : public QObject
{
Q_OBJECT
private Q_SLOTS:
void transformedUrlPatterns_data()
{
QTest::addColumn<QString>("pattern");
QTest::addColumn<QString>("transformedPattern");
QTest::addColumn<bool>("doTransformUrlPath");
// regular patterns
QTest::newRow("Valid pattern")
<< "https?://*.mydomain.com/*"
<< "https?://[^\\./]*.mydomain.com/[^\\s]*"
<< true;
QTest::newRow("Valid pattern with no tail replacement")
<< "https?://*.mydomain.com/l.php\\?\\w+=([^&]+).*"
<< "https?://[^\\./]*.mydomain.com/l.php\\?\\w+=([^&]+).*"
<< false;
QTest::newRow("Valid pattern - short url")
<< "https?://mydomain.com/*"
<< "https?://mydomain.com/[^\\s]*" << true;
QTest::newRow("Valid pattern - strict url")
<< "https?://www.mydomain.com/*"
<< "https?://www.mydomain.com/[^\\s]*" << true;
#define WEBAPP_INVALID_URL_PATTERN_TEST(id,invalid_url_pattern) \
QTest::newRow("Invalid pattern " #id) \
<< invalid_url_pattern \
<< QString() << true
WEBAPP_INVALID_URL_PATTERN_TEST(1, "http");
WEBAPP_INVALID_URL_PATTERN_TEST(2, "://");
WEBAPP_INVALID_URL_PATTERN_TEST(3, "file://");
WEBAPP_INVALID_URL_PATTERN_TEST(4, "https?://");
WEBAPP_INVALID_URL_PATTERN_TEST(5, "https?://*");
WEBAPP_INVALID_URL_PATTERN_TEST(6, "https?://foo.*");
WEBAPP_INVALID_URL_PATTERN_TEST(7, "https?://foo.ba*r.com");
WEBAPP_INVALID_URL_PATTERN_TEST(8, "https?://foo.*.com/");
WEBAPP_INVALID_URL_PATTERN_TEST(9, "https?://foo.bar.*/");
WEBAPP_INVALID_URL_PATTERN_TEST(10, "https?://*.bar.*");
WEBAPP_INVALID_URL_PATTERN_TEST(11, "https?://*.bar.*/");
WEBAPP_INVALID_URL_PATTERN_TEST(12, "https?://*.bar.*/");
WEBAPP_INVALID_URL_PATTERN_TEST(13, "httpsfoo?://*.bar.com/");
WEBAPP_INVALID_URL_PATTERN_TEST(14, "httppoo://*.bar.com/");
#undef WEBAPP_INVALID_URL_PATTERN_TEST
// Google patterns
QTest::newRow("Valid Google pattern")
<< "https?://mail.google.*/*"
<< "https?://mail.google.[^\\./]*/[^\\s]*" << true;
QTest::newRow("Valid Google com SLD pattern")
<< "https?://mail.google.com.*/*"
<< "https?://mail.google.com.[^\\./]*/[^\\s]*" << true;
QTest::newRow("Valid Google co SLD pattern")
<< "https?://mail.google.co.*/*"
<< "https?://mail.google.co.[^\\./]*/[^\\s]*" << true;
QTest::newRow("Valid non Google pattern")
<< "https://*.google.com/*"
<< "https://[^\\./]*.google.com/[^\\s]*" << true;
#define WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(id,invalid_google_url_pattern) \
QTest::newRow("Invalid Google App pattern " #id) \
<< invalid_google_url_pattern \
<< QString() << true
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(1, "https://*.google.*/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(2, "https://service.gooo*gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(3, "https://service.gooo?gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(4, "https://service.goo*gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(5, "https://serv?ice.goo*gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(6, "https://se*rv?ice.goo*gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(7, "https://se*rvice.goo*gle.com/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(8, "https://se*rvice.goo*gle.*/*");
WEBAPP_INVALID_GOOGLE_URL_PATTERN_TEST(8, "https://service.google.kom.*/*");
}
void transformedUrlPatterns()
{
QFETCH(QString, pattern);
QFETCH(QString, transformedPattern);
QFETCH(bool, doTransformUrlPath);
QCOMPARE(UrlPatternUtils::transformWebappSearchPatternToSafePattern(pattern, doTransformUrlPath), transformedPattern);
}
void filteredUrlPatterns_data()
{
QTest::addColumn<QStringList>("patterns");
QTest::addColumn<QStringList>("filteredPattern");
// regular patterns
QTest::newRow("Patterns with empty ones")
<< (QStringList() << QString("https?://*.mydomain.com/*")
<< QString()
<< QString("https?://www.mydomain.com/*")
<< QString())
<< (QStringList() << QString("https?://[^\\./]*.mydomain.com/[^\\s]*")
<< QString("https?://www.mydomain.com/[^\\s]*"));
QTest::newRow("Patterns with invalid ones")
<< (QStringList() << QString("https?://*.mydomain.com/*")
<< QString()
<< QString("https?://*")
<< QString())
<< (QStringList() << QString("https?://[^\\./]*.mydomain.com/[^\\s]*"));
}
void filteredUrlPatterns()
{
QFETCH(QStringList, patterns);
QFETCH(QStringList, filteredPattern);
QCOMPARE(UrlPatternUtils::filterAndTransformUrlPatterns(patterns), filteredPattern);
}
};
QTEST_MAIN(ContainerUrlPatternsTests)
#include "tst_ContainerUrlPatternsTests.moc"
|