File indexing completed on 2024-11-24 04:54:32

0001 /*
0002     SPDX-License-Identifier: MPL-2.0
0003 */
0004 
0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
0006  * This Source Code Form is subject to the terms of the Mozilla Public
0007  * License, v. 2.0. If a copy of the MPL was not distributed with this
0008  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
0009 
0010 #include <string>
0011 #include <iostream>
0012 #include "./CppUnitLite/TestHarness.h"
0013 #include "./CppUnitLite/Test.h"
0014 #include "./ad_block_client.h"
0015 
0016 TEST(preservesTag, basic) {
0017   const char * filterText =
0018     "filter$tag=blah\n";
0019   const char * currentPageDomain = "slashdot.org";
0020   const char * urlToCheck = "http://www.brianbondy.com/filter?c=a&view=ad&b=2";
0021 
0022   AdBlockClient client;
0023   client.parse(filterText, true);
0024   client.addTag("blah");
0025 
0026   Filter *filter;
0027   Filter *exceptionFilter;
0028 
0029   CHECK(client.findMatchingFilters(urlToCheck, FONoFilterOption,
0030     currentPageDomain, &filter, &exceptionFilter));
0031   CHECK(filter);
0032   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0033 
0034   int size;
0035   char* data = client.serialize(&size);
0036 
0037   AdBlockClient client2;
0038   client2.addTag("blah");
0039   client2.deserialize(data);
0040   // For valgrind only
0041   client2.deserialize(data);
0042 
0043   filter = nullptr;
0044   exceptionFilter = nullptr;
0045   CHECK(client2.findMatchingFilters(urlToCheck, FONoFilterOption,
0046     currentPageDomain, &filter, &exceptionFilter));
0047   CHECK(filter);
0048   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0049 }
0050 
0051 TEST(preservesTag, hostAnchored) {
0052   const char * filterText =
0053     "||www.brianbondy.com$tag=blah\n";
0054   const char * currentPageDomain = "slashdot.org";
0055   const char * urlToCheck = "http://www.brianbondy.com/filter?c=a&view=ad&b=2";
0056 
0057   AdBlockClient client;
0058   client.parse(filterText, true);
0059   client.addTag("blah");
0060 
0061   Filter *filter;
0062   Filter *exceptionFilter;
0063 
0064   CHECK(client.findMatchingFilters(urlToCheck, FONoFilterOption,
0065     currentPageDomain, &filter, &exceptionFilter));
0066   CHECK(filter);
0067   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0068 
0069   int size;
0070   char* data = client.serialize(&size);
0071 
0072   AdBlockClient client2;
0073   client2.addTag("blah");
0074   client2.deserialize(data);
0075   // For valgrind only
0076   client2.deserialize(data);
0077 
0078   filter = nullptr;
0079   exceptionFilter = nullptr;
0080   CHECK(client2.findMatchingFilters(urlToCheck, FONoFilterOption,
0081     currentPageDomain, &filter, &exceptionFilter));
0082   CHECK(filter);
0083   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0084 }
0085 
0086 TEST(preservesTag, basicWithDomain) {
0087   const char * filterText =
0088     "filter$tag=blah,domain=brianbondy.com|slashdot.org\n";
0089   const char * currentPageDomain = "slashdot.org";
0090   const char * urlToCheck = "http://www.brianbondy.com/filter?c=a&view=ad&b=2";
0091 
0092   AdBlockClient client;
0093   client.parse(filterText, true);
0094   client.addTag("blah");
0095 
0096   Filter *filter;
0097   Filter *exceptionFilter;
0098 
0099   CHECK(client.findMatchingFilters(urlToCheck, FONoFilterOption,
0100     currentPageDomain, &filter, &exceptionFilter));
0101   CHECK(filter);
0102   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0103 
0104   int size;
0105   char* data = client.serialize(&size);
0106 
0107   AdBlockClient client2;
0108   client2.addTag("blah");
0109   client2.deserialize(data);
0110   // For valgrind only
0111   client2.deserialize(data);
0112 
0113   filter = nullptr;
0114   exceptionFilter = nullptr;
0115   CHECK(client2.findMatchingFilters(urlToCheck, FONoFilterOption,
0116     currentPageDomain, &filter, &exceptionFilter));
0117   CHECK(filter);
0118   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0119 }
0120 
0121 TEST(preservesTag, hostAnchoredWithDomain) {
0122   const char * filterText =
0123     "||www.brianbondy.com$domain=brianbondy.com|slashdot.org,tag=blah\n";
0124   const char * currentPageDomain = "slashdot.org";
0125   const char * urlToCheck = "http://www.brianbondy.com/filter?c=a&view=ad&b=2";
0126 
0127   AdBlockClient client;
0128   client.parse(filterText, true);
0129   client.addTag("blah");
0130 
0131   Filter *filter;
0132   Filter *exceptionFilter;
0133 
0134   CHECK(client.findMatchingFilters(urlToCheck, FONoFilterOption,
0135     currentPageDomain, &filter, &exceptionFilter));
0136   CHECK(filter);
0137   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0138 
0139   int size;
0140   char* data = client.serialize(&size);
0141 
0142   AdBlockClient client2;
0143   client2.addTag("blah");
0144   client2.deserialize(data);
0145   // For valgrind only
0146   client2.deserialize(data);
0147 
0148   filter = nullptr;
0149   exceptionFilter = nullptr;
0150   CHECK(client2.findMatchingFilters(urlToCheck, FONoFilterOption,
0151     currentPageDomain, &filter, &exceptionFilter));
0152   CHECK(filter);
0153   CHECK(std::string(filter->tag, filter->tagLen) == "blah")
0154 }