The previous strategy to know if a file was correctly loaded was to
check for how many bytes were read and compare it against the file's
original size. Since this is not a good solution, replace it by
comparing the checksum of the loaded bytes against the original file's
checksum. Add more test cases: files at a sub-directory and non-existent
file.

Signed-off-by: Joao Marcos Costa <jmcosta...@gmail.com>
---
 .../test_fs/test_squashfs/test_sqfs_load.py   | 99 +++++++++++++------
 1 file changed, 69 insertions(+), 30 deletions(-)

diff --git a/test/py/tests/test_fs/test_squashfs/test_sqfs_load.py 
b/test/py/tests/test_fs/test_squashfs/test_sqfs_load.py
index 9e90062384..0b416eb4c3 100644
--- a/test/py/tests/test_fs/test_squashfs/test_sqfs_load.py
+++ b/test/py/tests/test_fs/test_squashfs/test_sqfs_load.py
@@ -1,11 +1,62 @@
 # SPDX-License-Identifier: GPL-2.0
-# Copyright (C) 2020 Bootlin
+# Copyright (C) 2021 Bootlin
 # Author: Joao Marcos Costa <joaomarcos.co...@bootlin.com>
 
 import os
 import pytest
 from sqfs_common import *
 
+@pytest.mark.requiredtool('md5sum')
+def original_md5sum(path):
+    out = subprocess.run(['md5sum ' + path], shell = True, check = True,
+            capture_output = True, text = True)
+    checksum = out.stdout.split()[0]
+
+    return checksum
+
+def uboot_md5sum(u_boot_console, address, count):
+    out = u_boot_console.run_command('md5sum {} {}'.format(address, count))
+    checksum = out.split()[-1]
+
+    return checksum
+
+# loads files and asserts checksums
+def sqfs_load_files(u_boot_console, files, sizes, address):
+    build_dir = u_boot_console.config.build_dir
+    for (f, size) in zip(files, sizes):
+        out = u_boot_console.run_command('sqfsload host 0 {} 
{}'.format(address, f))
+
+        # check if the right amount of bytes was read
+        assert size in out
+
+        # compare original file's checksum against u-boot's
+        u_boot_checksum = uboot_md5sum(u_boot_console, address, hex(int(size)))
+        original_checksum = original_md5sum(os.path.join(build_dir, 
sqfs_src_dir + '/' + f))
+        assert u_boot_checksum == original_checksum
+
+def sqfs_load_files_at_root(u_boot_console):
+    files = ['f4096', 'f5096', 'f1000']
+    sizes = ['4096', '5096', '1000']
+    address = '$kernel_addr_r'
+    sqfs_load_files(u_boot_console, files, sizes, address)
+
+def sqfs_load_files_at_subdir(u_boot_console):
+    files = ['subdir/subdir-file']
+    sizes = ['100']
+    address = '$kernel_addr_r'
+    sqfs_load_files(u_boot_console, files, sizes, address)
+
+def sqfs_load_non_existent_file(u_boot_console):
+    address = '$kernel_addr_r'
+    f = 'non-existent'
+    out = u_boot_console.run_command('sqfsload host 0 {} {}'.format(address, 
f))
+    assert 'Failed to load' in out
+
+def sqfs_run_all_load_tests(u_boot_console):
+    sqfs_load_files_at_root(u_boot_console)
+    sqfs_load_files_at_subdir(u_boot_console)
+    sqfs_load_non_existent_file(u_boot_console)
+
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('cmd_fs_generic')
 @pytest.mark.buildconfigspec('cmd_squashfs')
@@ -13,34 +64,22 @@ from sqfs_common import *
 @pytest.mark.requiredtool('mksquashfs')
 def test_sqfs_load(u_boot_console):
     build_dir = u_boot_console.config.build_dir
-    command = "sqfsload host 0 $kernel_addr_r "
 
-    for opt in comp_opts:
-        # generate and load the squashfs image
+    # setup test environment
+    generate_sqfs_src_dir(build_dir)
+    make_all_images(build_dir)
+
+    # run all tests for each image
+    for image in standard_table.keys():
         try:
-            opt.gen_image(build_dir)
-        except RuntimeError:
-            opt.clean_source(build_dir)
-            # skip unsupported compression types
-            continue
-
-        path = os.path.join(build_dir, "sqfs-" + opt.name)
-        output = u_boot_console.run_command("host bind 0 " + path)
-
-        output = u_boot_console.run_command(command + "xxx")
-        assert "File not found." in output
-
-        for (f, s) in zip(opt.files, opt.sizes):
-            try:
-                output = u_boot_console.run_command(command + f)
-                assert str(s) in output
-            except:
-                assert False
-                opt.cleanup(build_dir)
-
-        # test symbolic link
-        output = u_boot_console.run_command(command + "sym")
-        assert str(opt.sizes[0]) in output
-
-        # remove generated files
-        opt.cleanup(build_dir)
+            image_path = os.path.join(build_dir, image)
+            u_boot_console.run_command('host bind 0 {}'.format(image_path))
+            sqfs_run_all_load_tests(u_boot_console)
+        except:
+            clean_all_images(build_dir)
+            clean_sqfs_src_dir(build_dir)
+            raise AssertionError
+
+    # clean test environment
+    clean_all_images(build_dir)
+    clean_sqfs_src_dir(build_dir)
-- 
2.25.1

Reply via email to