1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-5-20 Zhujiale the first version
*/
#include <rtthread.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <msh.h>
#include "utest.h"
#include "utest_assert.h"
void run_copy()
{
int ret = 0;
ret = msh_exec("cd /tmp", 7);
if (ret != 0)
{
LOG_E("errno=%d, ret=%d\n", errno, ret);
LOG_E("cd /tmp error");
uassert_false(1);
}
uassert_true(1);
ret = msh_exec("touch test", 10);
if (ret != 0)
{
LOG_E("errno=%d, ret=%d\n", errno, ret);
LOG_E("touch test error");
uassert_false(1);
}
uassert_true(1);
ret = msh_exec("echo this_is_a_test_file test", 29);
if (ret != 0)
{
LOG_E("errno=%d, ret=%d\n", errno, ret);
LOG_E("echo this_is_a_test_file test error");
uassert_false(1);
}
uassert_true(1);
ret = msh_exec("cp test test1", 13);
if (ret != 0)
{
LOG_E("errno=%d, ret=%d\n", errno, ret);
LOG_E("cp test test1 error");
uassert_false(1);
}
uassert_true(1);
}
static void run_long_name(void)
{
static const char source[] =
"/tmp/iceoryx2_node_1234567890_abcdefghijklmnopqrstuvwxyz_shared";
static const char destination[] =
"/tmp/iceoryx2_node_abcdefghijklmnopqrstuvwxyz_0987654321_renamed";
static const char payload[] = "tmpfs-long-name";
char buffer[sizeof(payload)] = { 0 };
struct stat st = { 0 };
int fd;
fd = open(source, O_CREAT | O_RDWR | O_TRUNC, 0);
uassert_true(fd >= 0);
uassert_int_equal(fstat(fd, &st), 0);
uassert_int_equal(st.st_nlink, 1);
uassert_int_equal(write(fd, payload, sizeof(payload)), sizeof(payload));
uassert_int_equal(close(fd), 0);
uassert_int_equal(rename(source, destination), 0);
fd = open(destination, O_RDONLY, 0);
uassert_true(fd >= 0);
uassert_int_equal(read(fd, buffer, sizeof(buffer)), sizeof(buffer));
uassert_str_equal(buffer, payload);
uassert_int_equal(close(fd), 0);
uassert_int_equal(unlink(destination), 0);
}
static void run_too_long_name_reject(void)
{
static const char prefix[] = "/tmp/";
char path[(sizeof(prefix) - 1) + DIRENT_NAME_MAX + 1];
int fd;
rt_memcpy(path, prefix, sizeof(prefix) - 1);
rt_memset(path + sizeof(prefix) - 1, 'x', DIRENT_NAME_MAX);
path[sizeof(path) - 1] = '\0';
fd = open(path, O_CREAT | O_RDWR, 0);
uassert_int_equal(fd, -1);
uassert_int_equal(errno, -ENAMETOOLONG);
fd = open("/tmp/tmpfs-after-long-name", O_CREAT | O_RDWR | O_TRUNC, 0);
uassert_true(fd >= 0);
uassert_int_equal(close(fd), 0);
errno = 0;
uassert_int_equal(rename("/tmp/tmpfs-after-long-name", path), -1);
uassert_int_equal(errno, -ENAMETOOLONG);
uassert_int_equal(unlink("/tmp/tmpfs-after-long-name"), 0);
}
static void run_mode_change(void)
{
static const char path[] = "/tmp/tmpfs-mode-change";
struct stat st = { 0 };
int fd;
unlink(path);
fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0700);
uassert_true(fd >= 0);
uassert_int_equal(fstat(fd, &st), 0);
uassert_int_equal(st.st_mode & 0777, 0700);
uassert_int_equal(fchmod(fd, 0400 | S_ISVTX), 0);
uassert_int_equal(fstat(fd, &st), 0);
uassert_int_equal(st.st_mode & 0777, 0400);
uassert_true((st.st_mode & S_ISVTX) != 0);
uassert_int_equal(fchmod(fd, 0400), 0);
uassert_int_equal(fstat(fd, &st), 0);
uassert_int_equal(st.st_mode & S_ISVTX, 0);
uassert_int_equal(unlink(path), 0);
uassert_int_equal(fchmod(fd, 0600), 0);
uassert_int_equal(fstat(fd, &st), 0);
uassert_int_equal(st.st_mode & 0777, 0600);
uassert_int_equal(close(fd), 0);
errno = 0;
fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0400);
uassert_true(fd >= 0);
uassert_int_equal(close(fd), 0);
uassert_int_equal(open(path, O_WRONLY, 0), -1);
uassert_int_equal(errno, -EACCES);
uassert_int_equal(unlink(path), 0);
}
static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(run_copy);
UTEST_UNIT_RUN(run_long_name);
UTEST_UNIT_RUN(run_too_long_name_reject);
UTEST_UNIT_RUN(run_mode_change);
}
UTEST_TC_EXPORT(testcase, "testcase.tfs.tmpfs", utest_tc_init, utest_tc_cleanup, 10);
|