test_basic.py 12 KB

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