aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-10-19 11:30:00 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-10-19 12:12:58 +0200
commit72e0603d7a185f83f7f53b7c880936e8f89d683e (patch)
tree0aad467801442a9fd84ea430a616812f476045b8 /test
parentIntegrate FilterTree into browser (#6) (diff)
downloadsmolbote-72e0603d7a185f83f7f53b7c880936e8f89d683e.tar.xz
Hostlist test: test non-empty domain
Add optional load list benchmark
Diffstat (limited to 'test')
-rw-r--r--test/hostlist/hostlisttest.cpp67
-rw-r--r--test/hostlist/hostlisttest.h6
2 files changed, 48 insertions, 25 deletions
diff --git a/test/hostlist/hostlisttest.cpp b/test/hostlist/hostlisttest.cpp
index cd1d39f..5270118 100644
--- a/test/hostlist/hostlisttest.cpp
+++ b/test/hostlist/hostlisttest.cpp
@@ -3,40 +3,59 @@
void HostlistTest::parseList()
{
- //FilterTree tree;
-
// load filters
QFile hostlist("hostlist.txt");
QCOMPARE(hostlist.open(QIODevice::ReadOnly | QIODevice::Text), true);
QCOMPARE(loadHostlist(hostlist, &tree), true);
+ // hostlist filters are applied to all domains, so there should only be one branch
QCOMPARE(tree.branches().length(), 1);
}
+void HostlistTest::checkRules_data()
+{
+ QTest::addColumn<QUrl>("domain");
+ QTest::addColumn<QUrl>("request");
+ QTest::addColumn<int>("matches");
+ QTest::addColumn<FilterLeaf::Action>("action");
+
+ const QVector<QUrl> domains{ QUrl(), QUrl::fromUserInput("testdomain.host") };
+ for(const QUrl &domain : domains) {
+ QTest::newRow("block (1 domain per line)") << domain << QUrl::fromUserInput("blockeddomain.com") << 1 << FilterLeaf::Block;
+ QTest::newRow("block (2 domains per line #1)") << domain << QUrl::fromUserInput("blockeddomain.first") << 1 << FilterLeaf::Block;
+ QTest::newRow("block (2 domains per line #2)") << domain << QUrl::fromUserInput("blockeddomain.second") << 1 << FilterLeaf::Block;
+ QTest::newRow("redirect") << domain << QUrl::fromUserInput("localhost.localdomain") << 1 << FilterLeaf::Redirect;
+ QTest::newRow("domain not in hostlist") << domain << QUrl::fromUserInput("other.domain") << 0 << FilterLeaf::NotMatched;
+ }
+}
+
void HostlistTest::checkRules()
{
- // test block
- QVector<const FilterLeaf *> block = tree.match(QUrl(), QUrl::fromUserInput("blockeddomain.com"));
- QCOMPARE(block.length(), 1);
- QCOMPARE(block.constFirst()->action(), FilterLeaf::Block);
-
- // test redirect
- QVector<const FilterLeaf *> redirectResult = tree.match(QUrl(), QUrl::fromUserInput("localhost.localdomain"));
- QCOMPARE(redirectResult.length(), 1);
- QCOMPARE(redirectResult.at(0)->action(), FilterLeaf::Redirect);
- QCOMPARE(redirectResult.at(0)->redirect(), "127.0.0.1");
-
- // two domains on one line
- QVector<const FilterLeaf *> blockFirst = tree.match(QUrl(), QUrl::fromUserInput("blockeddomain.first"));
- QCOMPARE(blockFirst.length(), 1);
- QCOMPARE(blockFirst.constFirst()->action(), FilterLeaf::Block);
- QVector<const FilterLeaf *> blockSecond = tree.match(QUrl(), QUrl::fromUserInput("blockeddomain.second"));
- QCOMPARE(blockSecond.length(), 1);
- QCOMPARE(blockSecond.constFirst()->action(), FilterLeaf::Block);
-
- // domain not on list
- QVector<const FilterLeaf *> missing = tree.match(QUrl(), QUrl::fromUserInput("other.domain"));
- QCOMPARE(missing.length(), 0);
+ QFETCH(QUrl, domain);
+ QFETCH(QUrl, request);
+ QFETCH(int, matches);
+ QFETCH(FilterLeaf::Action, action);
+
+ auto result = tree.match(domain, request);
+ QCOMPARE(result.length(), matches);
+ if(matches > 0)
+ QCOMPARE(result.constFirst()->action(), action);
+ if(action == FilterLeaf::Redirect)
+ QCOMPARE(result.constFirst()->redirect(), QLatin1Literal("127.0.0.1"));
+}
+
+void HostlistTest::benchmark_parse()
+{
+ QFile hostlist("hostlist-benchmark.txt");
+ if(hostlist.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ FilterTree benchmarkTree;
+ bool loaded;
+ QBENCHMARK {
+ loaded = loadHostlist(hostlist, &benchmarkTree);
+ }
+ QCOMPARE(loaded, true);
+ hostlist.close();
+ }
}
QTEST_GUILESS_MAIN(HostlistTest)
diff --git a/test/hostlist/hostlisttest.h b/test/hostlist/hostlisttest.h
index 9a87e0d..96051a9 100644
--- a/test/hostlist/hostlisttest.h
+++ b/test/hostlist/hostlisttest.h
@@ -1,8 +1,8 @@
#ifndef HOSTLIST_TEST
#define HOSTLIST_TEST
-#include <QObject>
#include "filtertree.h"
+#include <QObject>
class HostlistTest : public QObject
{
@@ -10,8 +10,12 @@ class HostlistTest : public QObject
private slots:
void parseList();
+
+ void checkRules_data();
void checkRules();
+ void benchmark_parse();
+
private:
FilterTree tree;
};