Skip to content

Commit 7b5cfcb

Browse files
committed
Tests fixing and disabling.
1 parent 09b7b88 commit 7b5cfcb

11 files changed

+285
-214
lines changed

src/engine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::regex_manager::RegexManagerDiscardPolicy;
77
use crate::request::Request;
88
use crate::resources::{Resource, ResourceStorage};
99

10+
#[allow(unused_imports)]
1011
pub use crate::engine_serializer::Serialize;
1112

1213
use std::collections::HashSet;

src/filters/fb_network.rs

+11
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ impl<'a> FlatNetworkFiltersListBuilder<'a> {
8686
None
8787
};
8888

89+
let raw_line = network_filter
90+
.raw_line
91+
.as_ref()
92+
.map(|v| self.builder.create_string(v.as_str()));
93+
8994
let filter = fb::NetworkFilter::create(
9095
&mut self.builder,
9196
&fb::NetworkFilterArgs {
@@ -96,6 +101,7 @@ impl<'a> FlatNetworkFiltersListBuilder<'a> {
96101
opt_not_domains: opt_not_domains,
97102
hostname: hostname,
98103
tag: tag,
104+
raw_line: raw_line,
99105
},
100106
);
101107

@@ -247,6 +253,11 @@ impl<'a> FlatNetworkFilter<'a> {
247253
pub fn patterns(&self) -> FlatPatterns {
248254
FlatPatterns::new(self.fb_filter.patterns())
249255
}
256+
257+
#[inline(always)]
258+
pub fn raw_line(&self) -> Option<String> {
259+
self.fb_filter.raw_line().map(|v| v.to_string())
260+
}
250261
}
251262

252263
impl<'a> NetworkFilterMaskHelper for FlatNetworkFilter<'a> {

src/flat/fb_network_filter.fbs

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
// flatc --rust --gen-object-api -o src/flat/ src/flat/network_filter.fbs
2-
3-
namespace fb;
4-
5-
table NetworkFilter {
6-
mask: uint32; // NetworkFilterMask (network.rs)
7-
8-
// These arrays contain sorted(asc) indecies in the |unique_domains_hashes|
9-
// instead of hashes itself. It saves a lot of memory because there
10-
// aren't many unique hashes.
11-
opt_domains: [uint16];
12-
opt_not_domains: [uint16];
13-
14-
patterns: [string];
15-
modifier_option: string;
16-
hostname: string;
17-
18-
tag: string;
19-
}
20-
21-
table NetworkFilterList {
22-
network_filters: [NetworkFilter] (required);
23-
unique_domains_hashes: [uint64] (required);
24-
}
25-
26-
root_type NetworkFilterList;
1+
// flatc --rust --gen-object-api -o src/flat/ src/flat/network_filter.fbs
2+
3+
namespace fb;
4+
5+
table NetworkFilter {
6+
mask: uint32; // NetworkFilterMask (network.rs)
7+
8+
// These arrays contain sorted(asc) indecies in the |unique_domains_hashes|
9+
// instead of hashes itself. It saves a lot of memory because there
10+
// aren't many unique hashes.
11+
opt_domains: [uint16];
12+
opt_not_domains: [uint16];
13+
14+
patterns: [string];
15+
modifier_option: string;
16+
hostname: string;
17+
18+
tag: string;
19+
raw_line: string;
20+
}
21+
22+
table NetworkFilterList {
23+
network_filters: [NetworkFilter] (required);
24+
unique_domains_hashes: [uint64] (required);
25+
}
26+
27+
root_type NetworkFilterList;

src/flat/fb_network_filter_generated.rs

+35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod fb {
4242
pub const VT_MODIFIER_OPTION: flatbuffers::VOffsetT = 12;
4343
pub const VT_HOSTNAME: flatbuffers::VOffsetT = 14;
4444
pub const VT_TAG: flatbuffers::VOffsetT = 16;
45+
pub const VT_RAW_LINE: flatbuffers::VOffsetT = 18;
4546

4647
#[inline]
4748
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
@@ -58,6 +59,9 @@ pub mod fb {
5859
args: &'args NetworkFilterArgs<'args>,
5960
) -> flatbuffers::WIPOffset<NetworkFilter<'bldr>> {
6061
let mut builder = NetworkFilterBuilder::new(_fbb);
62+
if let Some(x) = args.raw_line {
63+
builder.add_raw_line(x);
64+
}
6165
if let Some(x) = args.tag {
6266
builder.add_tag(x);
6367
}
@@ -90,6 +94,7 @@ pub mod fb {
9094
let modifier_option = self.modifier_option().map(|x| x.to_string());
9195
let hostname = self.hostname().map(|x| x.to_string());
9296
let tag = self.tag().map(|x| x.to_string());
97+
let raw_line = self.raw_line().map(|x| x.to_string());
9398
NetworkFilterT {
9499
mask,
95100
opt_domains,
@@ -98,6 +103,7 @@ pub mod fb {
98103
modifier_option,
99104
hostname,
100105
tag,
106+
raw_line,
101107
}
102108
}
103109

@@ -183,6 +189,16 @@ pub mod fb {
183189
.get::<flatbuffers::ForwardsUOffset<&str>>(NetworkFilter::VT_TAG, None)
184190
}
185191
}
192+
#[inline]
193+
pub fn raw_line(&self) -> Option<&'a str> {
194+
// Safety:
195+
// Created from valid Table for this object
196+
// which contains a valid value in this slot
197+
unsafe {
198+
self._tab
199+
.get::<flatbuffers::ForwardsUOffset<&str>>(NetworkFilter::VT_RAW_LINE, None)
200+
}
201+
}
186202
}
187203

188204
impl flatbuffers::Verifiable for NetworkFilter<'_> {
@@ -218,6 +234,11 @@ pub mod fb {
218234
false,
219235
)?
220236
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("tag", Self::VT_TAG, false)?
237+
.visit_field::<flatbuffers::ForwardsUOffset<&str>>(
238+
"raw_line",
239+
Self::VT_RAW_LINE,
240+
false,
241+
)?
221242
.finish();
222243
Ok(())
223244
}
@@ -232,6 +253,7 @@ pub mod fb {
232253
pub modifier_option: Option<flatbuffers::WIPOffset<&'a str>>,
233254
pub hostname: Option<flatbuffers::WIPOffset<&'a str>>,
234255
pub tag: Option<flatbuffers::WIPOffset<&'a str>>,
256+
pub raw_line: Option<flatbuffers::WIPOffset<&'a str>>,
235257
}
236258
impl<'a> Default for NetworkFilterArgs<'a> {
237259
#[inline]
@@ -244,6 +266,7 @@ pub mod fb {
244266
modifier_option: None,
245267
hostname: None,
246268
tag: None,
269+
raw_line: None,
247270
}
248271
}
249272
}
@@ -309,6 +332,13 @@ pub mod fb {
309332
.push_slot_always::<flatbuffers::WIPOffset<_>>(NetworkFilter::VT_TAG, tag);
310333
}
311334
#[inline]
335+
pub fn add_raw_line(&mut self, raw_line: flatbuffers::WIPOffset<&'b str>) {
336+
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(
337+
NetworkFilter::VT_RAW_LINE,
338+
raw_line,
339+
);
340+
}
341+
#[inline]
312342
pub fn new(
313343
_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a, A>,
314344
) -> NetworkFilterBuilder<'a, 'b, A> {
@@ -335,6 +365,7 @@ pub mod fb {
335365
ds.field("modifier_option", &self.modifier_option());
336366
ds.field("hostname", &self.hostname());
337367
ds.field("tag", &self.tag());
368+
ds.field("raw_line", &self.raw_line());
338369
ds.finish()
339370
}
340371
}
@@ -348,6 +379,7 @@ pub mod fb {
348379
pub modifier_option: Option<String>,
349380
pub hostname: Option<String>,
350381
pub tag: Option<String>,
382+
pub raw_line: Option<String>,
351383
}
352384
impl Default for NetworkFilterT {
353385
fn default() -> Self {
@@ -359,6 +391,7 @@ pub mod fb {
359391
modifier_option: None,
360392
hostname: None,
361393
tag: None,
394+
raw_line: None,
362395
}
363396
}
364397
}
@@ -377,6 +410,7 @@ pub mod fb {
377410
let modifier_option = self.modifier_option.as_ref().map(|x| _fbb.create_string(x));
378411
let hostname = self.hostname.as_ref().map(|x| _fbb.create_string(x));
379412
let tag = self.tag.as_ref().map(|x| _fbb.create_string(x));
413+
let raw_line = self.raw_line.as_ref().map(|x| _fbb.create_string(x));
380414
NetworkFilter::create(
381415
_fbb,
382416
&NetworkFilterArgs {
@@ -387,6 +421,7 @@ pub mod fb {
387421
modifier_option,
388422
hostname,
389423
tag,
424+
raw_line,
390425
},
391426
)
392427
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod blocker;
2020
#[cfg(feature = "content-blocking")]
2121
pub mod content_blocking;
2222
pub mod cosmetic_filter_cache;
23-
#[cfg(not(feature = "flatbuffers"))]
23+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
2424
mod data_format;
2525
mod engine;
2626
mod engine_serializer;

src/network_filter_list.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub struct FlatNetworkFilterList {
277277
}
278278

279279
impl NetworkFilterListTrait for FlatNetworkFilterList {
280-
fn new(filters: Vec<NetworkFilter>, _optimize: bool) -> Self {
280+
fn new(filters: Vec<NetworkFilter>, optimize: bool) -> Self {
281281
// Compute tokens for all filters
282282
let filter_tokens: Vec<_> = filters
283283
.into_iter()
@@ -294,7 +294,9 @@ impl NetworkFilterListTrait for FlatNetworkFilterList {
294294
let mut optimizable = HashMap::<Hash, Vec<NetworkFilter>>::new();
295295
{
296296
for (network_filter, multi_tokens) in filter_tokens {
297-
let index = if !optimizer::is_filter_optimizable_by_patterns(&network_filter) {
297+
let index = if !optimize
298+
|| !optimizer::is_filter_optimizable_by_patterns(&network_filter)
299+
{
298300
Some(flat_builder.add(&network_filter))
299301
} else {
300302
None
@@ -325,13 +327,20 @@ impl NetworkFilterListTrait for FlatNetworkFilterList {
325327
}
326328
}
327329

328-
for (token, v) in optimizable {
329-
let optimized = optimizer::optimize_by_groupping_patterns(v);
330+
if optimize {
331+
for (token, v) in optimizable {
332+
let optimized = optimizer::optimize_by_groupping_patterns(v);
330333

331-
for filter in optimized {
332-
let index = flat_builder.add(&filter);
333-
insert_dup(&mut filter_map, token, index);
334+
for filter in optimized {
335+
let index = flat_builder.add(&filter);
336+
insert_dup(&mut filter_map, token, index);
337+
}
334338
}
339+
} else {
340+
debug_assert!(
341+
optimizable.is_empty(),
342+
"Should be empty if optimization is off"
343+
);
335344
}
336345

337346
let flatbuffer_memory = flat_builder.finish();
@@ -393,7 +402,7 @@ impl NetworkFilterListTrait for FlatNetworkFilterList {
393402
return Some(CheckResult {
394403
filter_mask: filter.mask,
395404
modifier_option: filter.modifier_option(),
396-
raw_line: None,
405+
raw_line: filter.raw_line(),
397406
});
398407
}
399408
}
@@ -436,7 +445,7 @@ impl NetworkFilterListTrait for FlatNetworkFilterList {
436445
filters.push(CheckResult {
437446
filter_mask: filter.mask,
438447
modifier_option: filter.modifier_option(),
439-
raw_line: None,
448+
raw_line: filter.raw_line(),
440449
});
441450
}
442451
}

tests/legacy_harness.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ mod legacy_test_filters {
314314
}
315315
}
316316

317+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
317318
mod legacy_check_match {
318319
use adblock::request::Request;
319320
use adblock::{Engine, Serialize};
@@ -854,7 +855,7 @@ mod legacy_check_options {
854855
mod legacy_misc_tests {
855856
use adblock::filters::network::NetworkFilter;
856857
use adblock::request::Request;
857-
use adblock::{Engine, Serialize};
858+
use adblock::Engine;
858859

859860
#[test]
860861
fn demo_app() {
@@ -888,8 +889,11 @@ mod legacy_misc_tests {
888889
assert_eq!(filter.hostname, Some(String::from("test.com")));
889890
}
890891

892+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
891893
#[test]
892894
fn serialization_tests() {
895+
use adblock::Serialize;
896+
893897
let engine = Engine::from_rules_parametrised(
894898
[
895899
"||googlesyndication.com$third-party",

tests/live.rs

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ fn check_live_redirects() {
354354
}
355355
}
356356

357+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
357358
#[test]
358359
/// Ensure that two different engines loaded from the same textual filter set serialize to
359360
/// identical buffers.
@@ -367,6 +368,7 @@ fn stable_serialization() {
367368
assert_eq!(ser1, ser2);
368369
}
369370

371+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
370372
#[test]
371373
/// Ensure that one engine's serialization result can be exactly reproduced by another engine after
372374
/// deserializing from it.

tests/ublock-coverage.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use adblock::request::Request;
2-
use adblock::{Engine, Serialize};
2+
use adblock::Engine;
33

44
use serde::Deserialize;
55

@@ -169,8 +169,11 @@ fn check_specifics_default() {
169169
}
170170
}
171171

172+
#[cfg(not(feature = "flatbuffers"))] // No serialization for flatbuffers yet.
172173
#[test]
173174
fn check_basic_works_after_deserialization() {
175+
use adblock::Serialize;
176+
174177
let engine = get_blocker_engine();
175178
let serialized = engine.serialize_raw().unwrap();
176179
let mut deserialized_engine = Engine::default();

0 commit comments

Comments
 (0)