1 /** |
1 /** |
2 * The main test code entry point |
2 * The main test code entry point |
3 */ |
3 */ |
4 #include "sock_test.h" |
4 #include "sock_test.h" |
|
5 #include "line_proto.h" |
5 #include "irc_conn.h" |
6 #include "irc_conn.h" |
6 #include "log.h" |
7 #include "log.h" |
7 |
8 #include "error.h" |
|
9 |
|
10 #include <stdlib.h> |
8 #include <string.h> |
11 #include <string.h> |
9 #include <assert.h> |
12 #include <assert.h> |
10 |
13 |
|
14 void assert_strcmp (const char *a, const char *b) |
|
15 { |
|
16 if (strcmp(a, b)) |
|
17 FATAL("'%s' != '%s'", a, b); |
|
18 } |
|
19 |
|
20 void assert_strncmp (const char *a, const char *b, size_t n) |
|
21 { |
|
22 if (strncmp(a, b, n)) |
|
23 FATAL("'%s':%d != '%s'", a, n, b); |
|
24 } |
|
25 |
|
26 void assert_strlen (const char *str, size_t n) |
|
27 { |
|
28 if (strlen(str) != n) |
|
29 FATAL("strlen('%s') != %u", str, n); |
|
30 } |
|
31 |
|
32 void assert_strnul (const char *str) |
|
33 { |
|
34 if (str != NULL) |
|
35 FATAL("'%s' != NULL", str); |
|
36 } |
|
37 |
|
38 void assert_success (err_t err) |
|
39 { |
|
40 if (err != SUCCESS) |
|
41 FATAL("error: %s", error_name(err)); |
|
42 |
|
43 } |
|
44 |
|
45 void assert_err (err_t err, err_t target) |
|
46 { |
|
47 if (err != target) |
|
48 FATAL("error: <%s> != target <%s>", error_name(err), error_name(target)); |
|
49 } |
|
50 |
11 void assert_sock_read (struct sock_stream *sock, const char *str) |
51 void assert_sock_read (struct sock_stream *sock, const char *str) |
12 { |
52 { |
13 char buf[strlen(str)]; |
53 char buf[strlen(str)]; |
14 |
54 |
15 log_debug("read: %p: '%s'", sock, str); |
55 log_debug("read: %p: '%s'", sock, str); |
16 |
56 |
17 // read it |
57 // read it |
18 assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str)); |
58 assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str)); |
19 |
59 |
20 // cmp |
60 // cmp |
21 assert(strncmp(buf, str, strlen(str)) == 0); |
61 assert_strncmp(buf, str, strlen(str)); |
22 } |
62 } |
23 |
63 |
24 void assert_sock_write (struct sock_stream *sock, const char *str) |
64 void assert_sock_write (struct sock_stream *sock, const char *str) |
25 { |
65 { |
26 log_debug("write: %p: '%s'", sock, str); |
66 log_debug("write: %p: '%s'", sock, str); |
27 |
67 |
28 // write it |
68 // write it |
29 assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str)); |
69 assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str)); |
|
70 } |
|
71 |
|
72 void assert_sock_eof (struct sock_stream *sock) |
|
73 { |
|
74 char buf; |
|
75 |
|
76 log_debug("eof: %p", sock); |
|
77 |
|
78 assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF); |
30 } |
79 } |
31 |
80 |
32 void test_sock_test (void) |
81 void test_sock_test (void) |
33 { |
82 { |
34 struct sock_test *sock = sock_test_create(); |
83 struct sock_test *sock = sock_test_create(); |
38 }; |
87 }; |
39 const char *_write_data = "test data"; |
88 const char *_write_data = "test data"; |
40 |
89 |
41 // put the read data |
90 // put the read data |
42 log_debug("set_recv_buffer: %p, %d", _read_data, 2); |
91 log_debug("set_recv_buffer: %p, %d", _read_data, 2); |
43 sock_test_set_recv_buffer(sock, _read_data, 2); |
92 sock_test_set_recv_buffer(sock, _read_data, 2, true); |
44 |
93 |
45 // read it out |
94 // read it out |
|
95 log_info("test sock_test_read"); |
|
96 |
46 assert_sock_read(SOCK_TEST_BASE(sock), "foo"); |
97 assert_sock_read(SOCK_TEST_BASE(sock), "foo"); |
47 assert_sock_read(SOCK_TEST_BASE(sock), "ba"); |
98 assert_sock_read(SOCK_TEST_BASE(sock), "ba"); |
48 assert_sock_read(SOCK_TEST_BASE(sock), "rx"); |
99 assert_sock_read(SOCK_TEST_BASE(sock), "rx"); |
|
100 assert_sock_eof(SOCK_TEST_BASE(sock)); |
49 |
101 |
50 // write the data in |
102 // write the data in |
|
103 log_info("test sock_test_write"); |
|
104 |
51 assert_sock_write(SOCK_TEST_BASE(sock), "test "); |
105 assert_sock_write(SOCK_TEST_BASE(sock), "test "); |
52 assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
106 assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
53 |
107 |
54 // get the data out |
108 // get the data out |
55 char *data; |
109 char *data; |
56 size_t len; |
110 size_t len; |
57 |
111 |
|
112 log_info("test get_send_data"); |
|
113 |
58 sock_test_get_send_data(sock, &data, &len); |
114 sock_test_get_send_data(sock, &data, &len); |
59 |
115 |
60 log_debug("get_send_data: %u: '%s'", len, data); |
|
61 |
|
62 // should be the same |
116 // should be the same |
63 assert(len == strlen(_write_data)); |
117 assert_strlen(_write_data, len); |
64 assert(strncmp(data, _write_data, len) == 0); |
118 assert_strncmp(data, _write_data, len); |
|
119 |
|
120 // cleanup |
|
121 free(data); |
|
122 sock_test_destroy(sock); |
|
123 } |
|
124 |
|
125 void assert_read_line (struct line_proto *lp, const char *line_str) |
|
126 { |
|
127 char *line_buf; |
|
128 |
|
129 log_debug("expect: '%s'", line_str); |
|
130 |
|
131 assert_success(line_proto_recv(lp, &line_buf)); |
|
132 |
|
133 if (line_str) { |
|
134 assert(line_buf != NULL); |
|
135 assert_strcmp(line_buf, line_str); |
|
136 |
|
137 } else { |
|
138 assert_strnul(line_buf); |
|
139 |
|
140 } |
|
141 } |
|
142 |
|
143 static struct line_proto_callbacks _lp_callbacks = { |
|
144 .on_line = NULL, |
|
145 .on_error = NULL, |
|
146 }; |
|
147 |
|
148 void test_line_proto (void) |
|
149 { |
|
150 struct sock_test *sock = sock_test_create(); |
|
151 struct io_vec _read_data[] = { |
|
152 { "hello\r\n", 7 }, |
|
153 { "world\n", 6 }, |
|
154 { "this ", 5 }, |
|
155 { "is a line\r", 10 }, |
|
156 { "\nfragment", 9 }, |
|
157 }, _trailing_data = { "\r\n", 2 }; |
|
158 struct line_proto *lp; |
|
159 struct error_info err; |
|
160 |
|
161 // put the read data |
|
162 log_debug("set_recv_buffer: %p, %d", _read_data, 5); |
|
163 sock_test_set_recv_buffer(sock, _read_data, 5, false); |
|
164 |
|
165 // create the lp |
|
166 assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err)); |
|
167 |
|
168 log_info("test line_proto_recv"); |
|
169 |
|
170 // then read some lines from it |
|
171 assert_read_line(lp, "hello"); |
|
172 assert_read_line(lp, "world"); |
|
173 assert_read_line(lp, "this is a line"); |
|
174 assert_read_line(lp, NULL); |
|
175 |
|
176 // then add a final bit |
|
177 sock_test_add_recv_vec(sock, _trailing_data); |
|
178 |
|
179 // read the final bit |
|
180 assert_read_line(lp, "fragment"); |
|
181 |
|
182 // cleanup |
|
183 line_proto_release(lp); |
65 } |
184 } |
66 |
185 |
67 static struct irc_conn_callbacks _conn_callbacks = { |
186 static struct irc_conn_callbacks _conn_callbacks = { |
68 .on_registered = NULL, |
187 .on_registered = NULL, |
69 }; |
188 }; |