|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2013 Red Hat, Inc. |
|
3
|
|
|
# |
|
4
|
|
|
# This copyrighted material is made available to anyone wishing to use, |
|
5
|
|
|
# modify, copy, or redistribute it subject to the terms and conditions of |
|
6
|
|
|
# the GNU General Public License v.2, or (at your option) any later version. |
|
7
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
8
|
|
|
# ANY WARRANTY expressed or implied, including the implied warranties of |
|
9
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
10
|
|
|
# Public License for more details. You should have received a copy of the |
|
11
|
|
|
# GNU General Public License along with this program; if not, write to the |
|
12
|
|
|
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
13
|
|
|
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
|
14
|
|
|
# source code or documentation are not subject to the GNU General Public |
|
15
|
|
|
# License and may only be used or replicated with the express permission of |
|
16
|
|
|
# Red Hat, Inc. |
|
17
|
|
|
# |
|
18
|
|
|
# Red Hat Author(s): Vratislav Podzimek <[email protected]> |
|
19
|
|
|
# |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
from unittest import mock |
|
23
|
|
|
import os |
|
24
|
|
|
from collections import namedtuple |
|
25
|
|
|
|
|
26
|
|
|
import pytest |
|
27
|
|
|
|
|
28
|
|
|
from org_fedora_oscap import utils |
|
29
|
|
|
|
|
30
|
|
|
import hashlib |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
@pytest.fixture() |
|
34
|
|
|
def mock_os(): |
|
35
|
|
|
mock_os = mock.Mock() |
|
36
|
|
|
mock_os.makedirs = mock.Mock() |
|
37
|
|
|
mock_os.path = mock.Mock() |
|
38
|
|
|
mock_os.path.isdir = mock.Mock() |
|
39
|
|
|
return mock_os |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def mock_utils_os(mock_os, monkeypatch): |
|
43
|
|
|
utils_module_symbols = utils.__dict__ |
|
44
|
|
|
|
|
45
|
|
|
monkeypatch.setitem(utils_module_symbols, "os", mock_os) |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_existing_dir(mock_os, monkeypatch): |
|
49
|
|
|
mock_utils_os(mock_os, monkeypatch) |
|
50
|
|
|
mock_os.path.isdir.return_value = True |
|
51
|
|
|
|
|
52
|
|
|
utils.ensure_dir_exists("/tmp/test_dir") |
|
53
|
|
|
|
|
54
|
|
|
mock_os.path.isdir.assert_called_with("/tmp/test_dir") |
|
55
|
|
|
assert not mock_os.makedirs.called |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
def test_nonexisting_dir(mock_os, monkeypatch): |
|
59
|
|
|
mock_utils_os(mock_os, monkeypatch) |
|
60
|
|
|
mock_os.path.isdir.return_value = False |
|
61
|
|
|
|
|
62
|
|
|
utils.ensure_dir_exists("/tmp/test_dir") |
|
63
|
|
|
|
|
64
|
|
|
mock_os.path.isdir.assert_called_with("/tmp/test_dir") |
|
65
|
|
|
mock_os.makedirs.assert_called_with("/tmp/test_dir") |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def test_no_dir(mock_os, monkeypatch): |
|
69
|
|
|
mock_utils_os(mock_os, monkeypatch) |
|
70
|
|
|
# shouldn't raise an exception |
|
71
|
|
|
utils.ensure_dir_exists("") |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_relative_relative(): |
|
75
|
|
|
assert utils.join_paths("foo", "blah") == "foo/blah" |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_relative_absolute(): |
|
79
|
|
|
assert utils.join_paths("foo", "/blah") == "foo/blah" |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def test_absolute_relative(): |
|
83
|
|
|
assert utils.join_paths("/foo", "blah") == "/foo/blah" |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
def test_absolute_absolute(): |
|
87
|
|
|
assert utils.join_paths("/foo", "/blah") == "/foo/blah" |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_dict(): |
|
91
|
|
|
dct = {"a": 1, "b": 2} |
|
92
|
|
|
|
|
93
|
|
|
mapped_dct = utils.keep_type_map(str.upper, dct) |
|
94
|
|
|
assert list(mapped_dct.keys()) == ["A", "B"] |
|
95
|
|
|
assert isinstance(mapped_dct, dict) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def test_list(): |
|
99
|
|
|
lst = [1, 2, 4, 5] |
|
100
|
|
|
|
|
101
|
|
|
mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst) |
|
102
|
|
|
assert mapped_lst == [1, 4, 16, 25] |
|
103
|
|
|
assert isinstance(mapped_lst, list) |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
def test_tuple(): |
|
107
|
|
|
tpl = (1, 2, 4, 5) |
|
108
|
|
|
|
|
109
|
|
|
mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl) |
|
110
|
|
|
assert mapped_tpl == (1, 4, 16, 25) |
|
111
|
|
|
assert isinstance(mapped_tpl, tuple) |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
def test_namedtuple(): |
|
115
|
|
|
NT = namedtuple("TestingNT", ["a", "b"]) |
|
116
|
|
|
ntpl = NT(2, 4) |
|
117
|
|
|
|
|
118
|
|
|
mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl) |
|
119
|
|
|
assert mapped_tpl == NT(4, 16) |
|
120
|
|
|
assert isinstance(mapped_tpl, tuple) |
|
121
|
|
|
assert isinstance(mapped_tpl, NT) |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
def test_set(): |
|
125
|
|
|
st = {1, 2, 4, 5} |
|
126
|
|
|
|
|
127
|
|
|
mapped_st = utils.keep_type_map(lambda x: x ** 2, st) |
|
128
|
|
|
assert mapped_st == {1, 4, 16, 25} |
|
129
|
|
|
assert isinstance(mapped_st, set) |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
def test_str(): |
|
133
|
|
|
stri = "abcd" |
|
134
|
|
|
|
|
135
|
|
|
mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri) |
|
136
|
|
|
assert mapped_stri == "cdef" |
|
137
|
|
|
assert isinstance(mapped_stri, str) |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
def test_gen(): |
|
141
|
|
|
generator = (el for el in (1, 2, 4, 5)) |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
mapped_generator = utils.keep_type_map(lambda x: x ** 2, generator) |
|
144
|
|
|
assert tuple(mapped_generator) == (1, 4, 16, 25) |
|
145
|
|
|
|
|
146
|
|
|
# any better test for this? |
|
147
|
|
|
assert "__next__" in dir(mapped_generator) |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
def test_hash(): |
|
151
|
|
|
file_hashes = { |
|
152
|
|
|
'md5': 'ea38136ca349e139c59f09e09d2aa956', |
|
153
|
|
|
'sha1': 'f905458483be8ac21002ab2c6409d3a10b3813f1', |
|
154
|
|
|
'sha224': '2b1e795db6b7397f47a270fbb5059e76b94a8c972240b17c45db1f13', |
|
155
|
|
|
'sha256': '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6', |
|
156
|
|
|
'sha384': 'b3ffdfad2bf33caf6e44a8b34386ad741bb80fb02306d3889b8a5645cde31e9d' |
|
157
|
|
|
'31ec44e0b0e6ce84d83a57339b75b9bf', |
|
158
|
|
|
'sha512': '7b05940e8d69e804a90f5110d22ad3a1cd03adc5bf4d0a4779790c78118b3c61' |
|
159
|
|
|
'b7f3a3cd39fcf2902ec92ac80df71b952a7aeb2d53c16f0e77436eeb91e33e1d' |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
for hash_id, file_hash in file_hashes.items(): |
|
163
|
|
|
if hash_id not in hashlib.algorithms_available: |
|
164
|
|
|
continue |
|
165
|
|
|
|
|
166
|
|
|
hash_obj = utils.get_hashing_algorithm(file_hash) |
|
167
|
|
|
assert hash_obj.name == hash_id |
|
168
|
|
|
|
|
169
|
|
|
filepath = os.path.join(os.path.dirname(__file__), 'data', 'file') |
|
170
|
|
|
computed_hash = utils.get_file_fingerprint(filepath, hash_obj) |
|
171
|
|
|
|
|
172
|
|
|
assert file_hash == computed_hash |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
def test_hash_unknown(): |
|
176
|
|
|
file_hash = 'XXXX' |
|
177
|
|
|
|
|
178
|
|
|
hash_obj = utils.get_hashing_algorithm(file_hash) |
|
179
|
|
|
assert hash_obj is None |
|
180
|
|
|
|