1# SPDX-License-Identifier:      GPL-2.0+
2# Copyright (c) 2019, Texas Instrument
3# Author: Jean-Jacques Hiblot <jjhiblot@ti.com>
4#
5# U-Boot File System:symlink Test
6
7"""
8This test verifies unlink operation (deleting a file or a directory)
9on file system.
10"""
11
12import pytest
13import re
14from fstest_defs import *
15from fstest_helpers import assert_fs_integrity
16
17
18@pytest.mark.boardspec('sandbox')
19@pytest.mark.slow
20class TestSymlink(object):
21    def test_symlink1(self, u_boot_console, fs_obj_symlink):
22        """
23        Test Case 1 - create a link. and follow it when reading
24        """
25        fs_type, fs_img, md5val = fs_obj_symlink
26        with u_boot_console.log.section('Test Case 1 - create link and read'):
27            output = u_boot_console.run_command_list([
28                'host bind 0 %s' % fs_img,
29                'setenv filesize',
30                'ln host 0:0 %s /%s.link ' % (SMALL_FILE, SMALL_FILE),
31            ])
32            assert('' in ''.join(output))
33
34            output = u_boot_console.run_command_list([
35                '%sload host 0:0 %x /%s.link' % (fs_type, ADDR, SMALL_FILE),
36                'printenv filesize'])
37            assert('filesize=100000' in ''.join(output))
38
39            # Test Case 4b - Read full 1MB of small file
40            output = u_boot_console.run_command_list([
41                'md5sum %x $filesize' % ADDR,
42                'setenv filesize'])
43            assert(md5val[0] in ''.join(output))
44            assert_fs_integrity(fs_type, fs_img)
45
46    def test_symlink2(self, u_boot_console, fs_obj_symlink):
47        """
48        Test Case 2 - create chained links
49        """
50        fs_type, fs_img, md5val = fs_obj_symlink
51        with u_boot_console.log.section('Test Case 2 - create chained links'):
52            output = u_boot_console.run_command_list([
53                'host bind 0 %s' % fs_img,
54                'setenv filesize',
55                'ln host 0:0 %s /%s.link1 ' % (SMALL_FILE, SMALL_FILE),
56                'ln host 0:0 /%s.link1 /SUBDIR/%s.link2' % (
57                    SMALL_FILE, SMALL_FILE),
58                'ln host 0:0 SUBDIR/%s.link2 /%s.link3' % (
59                    SMALL_FILE, SMALL_FILE),
60            ])
61            assert('' in ''.join(output))
62
63            output = u_boot_console.run_command_list([
64                '%sload host 0:0 %x /%s.link3' % (fs_type, ADDR, SMALL_FILE),
65                'printenv filesize'])
66            assert('filesize=100000' in ''.join(output))
67
68            # Test Case 4b - Read full 1MB of small file
69            output = u_boot_console.run_command_list([
70                'md5sum %x $filesize' % ADDR,
71                'setenv filesize'])
72            assert(md5val[0] in ''.join(output))
73            assert_fs_integrity(fs_type, fs_img)
74
75    def test_symlink3(self, u_boot_console, fs_obj_symlink):
76        """
77        Test Case 3 - replace file/link with link
78        """
79        fs_type, fs_img, md5val = fs_obj_symlink
80        with u_boot_console.log.section('Test Case 1 - create link and read'):
81            output = u_boot_console.run_command_list([
82                'host bind 0 %s' % fs_img,
83                'setenv filesize',
84                'ln host 0:0 %s /%s ' % (MEDIUM_FILE, SMALL_FILE),
85                'ln host 0:0 %s /%s.link ' % (MEDIUM_FILE, MEDIUM_FILE),
86            ])
87            assert('' in ''.join(output))
88
89            output = u_boot_console.run_command_list([
90                '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE),
91                'printenv filesize'])
92            assert('filesize=a00000' in ''.join(output))
93
94            output = u_boot_console.run_command_list([
95                'md5sum %x $filesize' % ADDR,
96                'setenv filesize'])
97            assert(md5val[1] in ''.join(output))
98
99            output = u_boot_console.run_command_list([
100                'ln host 0:0 %s.link /%s ' % (MEDIUM_FILE, SMALL_FILE),
101                '%sload host 0:0 %x /%s' % (fs_type, ADDR, SMALL_FILE),
102                'printenv filesize'])
103            assert('filesize=a00000' in ''.join(output))
104
105            output = u_boot_console.run_command_list([
106                'md5sum %x $filesize' % ADDR,
107                'setenv filesize'])
108            assert(md5val[1] in ''.join(output))
109            assert_fs_integrity(fs_type, fs_img)
110
111    def test_symlink4(self, u_boot_console, fs_obj_symlink):
112        """
113        Test Case 4 - create a broken link
114        """
115        fs_type, fs_img, md5val = fs_obj_symlink
116        with u_boot_console.log.section('Test Case 1 - create link and read'):
117
118            output = u_boot_console.run_command_list([
119                'setenv filesize',
120                'ln host 0:0 nowhere /link ',
121            ])
122            assert('' in ''.join(output))
123
124            output = u_boot_console.run_command(
125                '%sload host 0:0 %x /link' %
126                (fs_type, ADDR))
127            with u_boot_console.disable_check('error_notification'):
128                output = u_boot_console.run_command('printenv filesize')
129            assert('"filesize" not defined' in ''.join(output))
130            assert_fs_integrity(fs_type, fs_img)
131