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:unlink Test
6
7"""
8This test verifies unlink operation (deleting a file or a directory)
9on file system.
10"""
11
12import pytest
13from fstest_helpers import assert_fs_integrity
14
15@pytest.mark.boardspec('sandbox')
16@pytest.mark.slow
17class TestUnlink(object):
18    def test_unlink1(self, u_boot_console, fs_obj_unlink):
19        """
20        Test Case 1 - delete a file
21        """
22        fs_type,fs_img = fs_obj_unlink
23        with u_boot_console.log.section('Test Case 1 - unlink (file)'):
24            output = u_boot_console.run_command_list([
25                'host bind 0 %s' % fs_img,
26                '%srm host 0:0 dir1/file1' % fs_type,
27                '%sls host 0:0 dir1/file1' % fs_type])
28            assert('' == ''.join(output))
29
30            output = u_boot_console.run_command(
31                '%sls host 0:0 dir1/' % fs_type)
32            assert(not 'file1' in output)
33            assert('file2' in output)
34            assert_fs_integrity(fs_type, fs_img)
35
36    def test_unlink2(self, u_boot_console, fs_obj_unlink):
37        """
38        Test Case 2 - delete many files
39        """
40        fs_type,fs_img = fs_obj_unlink
41        with u_boot_console.log.section('Test Case 2 - unlink (many)'):
42            output = u_boot_console.run_command('host bind 0 %s' % fs_img)
43
44            for i in range(0, 20):
45                output = u_boot_console.run_command_list([
46                    '%srm host 0:0 dir2/0123456789abcdef%02x' % (fs_type, i),
47                    '%sls host 0:0 dir2/0123456789abcdef%02x' % (fs_type, i)])
48                assert('' == ''.join(output))
49
50            output = u_boot_console.run_command(
51                '%sls host 0:0 dir2' % fs_type)
52            assert('0 file(s), 2 dir(s)' in output)
53            assert_fs_integrity(fs_type, fs_img)
54
55    def test_unlink3(self, u_boot_console, fs_obj_unlink):
56        """
57        Test Case 3 - trying to delete a non-existing file should fail
58        """
59        fs_type,fs_img = fs_obj_unlink
60        with u_boot_console.log.section('Test Case 3 - unlink (non-existing)'):
61            output = u_boot_console.run_command_list([
62                'host bind 0 %s' % fs_img,
63                '%srm host 0:0 dir1/nofile' % fs_type])
64            assert('nofile: doesn\'t exist' in ''.join(output))
65            assert_fs_integrity(fs_type, fs_img)
66
67    def test_unlink4(self, u_boot_console, fs_obj_unlink):
68        """
69        Test Case 4 - delete an empty directory
70        """
71        fs_type,fs_img = fs_obj_unlink
72        with u_boot_console.log.section('Test Case 4 - unlink (directory)'):
73            output = u_boot_console.run_command_list([
74                'host bind 0 %s' % fs_img,
75                '%srm host 0:0 dir4' % fs_type])
76            assert('' == ''.join(output))
77
78            output = u_boot_console.run_command(
79                '%sls host 0:0 /' % fs_type)
80            assert(not 'dir4' in output)
81            assert_fs_integrity(fs_type, fs_img)
82
83    def test_unlink5(self, u_boot_console, fs_obj_unlink):
84        """
85        Test Case 5 - trying to deleting a non-empty directory ".."
86        should fail
87        """
88        fs_type,fs_img = fs_obj_unlink
89        with u_boot_console.log.section('Test Case 5 - unlink ("non-empty directory")'):
90            output = u_boot_console.run_command_list([
91                'host bind 0 %s' % fs_img,
92                '%srm host 0:0 dir5' % fs_type])
93            assert('directory is not empty' in ''.join(output))
94            assert_fs_integrity(fs_type, fs_img)
95
96    def test_unlink6(self, u_boot_console, fs_obj_unlink):
97        """
98        Test Case 6 - trying to deleting a "." should fail
99        """
100        fs_type,fs_img = fs_obj_unlink
101        with u_boot_console.log.section('Test Case 6 - unlink (".")'):
102            output = u_boot_console.run_command_list([
103                'host bind 0 %s' % fs_img,
104                '%srm host 0:0 dir5/.' % fs_type])
105            assert('directory is not empty' in ''.join(output))
106            assert_fs_integrity(fs_type, fs_img)
107
108    def test_unlink7(self, u_boot_console, fs_obj_unlink):
109        """
110        Test Case 7 - trying to deleting a ".." should fail
111        """
112        fs_type,fs_img = fs_obj_unlink
113        with u_boot_console.log.section('Test Case 7 - unlink ("..")'):
114            output = u_boot_console.run_command_list([
115                'host bind 0 %s' % fs_img,
116                '%srm host 0:0 dir5/..' % fs_type])
117            assert('directory is not empty' in ''.join(output))
118            assert_fs_integrity(fs_type, fs_img)
119