Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 2919690

Browse files
committed
complete init draft
1 parent 19095db commit 2919690

File tree

5 files changed

+140
-25
lines changed

5 files changed

+140
-25
lines changed

ad_block_client.cc

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,10 +1555,13 @@ char * AdBlockClient::serialize(int *totalSize,
15551555

15561556

15571557
SerializedBuffer serializedMatcherBuffer;
1558-
if (etldMatcher != nullptr) {
1559-
SerializationResult matcherSerializationResult = etldMatcher->Serialize();
1560-
serializedMatcherBuffer = matcherSerializationResult.buffer;
1558+
int serializedMatcherBufSize;
1559+
if (etldMatcher == nullptr) {
1560+
etldMatcher = new Matcher();
15611561
}
1562+
SerializationResult matcherSerializationResult = etldMatcher->Serialize();
1563+
serializedMatcherBuffer = matcherSerializationResult.buffer;
1564+
serializedMatcherBufSize = static_cast<int>(serializedMatcherBuffer.size());
15621565

15631566
// Get the number of bytes that we'll need
15641567
char sz[512];
@@ -1580,7 +1583,7 @@ char * AdBlockClient::serialize(int *totalSize,
15801583
noFingerprintAntiDomainHashSetSize,
15811584
noFingerprintDomainExceptionHashSetSize,
15821585
noFingerprintAntiDomainExceptionHashSetSize,
1583-
static_cast<int>(serializedMatcherBuffer.size()));
1586+
serializedMatcherBufSize);
15841587

15851588
*totalSize += serializeFilters(nullptr, 0, filters, numFilters) +
15861589
serializeFilters(nullptr, 0, exceptionFilters, numExceptionFilters) +
@@ -1609,7 +1612,7 @@ char * AdBlockClient::serialize(int *totalSize,
16091612
*totalSize += noFingerprintAntiDomainHashSetSize;
16101613
*totalSize += noFingerprintDomainExceptionHashSetSize;
16111614
*totalSize += noFingerprintAntiDomainExceptionHashSetSize;
1612-
*totalSize += serializedMatcherBuffer.size();
1615+
*totalSize += serializedMatcherBufSize;
16131616

16141617
// Allocate it
16151618
int pos = 0;
@@ -1689,10 +1692,10 @@ char * AdBlockClient::serialize(int *totalSize,
16891692
delete[] noFingerprintAntiDomainExceptionHashSetBuffer;
16901693
}
16911694

1692-
if (serializedMatcherBuffer.size() > 0) {
1695+
if (serializedMatcherBufSize > 0) {
16931696
memcpy(buffer + pos, serializedMatcherBuffer.c_str(),
1694-
serializedMatcherBuffer.size());
1695-
pos += serializedMatcherBuffer.size();
1697+
serializedMatcherBufSize);
1698+
pos += serializedMatcherBufSize;
16961699
}
16971700

16981701
return buffer;
@@ -1746,25 +1749,31 @@ bool AdBlockClient::deserialize(char *buffer) {
17461749
noFingerprintAntiDomainHashSetSize = 0,
17471750
noFingerprintDomainExceptionHashSetSize = 0,
17481751
noFingerprintAntiDomainExceptionHashSetSize = 0,
1749-
etldMatcherBufferSize = 0;
1752+
etldMatcherBufSize = 0;
17501753
int pos = 0;
17511754
sscanf(buffer + pos,
17521755
"%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",
17531756
&numFilters,
1754-
&numExceptionFilters, &numCosmeticFilters, &numHtmlFilters,
1755-
&numNoFingerprintFilters, &numNoFingerprintExceptionFilters,
1757+
&numExceptionFilters,
1758+
&numCosmeticFilters,
1759+
&numHtmlFilters,
1760+
&numNoFingerprintFilters,
1761+
&numNoFingerprintExceptionFilters,
17561762
&numNoFingerprintDomainOnlyFilters,
17571763
&numNoFingerprintAntiDomainOnlyFilters,
17581764
&numNoFingerprintDomainOnlyExceptionFilters,
17591765
&numNoFingerprintAntiDomainOnlyExceptionFilters,
1760-
&numHostAnchoredFilters, &numHostAnchoredExceptionFilters,
1761-
&bloomFilterSize, &exceptionBloomFilterSize,
1762-
&hostAnchoredHashSetSize, &hostAnchoredExceptionHashSetSize,
1766+
&numHostAnchoredFilters,
1767+
&numHostAnchoredExceptionFilters,
1768+
&bloomFilterSize,
1769+
&exceptionBloomFilterSize,
1770+
&hostAnchoredHashSetSize,
1771+
&hostAnchoredExceptionHashSetSize,
17631772
&noFingerprintDomainHashSetSize,
17641773
&noFingerprintAntiDomainHashSetSize,
17651774
&noFingerprintDomainExceptionHashSetSize,
17661775
&noFingerprintAntiDomainExceptionHashSetSize,
1767-
&etldMatcherBufferSize);
1776+
&etldMatcherBufSize);
17681777
pos += static_cast<int>(strlen(buffer + pos)) + 1;
17691778

17701779
filters = new Filter[numFilters];
@@ -1813,48 +1822,49 @@ bool AdBlockClient::deserialize(char *buffer) {
18131822
pos += exceptionBloomFilterSize;
18141823
if (!initHashSet(&hostAnchoredHashSet,
18151824
buffer + pos, hostAnchoredHashSetSize)) {
1816-
return false;
1825+
return false;
18171826
}
18181827
pos += hostAnchoredHashSetSize;
18191828
if (!initHashSet(&hostAnchoredExceptionHashSet,
18201829
buffer + pos, hostAnchoredExceptionHashSetSize)) {
1821-
return false;
1830+
return false;
18221831
}
18231832
pos += hostAnchoredExceptionHashSetSize;
18241833

18251834

18261835
if (!initHashSet(&noFingerprintDomainHashSet,
18271836
buffer + pos, noFingerprintDomainHashSetSize)) {
1828-
return false;
1837+
return false;
18291838
}
18301839
pos += noFingerprintDomainHashSetSize;
18311840

18321841
if (!initHashSet(&noFingerprintAntiDomainHashSet,
18331842
buffer + pos, noFingerprintAntiDomainHashSetSize)) {
1834-
return false;
1843+
return false;
18351844
}
18361845
pos += noFingerprintAntiDomainHashSetSize;
18371846

18381847
if (!initHashSet(&noFingerprintDomainExceptionHashSet,
18391848
buffer + pos, noFingerprintDomainExceptionHashSetSize)) {
1840-
return false;
1849+
return false;
18411850
}
18421851
pos += noFingerprintDomainExceptionHashSetSize;
18431852

18441853
if (!initHashSet(&noFingerprintAntiDomainExceptionHashSet,
18451854
buffer + pos, noFingerprintAntiDomainExceptionHashSetSize)) {
1846-
return false;
1855+
return false;
18471856
}
18481857
pos += noFingerprintAntiDomainExceptionHashSetSize;
18491858

18501859
if (etldMatcher != nullptr) {
18511860
delete etldMatcher;
18521861
}
1853-
if (etldMatcherBufferSize == 0) {
1862+
1863+
if (etldMatcherBufSize == 0) {
18541864
etldMatcher = new Matcher();
18551865
} else {
18561866
SerializedBuffer serializedmatcherBuffer = std::string(
1857-
buffer + pos, etldMatcherBufferSize);
1867+
buffer + pos, etldMatcherBufSize);
18581868
Matcher newMatcher = matcher_from_serialization(serializedmatcherBuffer);
18591869
etldMatcher = new Matcher(newMatcher);
18601870
}

etld/matcher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class Matcher {
3333
void ConsumeParseResult(const PublicSuffixParseResult &result);
3434
DomainInfo Match(const Domain &domain) const;
3535

36+
size_t NumRules() const {
37+
return rules_.Rules().size();
38+
}
39+
40+
size_t NumExceptionRules() const {
41+
return exception_rules_.Rules().size();
42+
}
43+
3644
private:
3745
DomainInfo BuildDomainInfo(const PublicSuffixRule &rule,
3846
const Domain &domain) const;

etld/public_suffix_rule_set.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ SerializationResult PublicSuffixRuleSet::Serialize() const {
6565
const size_t buffer_size = body_start + body_len + 1;
6666
char buffer[buffer_size];
6767

68-
int written = snprintf(
68+
snprintf(
6969
buffer,
7070
buffer_size,
7171
"%s:%s",

etld/serialization.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace brave_etld {
1313

1414
typedef std::string SerializedBuffer;
15-
1615
typedef std::vector<SerializedBuffer> SerializedChildBuffers;
1716

1817
struct SerializedBufferInfo {

test/etld_serialization_test.cc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fstream>
1111
#include "./CppUnitLite/TestHarness.h"
1212
#include "./CppUnitLite/Test.h"
13+
#include "./ad_block_client.h"
1314
#include "./etld/types.h"
1415
#include "./etld/matcher.h"
1516
#include "./etld/parser.h"
@@ -27,6 +28,7 @@ using brave_etld::SerializationResult;
2728
using brave_etld::rule_from_serialization;
2829
using brave_etld::rule_set_from_serialization;
2930
using brave_etld::matcher_from_serialization;
31+
using brave_etld::SerializedBuffer;
3032

3133
bool testSerializeRule(const string &rule_text) {
3234
PublicSuffixRule rule(rule_text);
@@ -69,4 +71,100 @@ TEST(eTLDSerializationMatcher, basic) {
6971
CHECK(testSerializeMatcher("!one.com"));
7072
CHECK(testSerializeMatcher("!one.com\n*.two.com"));
7173
CHECK(testSerializeMatcher("!one.com\n*.two.com\nthree.com"));
74+
}
75+
76+
bool testDeserializationCorrectness(const SerializedBuffer &rule_text,
77+
int num_rules, int num_exception_rules) {
78+
Matcher matcher = matcher_from_serialization(rule_text);
79+
int num_matcher_rules = static_cast<int>(matcher.NumRules());
80+
if (num_matcher_rules != num_rules) {
81+
std::cout << "Expected " << num_rules << " rules from buffer, but received "
82+
<< num_matcher_rules << " rules." << std::endl;
83+
return false;
84+
}
85+
86+
int num_matcher_except_rules = static_cast<int>(matcher.NumExceptionRules());
87+
if (num_matcher_except_rules != num_exception_rules) {
88+
std::cout << "Expected " << num_rules << " exception rules from buffer, "
89+
<< "but received " << num_exception_rules << " rules."
90+
<< std::endl;
91+
return false;
92+
}
93+
94+
return true;
95+
}
96+
97+
// TEST(eTLDDeserializationTests, basic) {
98+
// CHECK(testDeserializationCorrectness(
99+
// "47:42:29:ff||not-matching-anything.com8:fftrees^0:", 2, 0));
100+
// }
101+
102+
bool testSerializationCorrectness(const string &rule_text,
103+
const SerializedBuffer &serialized) {
104+
Matcher matcher(rule_text);
105+
SerializationResult result = matcher.Serialize();
106+
return result.buffer.compare(serialized) == 0;
107+
}
108+
109+
// TEST(eTLDSerializationTests, basic) {
110+
// const string rules = "||s3.amazonaws.com^$third-party\n"
111+
// "||not-matching-anything.com\n"
112+
// "trees^";
113+
// CHECK(testSerializationCorrectness(rules, ));
114+
// }
115+
116+
bool testMatcherSerializationTransitivity(const string &public_suffix_rules) {
117+
Matcher orig(public_suffix_rules);
118+
SerializationResult res = orig.Serialize();
119+
120+
Matcher second = matcher_from_serialization(res.buffer);
121+
SerializationResult res2 = second.Serialize();
122+
123+
if (res2.buffer.compare(res.buffer) != 0) {
124+
std::cout << "The serialized buffer of the initial Matcher does not match "
125+
<< "the serialized of the second, buffer initialized buffer."
126+
<< std::endl;
127+
return false;
128+
}
129+
130+
return true;
131+
}
132+
133+
bool testAdBlockClientSerializationTransitivity(const string &filter_rules,
134+
const string &public_suffix_rules) {
135+
AdBlockClient client;
136+
client.parse(filter_rules.c_str());
137+
client.parsePublicSuffixRules(public_suffix_rules.c_str());
138+
139+
int size;
140+
char* buffer = client.serialize(&size);
141+
142+
AdBlockClient client2;
143+
client2.deserialize(buffer);
144+
int size2;
145+
char* buffer2 = client2.serialize(&size2);
146+
147+
if (size != size2) {
148+
std::cout << "The size of the serialized buffers of the first and second "
149+
<< "client do not match." << std::endl;
150+
return false;
151+
}
152+
153+
if (strncmp(buffer, buffer2, size) != 0) {
154+
std::cout << "The serialized buffers of the first and second client do "
155+
<< "not have the same contents." << std::endl;
156+
return false;
157+
}
158+
159+
return true;
160+
}
161+
162+
TEST(eTLDSerializationTransitivity, basic) {
163+
std::ifstream ifs("./test/data/public_suffix_list_short.txt");
164+
const string public_suffix_rules((std::istreambuf_iterator<char>(ifs)),
165+
(std::istreambuf_iterator<char>()));
166+
const string filer_rules = "||not-matching-anything.com\ntrees^";
167+
CHECK(testMatcherSerializationTransitivity(public_suffix_rules));
168+
CHECK(testAdBlockClientSerializationTransitivity(
169+
filer_rules, public_suffix_rules));
72170
}

0 commit comments

Comments
 (0)