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