author | Tero Marttila <terom@fixme.fi> |
Thu, 12 Mar 2009 22:06:01 +0200 | |
changeset 44 | 6bd70113e1ed |
parent 43 | 42f5dc680930 |
child 45 | 71e65564afd2 |
permissions | -rw-r--r-- |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
/** |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
* The main test code entry point |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
*/ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
#include "sock_test.h" |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
5 |
#include "line_proto.h" |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
#include "irc_conn.h" |
44 | 7 |
#include "irc_net.h" |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
#include "log.h" |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
9 |
#include "error.h" |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
11 |
#include <stdlib.h> |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
#include <string.h> |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
#include <assert.h> |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
|
42 | 15 |
void assert_strcmp (const char *is, const char *should_be) |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
16 |
{ |
42 | 17 |
if (strcmp(is, should_be)) |
18 |
FATAL("'%s' != '%s'", is, should_be); |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
19 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
20 |
|
42 | 21 |
void assert_strncmp (const char *is, const char *should_be, size_t n) |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
22 |
{ |
42 | 23 |
if (strncmp(is, should_be, n)) |
44 | 24 |
FATAL("'%s':%u != '%s'", is, (unsigned) n, should_be); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
25 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
26 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
27 |
void assert_strlen (const char *str, size_t n) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
28 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
29 |
if (strlen(str) != n) |
44 | 30 |
FATAL("strlen('%s') != %u", str, (unsigned) n); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
31 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
32 |
|
42 | 33 |
void assert_strnull (const char *str) |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
34 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
35 |
if (str != NULL) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
36 |
FATAL("'%s' != NULL", str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
37 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
38 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
39 |
void assert_success (err_t err) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
40 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
41 |
if (err != SUCCESS) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
42 |
FATAL("error: %s", error_name(err)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
43 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
44 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
45 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
46 |
void assert_err (err_t err, err_t target) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
47 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
48 |
if (err != target) |
42 | 49 |
FATAL("error: <%s> != <%s>", error_name(err), error_name(target)); |
50 |
} |
|
51 |
||
52 |
void assert_error_info (struct error_info *is, struct error_info *should_be) |
|
53 |
{ |
|
54 |
if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be)) |
|
55 |
FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be)); |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
56 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
57 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
void assert_sock_read (struct sock_stream *sock, const char *str) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
char buf[strlen(str)]; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
log_debug("read: %p: '%s'", sock, str); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
// read it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str)); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
// cmp |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
68 |
assert_strncmp(buf, str, strlen(str)); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
void assert_sock_write (struct sock_stream *sock, const char *str) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
log_debug("write: %p: '%s'", sock, str); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
// write it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str)); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
79 |
void assert_sock_eof (struct sock_stream *sock) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
80 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
81 |
char buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
82 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
83 |
log_debug("eof: %p", sock); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
84 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
85 |
assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
86 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
87 |
|
44 | 88 |
void assert_sock_data (struct sock_test *sock, const char *data) |
43 | 89 |
{ |
90 |
// get the data out |
|
91 |
char *buf; |
|
92 |
size_t len; |
|
93 |
||
94 |
sock_test_get_send_data(sock, &buf, &len); |
|
95 |
||
96 |
log_debug("get_send_data: '%*s'", (int) len, buf); |
|
97 |
||
98 |
// should be the same |
|
99 |
assert_strncmp(buf, data, len); |
|
100 |
assert_strlen(data, len); |
|
101 |
||
102 |
// cleanup |
|
103 |
free(buf); |
|
104 |
} |
|
105 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
void test_sock_test (void) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
struct sock_test *sock = sock_test_create(); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
struct io_vec _read_data[] = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
{ "foo", 3 }, |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
{ "barx", 4 } |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
const char *_write_data = "test data"; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
// put the read data |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
log_debug("set_recv_buffer: %p, %d", _read_data, 2); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
117 |
sock_test_set_recv_buffer(sock, _read_data, 2, true); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
118 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
119 |
// read it out |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
120 |
log_info("test sock_test_read"); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
assert_sock_read(SOCK_TEST_BASE(sock), "foo"); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
assert_sock_read(SOCK_TEST_BASE(sock), "ba"); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
assert_sock_read(SOCK_TEST_BASE(sock), "rx"); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
125 |
assert_sock_eof(SOCK_TEST_BASE(sock)); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
// write the data in |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
128 |
log_info("test sock_test_write"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
129 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
assert_sock_write(SOCK_TEST_BASE(sock), "test "); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
43 | 132 |
|
133 |
// check output |
|
44 | 134 |
assert_sock_data(sock, _write_data); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
|
43 | 136 |
// check output |
44 | 137 |
assert_sock_data(sock, ""); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
138 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
139 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
140 |
sock_test_destroy(sock); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
141 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
142 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
143 |
void assert_read_line (struct line_proto *lp, const char *line_str) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
144 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
145 |
char *line_buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
146 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
147 |
log_debug("expect: '%s'", line_str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
148 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
149 |
assert_success(line_proto_recv(lp, &line_buf)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
150 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
151 |
if (line_str) { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
152 |
assert(line_buf != NULL); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
153 |
assert_strcmp(line_buf, line_str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
154 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
155 |
} else { |
42 | 156 |
assert_strnull(line_buf); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
157 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
158 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
159 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
160 |
|
42 | 161 |
/** |
162 |
* Context info for test_line_proto callbacks |
|
163 |
*/ |
|
164 |
struct _lp_test_ctx { |
|
165 |
/** Expected line */ |
|
166 |
const char *line; |
|
167 |
||
168 |
/** Expected error */ |
|
169 |
struct error_info err; |
|
170 |
}; |
|
171 |
||
172 |
static void _lp_on_line (char *line, void *arg) |
|
173 |
{ |
|
174 |
struct _lp_test_ctx *ctx = arg; |
|
175 |
||
176 |
log_debug("'%s'", line); |
|
177 |
||
178 |
assert_strcmp(line, ctx->line); |
|
179 |
||
180 |
ctx->line = NULL; |
|
181 |
} |
|
182 |
||
183 |
static void _lp_on_error (struct error_info *err, void *arg) |
|
184 |
{ |
|
185 |
struct _lp_test_ctx *ctx = arg; |
|
186 |
||
187 |
assert_error_info(err, &ctx->err); |
|
188 |
} |
|
189 |
||
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
190 |
static struct line_proto_callbacks _lp_callbacks = { |
42 | 191 |
.on_line = &_lp_on_line, |
192 |
.on_error = &_lp_on_error, |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
193 |
}; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
194 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
195 |
void test_line_proto (void) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
196 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
197 |
struct sock_test *sock = sock_test_create(); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
198 |
struct io_vec _read_data[] = { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
199 |
{ "hello\r\n", 7 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
200 |
{ "world\n", 6 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
201 |
{ "this ", 5 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
202 |
{ "is a line\r", 10 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
203 |
{ "\nfragment", 9 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
204 |
}, _trailing_data = { "\r\n", 2 }; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
205 |
struct line_proto *lp; |
42 | 206 |
struct _lp_test_ctx ctx; |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
207 |
struct error_info err; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
208 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
209 |
// put the read data |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
210 |
log_debug("set_recv_buffer: %p, %d", _read_data, 5); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
211 |
sock_test_set_recv_buffer(sock, _read_data, 5, false); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
212 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
213 |
// create the lp |
42 | 214 |
assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err)); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
215 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
216 |
log_info("test line_proto_recv"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
217 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
218 |
// then read some lines from it |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
219 |
assert_read_line(lp, "hello"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
220 |
assert_read_line(lp, "world"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
221 |
assert_read_line(lp, "this is a line"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
222 |
assert_read_line(lp, NULL); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
223 |
|
42 | 224 |
// then add a final bit to trigger on_line |
225 |
log_info("test on_line"); |
|
226 |
||
227 |
ctx.line = "fragment"; |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
228 |
sock_test_add_recv_vec(sock, _trailing_data); |
42 | 229 |
assert_strnull(ctx.line); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
230 |
|
43 | 231 |
// test writing |
232 |
log_info("test line_proto_send"); |
|
233 |
assert_success(-line_proto_send(lp, "foobar\r\n")); |
|
234 |
assert_success(-line_proto_send(lp, "quux\r\n")); |
|
44 | 235 |
assert_sock_data(sock, "foobar\r\nquux\r\n"); |
43 | 236 |
|
237 |
// XXX: test partial writes |
|
238 |
||
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
239 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
240 |
line_proto_release(lp); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
241 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
242 |
|
43 | 243 |
struct _test_irc_conn_ctx { |
244 |
bool on_registered, on_TEST; |
|
245 |
}; |
|
246 |
||
247 |
static void _conn_on_registered (struct irc_conn *conn, void *arg) |
|
248 |
{ |
|
249 |
struct _test_irc_conn_ctx *ctx = arg; |
|
250 |
||
251 |
(void) conn; |
|
252 |
||
44 | 253 |
if (ctx) ctx->on_registered = true; |
43 | 254 |
|
255 |
log_debug("registered"); |
|
256 |
} |
|
257 |
||
258 |
static void _conn_on_TEST (const struct irc_line *line, void *arg) |
|
259 |
{ |
|
260 |
struct _test_irc_conn_ctx *ctx = arg; |
|
261 |
||
262 |
assert_strcmp(line->prefix, "foobar-prefix"); |
|
263 |
assert_strcmp(line->command, "TEST"); |
|
264 |
assert_strcmp(line->args[0], "arg0"); |
|
265 |
assert_strnull(line->args[1]); |
|
266 |
||
44 | 267 |
if (ctx) ctx->on_TEST = true; |
43 | 268 |
|
269 |
log_debug("on_TEST"); |
|
270 |
} |
|
271 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
272 |
static struct irc_conn_callbacks _conn_callbacks = { |
43 | 273 |
.on_registered = &_conn_on_registered, |
274 |
}; |
|
275 |
||
276 |
static struct irc_cmd_handler _conn_handlers[] = { |
|
277 |
{ "TEST", &_conn_on_TEST }, |
|
278 |
{ NULL, NULL } |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
279 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
280 |
|
44 | 281 |
struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct _test_irc_conn_ctx *ctx) |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
282 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
283 |
struct irc_conn *conn; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
284 |
struct error_info err; |
43 | 285 |
struct irc_conn_register_info register_info = { |
286 |
"nick", "user", "realname" |
|
287 |
}; |
|
44 | 288 |
|
289 |
// create the irc_conn |
|
290 |
assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err)); |
|
291 |
||
292 |
// test register |
|
293 |
if (noisy) log_info("test irc_conn_register"); |
|
294 |
assert_success(irc_conn_register(conn, ®ister_info)); |
|
295 |
assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); |
|
296 |
||
297 |
// test on_register callback |
|
298 |
if (noisy) log_info("test irc_conn_callbacks.on_register"); |
|
299 |
sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n"); |
|
300 |
if (ctx) assert(ctx->on_registered); |
|
301 |
assert_strcmp(conn->nickname, "mynick"); |
|
302 |
||
303 |
// ok |
|
304 |
return conn; |
|
305 |
} |
|
306 |
||
307 |
void test_irc_conn (void) |
|
308 |
{ |
|
309 |
struct sock_test *sock; |
|
310 |
struct irc_conn *conn; |
|
43 | 311 |
struct _test_irc_conn_ctx ctx = { false, false }; |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
312 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
313 |
// create the test socket |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
314 |
assert((sock = sock_test_create())); |
43 | 315 |
|
44 | 316 |
// setup the basic irc_conn |
317 |
conn = setup_irc_conn(sock, true, &ctx); |
|
43 | 318 |
|
44 | 319 |
// add our test handlers |
320 |
assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx)); |
|
43 | 321 |
|
322 |
// test on_TEST handler |
|
323 |
log_info("test irc_conn.handlers"); |
|
324 |
sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n"); |
|
325 |
assert(ctx.on_TEST); |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
326 |
|
44 | 327 |
// test PING/PONG |
328 |
log_info("test PING/PONG"); |
|
329 |
sock_test_add_recv_str(sock, "PING foo\r\n"); |
|
330 |
assert_sock_data(sock, "PONG foo\r\n"); |
|
331 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
332 |
// destroy it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
333 |
irc_conn_destroy(conn); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
334 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
335 |
|
44 | 336 |
struct _test_net_ctx { |
337 |
struct irc_chan *chan; |
|
338 |
||
339 |
bool on_chan_self_join; |
|
340 |
}; |
|
341 |
||
342 |
void _on_chan_self_join (struct irc_chan *chan, void *arg) |
|
343 |
{ |
|
344 |
struct _test_net_ctx *ctx = arg; |
|
345 |
||
346 |
assert(chan == ctx->chan); |
|
347 |
||
348 |
ctx->on_chan_self_join = true; |
|
349 |
||
350 |
log_debug("on_self_join"); |
|
351 |
} |
|
352 |
||
353 |
struct irc_chan_callbacks _chan_callbacks = { |
|
354 |
.on_self_join = &_on_chan_self_join, |
|
355 |
}; |
|
356 |
||
357 |
void test_irc_net (void) |
|
358 |
{ |
|
359 |
struct sock_test *sock; |
|
360 |
struct irc_net *net; |
|
361 |
struct irc_net_info net_info = { |
|
362 |
.register_info = { |
|
363 |
"nick", "user", "realname" |
|
364 |
}, |
|
365 |
}; |
|
366 |
struct irc_chan *chan; |
|
367 |
struct irc_chan_info chan_info = { |
|
368 |
.channel = "#test", |
|
369 |
}; |
|
370 |
struct error_info err; |
|
371 |
struct _test_net_ctx ctx = { NULL, false }; |
|
372 |
||
373 |
// create the test socket |
|
374 |
assert((sock = sock_test_create())); |
|
375 |
||
376 |
// create the irc_net |
|
377 |
net_info.raw_sock = SOCK_TEST_BASE(sock); |
|
378 |
assert_success(irc_net_create(&net, &net_info, &err)); |
|
379 |
||
380 |
// add a channel |
|
381 |
assert((chan = irc_net_add_chan(net, &chan_info))); |
|
382 |
assert(!chan->state.joining && !chan->state.joined); |
|
383 |
assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, &ctx)); |
|
384 |
ctx.chan = chan; |
|
385 |
||
386 |
// test register output |
|
387 |
assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); |
|
388 |
||
389 |
// registration reply |
|
390 |
sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n"); |
|
391 |
assert(net->conn->registered); |
|
392 |
assert_strcmp(net->conn->nickname, "mynick"); |
|
393 |
||
394 |
// JOIN request |
|
395 |
assert(chan->state.joining); |
|
396 |
assert_sock_data(sock, "JOIN #test\r\n"); |
|
397 |
||
398 |
// JOIN reply |
|
399 |
sock_test_add_recv_str(sock, ":mynick!user@host JOIN #test\r\n"); |
|
400 |
assert(!chan->state.joining && chan->state.joined); |
|
401 |
assert(ctx.on_chan_self_join); |
|
402 |
} |
|
403 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
404 |
/** |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
405 |
* Test definition |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
406 |
*/ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
407 |
static struct test { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
408 |
/** Test name */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
409 |
const char *name; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
410 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
411 |
/** Test func */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
412 |
void (*func) (void); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
413 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
414 |
} _tests[] = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
415 |
{ "sock_test", &test_sock_test }, |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
416 |
{ "line_proto", &test_line_proto }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
417 |
{ "irc_conn", &test_irc_conn }, |
44 | 418 |
{ "irc_net", &test_irc_net }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
419 |
{ NULL, NULL } |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
420 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
421 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
422 |
int main (int argc, char **argv) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
423 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
424 |
struct test *test; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
425 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
426 |
(void) argv; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
427 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
428 |
// no arguments |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
429 |
assert(argc == 1); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
430 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
431 |
// run tests |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
432 |
for (test = _tests; test->name; test++) { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
433 |
log_info("Running test: %s", test->name); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
434 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
435 |
test->func(); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
436 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
437 |
} |