1# SPDX-License-Identifier: GPL-2.0
2# Copyright (C) 2020 Bootlin
3# Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
4
5import os
6import pytest
7
8from sqfs_common import STANDARD_TABLE
9from sqfs_common import generate_sqfs_src_dir, make_all_images
10from sqfs_common import clean_sqfs_src_dir, clean_all_images
11from sqfs_common import check_mksquashfs_version
12
13def sqfs_ls_at_root(ubman):
14    """ Runs sqfsls at image's root.
15
16    This test checks if all the present files and directories were listed. Also,
17    it checks if passing the slash or not changes the output, which it shouldn't.
18
19    Args:
20        ubman: provides the means to interact with U-Boot's console.
21    """
22
23    no_slash = ubman.run_command('sqfsls host 0')
24    slash = ubman.run_command('sqfsls host 0 /')
25    assert no_slash == slash
26
27    expected_lines = ['empty-dir/', '1000   f1000', '4096   f4096', '5096   f5096',
28                      'subdir/', '<SYM>   sym', '4 file(s), 2 dir(s)']
29
30    output = ubman.run_command('sqfsls host 0')
31    for line in expected_lines:
32        assert line in output
33
34def sqfs_ls_at_empty_dir(ubman):
35    """ Runs sqfsls at an empty directory.
36
37    This tests checks if sqfsls will print anything other than the 'Empty directory'
38    message.
39
40    Args:
41        ubman: provides the means to interact with U-Boot's console.
42    """
43    assert ubman.run_command('sqfsls host 0 empty-dir') == 'Empty directory.'
44
45def sqfs_ls_at_subdir(ubman):
46    """ Runs sqfsls at the SquashFS image's subdir.
47
48    This test checks if the path resolution works, since the directory is not the
49    root.
50
51    Args:
52        ubman: provides the means to interact with U-Boot's console.
53    """
54    expected_lines = ['100   subdir-file', '1 file(s), 0 dir(s)']
55    output = ubman.run_command('sqfsls host 0 subdir')
56    for line in expected_lines:
57        assert line in output
58
59def sqfs_ls_at_symlink(ubman):
60    """ Runs sqfsls at a SquashFS image's symbolic link.
61
62    This test checks if the symbolic link's target resolution works.
63
64    Args:
65        ubman: provides the means to interact with U-Boot's console.
66    """
67    # since sym -> subdir, the following outputs must be equal
68    output = ubman.run_command('sqfsls host 0 sym')
69    output_subdir = ubman.run_command('sqfsls host 0 subdir')
70    assert output == output_subdir
71
72    expected_lines = ['100   subdir-file', '1 file(s), 0 dir(s)']
73    for line in expected_lines:
74        assert line in output
75
76def sqfs_ls_at_non_existent_dir(ubman):
77    """ Runs sqfsls at a file and at a non-existent directory.
78
79    This test checks if the SquashFS support won't crash if it doesn't find the
80    specified directory or if it takes a file as an input instead of an actual
81    directory. In both cases, the output should be the same.
82
83    Args:
84        ubman: provides the means to interact with U-Boot's console.
85    """
86    out_non_existent = ubman.run_command('sqfsls host 0 fff')
87    out_not_dir = ubman.run_command('sqfsls host 0 f1000')
88    assert out_non_existent == out_not_dir
89    assert '** Cannot find directory. **' in out_non_existent
90
91def sqfs_run_all_ls_tests(ubman):
92    """ Runs all the previously defined test cases.
93
94    Args:
95        ubman: provides the means to interact with U-Boot's console.
96    """
97    sqfs_ls_at_root(ubman)
98    sqfs_ls_at_empty_dir(ubman)
99    sqfs_ls_at_subdir(ubman)
100    sqfs_ls_at_symlink(ubman)
101    sqfs_ls_at_non_existent_dir(ubman)
102
103@pytest.mark.boardspec('sandbox')
104@pytest.mark.buildconfigspec('cmd_fs_generic')
105@pytest.mark.buildconfigspec('cmd_squashfs')
106@pytest.mark.buildconfigspec('fs_squashfs')
107@pytest.mark.requiredtool('mksquashfs')
108@pytest.mark.singlethread
109def test_sqfs_ls(ubman):
110    """ Executes the sqfsls test suite.
111
112    First, it generates the SquashFS images, then it runs the test cases and
113    finally cleans the workspace. If an exception is raised, the workspace is
114    cleaned before exiting.
115
116    Args:
117        ubman: provides the means to interact with U-Boot's console.
118    """
119    build_dir = ubman.config.build_dir
120
121    # If the EFI subsystem is enabled and initialized, EFI subsystem tries to
122    # add EFI boot option when the new disk is detected. If there is no EFI
123    # System Partition exists, EFI subsystem outputs error messages and
124    # it ends up with test failure.
125    # Restart U-Boot to clear the previous state.
126    # TODO: Ideally EFI test cases need to be fixed, but it will
127    # increase the number of system reset.
128    ubman.restart_uboot()
129
130    # setup test environment
131    check_mksquashfs_version()
132    generate_sqfs_src_dir(build_dir)
133    make_all_images(build_dir)
134
135    # run all tests for each image
136    for image in STANDARD_TABLE:
137        try:
138            image_path = os.path.join(build_dir, image)
139            ubman.run_command('host bind 0 {}'.format(image_path))
140            sqfs_run_all_ls_tests(ubman)
141        except:
142            clean_all_images(build_dir)
143            clean_sqfs_src_dir(build_dir)
144            raise AssertionError
145
146    # clean test environment
147    clean_all_images(build_dir)
148    clean_sqfs_src_dir(build_dir)
149