Debian Codegraph @

test/unit/lib/test_transport.c

raw
1 #include <unistd.h>
2
3 #include "../../../src/lib/transport.h"
4
5 #include "../../lib/endpoint.h"
6 #include "../../lib/runner.h"
7 #include "../../lib/uv.h"
8
9 TEST_MODULE(lib_transport);
10
11 /******************************************************************************
12 *
13 * Fixture
14 *
15 ******************************************************************************/
16
17 struct fixture
18 {
19 struct test_endpoint endpoint;
20 struct uv_loop_s loop;
21 struct transport transport;
22 int client;
23 struct
24 {
25 bool invoked;
26 int status;
27 } read;
28 struct
29 {
30 bool invoked;
31 int status;
32 } write;
33 };
34
35 static void read_cb(struct transport *transport, int status)
36 {
37 struct fixture *f = transport->data;
38 f->read.invoked = true;
39 f->read.status = status;
40 }
41
42 static void write_cb(struct transport *transport, int status)
43 {
44 struct fixture *f = transport->data;
45 f->write.invoked = true;
46 f->write.status = status;
47 }
48
49 static void *setup(const MunitParameter params[], void *user_data)
50 {
51 struct fixture *f = munit_malloc(sizeof *f);
52 struct uv_stream_s *stream;
53 int rv;
54 int server;
55 (void)user_data;
56 test_endpoint_setup(&f->endpoint, params);
57 rv = listen(f->endpoint.fd, 16);
58 munit_assert_int(rv, ==, 0);
59 test_endpoint_pair(&f->endpoint, &server, &f->client);
60 test_uv_setup(params, &f->loop);
61 rv = transport__stream(&f->loop, server, &stream);
62 munit_assert_int(rv, ==, 0);
63 rv = transport__init(&f->transport, stream);
64 munit_assert_int(rv, ==, 0);
65 f->transport.data = f;
66 f->read.invoked = false;
67 f->read.status = -1;
68 f->write.invoked = false;
69 f->write.status = -1;
70 return f;
71 }
72
73 static void tear_down(void *data)
74 {
75 struct fixture *f = data;
76 int rv;
77 rv = close(f->client);
78 munit_assert_int(rv, ==, 0);
79 transport__close(&f->transport, NULL);
80 test_uv_stop(&f->loop);
81 test_uv_tear_down(&f->loop);
82 test_endpoint_tear_down(&f->endpoint);
83 free(data);
84 }
85
86 /******************************************************************************
87 *
88 * Helper macros.
89 *
90 ******************************************************************************/
91
92 /* Allocate a libuv buffer with the given amount of bytes. */
93 #define BUF_ALLOC(N) {munit_malloc(N), N};
94
95 /* Start reading into the current buffer */
96 #define READ(BUF) \
97 { \
98 int rv2; \
99 rv2 = transport__read(&f->transport, BUF, read_cb); \
100 munit_assert_int(rv2, ==, 0); \
101 }
102
103 /* Start writing the current buffer into the stream */
104 #define WRITE(BUF) \
105 { \
106 int rv2; \
107 rv2 = transport__write(&f->transport, BUF, write_cb); \
108 munit_assert_int(rv2, ==, 0); \
109 }
110
111 /* Write N bytes into the client buffer. Each byte will contain a progressive
112 * number starting from 1. */
113 #define CLIENT_WRITE(N) \
114 { \
115 uint8_t *buf_ = munit_malloc(N); \
116 unsigned i_; \
117 int rv_; \
118 for (i_ = 0; i_ < N; i_++) { \
119 buf_[i_] = i_ + 1; \
120 } \
121 rv_ = write(f->client, buf_, N); \
122 munit_assert_int(rv_, ==, N); \
123 free(buf_); \
124 }
125
126 /******************************************************************************
127 *
128 * Assertions.
129 *
130 ******************************************************************************/
131
132 /* Assert that the read callback was invoked with the given status. */
133 #define ASSERT_READ(STATUS) \
134 munit_assert_true(f->read.invoked); \
135 munit_assert_int(f->read.status, ==, STATUS); \
136 f->read.invoked = false; \
137 f->read.status = -1
138
139 /* Assert that the write callback was invoked with the given status. */
140 #define ASSERT_WRITE(STATUS) \
141 munit_assert_true(f->write.invoked); \
142 munit_assert_int(f->write.status, ==, STATUS); \
143 f->write.invoked = false; \
144 f->write.status = -1
145
146 /******************************************************************************
147 *
148 * transport__read
149 *
150 ******************************************************************************/
151
152 TEST_SUITE(read);
153 TEST_SETUP(read, setup);
154 TEST_TEAR_DOWN(read, tear_down);
155
156 TEST_CASE(read, success, NULL)
157 {
158 struct fixture *f = data;
159 uv_buf_t buf = BUF_ALLOC(2);
160 (void)params;
161 CLIENT_WRITE(2);
162 READ(&buf);
163 test_uv_run(&f->loop, 1);
164 ASSERT_READ(0);
165 munit_assert_int(((uint8_t *)buf.base)[0], ==, 1);
166 munit_assert_int(((uint8_t *)buf.base)[1], ==, 2);
167 free(buf.base);
168 return MUNIT_OK;
169 }
170
171 /******************************************************************************
172 *
173 * transport__write
174 *
175 ******************************************************************************/
176
177 TEST_SUITE(write);
178 TEST_SETUP(write, setup);
179 TEST_TEAR_DOWN(write, tear_down);
180
181 TEST_CASE(write, success, NULL)
182 {
183 struct fixture *f = data;
184 uv_buf_t buf = BUF_ALLOC(2);
185 (void)params;
186 WRITE(&buf);
187 test_uv_run(&f->loop, 1);
188 ASSERT_WRITE(0);
189 free(buf.base);
190 return MUNIT_OK;
191 }