File indexing completed on 2024-04-14 04:50:34

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "cookiestest.h"
0019 #include "autotests.h"
0020 #include "datapaths.h"
0021 #include "settings.h"
0022 
0023 #include <QDir>
0024 
0025 void CookiesTest::initTestCase()
0026 {
0027     m_cookieJar = new CookieJar_Tst;
0028 }
0029 
0030 void CookiesTest::cleanupTestCase()
0031 {
0032     delete m_cookieJar;
0033 }
0034 
0035 void CookiesTest::domainMatchingTest_data()
0036 {
0037     QTest::addColumn<QString>("cookieDomain");
0038     QTest::addColumn<QString>("siteDomain");
0039     QTest::addColumn<bool>("result");
0040 
0041     /* http://stackoverflow.com/questions/1062963/how-do-browser-cookie-domains-work
0042        1) Cookie with Domain=.example.com will be available for www.example.com
0043        2) Cookie with Domain=.example.com will be available for example.com
0044        3) Cookie with Domain=example.com will be converted to .example.com and thus will also be available for www.example.com
0045        4) Cookie with Domain=example.com will not be available for anotherexample.com
0046     */
0047 
0048     QTest::newRow("test1") << ".example.com" << "www.example.com" << true;
0049     QTest::newRow("test2") << ".example.com" << "example.com" << true;
0050     QTest::newRow("test3") << "example.com" << "www.example.com" << true;
0051     QTest::newRow("test4") << ".example.com" << "anotherexample.com" << false;
0052     QTest::newRow("test5") << "test.example.com" << "example.com" << false;
0053     QTest::newRow("test6") << ".www.example.com" << "www.example.com" << true;
0054     QTest::newRow("test7") << ".www.example.com" << "example.com" << false;
0055     QTest::newRow("test_empty") << ".www.example.com" << "" << false;
0056     QTest::newRow("test_empty2") << "" << "example.com" << false;
0057 }
0058 
0059 void CookiesTest::domainMatchingTest()
0060 {
0061     QFETCH(QString, cookieDomain);
0062     QFETCH(QString, siteDomain);
0063     QFETCH(bool, result);
0064 
0065     QCOMPARE(m_cookieJar->matchDomain(cookieDomain, siteDomain), result);
0066 }
0067 
0068 void CookiesTest::listMatchesDomainTest_data()
0069 {
0070     QTest::addColumn<QStringList>("list");
0071     QTest::addColumn<QString>("cookieDomain");
0072     QTest::addColumn<bool>("result");
0073 
0074     QStringList list;
0075     list << QSL("www.example.com") << QSL("accounts.google.com");
0076     QStringList list2;
0077     list2 << QSL("anotherexample.com") << QSL("a.b.x.google.com");
0078 
0079     QTest::newRow("test1") << list << ".www.example.com" << true;
0080     QTest::newRow("test2") << list << ".google.com" << false;
0081     QTest::newRow("test3") << list << ".accounts.google.com" << true;
0082     QTest::newRow("test4") << list << ".example.com" << false;
0083     QTest::newRow("test5") << list2 << "example.com" << false;
0084     QTest::newRow("test6") << list2 << "tst.anotherexample.com" << true;
0085     QTest::newRow("test7") << list2 << "b.x.google.com" << false;
0086     QTest::newRow("test8") << list2 << "c.a.b.x.google.com" << true;
0087     QTest::newRow("test9") << list2 << ".a.b.x.google.com" << true;
0088     QTest::newRow("test_empty") << list2 << "" << false;
0089 }
0090 
0091 void CookiesTest::listMatchesDomainTest()
0092 {
0093     QFETCH(QStringList, list);
0094     QFETCH(QString, cookieDomain);
0095     QFETCH(bool, result);
0096 
0097     QCOMPARE(m_cookieJar->listMatchesDomain(list, cookieDomain), result);
0098 }
0099 
0100 FALKONTEST_MAIN(CookiesTest)