/
content_classification_lens_unittest.py
141 lines (123 loc) · 5.69 KB
/
content_classification_lens_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import copy
import unittest
from content_classification_lens import (ContentClassificationLens,
_RulesMatcher)
from request_track import Request
import test_utils
class ContentClassificationLensTestCase(unittest.TestCase):
_DOCUMENT_URL = 'http://bla.com'
_MAIN_FRAME_ID = '123.1'
_REQUEST = Request.FromJsonDict({'url': 'http://bla.com',
'document_url': _DOCUMENT_URL,
'request_id': '1234.1',
'frame_id': _MAIN_FRAME_ID,
'initiator': {'type': 'other'},
'timestamp': 2,
'status': 200,
'timing': {}})
_PAGE_EVENTS = [{'method': 'Page.frameStartedLoading',
'frame_id': _MAIN_FRAME_ID},
{'method': 'Page.frameAttached',
'frame_id': '123.13', 'parent_frame_id': _MAIN_FRAME_ID}]
_RULES = ['bla.com']
def testGetDocumentUrl(self):
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, [], [])
self.assertEquals(self._DOCUMENT_URL, lens._GetDocumentUrl())
# Don't be fooled by redirects.
request = copy.deepcopy(self._REQUEST)
request.status = 302
request.document_url = 'http://www.bla.com'
trace = test_utils.LoadingTraceFromEvents(
[request, self._REQUEST], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, [], [])
self.assertEquals(self._DOCUMENT_URL, lens._GetDocumentUrl())
def testGetDocumentUrlSeveralChanges(self):
request = copy.deepcopy(self._REQUEST)
request.status = 200
request.document_url = 'http://www.blabla.com'
request2 = copy.deepcopy(request)
request2.document_url = 'http://www.blablabla.com'
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST, request, request2], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, [], [])
self.assertEquals(request2.document_url, lens._GetDocumentUrl())
def testNoRules(self):
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, [], [])
self.assertFalse(lens.IsAdRequest(self._REQUEST))
self.assertFalse(lens.IsTrackingRequest(self._REQUEST))
def testAdRequest(self):
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, self._RULES, [])
self.assertTrue(lens.IsAdRequest(self._REQUEST))
self.assertFalse(lens.IsTrackingRequest(self._REQUEST))
def testTrackingRequest(self):
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST], self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, [], self._RULES)
self.assertFalse(lens.IsAdRequest(self._REQUEST))
self.assertTrue(lens.IsTrackingRequest(self._REQUEST))
def testMainFrameIsNotAdFrame(self):
trace = test_utils.LoadingTraceFromEvents(
[self._REQUEST] * 10, self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, self._RULES, [])
self.assertFalse(lens.IsAdFrame(self._MAIN_FRAME_ID, .5))
def testAdFrame(self):
request = copy.deepcopy(self._REQUEST)
request.frame_id = '123.123'
trace = test_utils.LoadingTraceFromEvents(
[request] * 10 + [self._REQUEST] * 5, self._PAGE_EVENTS)
lens = ContentClassificationLens(trace, self._RULES, [])
self.assertTrue(lens.IsAdFrame(request.frame_id, .5))
class _MatcherTestCase(unittest.TestCase):
_RULES_WITH_WHITELIST = ['/thisisanad.', '@@myadvertisingdomain.com/*',
'@@||www.mydomain.com/ads/$elemhide']
_SCRIPT_RULE = 'domainwithscripts.com/*$script'
_THIRD_PARTY_RULE = 'domainwithscripts.com/*$third-party'
_SCRIPT_REQUEST = Request.FromJsonDict(
{'url': 'http://domainwithscripts.com/bla.js',
'resource_type': 'Script',
'request_id': '1234.1',
'frame_id': '123.1',
'initiator': {'type': 'other'},
'timestamp': 2,
'timing': {}})
def testRemovesWhitelistRules(self):
matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, False)
self.assertEquals(3, len(matcher._rules))
matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, True)
self.assertEquals(1, len(matcher._rules))
def testScriptRule(self):
matcher = _RulesMatcher([self._SCRIPT_RULE], False)
request = copy.deepcopy(self._SCRIPT_REQUEST)
request.resource_type = 'Stylesheet'
self.assertFalse(matcher.Matches(
request, ContentClassificationLensTestCase._DOCUMENT_URL))
self.assertTrue(matcher.Matches(
self._SCRIPT_REQUEST, ContentClassificationLensTestCase._DOCUMENT_URL))
def testGetTldPlusOne(self):
self.assertEquals(
'easy.com',
_RulesMatcher._GetTldPlusOne('http://www.easy.com/hello/you'))
self.assertEquals(
'not-so-easy.co.uk',
_RulesMatcher._GetTldPlusOne('http://www.not-so-easy.co.uk/hello/you'))
self.assertEquals(
'hard.co.uk',
_RulesMatcher._GetTldPlusOne('http://hard.co.uk/'))
def testThirdPartyRule(self):
matcher = _RulesMatcher([self._THIRD_PARTY_RULE], False)
request = copy.deepcopy(self._SCRIPT_REQUEST)
document_url = 'http://www.domainwithscripts.com/good-morning'
self.assertFalse(matcher.Matches(request, document_url))
document_url = 'http://anotherdomain.com/good-morning'
self.assertTrue(matcher.Matches(request, document_url))
if __name__ == '__main__':
unittest.main()