Project

General

Profile

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

haketilo / test / unit / test_patterns.py @ 5c583de8

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

    
3
"""
4
Haketilo unit tests - URL patterns
5
"""
6

    
7
# This file is part of Haketilo
8
#
9
# Copyright (C) 2021, Wojtek Kosior
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

    
22
from ..script_loader import load_script
23

    
24
@pytest.fixture(scope="session")
25
def patterns_code():
26
    yield load_script('common/patterns.js', ['common'])
27

    
28
def test_regexes(execute_in_page, patterns_code):
29
    """
30
    patterns.js contains regexes used for URL parsing.
31
    Verify they work properly.
32
    """
33
    execute_in_page(patterns_code, page='https://gotmyowndoma.in')
34

    
35
    valid_url = 'https://example.com/a/b?ver=1.2.3#heading2'
36
    valid_url_rest = 'example.com/a/b?ver=1.2.3#heading2'
37

    
38
    # Test matching of URL protocol.
39
    match = execute_in_page('returnval(proto_regex.exec(arguments[0]));',
40
                            valid_url)
41
    assert match
42
    assert match[1] == 'https'
43
    assert match[2] == valid_url_rest
44

    
45
    match = execute_in_page('returnval(proto_regex.exec(arguments[0]));',
46
                            '://bad-url.missing/protocol')
47
    assert match is None
48

    
49
    # Test matching of http(s) URLs.
50
    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
51
                            valid_url_rest)
52
    assert match
53
    assert match[1] == 'example.com'
54
    assert match[2] == '/a/b'
55
    assert match[3] == '?ver=1.2.3'
56

    
57
    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
58
                            'another.example.com')
59
    assert match
60
    assert match[1] == 'another.example.com'
61
    assert match[2] == ''
62
    assert match[3] == ''
63

    
64
    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
65
                            '/bad/http/example')
66
    assert match == None
67

    
68
    # Test matching of file URLs.
69
    match = execute_in_page('returnval(file_regex.exec(arguments[0]));',
70
                            '/good/file/example')
71
    assert match
72
    assert match[1] == '/good/file/example'
73

    
74
    # Test matching of ftp URLs.
75
    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
76
                            'example.com/a/b#heading2')
77
    assert match
78
    assert match[1] is None
79
    assert match[2] == 'example.com'
80
    assert match[3] == '/a/b'
81

    
82
    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
83
                            'some_user@localhost')
84
    assert match
85
    assert match[1] == 'some_user@'
86
    assert match[2] == 'localhost'
87
    assert match[3] == ''
88

    
89
    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
90
                            '@bad.url/')
91
    assert match is None
92

    
93
def test_deconstruct_url(execute_in_page, patterns_code):
94
    """
95
    patterns.js contains deconstruct_url() function that handles URL parsing.
96
    Verify it works properly.
97
    """
98
    execute_in_page(patterns_code, page='https://gotmyowndoma.in')
99

    
100
    deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
101
                           'https://eXaMpLe.com/a/b?ver=1.2.3#heading2')
102
    assert deco
103
    assert deco['trailing_dash'] == False
104
    assert deco['proto']         == 'https'
105
    assert deco['domain']        == ['example', 'com']
106
    assert deco['path']          == ['a', 'b']
107

    
108
    deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
109
                           'http://**.example.com/')
110
    assert deco
111
    assert deco['trailing_dash'] == True
112
    assert deco['proto']         == 'http'
113
    assert deco['domain']        == ['**', 'example', 'com']
114
    assert deco['path']          == []
115

    
116
    deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
117
                           'ftp://user@ftp.example.com/all///passwords.txt/')
118
    assert deco
119
    assert deco['trailing_dash'] == True
120
    assert deco['proto']         == 'ftp'
121
    assert deco['domain']        == ['ftp', 'example', 'com']
122
    assert deco['path']          == ['all', 'passwords.txt']
123

    
124
    deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
125
                           'ftp://mirror.edu.pl.eu.org')
126
    assert deco
127
    assert deco['trailing_dash'] == False
128
    assert deco['proto']         == 'ftp'
129
    assert deco['domain']        == ['mirror', 'edu', 'pl', 'eu', 'org']
130
    assert deco['path']          == []
131

    
132
    deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
133
                           'file:///mnt/parabola_chroot///etc/passwd')
134
    assert deco
135
    assert deco['trailing_dash'] == False
136
    assert deco['proto']         == 'file'
137
    assert deco['path']          == ['mnt', 'parabola_chroot', 'etc', 'passwd']
138

    
139
    for bad_url in [
140
            '://bad-url.missing/protocol',
141
            'http:/example.com/a/b',
142
            'unknown://example.com/a/b',
143
            'idontfancypineapple',
144
            'ftp://@example.org/',
145
            'https:///some/path/',
146
            'file://non-absolute/path'
147
    ]:
148
        with pytest.raises(Exception, match=r'Error in injected script'):
149
            deco = execute_in_page('returnval(deconstruct_url(arguments[0]));',
150
                                   bad_url)
151

    
152
    # at some point we might also consider testing url deconstruction with
153
    # length limits...
(4-4/5)