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
176
177
178
179
180
181
182
183
184
185
186
187
|
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "net.h"
#include "fdpass.h"
#include "istream.h"
#include "istream-unix.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
static int send_fd, send_fd2;
static void write_one(int fd)
{
if (write(fd, "1", 1) < 0)
i_fatal("write() failed: %m");
}
static void read_one(int fd)
{
char buf;
if (read(fd, &buf, 1) < 0)
i_fatal("read() failed: m");
}
static void
test_server_read_nofd(struct istream *input, unsigned int idx)
{
const unsigned char *data;
size_t size;
test_assert_idx(i_stream_read_more(input, &data, &size) == 1, idx);
i_stream_skip(input, 1);
test_assert_idx(i_stream_unix_get_read_fd(input) == -1, idx);
}
static void
test_server_read_fd(struct istream *input, int wanted_fd, unsigned int idx)
{
struct stat st1, st2;
const unsigned char *data;
size_t size;
int recv_fd;
test_assert_idx(i_stream_read_more(input, &data, &size) == 1, idx);
i_stream_skip(input, 1);
test_assert_idx((recv_fd = i_stream_unix_get_read_fd(input)) != -1, idx);
if (recv_fd != -1) {
if (fstat(recv_fd, &st1) < 0 || fstat(wanted_fd, &st2) < 0)
i_fatal("fstat() failed: %m");
test_assert_idx(st1.st_ino == st2.st_ino, idx);
i_close_fd(&recv_fd);
}
}
static void test_istream_unix_server(int fd)
{
struct istream *input;
const unsigned char *data;
size_t size;
input = i_stream_create_unix(fd, 1024);
/* 1) simple read */
test_server_read_nofd(input, 1);
write_one(fd);
/* 2) fd was sent but we won't get it */
test_server_read_nofd(input, 2);
/* we still shouldn't have the fd */
i_stream_set_blocking(input, FALSE);
i_stream_unix_set_read_fd(input);
test_assert(i_stream_read_more(input, &data, &size) == 0);
test_assert(i_stream_unix_get_read_fd(input) == -1);
i_stream_set_blocking(input, TRUE);
write_one(fd);
/* 3) the previous fd should be lost now */
test_server_read_nofd(input, 3);
write_one(fd);
/* 4) we should get the fd now */
test_server_read_fd(input, send_fd2, 4);
write_one(fd);
/* 5) the previous fd shouldn't be returned anymore */
i_stream_unix_set_read_fd(input);
test_server_read_nofd(input, 5);
write_one(fd);
/* 6) with i_stream_unix_unset_read_fd() we shouldn't get fd anymore */
i_stream_unix_unset_read_fd(input);
test_server_read_nofd(input, 6);
write_one(fd);
/* 7-8) two fds were sent, but we'll get only the first one */
i_stream_unix_set_read_fd(input);
test_server_read_fd(input, send_fd, 7);
test_server_read_nofd(input, 8);
write_one(fd);
/* 9-10) two fds were sent, and we'll get them both */
i_stream_unix_set_read_fd(input);
test_server_read_fd(input, send_fd, 9);
i_stream_unix_set_read_fd(input);
test_server_read_fd(input, send_fd2, 10);
write_one(fd);
i_stream_destroy(&input);
i_close_fd(&fd);
}
static void test_istream_unix_client(int fd)
{
/* 1) */
write_one(fd);
read_one(fd);
/* 2) */
if (fd_send(fd, send_fd, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
read_one(fd);
/* 3) */
write_one(fd);
read_one(fd);
/* 4) */
if (fd_send(fd, send_fd2, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
read_one(fd);
/* 5) */
write_one(fd);
read_one(fd);
/* 6) */
if (fd_send(fd, send_fd, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
read_one(fd);
/* 7-8) */
if (fd_send(fd, send_fd, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
if (fd_send(fd, send_fd2, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
read_one(fd);
/* 9-10) */
if (fd_send(fd, send_fd, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
if (fd_send(fd, send_fd2, "1", 1) < 0)
i_fatal("fd_send() failed: %m");
read_one(fd);
i_close_fd(&fd);
}
void test_istream_unix(void)
{
int fd[2];
test_begin("istream unix");
if ((send_fd = open("/dev/null", O_RDONLY)) == -1)
i_fatal("open(/dev/null) failed: %m");
if ((send_fd2 = open("/dev/zero", O_RDONLY)) == -1)
i_fatal("open(/dev/zero) failed: %m");
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
i_fatal("socketpair() failed: %m");
switch (fork()) {
case -1:
i_fatal("fork() failed: %m");
case 0:
i_close_fd(&fd[0]);
test_istream_unix_client(fd[1]);
test_exit(0);
default:
i_close_fd(&fd[1]);
test_istream_unix_server(fd[0]);
break;
}
i_close_fd(&send_fd);
i_close_fd(&send_fd2);
test_end();
}
|