Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
@testable import Client
6
7
import XCTest
8
9
class TPStatsBlocklistsTests: XCTestCase {
10
var blocklists: TPStatsBlocklists!
11
12
override func setUp() {
13
super.setUp()
14
15
blocklists = TPStatsBlocklists()
16
}
17
18
override func tearDown() {
19
super.tearDown()
20
blocklists = nil
21
}
22
23
func testLoadPerformance() {
24
self.measureMetrics([.wallClockTime], automaticallyStartMeasuring: true) {
25
blocklists.load()
26
self.stopMeasuring()
27
}
28
}
29
30
func testURLInListPerformance() {
31
blocklists.load()
32
33
let whitelistedRegexs = ["*google.com"].compactMap { (domain) -> String? in
34
return wildcardContentBlockerDomainToRegex(domain: domain)
35
}
36
37
self.measureMetrics([.wallClockTime], automaticallyStartMeasuring: true) {
38
for _ in 0..<100 {
39
_ = blocklists.urlIsInList(URL(string: "https://www.firefox.com")!, mainDocumentURL: URL(string: "http://foo.com")!, whitelistedDomains: whitelistedRegexs)
40
}
41
self.stopMeasuring()
42
}
43
}
44
45
func testURLInList() {
46
blocklists.load()
47
48
func blocklist(_ urlString: String, _ mainDoc: String = "https://foo.com", _ whitelistedDomains: [String] = []) -> BlocklistCategory? {
49
let whitelistedRegexs = whitelistedDomains.compactMap { (domain) -> String? in
50
return wildcardContentBlockerDomainToRegex(domain: domain)
51
}
52
let mainDoc = URL(string: mainDoc)!
53
return blocklists.urlIsInList(URL(string: urlString)!, mainDocumentURL: mainDoc, whitelistedDomains: whitelistedRegexs)
54
}
55
XCTAssertEqual(blocklist("https://www.facebook.com", "https://atlassolutions.com"), nil)
56
XCTAssertEqual(blocklist("https://www.firefox.com"), nil)
57
XCTAssertEqual(blocklist("https://2leep.com/track"), .advertising)
58
XCTAssertEqual(blocklist("https://sub.2leep.com/ad"), .advertising)
59
XCTAssertEqual(blocklist("https://admeld.com"), .advertising)
60
XCTAssertEqual(blocklist("https://admeld.com/popup"), .advertising)
61
XCTAssertEqual(blocklist("https://sub.admeld.com"), .advertising)
62
XCTAssertEqual(blocklist("https://subadmeld.com"), nil)
63
XCTAssertEqual(blocklist("https://aol.com.aolanswers.com", "https://foo.com", ["ers.com"]), nil)
64
XCTAssertEqual(blocklist("https://sub.xiti.com/track"), .analytics)
65
XCTAssertEqual(blocklist("https://atlassolutions.com"), .social)
66
XCTAssertEqual(blocklist("https://atlassolutions.com", "https://foo.com", ["*solutions.com"]), nil)
67
}
68
}