-
Notifications
You must be signed in to change notification settings - Fork 200
/
test_fs.py
388 lines (336 loc) · 18.8 KB
/
test_fs.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import filecmp
import os
import shutil
import unittest
from graminelibos.regression import (
HAS_SGX,
RegressionTestCase,
)
# Generic FS tests that mimic probable usage patterns in applications.
# pylint: disable=too-many-public-methods
class TC_00_FileSystem(RegressionTestCase):
@classmethod
def setUpClass(cls):
cls.FILE_SIZES = [0, 1, 2, 15, 16, 17, 255, 256, 257, 1023, 1024, 1025, 65535, 65536, 65537,
1048575, 1048576, 1048577]
cls.TEST_DIR = 'tmp'
cls.INDEXES = range(len(cls.FILE_SIZES))
cls.INPUT_DIR = os.path.join(cls.TEST_DIR, 'input')
cls.INPUT_FILES = [os.path.join(cls.INPUT_DIR, str(x)) for x in cls.FILE_SIZES]
cls.OUTPUT_DIR = os.path.join(cls.TEST_DIR, 'output')
cls.OUTPUT_FILES = [os.path.join(cls.OUTPUT_DIR, str(x)) for x in cls.FILE_SIZES]
# create directory structure and test files
os.mkdir(cls.TEST_DIR)
os.mkdir(cls.INPUT_DIR)
for i in cls.INDEXES:
with open(cls.INPUT_FILES[i], 'wb') as file:
file.write(os.urandom(cls.FILE_SIZES[i]))
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.TEST_DIR)
def setUp(self):
# clean output for each test
shutil.rmtree(self.OUTPUT_DIR, ignore_errors=True)
os.mkdir(self.OUTPUT_DIR)
# copy input file to output dir (for tests that alter the file so input remains untouched)
# pylint: disable=no-self-use
def copy_input(self, input_path, output_path):
shutil.copy(input_path, output_path)
def verify_open_close(self, stdout, stderr, path, mode):
self.assertNotIn('ERROR: ', stderr)
self.assertIn('open(' + path + ') ' + mode + ' OK', stdout)
self.assertIn('close(' + path + ') ' + mode + ' OK', stdout)
self.assertIn('open(' + path + ') ' + mode + ' 1 OK', stdout)
self.assertIn('open(' + path + ') ' + mode + ' 2 OK', stdout)
self.assertIn('close(' + path + ') ' + mode + ' 1 OK', stdout)
self.assertIn('close(' + path + ') ' + mode + ' 2 OK', stdout)
self.assertIn('fopen(' + path + ') ' + mode + ' OK', stdout)
self.assertIn('fclose(' + path + ') ' + mode + ' OK', stdout)
self.assertIn('fopen(' + path + ') ' + mode + ' 1 OK', stdout)
self.assertIn('fopen(' + path + ') ' + mode + ' 2 OK', stdout)
self.assertIn('fclose(' + path + ') ' + mode + ' 1 OK', stdout)
self.assertIn('fclose(' + path + ') ' + mode + ' 2 OK', stdout)
def test_100_open_close(self):
input_path = self.INPUT_FILES[-1] # existing file
output_path = os.path.join(self.OUTPUT_DIR, 'test_100') # new file to be created
stdout, stderr = self.run_binary(['open_close', 'R', input_path])
self.verify_open_close(stdout, stderr, input_path, 'input')
stdout, stderr = self.run_binary(['open_close', 'W', output_path])
self.verify_open_close(stdout, stderr, output_path, 'output')
self.assertTrue(os.path.isfile(output_path))
def verify_open_flags(self, stdout, stderr):
self.assertNotIn('ERROR: ', stderr)
self.assertIn('open(O_CREAT | O_EXCL | O_RDWR) [doesn\'t exist] succeeded as expected',
stdout)
self.assertIn('open(O_CREAT | O_EXCL | O_RDWR) [exists] failed as expected', stdout)
self.assertIn('open(O_CREAT | O_RDWR) [exists] succeeded as expected', stdout)
self.assertIn('open(O_CREAT | O_RDWR) [doesn\'t exist] succeeded as expected', stdout)
self.assertIn('open(O_CREAT | O_TRUNC | O_RDWR) [doesn\'t exist] succeeded as expected',
stdout)
self.assertIn('open(O_CREAT | O_TRUNC | O_RDWR) [exists] succeeded as expected', stdout)
def test_101_open_flags(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_101') # new file to be created
stdout, stderr = self.run_binary(['open_flags', file_path])
self.verify_open_flags(stdout, stderr)
def test_110_read_write(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_110') # new file to be created
stdout, stderr = self.run_binary(['read_write', file_path])
self.assertNotIn('ERROR: ', stderr)
self.assertTrue(os.path.isfile(file_path))
self.assertIn('open(' + file_path + ') RW OK', stdout)
self.assertIn('write(' + file_path + ') RW OK', stdout)
self.assertIn('seek(' + file_path + ') RW OK', stdout)
self.assertIn('read(' + file_path + ') RW OK', stdout)
self.assertIn('compare(' + file_path + ') RW OK', stdout)
self.assertIn('close(' + file_path + ') RW OK', stdout)
# Gramine's implementation of file_map doesn't currently support shared memory-mapped regular
# chroot files with write permission in PAL/Linux-SGX (like mmap(PROT_WRITE, MAP_SHARED, fd)).
# Below test requires it, so skip it. We decided not to implement it as we don't know any
# workload using it.
@unittest.skipIf(HAS_SGX, 'mmap(PROT_WRITE, MAP_SHARED, fd) not implemented in Linux-SGX PAL')
def test_111_read_write_mmap(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_111') # new file to be created
stdout, stderr = self.run_binary(['read_write_mmap', file_path])
size = '1048576'
self.assertNotIn('ERROR: ', stderr)
self.assertTrue(os.path.isfile(file_path))
self.assertIn('open(' + file_path + ') RW (mmap) OK', stdout)
self.assertIn('mmap_fd(' + size + ') OK', stdout)
self.assertIn('read(' + file_path + ') 1 RW (mmap) OK', stdout)
self.assertIn('seek(' + file_path + ') 1 RW (mmap) OK', stdout)
self.assertIn('write(' + file_path + ') RW (mmap) OK', stdout)
self.assertIn('seek(' + file_path + ') 2 RW (mmap) OK', stdout)
self.assertIn('read(' + file_path + ') 2 RW (mmap) OK', stdout)
self.assertIn('compare(' + file_path + ') RW (mmap) OK', stdout)
self.assertIn('munmap_fd(' + size + ') OK', stdout)
self.assertIn('close(' + file_path + ') RW (mmap) OK', stdout)
self.assertIn('open(' + file_path + ') RW fd1 (mmap) OK', stdout)
self.assertIn('open(' + file_path + ') RW fd2 OK', stdout)
self.assertIn('mmap_fd(' + size + ') fd1 OK', stdout)
self.assertIn('write(' + file_path + ') RW fd2 OK', stdout)
self.assertIn('munmap_fd(' + size + ') fd1 OK', stdout)
self.assertIn('close(' + file_path + ') RW fd1 (mmap) OK', stdout)
self.assertIn('close(' + file_path + ') RW fd2 OK', stdout)
def test_112_read_append(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_112') # new file to be created
stdout, stderr = self.run_binary(['read_append', file_path])
self.assertNotIn('ERROR: ', stderr)
self.assertTrue(os.path.isfile(file_path))
self.assertIn('TEST 1 (' + file_path + ')', stdout)
self.assertIn('TEST 2 (' + file_path + ')', stdout)
self.assertIn('TEST 3 (' + file_path + ')', stdout)
# pylint: disable=too-many-arguments
def verify_seek_tell(self, stdout, output_path_1, output_path_2, size):
self.assertIn('Test passed', stdout)
expected_size = size + 4098
self.verify_size(output_path_1, expected_size)
self.verify_size(output_path_2, expected_size)
def test_115_seek_tell(self):
input_path = self.INPUT_FILES[-1] # existing file
output_path_1 = os.path.join(self.OUTPUT_DIR, 'test_115a') # writable files
output_path_2 = os.path.join(self.OUTPUT_DIR, 'test_115b')
self.copy_input(input_path, output_path_1)
self.copy_input(input_path, output_path_2)
stdout, _ = self.run_binary(['seek_tell', input_path, output_path_1, output_path_2])
self.verify_seek_tell(stdout, output_path_1, output_path_2, self.FILE_SIZES[-1])
def test_120_file_delete(self):
file_path = 'test_120'
file_in = self.INPUT_FILES[-1] # existing file to be copied
file_out_1 = os.path.join(self.OUTPUT_DIR, file_path + 'a')
file_out_2 = os.path.join(self.OUTPUT_DIR, file_path + 'b')
file_out_3 = os.path.join(self.OUTPUT_DIR, file_path + 'c')
file_out_4 = os.path.join(self.OUTPUT_DIR, file_path + 'd')
file_out_5 = os.path.join(self.OUTPUT_DIR, file_path + 'e')
# 3 existing files, 2 new files
self.copy_input(file_in, file_out_1)
self.copy_input(file_in, file_out_2)
self.copy_input(file_in, file_out_3)
stdout, stderr = self.run_binary(['delete', file_out_1, file_out_2, file_out_3, file_out_4,
file_out_5])
# verify
self.assertNotIn('ERROR: ', stderr)
self.assertFalse(os.path.isfile(file_out_1))
self.assertFalse(os.path.isfile(file_out_2))
self.assertFalse(os.path.isfile(file_out_3))
self.assertFalse(os.path.isfile(file_out_4))
self.assertFalse(os.path.isfile(file_out_5))
self.assertIn('unlink(' + file_out_1 + ') OK', stdout)
self.assertIn('open(' + file_out_2 + ') input 1 OK', stdout)
self.assertIn('close(' + file_out_2 + ') input 1 OK', stdout)
self.assertIn('unlink(' + file_out_2 + ') input 1 OK', stdout)
self.assertIn('open(' + file_out_3 + ') input 2 OK', stdout)
self.assertIn('unlink(' + file_out_3 + ') input 2 OK', stdout)
self.assertIn('close(' + file_out_3 + ') input 2 OK', stdout)
self.assertIn('open(' + file_out_4 + ') output 1 OK', stdout)
self.assertIn('close(' + file_out_4 + ') output 1 OK', stdout)
self.assertIn('unlink(' + file_out_4 + ') output 1 OK', stdout)
self.assertIn('open(' + file_out_5 + ') output 2 OK', stdout)
self.assertIn('unlink(' + file_out_5 + ') output 2 OK', stdout)
self.assertIn('close(' + file_out_5 + ') output 2 OK', stdout)
# pylint: disable=too-many-arguments
def verify_stat(self, stdout, stderr, input_path, output_path, size):
self.assertNotIn('ERROR: ', stderr)
self.assertIn('stat(' + input_path + ') input 1 OK', stdout)
self.assertIn('open(' + input_path + ') input 2 OK', stdout)
self.assertIn('stat(' + input_path + ') input 2 OK: ' + size, stdout)
self.assertIn('fstat(' + input_path + ') input 2 OK: ' + size, stdout)
self.assertIn('close(' + input_path + ') input 2 OK', stdout)
self.assertIn('stat(' + output_path + ') output 1 OK', stdout)
self.assertIn('open(' + output_path + ') output 2 OK', stdout)
self.assertIn('stat(' + output_path + ') output 2 OK: ' + size, stdout)
self.assertIn('fstat(' + output_path + ') output 2 OK: ' + size, stdout)
self.assertIn('close(' + output_path + ') output 2 OK', stdout)
def test_130_file_stat(self):
# running for every file separately so the process doesn't need to enumerate directory
# (different code path, enumeration also performs stat)
for i in self.INDEXES:
input_path = self.INPUT_FILES[i] # existing file
output_path = self.OUTPUT_FILES[i] # file that will be opened in write mode
size = str(self.FILE_SIZES[i])
self.copy_input(input_path, output_path)
stdout, stderr = self.run_binary(['stat', input_path, output_path])
self.verify_stat(stdout, stderr, input_path, output_path, size)
def test_131_file_chmod_stat(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_131') # new file to be created
stdout, _ = self.run_binary(['chmod_stat', file_path])
self.assertIn('TEST OK', stdout)
def verify_size(self, file, size):
self.assertEqual(os.stat(file).st_size, size)
def do_truncate_test(self, size_in, size_out):
# prepare paths/files
i = self.FILE_SIZES.index(size_in)
input_path = self.INPUT_FILES[i] # source file to be truncated
out_1_path = self.OUTPUT_FILES[i] + 'a'
out_2_path = self.OUTPUT_FILES[i] + 'b'
self.copy_input(input_path, out_1_path)
self.copy_input(input_path, out_2_path)
# run test
stdout, stderr = self.run_binary(['truncate', out_1_path, out_2_path, str(size_out)])
self.assertNotIn('ERROR: ', stderr)
self.assertIn('truncate(' + out_1_path + ') to ' + str(size_out) + ' OK', stdout)
self.assertIn('open(' + out_2_path + ') output OK', stdout)
self.assertIn('ftruncate(' + out_2_path + ') to ' + str(size_out) + ' OK', stdout)
self.assertIn('close(' + out_2_path + ') output OK', stdout)
self.verify_size(out_1_path, size_out)
self.verify_size(out_2_path, size_out)
def test_140_file_truncate(self):
self.do_truncate_test(0, 1)
self.do_truncate_test(0, 16)
self.do_truncate_test(0, 65537)
self.do_truncate_test(1, 0)
self.do_truncate_test(1, 17)
self.do_truncate_test(16, 0)
self.do_truncate_test(16, 1048576)
self.do_truncate_test(255, 15)
self.do_truncate_test(255, 256)
self.do_truncate_test(65537, 65535)
self.do_truncate_test(65537, 65536)
def verify_seek_tell_truncate(self, file_out, file_size, file_pos, file_truncate):
stdout, _ = self.run_binary([
'seek_tell_truncate',
file_out,
str(file_size),
str(file_pos),
str(file_truncate)
])
self.assertIn('Test passed', stdout)
def test_141_file_seek_tell_truncate(self):
file_path = os.path.join(self.OUTPUT_DIR, 'test_141')
self.verify_seek_tell_truncate(f"{file_path}a", 0, 0, 100)
self.verify_seek_tell_truncate(f"{file_path}b", 512, 512, 65536)
self.verify_seek_tell_truncate(f"{file_path}c", 512, 64, 65536)
self.verify_seek_tell_truncate(f"{file_path}d", 512, 512, 0)
self.verify_seek_tell_truncate(f"{file_path}e", 512, 256, 0)
self.verify_seek_tell_truncate(f"{file_path}f", 31337, 20000, 7331)
def verify_copy_content(self, input_path, output_path):
self.assertTrue(filecmp.cmp(input_path, output_path, shallow=False))
def verify_copy(self, stdout, stderr, input_dir, executable):
self.assertNotIn('ERROR: ', stderr)
self.assertIn('opendir(' + input_dir + ') OK', stdout)
self.assertIn('readdir(.) OK', stdout)
if input_dir[0] != '/':
self.assertIn('readdir(..) OK', stdout)
for i in self.INDEXES:
size = str(self.FILE_SIZES[i])
self.assertIn('readdir(' + size + ') OK', stdout)
self.assertIn('open(' + size + ') input OK', stdout)
self.assertIn('fstat(' + size + ') input OK', stdout)
self.assertIn('open(' + size + ') output OK', stdout)
self.assertIn('fstat(' + size + ') output 1 OK', stdout)
if executable == 'copy_whole':
self.assertIn('read_fd(' + size + ') input OK', stdout)
self.assertIn('write_fd(' + size + ') output OK', stdout)
if executable == 'copy_sendfile':
self.assertIn('sendfile_fd(' + size + ') OK', stdout)
if size != '0':
if 'copy_mmap' in executable:
self.assertIn('mmap_fd(' + size + ') input OK', stdout)
self.assertIn('mmap_fd(' + size + ') output OK', stdout)
self.assertIn('munmap_fd(' + size + ') input OK', stdout)
self.assertIn('munmap_fd(' + size + ') output OK', stdout)
if executable == 'copy_mmap_rev':
self.assertIn('ftruncate(' + size + ') output OK', stdout)
self.assertIn('fstat(' + size + ') output 2 OK', stdout)
self.assertIn('close(' + size + ') input OK', stdout)
self.assertIn('close(' + size + ') output OK', stdout)
# compare
for i in self.INDEXES:
self.verify_copy_content(self.INPUT_FILES[i], self.OUTPUT_FILES[i])
def do_copy_test(self, executable, timeout):
stdout, stderr = self.run_binary([executable, self.INPUT_DIR, self.OUTPUT_DIR],
timeout=timeout)
self.verify_copy(stdout, stderr, self.INPUT_DIR, executable)
def test_200_copy_dir_whole(self):
self.do_copy_test('copy_whole', 30)
def test_201_copy_dir_seq(self):
self.do_copy_test('copy_seq', 60)
def test_202_copy_dir_rev(self):
self.do_copy_test('copy_rev', 60)
def test_203_copy_dir_sendfile(self):
self.do_copy_test('copy_sendfile', 60)
# Gramine's implementation of file_map doesn't currently support shared memory-mapped
# files with write permission in PAL/Linux-SGX (like mmap(PROT_WRITE, MAP_SHARED, fd)).
# These tests require it, so skip them. We decided not to implement it as we don't
# know any workload using it.
@unittest.skipIf(HAS_SGX, 'mmap(PROT_WRITE, MAP_SHARED, fd) not implemented in Linux-SGX PAL')
def test_204_copy_dir_mmap_whole(self):
self.do_copy_test('copy_mmap_whole', 30)
@unittest.skipIf(HAS_SGX, 'mmap(PROT_WRITE, MAP_SHARED, fd) not implemented in Linux-SGX PAL')
def test_205_copy_dir_mmap_seq(self):
self.do_copy_test('copy_mmap_seq', 60)
@unittest.skipIf(HAS_SGX, 'mmap(PROT_WRITE, MAP_SHARED, fd) not implemented in Linux-SGX PAL')
def test_206_copy_dir_mmap_rev(self):
self.do_copy_test('copy_mmap_rev', 60)
def test_210_copy_dir_mounted(self):
executable = 'copy_whole'
stdout, stderr = self.run_binary([executable, '/mounted/input', '/mounted/output'],
timeout=30)
self.verify_copy(stdout, stderr, '/mounted/input', executable)
class TC_01_Sync(RegressionTestCase):
TEST_DIR = 'tmp'
def setUp(self):
shutil.rmtree(self.TEST_DIR, ignore_errors=True)
os.mkdir(self.TEST_DIR)
def tearDown(self):
shutil.rmtree(self.TEST_DIR)
def _test_multiple_writers(self, n_lines, n_processes, n_threads):
output_path = os.path.join(self.TEST_DIR, 'output.txt')
self.run_binary(['multiple_writers',
output_path, str(n_lines), str(n_processes), str(n_threads)])
with open(output_path, 'r') as f:
lines = f.readlines()
expected_lines = []
for i in range(n_processes):
for j in range(n_threads):
for k in range(n_lines):
expected_lines.append('%04d %04d %04d: proc %d thread %d line %d\n' % (
i, j, k, i, j, k))
self.assertListEqual(sorted(lines), expected_lines)
def test_001_multiple_writers_many_threads(self):
self._test_multiple_writers(20, 1, 5)
@unittest.skip('file handle sync is not supported yet')
def test_002_multiple_writers_many_processes(self):
self._test_multiple_writers(20, 5, 1)
@unittest.skip('file handle sync is not supported yet')
def test_003_multiple_writers_many_processes_and_threads(self):
self._test_multiple_writers(20, 5, 5)