xref: /openbmc/u-boot/test/py/tests/test_fs/test_ext.py (revision 5396b2a9)
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:Exntented Test
6
7"""
8This test verifies extended 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 TestFsExt(object):
18    def test_fs_ext1(self, u_boot_console, fs_obj_ext):
19        """
20        Test Case 1 - write a file with absolute path
21        """
22        fs_type,fs_img,md5val = fs_obj_ext
23        with u_boot_console.log.section('Test Case 1 - write with abs path'):
24            # Test Case 1a - Check if command successfully returned
25            output = u_boot_console.run_command_list([
26                'host bind 0 %s' % fs_img,
27                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
28                '%swrite host 0:0 %x /dir1/%s.w1 $filesize'
29                    % (fs_type, ADDR, MIN_FILE)])
30            assert('20480 bytes written' in ''.join(output))
31
32            # Test Case 1b - Check md5 of file content
33            output = u_boot_console.run_command_list([
34                'mw.b %x 00 100' % ADDR,
35                '%sload host 0:0 %x /dir1/%s.w1' % (fs_type, ADDR, MIN_FILE),
36                'md5sum %x $filesize' % ADDR,
37                'setenv filesize'])
38            assert(md5val[0] in ''.join(output))
39
40    def test_fs_ext2(self, u_boot_console, fs_obj_ext):
41        """
42        Test Case 2 - write to a file with relative path
43        """
44        fs_type,fs_img,md5val = fs_obj_ext
45        with u_boot_console.log.section('Test Case 2 - write with rel path'):
46            # Test Case 2a - Check if command successfully returned
47            output = u_boot_console.run_command_list([
48                'host bind 0 %s' % fs_img,
49                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
50                '%swrite host 0:0 %x dir1/%s.w2 $filesize'
51                    % (fs_type, ADDR, MIN_FILE)])
52            assert('20480 bytes written' in ''.join(output))
53
54            # Test Case 2b - Check md5 of file content
55            output = u_boot_console.run_command_list([
56                'mw.b %x 00 100' % ADDR,
57                '%sload host 0:0 %x dir1/%s.w2' % (fs_type, ADDR, MIN_FILE),
58                'md5sum %x $filesize' % ADDR,
59                'setenv filesize'])
60            assert(md5val[0] in ''.join(output))
61
62    def test_fs_ext3(self, u_boot_console, fs_obj_ext):
63        """
64        Test Case 3 - write to a file with invalid path
65        """
66        fs_type,fs_img,md5val = fs_obj_ext
67        with u_boot_console.log.section('Test Case 3 - write with invalid path'):
68            # Test Case 3 - Check if command expectedly failed
69            output = u_boot_console.run_command_list([
70                'host bind 0 %s' % fs_img,
71                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
72                '%swrite host 0:0 %x /dir1/none/%s.w3 $filesize'
73                    % (fs_type, ADDR, MIN_FILE)])
74            assert('Unable to write "/dir1/none/' in ''.join(output))
75
76    def test_fs_ext4(self, u_boot_console, fs_obj_ext):
77        """
78        Test Case 4 - write at non-zero offset, enlarging file size
79        """
80        fs_type,fs_img,md5val = fs_obj_ext
81        with u_boot_console.log.section('Test Case 4 - write at non-zero offset, enlarging file size'):
82            # Test Case 4a - Check if command successfully returned
83            output = u_boot_console.run_command_list([
84                'host bind 0 %s' % fs_img,
85                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
86                '%swrite host 0:0 %x /dir1/%s.w4 $filesize'
87                    % (fs_type, ADDR, MIN_FILE)])
88            output = u_boot_console.run_command(
89                '%swrite host 0:0 %x /dir1/%s.w4 $filesize 0x1400'
90                    % (fs_type, ADDR, MIN_FILE))
91            assert('20480 bytes written' in output)
92
93            # Test Case 4b - Check size of written file
94            output = u_boot_console.run_command_list([
95                '%ssize host 0:0 /dir1/%s.w4' % (fs_type, MIN_FILE),
96                'printenv filesize',
97                'setenv filesize'])
98            assert('filesize=6400' in ''.join(output))
99
100            # Test Case 4c - Check md5 of file content
101            output = u_boot_console.run_command_list([
102                'mw.b %x 00 100' % ADDR,
103                '%sload host 0:0 %x /dir1/%s.w4' % (fs_type, ADDR, MIN_FILE),
104                'md5sum %x $filesize' % ADDR,
105                'setenv filesize'])
106            assert(md5val[1] in ''.join(output))
107
108    def test_fs_ext5(self, u_boot_console, fs_obj_ext):
109        """
110        Test Case 5 - write at non-zero offset, shrinking file size
111        """
112        fs_type,fs_img,md5val = fs_obj_ext
113        with u_boot_console.log.section('Test Case 5 - write at non-zero offset, shrinking file size'):
114            # Test Case 5a - Check if command successfully returned
115            output = u_boot_console.run_command_list([
116                'host bind 0 %s' % fs_img,
117                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
118                '%swrite host 0:0 %x /dir1/%s.w5 $filesize'
119                    % (fs_type, ADDR, MIN_FILE)])
120            output = u_boot_console.run_command(
121                '%swrite host 0:0 %x /dir1/%s.w5 0x1400 0x1400'
122                    % (fs_type, ADDR, MIN_FILE))
123            assert('5120 bytes written' in output)
124
125            # Test Case 5b - Check size of written file
126            output = u_boot_console.run_command_list([
127                '%ssize host 0:0 /dir1/%s.w5' % (fs_type, MIN_FILE),
128                'printenv filesize',
129                'setenv filesize'])
130            assert('filesize=2800' in ''.join(output))
131
132            # Test Case 5c - Check md5 of file content
133            output = u_boot_console.run_command_list([
134                'mw.b %x 00 100' % ADDR,
135                '%sload host 0:0 %x /dir1/%s.w5' % (fs_type, ADDR, MIN_FILE),
136                'md5sum %x $filesize' % ADDR,
137                'setenv filesize'])
138            assert(md5val[2] in ''.join(output))
139
140    def test_fs_ext6(self, u_boot_console, fs_obj_ext):
141        """
142        Test Case 6 - write nothing at the start, truncating to zero
143        """
144        fs_type,fs_img,md5val = fs_obj_ext
145        with u_boot_console.log.section('Test Case 6 - write nothing at the start, truncating to zero'):
146            # Test Case 6a - Check if command successfully returned
147            output = u_boot_console.run_command_list([
148                'host bind 0 %s' % fs_img,
149                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
150                '%swrite host 0:0 %x /dir1/%s.w6 $filesize'
151                    % (fs_type, ADDR, MIN_FILE)])
152            output = u_boot_console.run_command(
153                '%swrite host 0:0 %x /dir1/%s.w6 0 0'
154                    % (fs_type, ADDR, MIN_FILE))
155            assert('0 bytes written' in output)
156
157            # Test Case 6b - Check size of written file
158            output = u_boot_console.run_command_list([
159                '%ssize host 0:0 /dir1/%s.w6' % (fs_type, MIN_FILE),
160                'printenv filesize',
161                'setenv filesize'])
162            assert('filesize=0' in ''.join(output))
163
164    def test_fs_ext7(self, u_boot_console, fs_obj_ext):
165        """
166        Test Case 7 - write at the end (append)
167        """
168        fs_type,fs_img,md5val = fs_obj_ext
169        with u_boot_console.log.section('Test Case 7 - write at the end (append)'):
170            # Test Case 7a - Check if command successfully returned
171            output = u_boot_console.run_command_list([
172                'host bind 0 %s' % fs_img,
173                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
174                '%swrite host 0:0 %x /dir1/%s.w7 $filesize'
175                    % (fs_type, ADDR, MIN_FILE)])
176            output = u_boot_console.run_command(
177                '%swrite host 0:0 %x /dir1/%s.w7 $filesize $filesize'
178                    % (fs_type, ADDR, MIN_FILE))
179            assert('20480 bytes written' in output)
180
181            # Test Case 7b - Check size of written file
182            output = u_boot_console.run_command_list([
183                '%ssize host 0:0 /dir1/%s.w7' % (fs_type, MIN_FILE),
184                'printenv filesize',
185                'setenv filesize'])
186            assert('filesize=a000' in ''.join(output))
187
188            # Test Case 7c - Check md5 of file content
189            output = u_boot_console.run_command_list([
190                'mw.b %x 00 100' % ADDR,
191                '%sload host 0:0 %x /dir1/%s.w7' % (fs_type, ADDR, MIN_FILE),
192                'md5sum %x $filesize' % ADDR,
193                'setenv filesize'])
194            assert(md5val[3] in ''.join(output))
195
196    def test_fs_ext8(self, u_boot_console, fs_obj_ext):
197        """
198        Test Case 8 - write at offset beyond the end of file
199        """
200        fs_type,fs_img,md5val = fs_obj_ext
201        with u_boot_console.log.section('Test Case 8 - write beyond the end'):
202            # Test Case 8a - Check if command expectedly failed
203            output = u_boot_console.run_command_list([
204                'host bind 0 %s' % fs_img,
205                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
206                '%swrite host 0:0 %x /dir1/%s.w8 $filesize'
207                    % (fs_type, ADDR, MIN_FILE)])
208            output = u_boot_console.run_command(
209                '%swrite host 0:0 %x /dir1/%s.w8 0x1400 %x'
210                    % (fs_type, ADDR, MIN_FILE, 0x100000 + 0x1400))
211            assert('Unable to write "/dir1' in output)
212
213    def test_fs_ext9(self, u_boot_console, fs_obj_ext):
214        """
215        Test Case 9 - write to a non-existing file at non-zero offset
216        """
217        fs_type,fs_img,md5val = fs_obj_ext
218        with u_boot_console.log.section('Test Case 9 - write to non-existing file with non-zero offset'):
219            # Test Case 9a - Check if command expectedly failed
220            output = u_boot_console.run_command_list([
221                'host bind 0 %s' % fs_img,
222                '%sload host 0:0 %x /%s' % (fs_type, ADDR, MIN_FILE),
223                '%swrite host 0:0 %x /dir1/%s.w9 0x1400 0x1400'
224                    % (fs_type, ADDR, MIN_FILE)])
225            assert('Unable to write "/dir1' in ''.join(output))
226