4 Copyright (C) Amitay Isaacs 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
27 #include "common/sock_io.c"
29 static int socket_init(const char *sockpath)
31 struct sockaddr_un addr;
35 memset(&addr, 0, sizeof(addr));
36 addr.sun_family = AF_UNIX;
38 len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
39 assert(len < sizeof(addr.sun_path));
41 fd = socket(AF_UNIX, SOCK_STREAM, 0);
44 ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
53 static void test1_writer(int fd)
59 for (len = 10; len < 1000; len += 10) {
61 uint32_t buflen = len + sizeof(uint32_t);
63 memset(buf, value, buflen);
64 memcpy(buf, &buflen, sizeof(uint32_t));
66 nwritten = sys_write(fd, buf, buflen);
67 assert(nwritten == buflen);
71 struct test1_reader_state {
76 static void test1_reader(uint8_t *buf, size_t buflen, void *private_data)
78 struct test1_reader_state *state =
79 (struct test1_reader_state *)private_data;
86 assert(buflen == state->pkt_len);
91 static void test1(TALLOC_CTX *mem_ctx, const char *sockpath)
93 struct test1_reader_state state;
94 struct tevent_context *ev;
95 struct sock_queue *queue;
111 fd = socket_init(sockpath);
115 n = sys_write(pfd[1], &ret, sizeof(int));
116 assert(n == sizeof(int));
118 newfd = accept(fd, NULL, NULL);
130 n = sys_read(pfd[0], &ret, sizeof(int));
131 assert(n == sizeof(int));
136 fd = sock_connect(sockpath);
139 ev = tevent_context_init(mem_ctx);
142 state.pkt_len = 10 + sizeof(uint32_t);
145 queue = sock_queue_setup(mem_ctx, ev, fd, test1_reader, &state);
146 assert(queue != NULL);
148 while (! state.done) {
149 tevent_loop_once(ev);
159 static void test2_reader(int fd)
162 size_t pkt_len = 10 + sizeof(uint32_t);
166 n = sys_read(fd, buf, 1024);
173 assert((size_t)n == pkt_len);
178 static void test2_dummy_reader(uint8_t *buf, size_t buflen,
184 static void test2_writer(struct sock_queue *queue)
190 for (len = 10; len < 1000; len += 10) {
191 int value = len / 10;
192 uint32_t buflen = len + sizeof(uint32_t);
194 memset(buf, value, buflen);
195 memcpy(buf, &buflen, sizeof(uint32_t));
197 ret = sock_queue_write(queue, buf, buflen);
202 static void test2(TALLOC_CTX *mem_ctx, const char *sockpath)
204 struct tevent_context *ev;
205 struct sock_queue *queue;
221 fd = socket_init(sockpath);
225 n = sys_write(pfd[1], &ret, sizeof(int));
226 assert(n == sizeof(int));
228 newfd = accept(fd, NULL, NULL);
240 n = sys_read(pfd[0], &ret, sizeof(int));
241 assert(n == sizeof(int));
246 fd = sock_connect(sockpath);
249 ev = tevent_context_init(mem_ctx);
252 queue = sock_queue_setup(mem_ctx, ev, fd, test2_dummy_reader, NULL);
253 assert(queue != NULL);
264 int main(int argc, const char **argv)
267 const char *sockpath;
270 fprintf(stderr, "%s <sockpath>\n", argv[0]);
276 mem_ctx = talloc_new(NULL);
277 assert(mem_ctx != NULL);
279 test1(mem_ctx, sockpath);
280 test2(mem_ctx, sockpath);