blob: b5f4704172a28b213e42a265ce4b3885f18dce97 [file] [log] [blame]
AKASHI Takahiro615af9a2018-09-11 15:59:19 +09001# 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 *
Jean-Jacques Hiblotd2fd35c2019-02-13 12:15:23 +010014from fstest_helpers import assert_fs_integrity
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090015
16@pytest.mark.boardspec('sandbox')
Simon Glass3fcb8282018-11-18 08:14:29 -070017@pytest.mark.slow
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090018class TestFsBasic(object):
19 def test_fs1(self, u_boot_console, fs_obj_basic):
20 """
21 Test Case 1 - ls command, listing a root directory and invalid directory
22 """
23 fs_type,fs_img,md5val = fs_obj_basic
24 with u_boot_console.log.section('Test Case 1a - ls'):
25 # Test Case 1 - ls
26 output = u_boot_console.run_command_list([
27 'host bind 0 %s' % fs_img,
28 '%sls host 0:0' % fs_type])
29 assert(re.search('2621440000 *%s' % BIG_FILE, ''.join(output)))
30 assert(re.search('1048576 *%s' % SMALL_FILE, ''.join(output)))
31
32 with u_boot_console.log.section('Test Case 1b - ls (invalid dir)'):
33 # In addition, test with a nonexistent directory to see if we crash.
34 output = u_boot_console.run_command(
35 '%sls host 0:0 invalid_d' % fs_type)
Heinrich Schuchardt19c781d2024-10-26 08:40:48 +020036 assert('' == output)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090037
38 def test_fs2(self, u_boot_console, fs_obj_basic):
39 """
40 Test Case 2 - size command for a small file
41 """
42 fs_type,fs_img,md5val = fs_obj_basic
43 with u_boot_console.log.section('Test Case 2a - size (small)'):
44 # 1MB is 0x0010 0000
45 # Test Case 2a - size of small file
46 output = u_boot_console.run_command_list([
47 'host bind 0 %s' % fs_img,
48 '%ssize host 0:0 /%s' % (fs_type, SMALL_FILE),
49 'printenv filesize',
50 'setenv filesize'])
51 assert('filesize=100000' in ''.join(output))
52
53 with u_boot_console.log.section('Test Case 2b - size (/../<file>)'):
54 # Test Case 2b - size of small file via a path using '..'
55 output = u_boot_console.run_command_list([
56 '%ssize host 0:0 /SUBDIR/../%s' % (fs_type, SMALL_FILE),
57 'printenv filesize',
58 'setenv filesize'])
59 assert('filesize=100000' in ''.join(output))
60
61 def test_fs3(self, u_boot_console, fs_obj_basic):
62 """
63 Test Case 3 - size command for a large file
64 """
65 fs_type,fs_img,md5val = fs_obj_basic
66 with u_boot_console.log.section('Test Case 3 - size (large)'):
67 # 2.5GB (1024*1024*2500) is 0x9C40 0000
68 # Test Case 3 - size of big file
69 output = u_boot_console.run_command_list([
70 'host bind 0 %s' % fs_img,
71 '%ssize host 0:0 /%s' % (fs_type, BIG_FILE),
72 'printenv filesize',
73 'setenv filesize'])
74 assert('filesize=9c400000' in ''.join(output))
75
76 def test_fs4(self, u_boot_console, fs_obj_basic):
77 """
78 Test Case 4 - load a small file, 1MB
79 """
80 fs_type,fs_img,md5val = fs_obj_basic
81 with u_boot_console.log.section('Test Case 4 - load (small)'):
82 # Test Case 4a - Read full 1MB of small file
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, SMALL_FILE),
86 'printenv filesize'])
87 assert('filesize=100000' in ''.join(output))
88
89 # Test Case 4b - Read full 1MB of small file
90 output = u_boot_console.run_command_list([
91 'md5sum %x $filesize' % ADDR,
92 'setenv filesize'])
93 assert(md5val[0] in ''.join(output))
94
95 def test_fs5(self, u_boot_console, fs_obj_basic):
96 """
97 Test Case 5 - load, reading first 1MB of 3GB file
98 """
99 fs_type,fs_img,md5val = fs_obj_basic
100 with u_boot_console.log.section('Test Case 5 - load (first 1MB)'):
101 # Test Case 5a - First 1MB of big file
102 output = u_boot_console.run_command_list([
103 'host bind 0 %s' % fs_img,
104 '%sload host 0:0 %x /%s %x 0x0' % (fs_type, ADDR, BIG_FILE, LENGTH),
105 'printenv filesize'])
106 assert('filesize=100000' in ''.join(output))
107
108 # Test Case 5b - First 1MB of big file
109 output = u_boot_console.run_command_list([
110 'md5sum %x $filesize' % ADDR,
111 'setenv filesize'])
112 assert(md5val[1] in ''.join(output))
113
114 def test_fs6(self, u_boot_console, fs_obj_basic):
115 """
116 Test Case 6 - load, reading last 1MB of 3GB file
117 """
118 fs_type,fs_img,md5val = fs_obj_basic
119 with u_boot_console.log.section('Test Case 6 - load (last 1MB)'):
120 # fails for ext as no offset support
121 # Test Case 6a - Last 1MB of big file
122 output = u_boot_console.run_command_list([
123 'host bind 0 %s' % fs_img,
124 '%sload host 0:0 %x /%s %x 0x9c300000'
125 % (fs_type, ADDR, BIG_FILE, LENGTH),
126 'printenv filesize'])
127 assert('filesize=100000' in ''.join(output))
128
129 # Test Case 6b - Last 1MB of big file
130 output = u_boot_console.run_command_list([
131 'md5sum %x $filesize' % ADDR,
132 'setenv filesize'])
133 assert(md5val[2] in ''.join(output))
134
135 def test_fs7(self, u_boot_console, fs_obj_basic):
136 """
137 Test Case 7 - load, 1MB from the last 1MB in 2GB
138 """
139 fs_type,fs_img,md5val = fs_obj_basic
140 with u_boot_console.log.section('Test Case 7 - load (last 1MB in 2GB)'):
141 # fails for ext as no offset support
142 # Test Case 7a - One from the last 1MB chunk of 2GB
143 output = u_boot_console.run_command_list([
144 'host bind 0 %s' % fs_img,
145 '%sload host 0:0 %x /%s %x 0x7ff00000'
146 % (fs_type, ADDR, BIG_FILE, LENGTH),
147 'printenv filesize'])
148 assert('filesize=100000' in ''.join(output))
149
150 # Test Case 7b - One from the last 1MB chunk of 2GB
151 output = u_boot_console.run_command_list([
152 'md5sum %x $filesize' % ADDR,
153 'setenv filesize'])
154 assert(md5val[3] in ''.join(output))
155
156 def test_fs8(self, u_boot_console, fs_obj_basic):
157 """
158 Test Case 8 - load, reading first 1MB in 2GB
159 """
160 fs_type,fs_img,md5val = fs_obj_basic
161 with u_boot_console.log.section('Test Case 8 - load (first 1MB in 2GB)'):
162 # fails for ext as no offset support
163 # Test Case 8a - One from the start 1MB chunk from 2GB
164 output = u_boot_console.run_command_list([
165 'host bind 0 %s' % fs_img,
166 '%sload host 0:0 %x /%s %x 0x80000000'
167 % (fs_type, ADDR, BIG_FILE, LENGTH),
168 'printenv filesize'])
169 assert('filesize=100000' in ''.join(output))
170
171 # Test Case 8b - One from the start 1MB chunk from 2GB
172 output = u_boot_console.run_command_list([
173 'md5sum %x $filesize' % ADDR,
174 'setenv filesize'])
175 assert(md5val[4] in ''.join(output))
176
177 def test_fs9(self, u_boot_console, fs_obj_basic):
178 """
179 Test Case 9 - load, 1MB crossing 2GB boundary
180 """
181 fs_type,fs_img,md5val = fs_obj_basic
182 with u_boot_console.log.section('Test Case 9 - load (crossing 2GB boundary)'):
183 # fails for ext as no offset support
184 # Test Case 9a - One 1MB chunk crossing the 2GB boundary
185 output = u_boot_console.run_command_list([
186 'host bind 0 %s' % fs_img,
187 '%sload host 0:0 %x /%s %x 0x7ff80000'
188 % (fs_type, ADDR, BIG_FILE, LENGTH),
189 'printenv filesize'])
190 assert('filesize=100000' in ''.join(output))
191
192 # Test Case 9b - One 1MB chunk crossing the 2GB boundary
193 output = u_boot_console.run_command_list([
194 'md5sum %x $filesize' % ADDR,
195 'setenv filesize'])
196 assert(md5val[5] in ''.join(output))
197
198 def test_fs10(self, u_boot_console, fs_obj_basic):
199 """
200 Test Case 10 - load, reading beyond file end'):
201 """
202 fs_type,fs_img,md5val = fs_obj_basic
203 with u_boot_console.log.section('Test Case 10 - load (beyond file end)'):
204 # Generic failure case
205 # Test Case 10 - 2MB chunk from the last 1MB of big file
206 output = u_boot_console.run_command_list([
207 'host bind 0 %s' % fs_img,
208 '%sload host 0:0 %x /%s 0x00200000 0x9c300000'
209 % (fs_type, ADDR, BIG_FILE),
210 'printenv filesize',
211 'md5sum %x $filesize' % ADDR,
212 'setenv filesize'])
213 assert('filesize=100000' in ''.join(output))
214
215 def test_fs11(self, u_boot_console, fs_obj_basic):
216 """
217 Test Case 11 - write'
218 """
219 fs_type,fs_img,md5val = fs_obj_basic
220 with u_boot_console.log.section('Test Case 11 - write'):
221 # Read 1MB from small file
222 # Write it back to test the writes
223 # Test Case 11a - Check that the write succeeded
224 output = u_boot_console.run_command_list([
225 'host bind 0 %s' % fs_img,
226 '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE),
227 '%swrite host 0:0 %x /%s.w $filesize'
228 % (fs_type, ADDR, SMALL_FILE)])
229 assert('1048576 bytes written' in ''.join(output))
230
231 # Test Case 11b - Check md5 of written to is same
232 # as the one read from
233 output = u_boot_console.run_command_list([
234 '%sload host 0:0 %x /%s.w' % (fs_type, ADDR, SMALL_FILE),
235 'md5sum %x $filesize' % ADDR,
236 'setenv filesize'])
237 assert(md5val[0] in ''.join(output))
Jean-Jacques Hiblotd2fd35c2019-02-13 12:15:23 +0100238 assert_fs_integrity(fs_type, fs_img)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900239
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))
Jean-Jacques Hiblotd2fd35c2019-02-13 12:15:23 +0100254 assert_fs_integrity(fs_type, fs_img)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900255
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))
Jean-Jacques Hiblotd2fd35c2019-02-13 12:15:23 +0100289 assert_fs_integrity(fs_type, fs_img)