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