Project

General

Profile

Download (4.04 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / unit / test_content.py @ 9d825eaa

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
"""
4
Haketilo unit tests - main content script
5
"""
6

    
7
# This file is part of Haketilo
8
#
9
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
10
#
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the CC0 1.0 Universal License as published by
13
# the Creative Commons Corporation.
14
#
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# CC0 1.0 Universal License for more details.
19

    
20
import pytest
21
import json
22
from selenium.webdriver.support.ui import WebDriverWait
23

    
24
from ..script_loader import load_script
25

    
26
# From:
27
# https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/contentScripts/register
28
# it is unclear whether the dynamically-registered content script is guaranteed
29
# to be always executed after statically-registered ones. We want to test both
30
# cases, so we'll make the mocked dynamic content script execute before
31
# content.js on http:// pages and after it on https:// pages.
32
dynamic_script = \
33
    ''';
34
    this.haketilo_secret        = "abracadabra";
35
    this.haketilo_pattern_tree  = {};
36
    this.haketilo_defualt_allow = false;
37

    
38
    if (this.haketilo_content_script_main)
39
        this.haketilo_content_script_main();
40
    '''
41

    
42
content_script = \
43
    '''
44
    /* Mock dynamic content script - case 'before'. */
45
    if (/#dynamic_before$/.test(document.URL)) {
46
        %s;
47
    }
48

    
49
    /* Place amalgamated content.js here. */
50
    %s;
51

    
52
    /* Rest of mocks */
53
    const data_to_verify = {};
54
    function data_set(prop, val) {
55
        data_to_verify[prop] = val;
56
        window.wrappedJSObject.data_to_verify = JSON.stringify(data_to_verify);
57
    }
58

    
59
    repo_query_cacher.start = () => data_set("cacher_started", true);
60

    
61
    enforce_blocking = policy => data_set("enforcing", policy);
62

    
63
    browser.runtime.onMessage.addListener = async function (listener_cb) {
64
        await new Promise(cb => setTimeout(cb, 0));
65

    
66
        /* Mock a good request. */
67
        const set_good = val => data_set("good_request_result", val);
68
        listener_cb(["page_info"], {}, val => set_good(val));
69

    
70
        /* Mock a bad request. */
71
        const set_bad = val => data_set("bad_request_result", val);
72
        listener_cb(["???"], {}, val => set_bad(val));
73
    }
74

    
75
    /* main() call - normally present in content.js, inside '#IF !UNIT_TEST'. */
76
    main();
77

    
78
    /* Mock dynamic content script - case 'after'. */
79
    if (/#dynamic_after$/.test(document.URL)) {
80
        %s;
81
    }
82

    
83
    data_set("script_run_without_errors", true);
84
    ''' % (dynamic_script, load_script('content/content.js'), dynamic_script)
85

    
86
@pytest.mark.ext_data({'content_script': content_script})
87
@pytest.mark.usefixtures('webextension')
88
@pytest.mark.parametrize('target', ['dynamic_before', 'dynamic_after'])
89
def test_content_unprivileged_page(driver, execute_in_page, target):
90
    """
91
    Test functioning of content.js on an page using unprivileged schema (e.g.
92
    'https://' and not 'about:').
93
    """
94
    driver.get(f'https://gotmyowndoma.in/index.html#{target}')
95
    data = json.loads(driver.execute_script('return window.data_to_verify;'))
96

    
97
    assert 'gotmyowndoma.in' in data['good_request_result']['url']
98
    assert 'bad_request_result' not in data
99

    
100
    assert data['cacher_started'] == True
101

    
102
    assert data['enforcing']['allow']  == False
103
    assert 'mapping' not in data['enforcing']
104
    assert 'error'   not in data['enforcing']
105

    
106
    assert data['script_run_without_errors'] == True
107

    
108
@pytest.mark.ext_data({'content_script': content_script})
109
@pytest.mark.usefixtures('webextension')
110
@pytest.mark.parametrize('target', ['dynamic_before', 'dynamic_after'])
111
def test_content_privileged_page(driver, execute_in_page, target):
112
    """
113
    Test functioning of content.js on an page considered privileged (e.g. a
114
    directory listing at 'file:///').
115
    """
116
    driver.get(f'file:///#{target}')
117
    data = json.loads(driver.execute_script('return window.data_to_verify;'))
118

    
119
    assert data == {'script_run_without_errors': True}
(6-6/25)