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