author | Tero Marttila <terom@fixme.fi> |
Wed, 27 May 2009 23:57:48 +0300 | |
branch | new-lib-errors |
changeset 217 | 7728d6ec3abf |
parent 196 | 873796250c60 |
permissions | -rw-r--r-- |
168 | 1 |
#include "transport.h" |
2 |
#include "test.h" |
|
3 |
||
4 |
void assert_transport_read (transport_t *transport, const char *str) |
|
5 |
{ |
|
6 |
size_t len = strlen(str); |
|
7 |
char buf[len]; |
|
8 |
error_t err; |
|
9 |
||
10 |
log_debug("read: %p: %s", transport, dump_str(str)); |
|
11 |
||
12 |
// read it |
|
13 |
assert(transport_read(transport, buf, len, &err) == (int) len); |
|
14 |
||
15 |
// cmp |
|
16 |
assert_strncmp(buf, str, len); |
|
17 |
} |
|
18 |
||
19 |
void assert_transport_write (transport_t *transport, const char *str) |
|
20 |
{ |
|
21 |
size_t len = strlen(str); |
|
22 |
error_t err; |
|
23 |
||
24 |
log_debug("write: %p: %s", transport, dump_str(str)); |
|
25 |
||
26 |
// write it |
|
27 |
assert(transport_write(transport, str, len, &err) == (int) len); |
|
28 |
} |
|
29 |
||
30 |
void assert_transport_eof (transport_t *transport) |
|
31 |
{ |
|
32 |
char buf; |
|
33 |
error_t err; |
|
34 |
||
35 |
log_debug("eof: %p", transport); |
|
36 |
||
37 |
assert_err(-transport_read(transport, &buf, 1, &err), ERR_EOF); |
|
38 |
} |
|
39 |
||
196
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
40 |
void assert_transport_data_buf (struct transport_test *tp, const char *data, size_t len) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
41 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
42 |
// get the data out |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
43 |
char *out; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
44 |
size_t out_len; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
45 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
46 |
transport_test_pull_buf(tp, &out, &out_len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
47 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
48 |
log_debug("pull_buf: %s", dump_strn(out, out_len)); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
49 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
50 |
// should be the same |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
51 |
assert_memcmp(out, data, out_len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
52 |
assert(out_len == len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
53 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
54 |
// cleanup |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
55 |
free(out); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
56 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
57 |
|
168 | 58 |
void assert_transport_data (struct transport_test *tp, const char *fmt, ...) |
59 |
{ |
|
60 |
char buf[TRANSPORT_TEST_FMT_MAX]; |
|
61 |
va_list vargs; |
|
62 |
size_t len; |
|
63 |
||
64 |
va_start(vargs, fmt); |
|
65 |
||
66 |
if ((len = vsnprintf(buf, sizeof(buf), fmt, vargs)) >= sizeof(buf)) |
|
67 |
FATAL("input too long: %zu bytes", len); |
|
68 |
||
69 |
va_end(vargs); |
|
70 |
||
196
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
171
diff
changeset
|
71 |
assert_transport_data_buf(tp, buf, len); |
168 | 72 |
} |
73 |
||
74 |
struct transport_test* setup_transport_test (void) |
|
75 |
{ |
|
76 |
struct transport_test *tp; |
|
77 |
||
78 |
assert ((tp = transport_test_create(NULL)) != NULL); |
|
79 |
||
80 |
return tp; |
|
81 |
} |
|
82 |
||
83 |
void test_transport_test (void) |
|
84 |
{ |
|
85 |
struct transport_info info = { NULL, NULL, 0 }; |
|
86 |
struct transport_test *tp = transport_test_create(&info); |
|
87 |
transport_t *transport = transport_test_cast(tp); |
|
88 |
||
89 |
// put the read data |
|
90 |
log_info("test transport_test_push_*"); |
|
91 |
transport_test_push_buf(tp, "foo", 3); |
|
92 |
transport_test_push_str(tp, "barx"); |
|
93 |
transport_test_push_fmt(tp, "xx %s xx", "quux"); |
|
94 |
transport_test_push_eof(tp); |
|
95 |
||
96 |
// read it out |
|
97 |
log_info("test transport_test_read"); |
|
98 |
||
99 |
assert_transport_read(transport, "foo"); |
|
100 |
assert_transport_read(transport, "ba"); |
|
101 |
assert_transport_read(transport, "rx"); |
|
102 |
assert_transport_read(transport, "xx quux xx"); |
|
103 |
assert_transport_eof(transport); |
|
104 |
||
105 |
// write some data in |
|
106 |
log_info("test transport_test_write"); |
|
107 |
||
108 |
assert_transport_write(transport, "test "); |
|
109 |
assert_transport_write(transport, "data"); |
|
110 |
||
111 |
// check output |
|
112 |
log_info("test transport_test_pull_*"); |
|
113 |
||
114 |
assert_transport_data(tp, "test data"); |
|
115 |
assert_transport_data(tp, ""); |
|
116 |
||
117 |
// cleanup |
|
171
b54f393c3df0
evil chain.h macro magic, fix irc_conn_set_nickname bug, misc. test bugs (mem leaks, missing #includes)
Tero Marttila <terom@fixme.fi>
parents:
168
diff
changeset
|
118 |
transport_destroy(transport); |
168 | 119 |
} |
120 |