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:Basic Test 6 7""" 8This test verifies basic read/write operation on file system. 9""" 10 11import pytest 12import re 13from fstest_defs import * 14from fstest_helpers import assert_fs_integrity 15 16@pytest.mark.boardspec('sandbox') 17@pytest.mark.slow 18class 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) 36 if fs_type == 'ext4': 37 assert('Can not find directory' in output) 38 else: 39 assert('' == output) 40 41 def test_fs2(self, u_boot_console, fs_obj_basic): 42 """ 43 Test Case 2 - size command for a small file 44 """ 45 fs_type,fs_img,md5val = fs_obj_basic 46 with u_boot_console.log.section('Test Case 2a - size (small)'): 47 # 1MB is 0x0010 0000 48 # Test Case 2a - size of small file 49 output = u_boot_console.run_command_list([ 50 'host bind 0 %s' % fs_img, 51 '%ssize host 0:0 /%s' % (fs_type, SMALL_FILE), 52 'printenv filesize', 53 'setenv filesize']) 54 assert('filesize=100000' in ''.join(output)) 55 56 with u_boot_console.log.section('Test Case 2b - size (/../<file>)'): 57 # Test Case 2b - size of small file via a path using '..' 58 output = u_boot_console.run_command_list([ 59 '%ssize host 0:0 /SUBDIR/../%s' % (fs_type, SMALL_FILE), 60 'printenv filesize', 61 'setenv filesize']) 62 assert('filesize=100000' in ''.join(output)) 63 64 def test_fs3(self, u_boot_console, fs_obj_basic): 65 """ 66 Test Case 3 - size command for a large file 67 """ 68 fs_type,fs_img,md5val = fs_obj_basic 69 with u_boot_console.log.section('Test Case 3 - size (large)'): 70 # 2.5GB (1024*1024*2500) is 0x9C40 0000 71 # Test Case 3 - size of big file 72 output = u_boot_console.run_command_list([ 73 'host bind 0 %s' % fs_img, 74 '%ssize host 0:0 /%s' % (fs_type, BIG_FILE), 75 'printenv filesize', 76 'setenv filesize']) 77 assert('filesize=9c400000' in ''.join(output)) 78 79 def test_fs4(self, u_boot_console, fs_obj_basic): 80 """ 81 Test Case 4 - load a small file, 1MB 82 """ 83 fs_type,fs_img,md5val = fs_obj_basic 84 with u_boot_console.log.section('Test Case 4 - load (small)'): 85 # Test Case 4a - Read full 1MB of small file 86 output = u_boot_console.run_command_list([ 87 'host bind 0 %s' % fs_img, 88 '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE), 89 'printenv filesize']) 90 assert('filesize=100000' in ''.join(output)) 91 92 # Test Case 4b - Read full 1MB of small file 93 output = u_boot_console.run_command_list([ 94 'md5sum %x $filesize' % ADDR, 95 'setenv filesize']) 96 assert(md5val[0] in ''.join(output)) 97 98 def test_fs5(self, u_boot_console, fs_obj_basic): 99 """ 100 Test Case 5 - load, reading first 1MB of 3GB file 101 """ 102 fs_type,fs_img,md5val = fs_obj_basic 103 with u_boot_console.log.section('Test Case 5 - load (first 1MB)'): 104 # Test Case 5a - First 1MB of big file 105 output = u_boot_console.run_command_list([ 106 'host bind 0 %s' % fs_img, 107 '%sload host 0:0 %x /%s %x 0x0' % (fs_type, ADDR, BIG_FILE, LENGTH), 108 'printenv filesize']) 109 assert('filesize=100000' in ''.join(output)) 110 111 # Test Case 5b - First 1MB of big file 112 output = u_boot_console.run_command_list([ 113 'md5sum %x $filesize' % ADDR, 114 'setenv filesize']) 115 assert(md5val[1] in ''.join(output)) 116 117 def test_fs6(self, u_boot_console, fs_obj_basic): 118 """ 119 Test Case 6 - load, reading last 1MB of 3GB file 120 """ 121 fs_type,fs_img,md5val = fs_obj_basic 122 with u_boot_console.log.section('Test Case 6 - load (last 1MB)'): 123 # fails for ext as no offset support 124 # Test Case 6a - Last 1MB of big file 125 output = u_boot_console.run_command_list([ 126 'host bind 0 %s' % fs_img, 127 '%sload host 0:0 %x /%s %x 0x9c300000' 128 % (fs_type, ADDR, BIG_FILE, LENGTH), 129 'printenv filesize']) 130 assert('filesize=100000' in ''.join(output)) 131 132 # Test Case 6b - Last 1MB of big file 133 output = u_boot_console.run_command_list([ 134 'md5sum %x $filesize' % ADDR, 135 'setenv filesize']) 136 assert(md5val[2] in ''.join(output)) 137 138 def test_fs7(self, u_boot_console, fs_obj_basic): 139 """ 140 Test Case 7 - load, 1MB from the last 1MB in 2GB 141 """ 142 fs_type,fs_img,md5val = fs_obj_basic 143 with u_boot_console.log.section('Test Case 7 - load (last 1MB in 2GB)'): 144 # fails for ext as no offset support 145 # Test Case 7a - One from the last 1MB chunk of 2GB 146 output = u_boot_console.run_command_list([ 147 'host bind 0 %s' % fs_img, 148 '%sload host 0:0 %x /%s %x 0x7ff00000' 149 % (fs_type, ADDR, BIG_FILE, LENGTH), 150 'printenv filesize']) 151 assert('filesize=100000' in ''.join(output)) 152 153 # Test Case 7b - One from the last 1MB chunk of 2GB 154 output = u_boot_console.run_command_list([ 155 'md5sum %x $filesize' % ADDR, 156 'setenv filesize']) 157 assert(md5val[3] in ''.join(output)) 158 159 def test_fs8(self, u_boot_console, fs_obj_basic): 160 """ 161 Test Case 8 - load, reading first 1MB in 2GB 162 """ 163 fs_type,fs_img,md5val = fs_obj_basic 164 with u_boot_console.log.section('Test Case 8 - load (first 1MB in 2GB)'): 165 # fails for ext as no offset support 166 # Test Case 8a - One from the start 1MB chunk from 2GB 167 output = u_boot_console.run_command_list([ 168 'host bind 0 %s' % fs_img, 169 '%sload host 0:0 %x /%s %x 0x80000000' 170 % (fs_type, ADDR, BIG_FILE, LENGTH), 171 'printenv filesize']) 172 assert('filesize=100000' in ''.join(output)) 173 174 # Test Case 8b - One from the start 1MB chunk from 2GB 175 output = u_boot_console.run_command_list([ 176 'md5sum %x $filesize' % ADDR, 177 'setenv filesize']) 178 assert(md5val[4] in ''.join(output)) 179 180 def test_fs9(self, u_boot_console, fs_obj_basic): 181 """ 182 Test Case 9 - load, 1MB crossing 2GB boundary 183 """ 184 fs_type,fs_img,md5val = fs_obj_basic 185 with u_boot_console.log.section('Test Case 9 - load (crossing 2GB boundary)'): 186 # fails for ext as no offset support 187 # Test Case 9a - One 1MB chunk crossing the 2GB boundary 188 output = u_boot_console.run_command_list([ 189 'host bind 0 %s' % fs_img, 190 '%sload host 0:0 %x /%s %x 0x7ff80000' 191 % (fs_type, ADDR, BIG_FILE, LENGTH), 192 'printenv filesize']) 193 assert('filesize=100000' in ''.join(output)) 194 195 # Test Case 9b - One 1MB chunk crossing the 2GB boundary 196 output = u_boot_console.run_command_list([ 197 'md5sum %x $filesize' % ADDR, 198 'setenv filesize']) 199 assert(md5val[5] in ''.join(output)) 200 201 def test_fs10(self, u_boot_console, fs_obj_basic): 202 """ 203 Test Case 10 - load, reading beyond file end'): 204 """ 205 fs_type,fs_img,md5val = fs_obj_basic 206 with u_boot_console.log.section('Test Case 10 - load (beyond file end)'): 207 # Generic failure case 208 # Test Case 10 - 2MB chunk from the last 1MB of big file 209 output = u_boot_console.run_command_list([ 210 'host bind 0 %s' % fs_img, 211 '%sload host 0:0 %x /%s 0x00200000 0x9c300000' 212 % (fs_type, ADDR, BIG_FILE), 213 'printenv filesize', 214 'md5sum %x $filesize' % ADDR, 215 'setenv filesize']) 216 assert('filesize=100000' in ''.join(output)) 217 218 def test_fs11(self, u_boot_console, fs_obj_basic): 219 """ 220 Test Case 11 - write' 221 """ 222 fs_type,fs_img,md5val = fs_obj_basic 223 with u_boot_console.log.section('Test Case 11 - write'): 224 # Read 1MB from small file 225 # Write it back to test the writes 226 # Test Case 11a - Check that the write succeeded 227 output = u_boot_console.run_command_list([ 228 'host bind 0 %s' % fs_img, 229 '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE), 230 '%swrite host 0:0 %x /%s.w $filesize' 231 % (fs_type, ADDR, SMALL_FILE)]) 232 assert('1048576 bytes written' in ''.join(output)) 233 234 # Test Case 11b - Check md5 of written to is same 235 # as the one read from 236 output = u_boot_console.run_command_list([ 237 '%sload host 0:0 %x /%s.w' % (fs_type, ADDR, SMALL_FILE), 238 'md5sum %x $filesize' % ADDR, 239 'setenv filesize']) 240 assert(md5val[0] in ''.join(output)) 241 assert_fs_integrity(fs_type, fs_img) 242 243 def test_fs12(self, u_boot_console, fs_obj_basic): 244 """ 245 Test Case 12 - write to "." directory 246 """ 247 fs_type,fs_img,md5val = fs_obj_basic 248 with u_boot_console.log.section('Test Case 12 - write (".")'): 249 # Next test case checks writing a file whose dirent 250 # is the first in the block, which is always true for "." 251 # The write should fail, but the lookup should work 252 # Test Case 12 - Check directory traversal 253 output = u_boot_console.run_command_list([ 254 'host bind 0 %s' % fs_img, 255 '%swrite host 0:0 %x /. 0x10' % (fs_type, ADDR)]) 256 assert('Unable to write' in ''.join(output)) 257 assert_fs_integrity(fs_type, fs_img) 258 259 def test_fs13(self, u_boot_console, fs_obj_basic): 260 """ 261 Test Case 13 - write to a file with "/./<filename>" 262 """ 263 fs_type,fs_img,md5val = fs_obj_basic 264 with u_boot_console.log.section('Test Case 13 - write ("./<file>")'): 265 # Read 1MB from small file 266 # Write it via "same directory", i.e. "." dirent 267 # Test Case 13a - Check directory traversal 268 output = u_boot_console.run_command_list([ 269 'host bind 0 %s' % fs_img, 270 '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE), 271 '%swrite host 0:0 %x /./%s2 $filesize' 272 % (fs_type, ADDR, SMALL_FILE)]) 273 assert('1048576 bytes written' in ''.join(output)) 274 275 # Test Case 13b - Check md5 of written to is same 276 # as the one read from 277 output = u_boot_console.run_command_list([ 278 'mw.b %x 00 100' % ADDR, 279 '%sload host 0:0 %x /./%s2' % (fs_type, ADDR, SMALL_FILE), 280 'md5sum %x $filesize' % ADDR, 281 'setenv filesize']) 282 assert(md5val[0] in ''.join(output)) 283 284 # Test Case 13c - Check md5 of written to is same 285 # as the one read from 286 output = u_boot_console.run_command_list([ 287 'mw.b %x 00 100' % ADDR, 288 '%sload host 0:0 %x /%s2' % (fs_type, ADDR, SMALL_FILE), 289 'md5sum %x $filesize' % ADDR, 290 'setenv filesize']) 291 assert(md5val[0] in ''.join(output)) 292 assert_fs_integrity(fs_type, fs_img) 293