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