OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import copy |
| 6 import unittest |
| 7 |
| 8 from content_classification_lens import (ContentClassificationLens, |
| 9 _RulesMatcher) |
| 10 from request_track import (Request, TimingFromDict) |
| 11 import test_utils |
| 12 |
| 13 |
| 14 class ContentClassificationLensTestCase(unittest.TestCase): |
| 15 _REQUEST = Request.FromJsonDict({'url': 'http://bla.com', |
| 16 'request_id': '1234.1', |
| 17 'frame_id': '123.1', |
| 18 'initiator': {'type': 'other'}, |
| 19 'timestamp': 2, |
| 20 'timing': TimingFromDict({})}) |
| 21 _MAIN_FRAME_ID = '123.1' |
| 22 _PAGE_EVENTS = [{'method': 'Page.frameStartedLoading', |
| 23 'frame_id': _MAIN_FRAME_ID}, |
| 24 {'method': 'Page.frameAttached', |
| 25 'frame_id': '123.13', 'parent_frame_id': _MAIN_FRAME_ID}] |
| 26 _RULES = ['bla.com'] |
| 27 |
| 28 def testAdRequest(self): |
| 29 trace = test_utils.LoadingTraceFromEvents( |
| 30 [self._REQUEST], self._PAGE_EVENTS) |
| 31 lens = ContentClassificationLens(trace, self._RULES, []) |
| 32 self.assertTrue(lens.IsAdRequest(self._REQUEST)) |
| 33 self.assertFalse(lens.IsTrackingRequest(self._REQUEST)) |
| 34 |
| 35 def testTrackingRequest(self): |
| 36 trace = test_utils.LoadingTraceFromEvents( |
| 37 [self._REQUEST], self._PAGE_EVENTS) |
| 38 lens = ContentClassificationLens(trace, [], self._RULES) |
| 39 self.assertFalse(lens.IsAdRequest(self._REQUEST)) |
| 40 self.assertTrue(lens.IsTrackingRequest(self._REQUEST)) |
| 41 |
| 42 def testMainFrameIsNotAdFrame(self): |
| 43 trace = test_utils.LoadingTraceFromEvents( |
| 44 [self._REQUEST] * 10, self._PAGE_EVENTS) |
| 45 lens = ContentClassificationLens(trace, self._RULES, []) |
| 46 self.assertFalse(lens.IsAdFrame(self._MAIN_FRAME_ID, .5)) |
| 47 |
| 48 def testAdFrame(self): |
| 49 request = self._REQUEST |
| 50 request.frame_id = '123.123' |
| 51 trace = test_utils.LoadingTraceFromEvents( |
| 52 [request] * 10 + [self._REQUEST] * 5, self._PAGE_EVENTS) |
| 53 lens = ContentClassificationLens(trace, self._RULES, []) |
| 54 self.assertTrue(lens.IsAdFrame(request.frame_id, .5)) |
| 55 |
| 56 |
| 57 class _MatcherTestCase(unittest.TestCase): |
| 58 _RULES_WITH_WHITELIST = ['/thisisanad.', '@@myadvertisingdomain.com/*', |
| 59 '@@||www.mydomain.com/ads/$elemhide'] |
| 60 _SCRIPT_RULE = 'domainwithscripts.com/*$script' |
| 61 _SCRIPT_REQUEST = Request.FromJsonDict( |
| 62 {'url': 'http://domainwithscripts.com/bla.js', |
| 63 'resource_type': 'Script', |
| 64 'request_id': '1234.1', |
| 65 'frame_id': '123.1', |
| 66 'initiator': {'type': 'other'}, |
| 67 'timestamp': 2, |
| 68 'timing': TimingFromDict({})}) |
| 69 |
| 70 def testRemovesWhitelistRules(self): |
| 71 matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, False) |
| 72 self.assertEquals(3, len(matcher._rules)) |
| 73 matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, True) |
| 74 self.assertEquals(1, len(matcher._rules)) |
| 75 |
| 76 def testScriptRule(self): |
| 77 matcher = _RulesMatcher([self._SCRIPT_RULE], False) |
| 78 request = copy.deepcopy(self._SCRIPT_REQUEST) |
| 79 request.resource_type = 'Stylesheet' |
| 80 self.assertFalse(matcher.Matches(request)) |
| 81 self.assertTrue(matcher.Matches(self._SCRIPT_REQUEST)) |
| 82 |
| 83 |
| 84 if __name__ == '__main__': |
| 85 unittest.main() |
OLD | NEW |