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