blob: af2adaf1645bdf4bb4fd1aa1d32b95d102c2f16f [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
5import os
6import os.path
7import pytest
8import re
9from subprocess import call, check_call, check_output, CalledProcessError
10from fstest_defs import *
Alper Nebi Yasak7ff17f22021-06-04 22:04:46 +030011import u_boot_utils as util
Tom Rini50ad0192023-12-09 14:52:46 -050012# pylint: disable=E0611
Simon Glass1d5006c2022-10-29 19:47:05 -060013from tests import fs_helper
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090014
15supported_fs_basic = ['fat16', 'fat32', 'ext4']
Christian Taedckea1fd7352023-11-15 13:44:22 +010016supported_fs_ext = ['fat12', 'fat16', 'fat32']
Christian Taedcke570dc362023-11-15 13:44:24 +010017supported_fs_fat = ['fat12', 'fat16']
Christian Taedckea1fd7352023-11-15 13:44:22 +010018supported_fs_mkdir = ['fat12', 'fat16', 'fat32']
19supported_fs_unlink = ['fat12', 'fat16', 'fat32']
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010020supported_fs_symlink = ['ext4']
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090021
22#
23# Filesystem test specific setup
24#
25def pytest_addoption(parser):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090026 """Enable --fs-type option.
27
28 See pytest_configure() about how it works.
29
30 Args:
31 parser: Pytest command-line parser.
32
33 Returns:
34 Nothing.
35 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090036 parser.addoption('--fs-type', action='append', default=None,
37 help='Targeting Filesystem Types')
38
39def pytest_configure(config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090040 """Restrict a file system(s) to be tested.
41
42 A file system explicitly named with --fs-type option is selected
43 if it belongs to a default supported_fs_xxx list.
44 Multiple options can be specified.
45
46 Args:
47 config: Pytest configuration.
48
49 Returns:
50 Nothing.
51 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090052 global supported_fs_basic
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090053 global supported_fs_ext
Christian Taedcke570dc362023-11-15 13:44:24 +010054 global supported_fs_fat
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090055 global supported_fs_mkdir
Akashi, Takahirod49e7992018-09-11 16:06:03 +090056 global supported_fs_unlink
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010057 global supported_fs_symlink
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090058
59 def intersect(listA, listB):
60 return [x for x in listA if x in listB]
61
62 supported_fs = config.getoption('fs_type')
63 if supported_fs:
Simon Glasse9f4d872018-12-27 08:11:13 -070064 print('*** FS TYPE modified: %s' % supported_fs)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090065 supported_fs_basic = intersect(supported_fs, supported_fs_basic)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090066 supported_fs_ext = intersect(supported_fs, supported_fs_ext)
Christian Taedcke570dc362023-11-15 13:44:24 +010067 supported_fs_fat = intersect(supported_fs, supported_fs_fat)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090068 supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +090069 supported_fs_unlink = intersect(supported_fs, supported_fs_unlink)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010070 supported_fs_symlink = intersect(supported_fs, supported_fs_symlink)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090071
72def pytest_generate_tests(metafunc):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090073 """Parametrize fixtures, fs_obj_xxx
74
75 Each fixture will be parametrized with a corresponding support_fs_xxx
76 list.
77
78 Args:
79 metafunc: Pytest test function.
80
81 Returns:
82 Nothing.
83 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090084 if 'fs_obj_basic' in metafunc.fixturenames:
85 metafunc.parametrize('fs_obj_basic', supported_fs_basic,
86 indirect=True, scope='module')
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090087 if 'fs_obj_ext' in metafunc.fixturenames:
88 metafunc.parametrize('fs_obj_ext', supported_fs_ext,
89 indirect=True, scope='module')
Christian Taedcke570dc362023-11-15 13:44:24 +010090 if 'fs_obj_fat' in metafunc.fixturenames:
91 metafunc.parametrize('fs_obj_fat', supported_fs_fat,
92 indirect=True, scope='module')
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090093 if 'fs_obj_mkdir' in metafunc.fixturenames:
94 metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
95 indirect=True, scope='module')
Akashi, Takahirod49e7992018-09-11 16:06:03 +090096 if 'fs_obj_unlink' in metafunc.fixturenames:
97 metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
98 indirect=True, scope='module')
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010099 if 'fs_obj_symlink' in metafunc.fixturenames:
100 metafunc.parametrize('fs_obj_symlink', supported_fs_symlink,
101 indirect=True, scope='module')
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900102
103#
104# Helper functions
105#
106def fstype_to_ubname(fs_type):
Michal Simek50fa1182023-05-17 09:17:16 +0200107 """Convert a file system type to an U-Boot specific string
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900108
109 A generated string can be used as part of file system related commands
110 or a config name in u-boot. Currently fat16 and fat32 are handled
111 specifically.
112
113 Args:
114 fs_type: File system type.
115
116 Return:
117 A corresponding string for file system type.
118 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900119 if re.match('fat', fs_type):
120 return 'fat'
121 else:
122 return fs_type
123
124def check_ubconfig(config, fs_type):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900125 """Check whether a file system is enabled in u-boot configuration.
126
127 This function is assumed to be called in a fixture function so that
128 the whole test cases will be skipped if a given file system is not
129 enabled.
130
131 Args:
132 fs_type: File system type.
133
134 Return:
135 Nothing.
136 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900137 if not config.buildconfig.get('config_cmd_%s' % fs_type, None):
138 pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper())
139 if not config.buildconfig.get('config_%s_write' % fs_type, None):
140 pytest.skip('.config feature "%s_WRITE" not enabled'
141 % fs_type.upper())
142
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900143# from test/py/conftest.py
144def tool_is_in_path(tool):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900145 """Check whether a given command is available on host.
146
147 Args:
148 tool: Command name.
149
150 Return:
151 True if available, False if not.
152 """
Simon Glasse9f4d872018-12-27 08:11:13 -0700153 for path in os.environ['PATH'].split(os.pathsep):
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900154 fn = os.path.join(path, tool)
155 if os.path.isfile(fn) and os.access(fn, os.X_OK):
156 return True
157 return False
158
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900159#
160# Fixture for basic fs test
161# derived from test/fs/fs-test.sh
162#
Tom Rini1b91cee2021-01-28 14:39:56 -0500163@pytest.fixture()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900164def fs_obj_basic(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900165 """Set up a file system to be used in basic fs test.
166
167 Args:
168 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200169 u_boot_config: U-Boot configuration.
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900170
171 Return:
172 A fixture for basic fs test, i.e. a triplet of file system type,
173 volume file name and a list of MD5 hashes.
174 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900175 fs_type = request.param
176 fs_img = ''
177
178 fs_ubtype = fstype_to_ubname(fs_type)
179 check_ubconfig(u_boot_config, fs_ubtype)
180
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700181 scratch_dir = u_boot_config.persistent_data_dir + '/scratch'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900182
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700183 small_file = scratch_dir + '/' + SMALL_FILE
184 big_file = scratch_dir + '/' + BIG_FILE
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900185
186 try:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700187 check_call('mkdir -p %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200188 except CalledProcessError as err:
189 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200190 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300191 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200192
193 try:
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900194 # Create a subdirectory.
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700195 check_call('mkdir %s/SUBDIR' % scratch_dir, shell=True)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900196
197 # Create big file in this image.
198 # Note that we work only on the start 1MB, couple MBs in the 2GB range
199 # and the last 1 MB of the huge 2.5GB file.
200 # So, just put random values only in those areas.
201 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
202 % big_file, shell=True)
203 check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047'
204 % big_file, shell=True)
205 check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499'
206 % big_file, shell=True)
207
208 # Create a small file in this image.
209 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
210 % small_file, shell=True)
211
212 # Delete the small file copies which possibly are written as part of a
213 # previous test.
214 # check_call('rm -f "%s.w"' % MB1, shell=True)
215 # check_call('rm -f "%s.w2"' % MB1, shell=True)
216
217 # Generate the md5sums of reads that we will test against small file
218 out = check_output(
219 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400220 % small_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900221 md5val = [ out.split()[0] ]
222
223 # Generate the md5sums of reads that we will test against big file
224 # One from beginning of file.
225 out = check_output(
226 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400227 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900228 md5val.append(out.split()[0])
229
230 # One from end of file.
231 out = check_output(
232 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400233 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900234 md5val.append(out.split()[0])
235
236 # One from the last 1MB chunk of 2GB
237 out = check_output(
238 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400239 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900240 md5val.append(out.split()[0])
241
242 # One from the start 1MB chunk from 2GB
243 out = check_output(
244 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400245 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900246 md5val.append(out.split()[0])
247
248 # One 1MB chunk crossing the 2GB boundary
249 out = check_output(
250 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400251 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900252 md5val.append(out.split()[0])
253
Richard Weinberger41eca322024-11-21 15:32:07 -0700254 try:
255 # 3GiB volume
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700256 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB', scratch_dir)
Richard Weinberger41eca322024-11-21 15:32:07 -0700257 except CalledProcessError as err:
258 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
259 return
260
Heinrich Schuchardtbc856172020-04-20 20:48:40 +0200261 except CalledProcessError as err:
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200262 pytest.skip('Setup failed for filesystem: ' + fs_type + '. {}'.format(err))
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900263 return
264 else:
265 yield [fs_ubtype, fs_img, md5val]
266 finally:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700267 call('rm -rf %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200268 call('rm -f %s' % fs_img, shell=True)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900269
270#
271# Fixture for extended fs test
272#
Tom Rini1b91cee2021-01-28 14:39:56 -0500273@pytest.fixture()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900274def fs_obj_ext(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900275 """Set up a file system to be used in extended fs test.
276
277 Args:
278 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200279 u_boot_config: U-Boot configuration.
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900280
281 Return:
282 A fixture for extended fs test, i.e. a triplet of file system type,
283 volume file name and a list of MD5 hashes.
284 """
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900285 fs_type = request.param
286 fs_img = ''
287
288 fs_ubtype = fstype_to_ubname(fs_type)
289 check_ubconfig(u_boot_config, fs_ubtype)
290
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700291 scratch_dir = u_boot_config.persistent_data_dir + '/scratch'
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900292
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700293 min_file = scratch_dir + '/' + MIN_FILE
294 tmp_file = scratch_dir + '/tmpfile'
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900295
296 try:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700297 check_call('mkdir -p %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200298 except CalledProcessError as err:
299 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200300 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300301 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200302
303 try:
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900304 # Create a test directory
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700305 check_call('mkdir %s/dir1' % scratch_dir, shell=True)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900306
307 # Create a small file and calculate md5
308 check_call('dd if=/dev/urandom of=%s bs=1K count=20'
309 % min_file, shell=True)
310 out = check_output(
311 'dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400312 % min_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900313 md5val = [ out.split()[0] ]
314
315 # Calculate md5sum of Test Case 4
316 check_call('dd if=%s of=%s bs=1K count=20'
317 % (min_file, tmp_file), shell=True)
318 check_call('dd if=%s of=%s bs=1K seek=5 count=20'
319 % (min_file, tmp_file), shell=True)
320 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400321 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900322 md5val.append(out.split()[0])
323
324 # Calculate md5sum of Test Case 5
325 check_call('dd if=%s of=%s bs=1K count=20'
326 % (min_file, tmp_file), shell=True)
327 check_call('dd if=%s of=%s bs=1K seek=5 count=5'
328 % (min_file, tmp_file), shell=True)
329 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400330 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900331 md5val.append(out.split()[0])
332
333 # Calculate md5sum of Test Case 7
334 check_call('dd if=%s of=%s bs=1K count=20'
335 % (min_file, tmp_file), shell=True)
336 check_call('dd if=%s of=%s bs=1K seek=20 count=20'
337 % (min_file, tmp_file), shell=True)
338 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400339 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900340 md5val.append(out.split()[0])
341
342 check_call('rm %s' % tmp_file, shell=True)
Richard Weinberger41eca322024-11-21 15:32:07 -0700343
344 try:
345 # 128MiB volume
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700346 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', scratch_dir)
Richard Weinberger41eca322024-11-21 15:32:07 -0700347 except CalledProcessError as err:
348 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
349 return
350
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900351 except CalledProcessError:
352 pytest.skip('Setup failed for filesystem: ' + fs_type)
353 return
354 else:
355 yield [fs_ubtype, fs_img, md5val]
356 finally:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700357 call('rm -rf %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200358 call('rm -f %s' % fs_img, shell=True)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900359
360#
361# Fixture for mkdir test
362#
Tom Rini1b91cee2021-01-28 14:39:56 -0500363@pytest.fixture()
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900364def fs_obj_mkdir(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900365 """Set up a file system to be used in mkdir test.
366
367 Args:
368 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200369 u_boot_config: U-Boot configuration.
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900370
371 Return:
372 A fixture for mkdir test, i.e. a duplet of file system type and
373 volume file name.
374 """
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900375 fs_type = request.param
376 fs_img = ''
377
378 fs_ubtype = fstype_to_ubname(fs_type)
379 check_ubconfig(u_boot_config, fs_ubtype)
380
381 try:
382 # 128MiB volume
Richard Weinberger41eca322024-11-21 15:32:07 -0700383 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', None)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900384 except:
385 pytest.skip('Setup failed for filesystem: ' + fs_type)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200386 return
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900387 else:
388 yield [fs_ubtype, fs_img]
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200389 call('rm -f %s' % fs_img, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900390
391#
392# Fixture for unlink test
393#
Tom Rini1b91cee2021-01-28 14:39:56 -0500394@pytest.fixture()
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900395def fs_obj_unlink(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900396 """Set up a file system to be used in unlink test.
397
398 Args:
399 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200400 u_boot_config: U-Boot configuration.
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900401
402 Return:
403 A fixture for unlink test, i.e. a duplet of file system type and
404 volume file name.
405 """
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900406 fs_type = request.param
407 fs_img = ''
408
409 fs_ubtype = fstype_to_ubname(fs_type)
410 check_ubconfig(u_boot_config, fs_ubtype)
411
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700412 scratch_dir = u_boot_config.persistent_data_dir + '/scratch'
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900413
414 try:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700415 check_call('mkdir -p %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200416 except CalledProcessError as err:
417 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200418 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300419 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200420
421 try:
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900422 # Test Case 1 & 3
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700423 check_call('mkdir %s/dir1' % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900424 check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700425 % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900426 check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1'
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700427 % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900428
429 # Test Case 2
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700430 check_call('mkdir %s/dir2' % scratch_dir, shell=True)
Tom Rini7f24c192019-10-24 11:59:20 -0400431 for i in range(0, 20):
432 check_call('mkdir %s/dir2/0123456789abcdef%02x'
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700433 % (scratch_dir, i), shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900434
435 # Test Case 4
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700436 check_call('mkdir %s/dir4' % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900437
438 # Test Case 5, 6 & 7
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700439 check_call('mkdir %s/dir5' % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900440 check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700441 % scratch_dir, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900442
Richard Weinberger41eca322024-11-21 15:32:07 -0700443 try:
444 # 128MiB volume
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700445 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', scratch_dir)
Richard Weinberger41eca322024-11-21 15:32:07 -0700446 except CalledProcessError as err:
447 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
448 return
449
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900450 except CalledProcessError:
451 pytest.skip('Setup failed for filesystem: ' + fs_type)
452 return
453 else:
454 yield [fs_ubtype, fs_img]
455 finally:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700456 call('rm -rf %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200457 call('rm -f %s' % fs_img, shell=True)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100458
459#
460# Fixture for symlink fs test
461#
Tom Rini1b91cee2021-01-28 14:39:56 -0500462@pytest.fixture()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100463def fs_obj_symlink(request, u_boot_config):
464 """Set up a file system to be used in symlink fs test.
465
466 Args:
467 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200468 u_boot_config: U-Boot configuration.
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100469
470 Return:
471 A fixture for basic fs test, i.e. a triplet of file system type,
472 volume file name and a list of MD5 hashes.
473 """
474 fs_type = request.param
475 fs_img = ''
476
477 fs_ubtype = fstype_to_ubname(fs_type)
478 check_ubconfig(u_boot_config, fs_ubtype)
479
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700480 scratch_dir = u_boot_config.persistent_data_dir + '/scratch'
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100481
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700482 small_file = scratch_dir + '/' + SMALL_FILE
483 medium_file = scratch_dir + '/' + MEDIUM_FILE
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100484
485 try:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700486 check_call('mkdir -p %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200487 except CalledProcessError as err:
488 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200489 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300490 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200491
492 try:
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100493 # Create a subdirectory.
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700494 check_call('mkdir %s/SUBDIR' % scratch_dir, shell=True)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100495
496 # Create a small file in this image.
497 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
498 % small_file, shell=True)
499
500 # Create a medium file in this image.
501 check_call('dd if=/dev/urandom of=%s bs=10M count=1'
502 % medium_file, shell=True)
503
504 # Generate the md5sums of reads that we will test against small file
505 out = check_output(
506 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400507 % small_file, shell=True).decode()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100508 md5val = [out.split()[0]]
509 out = check_output(
510 'dd if=%s bs=10M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400511 % medium_file, shell=True).decode()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100512 md5val.extend([out.split()[0]])
513
Richard Weinberger41eca322024-11-21 15:32:07 -0700514 try:
515 # 1GiB volume
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700516 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x40000000, '1GB', scratch_dir)
Richard Weinberger41eca322024-11-21 15:32:07 -0700517 except CalledProcessError as err:
518 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
519 return
520
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100521 except CalledProcessError:
522 pytest.skip('Setup failed for filesystem: ' + fs_type)
523 return
524 else:
525 yield [fs_ubtype, fs_img, md5val]
526 finally:
Richard Weinbergerba878ce2024-11-21 15:32:08 -0700527 call('rm -rf %s' % scratch_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200528 call('rm -f %s' % fs_img, shell=True)
Christian Taedcke570dc362023-11-15 13:44:24 +0100529
530#
531# Fixture for fat test
532#
533@pytest.fixture()
534def fs_obj_fat(request, u_boot_config):
535 """Set up a file system to be used in fat test.
536
537 Args:
538 request: Pytest request object.
539 u_boot_config: U-Boot configuration.
540
541 Return:
542 A fixture for fat test, i.e. a duplet of file system type and
543 volume file name.
544 """
545
546 # the maximum size of a FAT12 filesystem resulting in 4084 clusters
547 MAX_FAT12_SIZE = 261695 * 1024
548
549 # the minimum size of a FAT16 filesystem that can be created with
550 # mkfs.vfat resulting in 4087 clusters
551 MIN_FAT16_SIZE = 8208 * 1024
552
553 fs_type = request.param
554 fs_img = ''
555
556 fs_ubtype = fstype_to_ubname(fs_type)
557 check_ubconfig(u_boot_config, fs_ubtype)
558
559 fs_size = MAX_FAT12_SIZE if fs_type == 'fat12' else MIN_FAT16_SIZE
560
561 try:
562 # the volume size depends on the filesystem
Richard Weinberger41eca322024-11-21 15:32:07 -0700563 fs_img = fs_helper.mk_fs(u_boot_config, fs_type, fs_size, f'{fs_size}', None, 1024)
Christian Taedcke570dc362023-11-15 13:44:24 +0100564 except:
565 pytest.skip('Setup failed for filesystem: ' + fs_type)
566 return
567 else:
568 yield [fs_ubtype, fs_img]
569 call('rm -f %s' % fs_img, shell=True)