/
trust.py
executable file
·332 lines (283 loc) · 12.3 KB
/
trust.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Andrew Morgan <andrew@amorgan.xyz>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
import unittest
import unittest.mock
import getpass
import xattr
import sys
import io
import os
import qubesfiletrust.qvm_file_trust as qvm_file_trust
user_home = os.path.expanduser('~')
class TC_00_trust(unittest.TestCase):
@unittest.mock.patch('qubesfiletrust.qvm_file_trust.open',
new_callable=unittest.mock.mock_open(), create=True)
def test_000_retrieve_folders(self, list_mock):
"""Create a mock global and local list and check resulting rules.
Ensure all injected rules are accounted for
"""
handlers = (unittest.mock.mock_open(
read_data='/home/user/Downloads'
'\n/home/user/QubesIncoming'.replace(
'/home/user', user_home)
).return_value,
unittest.mock.mock_open(
read_data='/home/user/Downloads'
'\n/home/user/QubesIncoming'
'\n/home/user/Pictures'
'\n/var/log/'
'\n/etc/'
'\n~/terrible files'
'\n~/my way too long path name with spaces'.replace(
'/home/user', user_home)).return_value)
list_mock.side_effect = handlers
untrusted_folder_paths = []
try:
untrusted_folder_paths = qvm_file_trust.retrieve_untrusted_folders()
finally:
# Order not garunteed, therefore assert with assertCountEqual
# Despite the name, it does check for the same elements in each
# list, not just the amount of elements
self.assertCountEqual(untrusted_folder_paths,
[w.replace('/home/user', user_home) for w in
['/home/user/Downloads', '/home/user/QubesIncoming',
'/home/user/Pictures', '/var/log', '/etc',
'/home/user/terrible files',
'/home/user/my way too long path name with spaces']])
@unittest.mock.patch('qubesfiletrust.qvm_file_trust.open',
new_callable=unittest.mock.mock_open(), create=True)
def test_001_retrieve_folders_override(self, list_mock):
"""Create a mock global and local list and check resulting rules.
Check global rules are properly overridden by '-' prepended local rules
"""
handlers = (unittest.mock.mock_open(
read_data='~/Downloads'
'\n~/QubesIncoming'
).return_value,
unittest.mock.mock_open(
read_data='/home/user/Downloads'
'\n-/home/user/QubesIncoming'.replace(
'/home/user', user_home)
).return_value)
list_mock.side_effect = handlers
untrusted_folder_paths = []
try:
untrusted_folder_paths = qvm_file_trust.retrieve_untrusted_folders()
finally:
self.assertCountEqual(untrusted_folder_paths,
[w.replace('/home/user', user_home) for w in
['/home/user/Downloads']])
def test_010_check_read_attribute_success(self):
"""Check whether our untrusted attribute is successfully found"""
xattr.get_all = unittest.mock.MagicMock(
return_value=[(b'user.qubes.untrusted', b'true'),
(b'user.something.else', b'false')])
os.chmod = unittest.mock.MagicMock()
test_result = False
try:
test_result = qvm_file_trust.is_untrusted_xattr('', '777')
self.assertTrue(test_result)
except SystemExit as err:
self.fail('System Exit caught: {}'.format(err))
def test_011_check_read_attribute_failure(self):
"""Check whether we support not finding our attribute"""
xattr.get_all = unittest.mock.MagicMock(
return_value=['user.bla.something', 'user.something.else'])
os.chmod = unittest.mock.MagicMock()
test_result = True
try:
test_result = qvm_file_trust.is_untrusted_xattr('', '777')
self.assertFalse(test_result)
except SystemExit as err:
self.fail('System Exit caught: {}'.format(err))
@unittest.mock.patch('qubesfiletrust.qvm_file_trust.open',
new_callable=unittest.mock.mock_open(), create=True)
def test_020_check_untrusted_path_path_based(self, list_mock):
"""Check if a path is untrusted based on untrusted folders lists"""
dummy_untrusted_phrase = '.untrusted'
dummy_file_data = '~/Downloads/\n~/QubesIncoming'
handlers = (unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_untrusted_phrase
).return_value)
list_mock.side_effect = handlers
test_result = False
try:
# This method expects os.path.expanduser to have already been run
test_result = qvm_file_trust.is_untrusted_path(
user_home + '/Downloads')
finally:
self.assertTrue(test_result)
test_result = True
try:
test_result = qvm_file_trust.is_untrusted_path(
user_home + '/Trusted Folder')
finally:
self.assertFalse(test_result)
@unittest.mock.patch('qubesfiletrust.qvm_file_trust.open',
new_callable=unittest.mock.mock_open(), create=True)
def test_021_check_untrusted_path_phrase_based(self, list_mock):
"""Check if path is untrusted based on untrusted phrase"""
dummy_untrusted_phrase = '.untrusted'
dummy_file_data = '/home/user/Downloads\n/home/user/QubesIncoming'
# Check for three phrase and path combos
handlers = (unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_untrusted_phrase
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_untrusted_phrase
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_file_data
).return_value,
unittest.mock.mock_open(
read_data=dummy_untrusted_phrase
).return_value)
list_mock.side_effect = handlers
test_result = False
try:
# Check if '.untrusted' in filepath
test_result = qvm_file_trust.is_untrusted_path(
'/path/to/.untrusted/folder')
finally:
self.assertTrue(test_result)
test_result = True
try:
test_result = qvm_file_trust.is_untrusted_path(
user_home + '/Trusted_Folder')
finally:
self.assertFalse(test_result)
# Check that phrase is case insensitive
test_result = False
try:
# Check if '.untrusted' in filepath
test_result = qvm_file_trust.is_untrusted_path(
'/path/to/.uNtrUsTeD/folder')
finally:
self.assertTrue(test_result)
def test_030_check_exits_properly_on_bad_chmod(self):
"""Ensure script exits with proper return code on chmod failure"""
def failing_chmod():
# This is our failing os.chmod replacement
raise Exception
os.chmod = failing_chmod
# Dummy data
os.stat = unittest.mock.Mock()
os.stat.st_mode = 0
# When an exception is raised, make sure it is exit code 77
# i.e., chmod issue
with self.assertRaises(SystemExit) as cm:
qvm_file_trust.OUTPUT_QUIET = True
qvm_file_trust.change_file('', True)
self.assertEqual(cm.exception.code, 77)
def test_031_xattr_called_when_setting_file_trust(self):
"""Ensure our attribute is added/removed when setting file trust"""
os.chmod = unittest.mock.MagicMock()
# Dummy data
os.stat = unittest.mock.Mock()
os.stat.st_mode = 0
xattr.removexattr = unittest.mock.MagicMock()
qvm_file_trust.change_file('do_trust_me', True)
xattr.removexattr.assert_called_once_with('do_trust_me',
'user.qubes.untrusted')
xattr.setxattr = unittest.mock.MagicMock()
qvm_file_trust.change_file('dont_trust_me', False)
xattr.setxattr.assert_called_once_with('dont_trust_me',
'user.qubes.untrusted', 'true')
'''
# TODO: Do some tests based on command line arguments and that correct
# qvm-file-trust methods are called based on the arguments
def test_040_parse_args(self):
"""Check to make sure correct methods are called given arguments"""
'''
class TC_10_misc(unittest.TestCase):
def test_000_quiet(self):
"""Make sure we're not printing when we shouldn't be."""
qvm_file_trust.OUTPUT_QUIET = True
captured_obj = io.StringIO()
sys.stdout = captured_obj
try:
qvm_file_trust.qprint('Test string!', False)
finally:
sys.stdout = sys.__stdout__
self.assertEqual(captured_obj.getvalue(), '')
qvm_file_trust.OUTPUT_QUIET = False
captured_obj = io.StringIO()
sys.stdout = captured_obj
try:
qvm_file_trust.qprint('Test string!', False)
finally:
sys.stdout = sys.__stdout__
self.assertEqual(captured_obj.getvalue(), 'Test string!\n')
def test_010_error(self):
"""Ensure errors are formatted properly."""
output = ""
qvm_file_trust.OUTPUT_QUIET = False
out = io.StringIO()
sys.stdout = out
qvm_file_trust.error('Test string!')
output = out.getvalue()
self.assertEqual(output, 'Error: Test string!\n')
sys.stdout = sys.__stdout__
print('!!output: ' + output)
qvm_file_trust.OUTPUT_QUIET = True
captured_obj = io.StringIO()
sys.stdout = captured_obj
try:
qvm_file_trust.error('Test string!')
finally:
sys.stdout = sys.__stdout__
self.assertEqual(captured_obj.getvalue(), '')
def list_tests():
return (
TC_00_trust,
TC_10_misc
)
if __name__ == '__main__':
unittest.main()