author | Tero Marttila <terom@fixme.fi> |
Thu, 12 Mar 2009 21:12:48 +0200 | |
changeset 41 | 40f7aa051acb |
parent 40 | 51678c7eae03 |
child 42 | 13cfc41f76a7 |
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" |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
#include "log.h" |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
8 |
#include "error.h" |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
10 |
#include <stdlib.h> |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
#include <string.h> |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
#include <assert.h> |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
14 |
void assert_strcmp (const char *a, const char *b) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
15 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
16 |
if (strcmp(a, b)) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
17 |
FATAL("'%s' != '%s'", a, b); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
18 |
} |
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 |
void assert_strncmp (const char *a, const char *b, size_t n) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
21 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
22 |
if (strncmp(a, b, n)) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
23 |
FATAL("'%s':%d != '%s'", a, n, b); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
24 |
} |
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 |
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
|
27 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
28 |
if (strlen(str) != n) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
29 |
FATAL("strlen('%s') != %u", str, n); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
30 |
} |
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 |
void assert_strnul (const char *str) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
33 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
34 |
if (str != NULL) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
35 |
FATAL("'%s' != NULL", str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
36 |
} |
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 |
void assert_success (err_t err) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
39 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
40 |
if (err != SUCCESS) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
41 |
FATAL("error: %s", error_name(err)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
42 |
|
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 |
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
|
46 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
47 |
if (err != target) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
48 |
FATAL("error: <%s> != target <%s>", error_name(err), error_name(target)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
49 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
50 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
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
|
52 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
char buf[strlen(str)]; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
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
|
56 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
// read it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
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
|
59 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
// cmp |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
61 |
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
|
62 |
} |
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 |
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
|
65 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
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
|
67 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
// write it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
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
|
70 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
72 |
void assert_sock_eof (struct sock_stream *sock) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
73 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
74 |
char buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
75 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
76 |
log_debug("eof: %p", sock); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
77 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
78 |
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
|
79 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
80 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
void test_sock_test (void) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
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
|
84 |
struct io_vec _read_data[] = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
{ "foo", 3 }, |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
{ "barx", 4 } |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
const char *_write_data = "test data"; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
// put the read data |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
94 |
// read it out |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
95 |
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
|
96 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
// write the data in |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
103 |
log_info("test sock_test_write"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
104 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
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
|
106 |
assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
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 |
// get the data out |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
char *data; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
size_t len; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
112 |
log_info("test get_send_data"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
113 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
sock_test_get_send_data(sock, &data, &len); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
116 |
// should be the same |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
117 |
assert_strlen(_write_data, len); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
118 |
assert_strncmp(data, _write_data, len); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
120 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
121 |
free(data); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
122 |
sock_test_destroy(sock); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
123 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
124 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
125 |
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
|
126 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
127 |
char *line_buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
128 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
129 |
log_debug("expect: '%s'", line_str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
130 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
131 |
assert_success(line_proto_recv(lp, &line_buf)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
132 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
133 |
if (line_str) { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
134 |
assert(line_buf != NULL); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
135 |
assert_strcmp(line_buf, line_str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
136 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
137 |
} else { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
138 |
assert_strnul(line_buf); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
139 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
140 |
} |
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 |
static struct line_proto_callbacks _lp_callbacks = { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
144 |
.on_line = NULL, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
145 |
.on_error = NULL, |
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 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
148 |
void test_line_proto (void) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
149 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
150 |
struct sock_test *sock = sock_test_create(); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
151 |
struct io_vec _read_data[] = { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
152 |
{ "hello\r\n", 7 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
153 |
{ "world\n", 6 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
154 |
{ "this ", 5 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
155 |
{ "is a line\r", 10 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
156 |
{ "\nfragment", 9 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
157 |
}, _trailing_data = { "\r\n", 2 }; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
158 |
struct line_proto *lp; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
159 |
struct error_info err; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
160 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
161 |
// put the read data |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
162 |
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
|
163 |
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
|
164 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
165 |
// create the lp |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
166 |
assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
167 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
168 |
log_info("test line_proto_recv"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
169 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
170 |
// then read some lines from it |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
171 |
assert_read_line(lp, "hello"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
172 |
assert_read_line(lp, "world"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
173 |
assert_read_line(lp, "this is a line"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
174 |
assert_read_line(lp, NULL); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
175 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
176 |
// then add a final bit |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
177 |
sock_test_add_recv_vec(sock, _trailing_data); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
178 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
179 |
// read the final bit |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
180 |
assert_read_line(lp, "fragment"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
181 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
182 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
183 |
line_proto_release(lp); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
186 |
static struct irc_conn_callbacks _conn_callbacks = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
.on_registered = NULL, |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
188 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
189 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
190 |
void test_irc_conn (void) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
struct sock_test *sock; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
193 |
struct irc_conn *conn; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
194 |
struct error_info err; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
195 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
196 |
// create the test socket |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
197 |
assert((sock = sock_test_create())); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
198 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
199 |
// create the irc_conn |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
200 |
assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err)); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
201 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
202 |
// destroy it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
203 |
irc_conn_destroy(conn); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
204 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
205 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
206 |
/** |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
207 |
* Test definition |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
208 |
*/ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
209 |
static struct test { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
/** Test name */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
const char *name; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
/** Test func */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
void (*func) (void); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
} _tests[] = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
{ "sock_test", &test_sock_test }, |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
218 |
{ "line_proto", &test_line_proto }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
219 |
{ "irc_conn", &test_irc_conn }, |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
{ NULL, NULL } |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
222 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
223 |
int main (int argc, char **argv) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
224 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
225 |
struct test *test; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
226 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
227 |
(void) argv; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
228 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
229 |
// no arguments |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
230 |
assert(argc == 1); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
231 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
232 |
// run tests |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
233 |
for (test = _tests; test->name; test++) { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
234 |
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
|
235 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
236 |
test->func(); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
237 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
238 |
} |