1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (C) 2022 Huang Jianan <jnhuang95@gmail.com>
3 # Author: Huang Jianan <jnhuang95@gmail.com>
10 EROFS_SRC_DIR = 'erofs_src_dir'
11 EROFS_IMAGE_NAME = 'erofs.img'
13 def generate_file(name, size):
15 Generates a file filled with 'x'.
18 file = open(name, 'w')
22 def make_erofs_image(build_dir):
24 Makes the EROFS images used for the test.
26 The image is generated at build_dir with the following structure:
35 root = os.path.join(build_dir, EROFS_SRC_DIR)
38 # 4096: uncompressed file
39 generate_file(os.path.join(root, 'f4096'), 4096)
41 # 7812: Compressed file
42 generate_file(os.path.join(root, 'f7812'), 7812)
44 # sub-directory with a single file inside
45 subdir_path = os.path.join(root, 'subdir')
46 os.makedirs(subdir_path)
47 generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
50 os.symlink('subdir', os.path.join(root, 'symdir'))
51 os.symlink('f7812', os.path.join(root, 'symfile'))
53 input_path = os.path.join(build_dir, EROFS_SRC_DIR)
54 output_path = os.path.join(build_dir, EROFS_IMAGE_NAME)
55 args = ' '.join([output_path, input_path])
56 subprocess.run(['mkfs.erofs -zlz4 ' + args], shell=True, check=True,
57 stdout=subprocess.DEVNULL)
59 def clean_erofs_image(build_dir):
61 Deletes the image and src_dir at build_dir.
63 path = os.path.join(build_dir, EROFS_SRC_DIR)
65 image_path = os.path.join(build_dir, EROFS_IMAGE_NAME)
68 def erofs_ls_at_root(u_boot_console):
70 Test if all the present files and directories were listed.
72 no_slash = u_boot_console.run_command('erofsls host 0')
73 slash = u_boot_console.run_command('erofsls host 0 /')
74 assert no_slash == slash
76 expected_lines = ['./', '../', '4096 f4096', '7812 f7812', 'subdir/',
77 '<SYM> symdir', '<SYM> symfile', '4 file(s), 3 dir(s)']
79 output = u_boot_console.run_command('erofsls host 0')
80 for line in expected_lines:
83 def erofs_ls_at_subdir(u_boot_console):
85 Test if the path resolution works.
87 expected_lines = ['./', '../', '100 subdir-file', '1 file(s), 2 dir(s)']
88 output = u_boot_console.run_command('erofsls host 0 subdir')
89 for line in expected_lines:
92 def erofs_ls_at_symlink(u_boot_console):
94 Test if the symbolic link's target resolution works.
96 output = u_boot_console.run_command('erofsls host 0 symdir')
97 output_subdir = u_boot_console.run_command('erofsls host 0 subdir')
98 assert output == output_subdir
100 expected_lines = ['./', '../', '100 subdir-file', '1 file(s), 2 dir(s)']
101 for line in expected_lines:
102 assert line in output
104 def erofs_ls_at_non_existent_dir(u_boot_console):
106 Test if the EROFS support will crash when get a nonexistent directory.
108 out_non_existent = u_boot_console.run_command('erofsls host 0 fff')
109 out_not_dir = u_boot_console.run_command('erofsls host 0 f1000')
110 assert out_non_existent == out_not_dir
111 assert '' in out_non_existent
113 def erofs_load_files(u_boot_console, files, sizes, address):
115 Loads files and asserts their checksums.
117 build_dir = u_boot_console.config.build_dir
118 for (file, size) in zip(files, sizes):
119 out = u_boot_console.run_command('erofsload host 0 {} {}'.format(address, file))
121 # check if the right amount of bytes was read
124 # calculate u-boot file's checksum
125 out = u_boot_console.run_command('md5sum {} {}'.format(address, hex(int(size))))
126 u_boot_checksum = out.split()[-1]
128 # calculate original file's checksum
129 original_file_path = os.path.join(build_dir, EROFS_SRC_DIR + '/' + file)
130 out = subprocess.run(['md5sum ' + original_file_path], shell=True, check=True,
131 capture_output=True, text=True)
132 original_checksum = out.stdout.split()[0]
135 assert u_boot_checksum == original_checksum
137 def erofs_load_files_at_root(u_boot_console):
139 Test load file from the root directory.
141 files = ['f4096', 'f7812']
142 sizes = ['4096', '7812']
143 address = '$kernel_addr_r'
144 erofs_load_files(u_boot_console, files, sizes, address)
146 def erofs_load_files_at_subdir(u_boot_console):
148 Test load file from the subdirectory.
150 files = ['subdir/subdir-file']
152 address = '$kernel_addr_r'
153 erofs_load_files(u_boot_console, files, sizes, address)
155 def erofs_load_files_at_symlink(u_boot_console):
157 Test load file from the symlink.
161 address = '$kernel_addr_r'
162 erofs_load_files(u_boot_console, files, sizes, address)
164 def erofs_load_non_existent_file(u_boot_console):
166 Test if the EROFS support will crash when load a nonexistent file.
168 address = '$kernel_addr_r'
169 file = 'non-existent'
170 out = u_boot_console.run_command('erofsload host 0 {} {}'.format(address, file))
171 assert 'Failed to load' in out
173 def erofs_run_all_tests(u_boot_console):
177 erofs_ls_at_root(u_boot_console)
178 erofs_ls_at_subdir(u_boot_console)
179 erofs_ls_at_symlink(u_boot_console)
180 erofs_ls_at_non_existent_dir(u_boot_console)
181 erofs_load_files_at_root(u_boot_console)
182 erofs_load_files_at_subdir(u_boot_console)
183 erofs_load_files_at_symlink(u_boot_console)
184 erofs_load_non_existent_file(u_boot_console)
186 @pytest.mark.boardspec('sandbox')
187 @pytest.mark.buildconfigspec('cmd_fs_generic')
188 @pytest.mark.buildconfigspec('cmd_erofs')
189 @pytest.mark.buildconfigspec('fs_erofs')
190 @pytest.mark.requiredtool('mkfs.erofs')
191 @pytest.mark.requiredtool('md5sum')
193 def test_erofs(u_boot_console):
195 Executes the erofs test suite.
197 build_dir = u_boot_console.config.build_dir
200 # setup test environment
201 make_erofs_image(build_dir)
202 image_path = os.path.join(build_dir, EROFS_IMAGE_NAME)
203 u_boot_console.run_command('host bind 0 {}'.format(image_path))
205 erofs_run_all_tests(u_boot_console)
207 clean_erofs_image(build_dir)
210 # clean test environment
211 clean_erofs_image(build_dir)