1# SPDX-License-Identifier:      GPL-2.0+
2# Copyright (c) 2018, Linaro Limited
3# Author: Takahiro Akashi <takahiro.akashi@linaro.org>
4#
5# U-Boot File System:Basic Test
6
7"""
8This test verifies basic read/write operation on file system.
9"""
10
11import pytest
12import re
13from fstest_defs import *
14from fstest_helpers import assert_fs_integrity
15
16@pytest.mark.boardspec('sandbox')
17@pytest.mark.slow
18class TestFsBasic(object):
19    def test_fs1(self, ubman, fs_obj_basic):
20        """
21        Test Case 1 - ls command, listing a root directory and invalid directory
22        """
23        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
24        with ubman.log.section('Test Case 1a - ls'):
25            # Test Case 1 - ls
26            output = ubman.run_command_list([
27                'host bind 0 %s' % fs_img,
28                '%sls host 0:0' % fs_cmd_prefix])
29            assert(re.search('2621440000 *%s' % BIG_FILE, ''.join(output)))
30            assert(re.search('1048576 *%s' % SMALL_FILE, ''.join(output)))
31
32        with ubman.log.section('Test Case 1b - ls (invalid dir)'):
33            # In addition, test with a nonexistent directory to see if we crash.
34            output = ubman.run_command(
35                '%sls host 0:0 invalid_d' % fs_cmd_prefix)
36            assert('' == output)
37
38        with ubman.log.section('Test Case 1c - test -e'):
39            # Test Case 1 - test -e
40            output = ubman.run_command_list([
41                'host bind 0 %s' % fs_img,
42                'test -e host 0:0 1MB.file && echo PASS'])
43            assert('PASS' in ''.join(output))
44
45        with ubman.log.section('Test Case 1d - test -e (invalid file)'):
46            # In addition, test with a nonexistent file to see if we crash.
47            output = ubman.run_command(
48                'test -e host 0:0 2MB.file || echo PASS')
49            assert('PASS' in ''.join(output))
50
51    def test_fs2(self, ubman, fs_obj_basic):
52        """
53        Test Case 2 - size command for a small file
54        """
55        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
56        with ubman.log.section('Test Case 2a - size (small)'):
57            # 1MB is 0x0010 0000
58            # Test Case 2a - size of small file
59            output = ubman.run_command_list([
60                'host bind 0 %s' % fs_img,
61                '%ssize host 0:0 /%s' % (fs_cmd_prefix, SMALL_FILE),
62                'printenv filesize',
63                'setenv filesize'])
64            assert('filesize=100000' in ''.join(output))
65
66        with ubman.log.section('Test Case 2b - size (/../<file>)'):
67            # Test Case 2b - size of small file via a path using '..'
68            output = ubman.run_command_list([
69                '%ssize host 0:0 /SUBDIR/../%s' % (fs_cmd_prefix, SMALL_FILE),
70                'printenv filesize',
71                'setenv filesize'])
72            assert('filesize=100000' in ''.join(output))
73
74    def test_fs3(self, ubman, fs_obj_basic):
75        """
76        Test Case 3 - size command for a large file
77        """
78        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
79        with ubman.log.section('Test Case 3 - size (large)'):
80            # 2.5GB (1024*1024*2500) is 0x9C40 0000
81            # Test Case 3 - size of big file
82            output = ubman.run_command_list([
83                'host bind 0 %s' % fs_img,
84                '%ssize host 0:0 /%s' % (fs_cmd_prefix, BIG_FILE),
85                'printenv filesize',
86                'setenv filesize'])
87            assert('filesize=9c400000' in ''.join(output))
88
89    def test_fs4(self, ubman, fs_obj_basic):
90        """
91        Test Case 4 - load a small file, 1MB
92        """
93        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
94        with ubman.log.section('Test Case 4 - load (small)'):
95            # Test Case 4a - Read full 1MB of small file
96            output = ubman.run_command_list([
97                'host bind 0 %s' % fs_img,
98                '%sload host 0:0 %x /%s' % (fs_cmd_prefix, ADDR, SMALL_FILE),
99                'printenv filesize'])
100            assert('filesize=100000' in ''.join(output))
101
102            # Test Case 4b - Read full 1MB of small file
103            output = ubman.run_command_list([
104                'md5sum %x $filesize' % ADDR,
105                'setenv filesize'])
106            assert(md5val[0] in ''.join(output))
107
108    def test_fs5(self, ubman, fs_obj_basic):
109        """
110        Test Case 5 - load, reading first 1MB of 3GB file
111        """
112        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
113        with ubman.log.section('Test Case 5 - load (first 1MB)'):
114            # Test Case 5a - First 1MB of big file
115            output = ubman.run_command_list([
116                'host bind 0 %s' % fs_img,
117                '%sload host 0:0 %x /%s %x 0x0' % (fs_cmd_prefix, ADDR, BIG_FILE, LENGTH),
118                'printenv filesize'])
119            assert('filesize=100000' in ''.join(output))
120
121            # Test Case 5b - First 1MB of big file
122            output = ubman.run_command_list([
123                'md5sum %x $filesize' % ADDR,
124                'setenv filesize'])
125            assert(md5val[1] in ''.join(output))
126
127    def test_fs6(self, ubman, fs_obj_basic):
128        """
129        Test Case 6 - load, reading last 1MB of 3GB file
130        """
131        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
132        with ubman.log.section('Test Case 6 - load (last 1MB)'):
133            # fails for ext as no offset support
134            # Test Case 6a - Last 1MB of big file
135            output = ubman.run_command_list([
136                'host bind 0 %s' % fs_img,
137                '%sload host 0:0 %x /%s %x 0x9c300000'
138                    % (fs_cmd_prefix, ADDR, BIG_FILE, LENGTH),
139                'printenv filesize'])
140            assert('filesize=100000' in ''.join(output))
141
142            # Test Case 6b - Last 1MB of big file
143            output = ubman.run_command_list([
144                'md5sum %x $filesize' % ADDR,
145                'setenv filesize'])
146            assert(md5val[2] in ''.join(output))
147
148    def test_fs7(self, ubman, fs_obj_basic):
149        """
150        Test Case 7 - load, 1MB from the last 1MB in 2GB
151        """
152        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
153        with ubman.log.section('Test Case 7 - load (last 1MB in 2GB)'):
154            # fails for ext as no offset support
155            # Test Case 7a - One from the last 1MB chunk of 2GB
156            output = ubman.run_command_list([
157                'host bind 0 %s' % fs_img,
158                '%sload host 0:0 %x /%s %x 0x7ff00000'
159                    % (fs_cmd_prefix, ADDR, BIG_FILE, LENGTH),
160                'printenv filesize'])
161            assert('filesize=100000' in ''.join(output))
162
163            # Test Case 7b - One from the last 1MB chunk of 2GB
164            output = ubman.run_command_list([
165                'md5sum %x $filesize' % ADDR,
166                'setenv filesize'])
167            assert(md5val[3] in ''.join(output))
168
169    def test_fs8(self, ubman, fs_obj_basic):
170        """
171        Test Case 8 - load, reading first 1MB in 2GB
172        """
173        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
174        with ubman.log.section('Test Case 8 - load (first 1MB in 2GB)'):
175            # fails for ext as no offset support
176            # Test Case 8a - One from the start 1MB chunk from 2GB
177            output = ubman.run_command_list([
178                'host bind 0 %s' % fs_img,
179                '%sload host 0:0 %x /%s %x 0x80000000'
180                    % (fs_cmd_prefix, ADDR, BIG_FILE, LENGTH),
181                'printenv filesize'])
182            assert('filesize=100000' in ''.join(output))
183
184            # Test Case 8b - One from the start 1MB chunk from 2GB
185            output = ubman.run_command_list([
186                'md5sum %x $filesize' % ADDR,
187                'setenv filesize'])
188            assert(md5val[4] in ''.join(output))
189
190    def test_fs9(self, ubman, fs_obj_basic):
191        """
192        Test Case 9 - load, 1MB crossing 2GB boundary
193        """
194        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
195        with ubman.log.section('Test Case 9 - load (crossing 2GB boundary)'):
196            # fails for ext as no offset support
197            # Test Case 9a - One 1MB chunk crossing the 2GB boundary
198            output = ubman.run_command_list([
199                'host bind 0 %s' % fs_img,
200                '%sload host 0:0 %x /%s %x 0x7ff80000'
201                    % (fs_cmd_prefix, ADDR, BIG_FILE, LENGTH),
202                'printenv filesize'])
203            assert('filesize=100000' in ''.join(output))
204
205            # Test Case 9b - One 1MB chunk crossing the 2GB boundary
206            output = ubman.run_command_list([
207                'md5sum %x $filesize' % ADDR,
208                'setenv filesize'])
209            assert(md5val[5] in ''.join(output))
210
211    def test_fs10(self, ubman, fs_obj_basic):
212        """
213        Test Case 10 - load, reading beyond file end'):
214        """
215        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
216        with ubman.log.section('Test Case 10 - load (beyond file end)'):
217            # Generic failure case
218            # Test Case 10 - 2MB chunk from the last 1MB of big file
219            output = ubman.run_command_list([
220                'host bind 0 %s' % fs_img,
221                '%sload host 0:0 %x /%s 0x00200000 0x9c300000'
222                    % (fs_cmd_prefix, ADDR, BIG_FILE),
223                'printenv filesize',
224                'md5sum %x $filesize' % ADDR,
225                'setenv filesize'])
226        assert('filesize=100000' in ''.join(output))
227
228    def test_fs11(self, ubman, fs_obj_basic):
229        """
230        Test Case 11 - write'
231        """
232        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
233        with ubman.log.section('Test Case 11 - write'):
234            # Read 1MB from small file
235            # Write it back to test the writes
236            # Test Case 11a - Check that the write succeeded
237            output = ubman.run_command_list([
238                'host bind 0 %s' % fs_img,
239                '%sload host 0:0 %x /%s' % (fs_cmd_prefix, ADDR, SMALL_FILE),
240                '%s%s host 0:0 %x /%s.w $filesize'
241                    % (fs_cmd_prefix, fs_cmd_write, ADDR, SMALL_FILE)])
242            assert('1048576 bytes written' in ''.join(output))
243
244            # Test Case 11b - Check md5 of written to is same
245            # as the one read from
246            output = ubman.run_command_list([
247                '%sload host 0:0 %x /%s.w' % (fs_cmd_prefix, ADDR, SMALL_FILE),
248                'md5sum %x $filesize' % ADDR,
249                'setenv filesize'])
250            assert(md5val[0] in ''.join(output))
251            assert_fs_integrity(fs_type, fs_img)
252
253    def test_fs12(self, ubman, fs_obj_basic):
254        """
255        Test Case 12 - write to "." directory
256        """
257        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
258        with ubman.log.section('Test Case 12 - write (".")'):
259            # Next test case checks writing a file whose dirent
260            # is the first in the block, which is always true for "."
261            # The write should fail, but the lookup should work
262            # Test Case 12 - Check directory traversal
263            output = ubman.run_command_list([
264                'host bind 0 %s' % fs_img,
265                '%s%s host 0:0 %x /. 0x10'
266                    % (fs_cmd_prefix, fs_cmd_write, ADDR)])
267            assert('Unable to write' in ''.join(output))
268            assert_fs_integrity(fs_type, fs_img)
269
270    def test_fs13(self, ubman, fs_obj_basic):
271        """
272        Test Case 13 - write to a file with "/./<filename>"
273        """
274        fs_type,fs_cmd_prefix,fs_cmd_write,fs_img,md5val = fs_obj_basic
275        with ubman.log.section('Test Case 13 - write  ("./<file>")'):
276            # Read 1MB from small file
277            # Write it via "same directory", i.e. "." dirent
278            # Test Case 13a - Check directory traversal
279            output = ubman.run_command_list([
280                'host bind 0 %s' % fs_img,
281                '%sload host 0:0 %x /%s' % (fs_cmd_prefix, ADDR, SMALL_FILE),
282                '%s%s host 0:0 %x /./%s2 $filesize'
283                    % (fs_cmd_prefix, fs_cmd_write, ADDR, SMALL_FILE)])
284            assert('1048576 bytes written' in ''.join(output))
285
286            # Test Case 13b - Check md5 of written to is same
287            # as the one read from
288            output = ubman.run_command_list([
289                'mw.b %x 00 100' % ADDR,
290                '%sload host 0:0 %x /./%s2' % (fs_cmd_prefix, ADDR, SMALL_FILE),
291                'md5sum %x $filesize' % ADDR,
292                'setenv filesize'])
293            assert(md5val[0] in ''.join(output))
294
295            # Test Case 13c - Check md5 of written to is same
296            # as the one read from
297            output = ubman.run_command_list([
298                'mw.b %x 00 100' % ADDR,
299                '%sload host 0:0 %x /%s2' % (fs_cmd_prefix, ADDR, SMALL_FILE),
300                'md5sum %x $filesize' % ADDR,
301                'setenv filesize'])
302            assert(md5val[0] in ''.join(output))
303            assert_fs_integrity(fs_type, fs_img)
304