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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (c) 2006-2026, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "tc_af_unix.h"
#define TC_AF_UNIX_NAMESPACE_REPEAT 8
static void tc_af_unix_pathname_lifetime(void)
{
char path[TC_AF_UNIX_PATH_MAX];
int error;
int first = -1;
int result;
int second = -1;
struct sockaddr_un address;
struct stat file_stat;
tc_af_unix_path(path, sizeof(path), "utafupath");
(void)unlink(path);
tc_af_unix_make_address(&address, path);
first = socket(AF_UNIX, SOCK_DGRAM, 0);
uassert_true(first >= 0);
if (first < 0)
{
goto __exit;
}
uassert_int_equal(bind(first, (struct sockaddr *)&address, sizeof(address)),
0);
uassert_int_equal(stat(path, &file_stat), 0);
uassert_true(S_ISSOCK(file_stat.st_mode));
tc_af_unix_closesocket(&first);
uassert_int_equal(stat(path, &file_stat), 0);
uassert_true(S_ISSOCK(file_stat.st_mode));
second = socket(AF_UNIX, SOCK_DGRAM, 0);
uassert_true(second >= 0);
if (second < 0)
{
goto __exit;
}
result = bind(second, (struct sockaddr *)&address, sizeof(address));
error = rt_get_errno();
uassert_int_equal(result, -1);
uassert_int_equal(error, EADDRINUSE);
uassert_int_equal(unlink(path), 0);
uassert_int_equal(bind(second, (struct sockaddr *)&address,
sizeof(address)),
0);
__exit:
tc_af_unix_closesocket(&second);
tc_af_unix_closesocket(&first);
(void)unlink(path);
}
static void tc_af_unix_lookup_after_close(void)
{
char path[TC_AF_UNIX_PATH_MAX];
int client = -1;
int error;
int result;
int server = -1;
struct sockaddr_un address;
tc_af_unix_path(path, sizeof(path), "utafuref");
(void)unlink(path);
tc_af_unix_make_address(&address, path);
server = socket(AF_UNIX, SOCK_STREAM, 0);
client = socket(AF_UNIX, SOCK_STREAM, 0);
uassert_true(server >= 0);
uassert_true(client >= 0);
if (server < 0 || client < 0)
{
goto __exit;
}
uassert_int_equal(bind(server, (struct sockaddr *)&address,
sizeof(address)),
0);
uassert_int_equal(listen(server, 1), 0);
tc_af_unix_closesocket(&server);
result = connect(client, (struct sockaddr *)&address, sizeof(address));
error = rt_get_errno();
uassert_int_equal(result, -1);
uassert_int_equal(error, ECONNREFUSED);
uassert_int_equal(unlink(path), 0);
result = connect(client, (struct sockaddr *)&address, sizeof(address));
error = rt_get_errno();
uassert_int_equal(result, -1);
uassert_int_equal(error, ENOENT);
__exit:
tc_af_unix_closesocket(&client);
tc_af_unix_closesocket(&server);
(void)unlink(path);
}
static void tc_af_unix_namespace_reuse(void)
{
char name[16];
char path[TC_AF_UNIX_PATH_MAX];
int client = -1;
int error;
int index;
int result;
int sock = -1;
struct sockaddr_un address;
for (index = 0; index < TC_AF_UNIX_NAMESPACE_REPEAT; index++)
{
rt_snprintf(name, sizeof(name), "utafuns%d", index);
tc_af_unix_path(path, sizeof(path), name);
(void)unlink(path);
tc_af_unix_make_address(&address, path);
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
uassert_true(sock >= 0);
if (sock < 0)
{
return;
}
uassert_int_equal(bind(sock, (struct sockaddr *)&address,
sizeof(address)),
0);
tc_af_unix_closesocket(&sock);
uassert_int_equal(unlink(path), 0);
}
tc_af_unix_path(path, sizeof(path), "utafuns0");
tc_af_unix_make_address(&address, path);
client = socket(AF_UNIX, SOCK_DGRAM, 0);
uassert_true(client >= 0);
if (client < 0)
{
return;
}
result = connect(client, (struct sockaddr *)&address, sizeof(address));
error = rt_get_errno();
uassert_int_equal(result, -1);
uassert_int_equal(error, ENOENT);
tc_af_unix_closesocket(&client);
tc_af_unix_path(path, sizeof(path), "utafunew");
(void)unlink(path);
tc_af_unix_make_address(&address, path);
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
uassert_true(sock >= 0);
if (sock < 0)
{
return;
}
uassert_int_equal(bind(sock, (struct sockaddr *)&address, sizeof(address)),
0);
tc_af_unix_closesocket(&sock);
(void)unlink(path);
}
static void tc_af_unix_namespace_testcase(void)
{
UTEST_UNIT_RUN(tc_af_unix_pathname_lifetime);
UTEST_UNIT_RUN(tc_af_unix_lookup_after_close);
UTEST_UNIT_RUN(tc_af_unix_namespace_reuse);
}
UTEST_TC_EXPORT(tc_af_unix_namespace_testcase,
"components.dfs.v2.af_unix.namespace", RT_NULL, RT_NULL, 15);
|