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:mkdir Test
6
7"""
8This test verifies mkdir operation on file system.
9"""
10
11import pytest
12
13@pytest.mark.boardspec('sandbox')
14class TestMkdir(object):
15    def test_mkdir1(self, u_boot_console, fs_obj_mkdir):
16        """
17        Test Case 1 - create a directory under a root
18        """
19        fs_type,fs_img = fs_obj_mkdir
20        with u_boot_console.log.section('Test Case 1 - mkdir'):
21            output = u_boot_console.run_command_list([
22                'host bind 0 %s' % fs_img,
23                '%smkdir host 0:0 dir1' % fs_type,
24                '%sls host 0:0 /' % fs_type])
25            assert('dir1/' in ''.join(output))
26
27            output = u_boot_console.run_command(
28                '%sls host 0:0 dir1' % fs_type)
29            assert('./'   in output)
30            assert('../'  in output)
31
32    def test_mkdir2(self, u_boot_console, fs_obj_mkdir):
33        """
34        Test Case 2 - create a directory under a sub-directory
35        """
36        fs_type,fs_img = fs_obj_mkdir
37        with u_boot_console.log.section('Test Case 2 - mkdir (sub-sub directory)'):
38            output = u_boot_console.run_command_list([
39                'host bind 0 %s' % fs_img,
40                '%smkdir host 0:0 dir1/dir2' % fs_type,
41                '%sls host 0:0 dir1' % fs_type])
42            assert('dir2/' in ''.join(output))
43
44            output = u_boot_console.run_command(
45                '%sls host 0:0 dir1/dir2' % fs_type)
46            assert('./'   in output)
47            assert('../'  in output)
48
49    def test_mkdir3(self, u_boot_console, fs_obj_mkdir):
50        """
51        Test Case 3 - trying to create a directory with a non-existing
52        path should fail
53        """
54        fs_type,fs_img = fs_obj_mkdir
55        with u_boot_console.log.section('Test Case 3 - mkdir (non-existing path)'):
56            output = u_boot_console.run_command_list([
57                'host bind 0 %s' % fs_img,
58                '%smkdir host 0:0 none/dir3' % fs_type])
59            assert('Unable to create a directory' in ''.join(output))
60
61    def test_mkdir4(self, u_boot_console, fs_obj_mkdir):
62        """
63        Test Case 4 - trying to create "." should fail
64        """
65        fs_type,fs_img = fs_obj_mkdir
66        with u_boot_console.log.section('Test Case 4 - mkdir (".")'):
67            output = u_boot_console.run_command_list([
68                'host bind 0 %s' % fs_img,
69                '%smkdir host 0:0 .' % fs_type])
70            assert('Unable to create a directory' in ''.join(output))
71
72    def test_mkdir5(self, u_boot_console, fs_obj_mkdir):
73        """
74        Test Case 5 - trying to create ".." should fail
75        """
76        fs_type,fs_img = fs_obj_mkdir
77        with u_boot_console.log.section('Test Case 5 - mkdir ("..")'):
78            output = u_boot_console.run_command_list([
79                'host bind 0 %s' % fs_img,
80                '%smkdir host 0:0 ..' % fs_type])
81            assert('Unable to create a directory' in ''.join(output))
82
83    def test_mkdir6(self, u_boot_console, fs_obj_mkdir):
84        """
85        'Test Case 6 - create as many directories as amount of directory
86        entries goes beyond a cluster size)'
87        """
88        fs_type,fs_img = fs_obj_mkdir
89        with u_boot_console.log.section('Test Case 6 - mkdir (create many)'):
90            output = u_boot_console.run_command_list([
91                'host bind 0 %s' % fs_img,
92                '%smkdir host 0:0 dir6' % fs_type,
93                '%sls host 0:0 /' % fs_type])
94            assert('dir6/' in ''.join(output))
95
96            for i in range(0, 20):
97                output = u_boot_console.run_command(
98                    '%smkdir host 0:0 dir6/0123456789abcdef%02x'
99                    % (fs_type, i))
100            output = u_boot_console.run_command('%sls host 0:0 dir6' % fs_type)
101            assert('0123456789abcdef00/'  in output)
102            assert('0123456789abcdef13/'  in output)
103
104            output = u_boot_console.run_command(
105                '%sls host 0:0 dir6/0123456789abcdef13/.' % fs_type)
106            assert('./'   in output)
107            assert('../'  in output)
108
109            output = u_boot_console.run_command(
110                '%sls host 0:0 dir6/0123456789abcdef13/..' % fs_type)
111            assert('0123456789abcdef00/'  in output)
112            assert('0123456789abcdef13/'  in output)
113