author | Tero Marttila <terom@fixme.fi> |
Fri, 13 Mar 2009 15:22:46 +0200 | |
changeset 52 | 97604efda1ce |
parent 51 | cc61eaa841ef |
child 72 | 43084f103c2a |
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> |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
14 |
#include <ctype.h> |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
15 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
16 |
#define DUMP_STR_BUF 1024 |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
17 |
#define DUMP_STR_COUNT 8 |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
18 |
#define DUMP_STR_TAIL 10 |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
19 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
20 |
char *dump_str_append (char *buf, const char *str) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
21 |
{ |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
22 |
while (*str) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
23 |
*buf++ = *str++; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
24 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
25 |
return buf; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
26 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
27 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
28 |
/** |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
29 |
* This re-formats the given string to escape values, and returns a pointer to an internal static buffer. |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
30 |
* |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
31 |
* If len is given as >= 0, only the given number of chars will be dumped from str. |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
32 |
* |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
33 |
* The buffer cycles a bit, so the returned pointers remain valid across DUMP_STR_COUNT calls. |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
34 |
* |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
35 |
* The resulting string is truncated to (DUMP_STR_BUF - DUMP_STR_TAIL) bytes, not including the ending "...'\0". |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
36 |
* |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
37 |
* @param str the string to dump, should be NUL-terminated unless len is given |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
38 |
* @param len if negative, ignored, otherwise, only this many bytes are dumped from str |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
39 |
* @param return a pointer to a static buffer that remains valid across DUMP_STR_COUNT calls to this function |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
40 |
*/ |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
41 |
const char *dump_strn (const char *str, ssize_t len) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
42 |
{ |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
43 |
static char dump_buf[DUMP_STR_COUNT][DUMP_STR_BUF]; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
44 |
static size_t dump_idx = 0; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
45 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
46 |
// pick a buffer to use |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
47 |
const char *str_ptr = str; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
48 |
char *buf_ptr = dump_buf[dump_idx++], *buf = buf_ptr; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
49 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
50 |
// cycle |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
51 |
if (dump_idx >= DUMP_STR_COUNT) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
52 |
dump_idx = 0; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
53 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
54 |
// NULL? |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
55 |
if (str == NULL) { |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
56 |
buf = dump_str_append(buf, "NULL"); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
57 |
*buf = '\0'; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
58 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
59 |
return buf_ptr; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
60 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
61 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
62 |
// quote |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
63 |
*buf++ = '\''; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
64 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
65 |
// dump each char |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
66 |
for (; ( |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
67 |
// ...stop on NUL |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
68 |
*str |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
69 |
|
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
70 |
// ...leave DUMP_STR_TAIL bytes room at the end of buf |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
71 |
&& (size_t) (buf - buf_ptr) < DUMP_STR_BUF - DUMP_STR_TAIL |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
72 |
|
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
73 |
// ...don't consume more than len bytes of str, unless len < 0 |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
74 |
&& (len < 0 || (size_t) (str - str_ptr) < (size_t) len) |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
75 |
); str++ |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
76 |
) { |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
77 |
if (*str == '\'') { |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
78 |
// escape quotes |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
79 |
buf = dump_str_append(buf, "\\'"); |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
80 |
|
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
81 |
} else if (*str == '\\') { |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
82 |
// escape escapes |
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
83 |
buf = dump_str_append(buf, "\\\\"); |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
84 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
85 |
} else if (isprint(*str)) { |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
86 |
// normal char |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
87 |
*buf++ = *str; |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
88 |
|
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
89 |
} else { |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
90 |
// something more special |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
91 |
switch (*str) { |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
92 |
case '\r': |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
93 |
buf = dump_str_append(buf, "\\r"); break; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
94 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
95 |
case '\n': |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
96 |
buf = dump_str_append(buf, "\\n"); break; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
97 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
98 |
default: |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
99 |
// format as "\xFF" |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
100 |
buf += snprintf(buf, (DUMP_STR_BUF - (buf - buf_ptr)), "\\x%02x", *str); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
101 |
break; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
102 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
103 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
104 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
105 |
|
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
106 |
// end quote |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
107 |
*buf++ = '\''; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
108 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
109 |
// overflow? |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
110 |
if ((size_t)(buf - buf_ptr) == DUMP_STR_BUF - DUMP_STR_TAIL) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
111 |
buf = dump_str_append(buf, "..."); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
112 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
113 |
// terminate |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
114 |
*buf = '\0'; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
115 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
116 |
// ok |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
117 |
return buf_ptr; |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
118 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
119 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
120 |
const char *dump_str (const char *str) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
121 |
{ |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
122 |
return dump_strn(str, -1); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
123 |
} |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
|
42 | 125 |
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
|
126 |
{ |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
127 |
if (!is || strcmp(is, should_be)) |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
128 |
FATAL("%s != %s", dump_str(is), dump_str(should_be)); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
129 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
130 |
|
42 | 131 |
void assert_strncmp (const char *is, const char *should_be, size_t n) |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
132 |
{ |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
133 |
if (!is || strncmp(is, should_be, n)) |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
134 |
FATAL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n)); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
135 |
} |
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 |
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
|
138 |
{ |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
139 |
if (!str || strlen(str) != n) |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
140 |
FATAL("strlen(%s) != %u", dump_str(str), (unsigned) n); |
41
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 |
|
42 | 143 |
void assert_strnull (const char *str) |
41
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 |
if (str != NULL) |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
146 |
FATAL("%s != NULL", dump_str(str)); |
41
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 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
149 |
void assert_success (err_t err) |
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 (err != SUCCESS) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
152 |
FATAL("error: %s", error_name(err)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
153 |
} |
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 |
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
|
156 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
157 |
if (err != target) |
42 | 158 |
FATAL("error: <%s> != <%s>", error_name(err), error_name(target)); |
159 |
} |
|
160 |
||
161 |
void assert_error_info (struct error_info *is, struct error_info *should_be) |
|
162 |
{ |
|
163 |
if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be)) |
|
164 |
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
|
165 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
166 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
167 |
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
|
168 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
169 |
char buf[strlen(str)]; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
170 |
|
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
171 |
log_debug("read: %p: %s", sock, dump_str(str)); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
172 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
173 |
// read it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
174 |
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
|
175 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
176 |
// cmp |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
177 |
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
|
178 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
179 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
180 |
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
|
181 |
{ |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
182 |
log_debug("write: %p: %s", sock, dump_str(str)); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
183 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
// write it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
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
|
186 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
188 |
void assert_sock_eof (struct sock_stream *sock) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
189 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
190 |
char buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
191 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
192 |
log_debug("eof: %p", sock); |
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 |
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
|
195 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
196 |
|
44 | 197 |
void assert_sock_data (struct sock_test *sock, const char *data) |
43 | 198 |
{ |
199 |
// get the data out |
|
200 |
char *buf; |
|
201 |
size_t len; |
|
202 |
||
203 |
sock_test_get_send_data(sock, &buf, &len); |
|
204 |
||
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
205 |
log_debug("get_send_data: %s", dump_strn(buf, len)); |
43 | 206 |
|
207 |
// should be the same |
|
208 |
assert_strncmp(buf, data, len); |
|
209 |
assert_strlen(data, len); |
|
210 |
||
211 |
// cleanup |
|
212 |
free(buf); |
|
213 |
} |
|
214 |
||
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
215 |
/** |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
216 |
* Nicer name for test_sock_add_recv_str |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
217 |
*/ |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
218 |
void test_sock_push (struct sock_test *sock, const char *str) |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
219 |
{ |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
220 |
return sock_test_add_recv_str(sock, str); |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
221 |
} |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
222 |
|
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
223 |
void test_dump_str (void) |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
224 |
{ |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
225 |
log_info("dumping example strings on stdout:"); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
226 |
|
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
227 |
log_debug("normal: %s", dump_str("Hello World")); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
228 |
log_debug("escapes: %s", dump_str("foo\r\nbar\a\001")); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
229 |
log_debug("length: %s", dump_strn("<-->**", 4)); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
230 |
log_debug("overflow: %s", dump_str( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
231 |
log_debug("null: %s", dump_str(NULL)); |
51
cc61eaa841ef
add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
232 |
log_debug("quote: %s", dump_str("foo\\bar'quux")); |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
233 |
} |
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
234 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
235 |
void test_sock_test (void) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
236 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
237 |
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
|
238 |
struct io_vec _read_data[] = { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
239 |
{ "foo", 3 }, |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
240 |
{ "barx", 4 } |
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 |
const char *_write_data = "test data"; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
243 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
244 |
// put the read data |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
245 |
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
|
246 |
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
|
247 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
248 |
// read it out |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
249 |
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
|
250 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
256 |
// write the data in |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
257 |
log_info("test sock_test_write"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
258 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
259 |
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
|
260 |
assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
43 | 261 |
|
262 |
// check output |
|
44 | 263 |
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
|
264 |
|
43 | 265 |
// check output |
44 | 266 |
assert_sock_data(sock, ""); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
267 |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
268 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
269 |
sock_test_destroy(sock); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
270 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
271 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
272 |
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
|
273 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
274 |
char *line_buf; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
275 |
|
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
276 |
log_debug("expect: %s", dump_str(line_str)); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
277 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
278 |
assert_success(line_proto_recv(lp, &line_buf)); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
279 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
280 |
if (line_str) { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
281 |
assert_strcmp(line_buf, line_str); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
282 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
283 |
} else { |
42 | 284 |
assert_strnull(line_buf); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
285 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
286 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
287 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
288 |
|
42 | 289 |
/** |
290 |
* Context info for test_line_proto callbacks |
|
291 |
*/ |
|
292 |
struct _lp_test_ctx { |
|
293 |
/** Expected line */ |
|
294 |
const char *line; |
|
295 |
||
296 |
/** Expected error */ |
|
297 |
struct error_info err; |
|
298 |
}; |
|
299 |
||
300 |
static void _lp_on_line (char *line, void *arg) |
|
301 |
{ |
|
302 |
struct _lp_test_ctx *ctx = arg; |
|
303 |
||
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
304 |
log_debug("%s", dump_str(line)); |
42 | 305 |
|
306 |
assert_strcmp(line, ctx->line); |
|
307 |
||
308 |
ctx->line = NULL; |
|
309 |
} |
|
310 |
||
311 |
static void _lp_on_error (struct error_info *err, void *arg) |
|
312 |
{ |
|
313 |
struct _lp_test_ctx *ctx = arg; |
|
314 |
||
315 |
assert_error_info(err, &ctx->err); |
|
316 |
} |
|
317 |
||
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
318 |
static struct line_proto_callbacks _lp_callbacks = { |
42 | 319 |
.on_line = &_lp_on_line, |
320 |
.on_error = &_lp_on_error, |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
321 |
}; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
322 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
323 |
void test_line_proto (void) |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
324 |
{ |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
325 |
struct sock_test *sock = sock_test_create(); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
326 |
struct io_vec _read_data[] = { |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
327 |
{ "hello\r\n", 7 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
328 |
{ "world\n", 6 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
329 |
{ "this ", 5 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
330 |
{ "is a line\r", 10 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
331 |
{ "\nfragment", 9 }, |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
332 |
}, _trailing_data = { "\r\n", 2 }; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
333 |
struct line_proto *lp; |
42 | 334 |
struct _lp_test_ctx ctx; |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
335 |
struct error_info err; |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
336 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
337 |
// put the read data |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
338 |
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
|
339 |
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
|
340 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
341 |
// create the lp |
42 | 342 |
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
|
343 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
344 |
log_info("test line_proto_recv"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
345 |
|
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
346 |
// then read some lines from it |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
347 |
assert_read_line(lp, "hello"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
348 |
assert_read_line(lp, "world"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
349 |
assert_read_line(lp, "this is a line"); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
350 |
assert_read_line(lp, NULL); |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
351 |
|
42 | 352 |
// then add a final bit to trigger on_line |
353 |
log_info("test on_line"); |
|
354 |
||
355 |
ctx.line = "fragment"; |
|
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
356 |
sock_test_add_recv_vec(sock, _trailing_data); |
42 | 357 |
assert_strnull(ctx.line); |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
358 |
|
43 | 359 |
// test writing |
360 |
log_info("test line_proto_send"); |
|
361 |
assert_success(-line_proto_send(lp, "foobar\r\n")); |
|
362 |
assert_success(-line_proto_send(lp, "quux\r\n")); |
|
44 | 363 |
assert_sock_data(sock, "foobar\r\nquux\r\n"); |
43 | 364 |
|
365 |
// XXX: test partial writes |
|
366 |
||
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
367 |
// cleanup |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
368 |
line_proto_release(lp); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
369 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
370 |
|
43 | 371 |
struct _test_irc_conn_ctx { |
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
372 |
bool on_registered, on_TEST, on_error, on_quit; |
43 | 373 |
}; |
374 |
||
375 |
static void _conn_on_registered (struct irc_conn *conn, void *arg) |
|
376 |
{ |
|
377 |
struct _test_irc_conn_ctx *ctx = arg; |
|
378 |
||
379 |
(void) conn; |
|
380 |
||
44 | 381 |
if (ctx) ctx->on_registered = true; |
43 | 382 |
|
383 |
log_debug("registered"); |
|
384 |
} |
|
385 |
||
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
386 |
static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg) |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
387 |
{ |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
388 |
struct _test_irc_conn_ctx *ctx = arg; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
389 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
390 |
(void) conn; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
391 |
(void) err; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
392 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
393 |
if (ctx) ctx->on_error = true; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
394 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
395 |
log_debug("on_error"); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
396 |
} |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
397 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
398 |
static void _conn_on_quit (struct irc_conn *conn, void *arg) |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
399 |
{ |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
400 |
struct _test_irc_conn_ctx *ctx = arg; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
401 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
402 |
(void) conn; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
403 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
404 |
if (ctx) ctx->on_quit = true; |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
405 |
|
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
406 |
log_debug("on_quit"); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
407 |
} |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
408 |
|
43 | 409 |
static void _conn_on_TEST (const struct irc_line *line, void *arg) |
410 |
{ |
|
411 |
struct _test_irc_conn_ctx *ctx = arg; |
|
412 |
||
413 |
assert_strcmp(line->prefix, "foobar-prefix"); |
|
414 |
assert_strcmp(line->command, "TEST"); |
|
415 |
assert_strcmp(line->args[0], "arg0"); |
|
416 |
assert_strnull(line->args[1]); |
|
417 |
||
44 | 418 |
if (ctx) ctx->on_TEST = true; |
43 | 419 |
|
420 |
log_debug("on_TEST"); |
|
421 |
} |
|
422 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
423 |
static struct irc_conn_callbacks _conn_callbacks = { |
43 | 424 |
.on_registered = &_conn_on_registered, |
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
425 |
.on_error = &_conn_on_error, |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
426 |
.on_quit = &_conn_on_quit, |
43 | 427 |
}; |
428 |
||
429 |
static struct irc_cmd_handler _conn_handlers[] = { |
|
430 |
{ "TEST", &_conn_on_TEST }, |
|
431 |
{ NULL, NULL } |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
432 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
433 |
|
44 | 434 |
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
|
435 |
{ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
436 |
struct irc_conn *conn; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
437 |
struct error_info err; |
43 | 438 |
struct irc_conn_register_info register_info = { |
439 |
"nick", "user", "realname" |
|
440 |
}; |
|
44 | 441 |
|
442 |
// create the irc_conn |
|
443 |
assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err)); |
|
444 |
||
445 |
// test register |
|
446 |
if (noisy) log_info("test irc_conn_register"); |
|
447 |
assert_success(irc_conn_register(conn, ®ister_info)); |
|
448 |
assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); |
|
449 |
||
450 |
// test on_register callback |
|
451 |
if (noisy) log_info("test irc_conn_callbacks.on_register"); |
|
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
452 |
test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n"); |
44 | 453 |
if (ctx) assert(ctx->on_registered); |
454 |
assert_strcmp(conn->nickname, "mynick"); |
|
455 |
||
456 |
// ok |
|
457 |
return conn; |
|
458 |
} |
|
459 |
||
460 |
void test_irc_conn (void) |
|
461 |
{ |
|
462 |
struct sock_test *sock; |
|
463 |
struct irc_conn *conn; |
|
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
464 |
struct _test_irc_conn_ctx ctx = { false, false, false, false }; |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
465 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
466 |
// create the test socket |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
467 |
assert((sock = sock_test_create())); |
43 | 468 |
|
44 | 469 |
// setup the basic irc_conn |
470 |
conn = setup_irc_conn(sock, true, &ctx); |
|
43 | 471 |
|
44 | 472 |
// add our test handlers |
473 |
assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx)); |
|
43 | 474 |
|
475 |
// test on_TEST handler |
|
476 |
log_info("test irc_conn.handlers"); |
|
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
477 |
test_sock_push(sock, ":foobar-prefix TEST arg0\r\n"); |
43 | 478 |
assert(ctx.on_TEST); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
479 |
|
44 | 480 |
// test PING/PONG |
481 |
log_info("test PING/PONG"); |
|
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
482 |
test_sock_push(sock, "PING foo\r\n"); |
44 | 483 |
assert_sock_data(sock, "PONG foo\r\n"); |
484 |
||
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
485 |
// quit nicely |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
486 |
log_info("test QUIT"); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
487 |
assert_success(irc_conn_QUIT(conn, "bye now")); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
488 |
assert_sock_data(sock, "QUIT :bye now\r\n"); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
489 |
assert(conn->quitting); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
490 |
|
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
491 |
test_sock_push(sock, "ERROR :Closing Link: Quit\r\n"); |
49
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
492 |
sock_test_set_recv_eof(sock); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
493 |
assert(conn->quit && !conn->quitting && !conn->registered); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
494 |
assert(ctx.on_quit); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
495 |
assert(!ctx.on_error); |
96e0f703a58c
update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents:
47
diff
changeset
|
496 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
497 |
// destroy it |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
498 |
irc_conn_destroy(conn); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
499 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
500 |
|
44 | 501 |
struct _test_net_ctx { |
502 |
struct irc_chan *chan; |
|
503 |
||
504 |
bool on_chan_self_join; |
|
505 |
}; |
|
506 |
||
507 |
void _on_chan_self_join (struct irc_chan *chan, void *arg) |
|
508 |
{ |
|
509 |
struct _test_net_ctx *ctx = arg; |
|
510 |
||
511 |
assert(chan == ctx->chan); |
|
512 |
||
513 |
ctx->on_chan_self_join = true; |
|
514 |
||
515 |
log_debug("on_self_join"); |
|
516 |
} |
|
517 |
||
518 |
struct irc_chan_callbacks _chan_callbacks = { |
|
519 |
.on_self_join = &_on_chan_self_join, |
|
520 |
}; |
|
521 |
||
522 |
void test_irc_net (void) |
|
523 |
{ |
|
524 |
struct sock_test *sock; |
|
525 |
struct irc_net *net; |
|
526 |
struct irc_net_info net_info = { |
|
527 |
.register_info = { |
|
528 |
"nick", "user", "realname" |
|
529 |
}, |
|
530 |
}; |
|
531 |
struct irc_chan *chan; |
|
532 |
struct irc_chan_info chan_info = { |
|
533 |
.channel = "#test", |
|
534 |
}; |
|
535 |
struct error_info err; |
|
536 |
struct _test_net_ctx ctx = { NULL, false }; |
|
537 |
||
538 |
// create the test socket |
|
539 |
assert((sock = sock_test_create())); |
|
540 |
||
541 |
// create the irc_net |
|
542 |
net_info.raw_sock = SOCK_TEST_BASE(sock); |
|
543 |
assert_success(irc_net_create(&net, &net_info, &err)); |
|
544 |
||
545 |
// add a channel |
|
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
546 |
log_info("test offline irc_net_add_chan"); |
44 | 547 |
assert((chan = irc_net_add_chan(net, &chan_info))); |
45
71e65564afd2
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
548 |
assert(!chan->joining && !chan->joined); |
44 | 549 |
assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, &ctx)); |
550 |
ctx.chan = chan; |
|
551 |
||
552 |
// test register output |
|
553 |
assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); |
|
554 |
||
555 |
// registration reply |
|
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
556 |
log_info("test irc_conn_on_RPL_WELCOME"); |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
557 |
test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n"); |
44 | 558 |
assert(net->conn->registered); |
559 |
assert_strcmp(net->conn->nickname, "mynick"); |
|
560 |
||
561 |
// JOIN request |
|
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
562 |
log_info("test irc_net_conn_registered -> irc_chan_join"); |
45
71e65564afd2
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
563 |
assert(chan->joining); |
44 | 564 |
assert_sock_data(sock, "JOIN #test\r\n"); |
565 |
||
566 |
// JOIN reply |
|
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
567 |
log_info("test irc_chan_on_JOIN"); |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
568 |
test_sock_push(sock, ":mynick!user@host JOIN #test\r\n"); |
45
71e65564afd2
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
569 |
assert(!chan->joining && chan->joined); |
44 | 570 |
assert(ctx.on_chan_self_join); |
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
571 |
|
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
572 |
// test errors by setting EOF |
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
573 |
log_info("test irc_net_error"); |
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
574 |
sock_test_set_recv_eof(sock); |
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
575 |
assert(net->conn == NULL); |
47
7d4094eb3117
add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
576 |
|
7d4094eb3117
add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
577 |
// cleanup |
7d4094eb3117
add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
578 |
irc_net_destroy(net); |
44 | 579 |
} |
580 |
||
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
581 |
/** |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
582 |
* Test definition |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
583 |
*/ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
584 |
static struct test { |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
585 |
/** Test name */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
586 |
const char *name; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
587 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
588 |
/** Test func */ |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
589 |
void (*func) (void); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
590 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
591 |
} _tests[] = { |
50
46c3983638d3
add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents:
49
diff
changeset
|
592 |
{ "dump_str", &test_dump_str }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
593 |
{ "sock_test", &test_sock_test }, |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
594 |
{ "line_proto", &test_line_proto }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
595 |
{ "irc_conn", &test_irc_conn }, |
44 | 596 |
{ "irc_net", &test_irc_net }, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
597 |
{ NULL, NULL } |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
598 |
}; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
599 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
600 |
int main (int argc, char **argv) |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
601 |
{ |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
602 |
const char *filter; |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
603 |
struct test *test; |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
604 |
|
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
605 |
if (argc == 1) { |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
606 |
// no arguments |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
607 |
filter = NULL; |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
608 |
|
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
609 |
} else if (argc == 2) { |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
610 |
// filter |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
611 |
filter = argv[1]; |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
612 |
|
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
613 |
log_info("only running tests: %s", filter); |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
614 |
|
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
615 |
} else { |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
616 |
FATAL("too many arguments"); |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
617 |
} |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
618 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
619 |
// run tests |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
620 |
for (test = _tests; test->name; test++) { |
52
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
621 |
if (filter && strcmp(test->name, filter)) |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
622 |
continue; |
97604efda1ce
add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents:
51
diff
changeset
|
623 |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
624 |
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
|
625 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
626 |
test->func(); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
627 |
} |
46
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
628 |
|
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
45
diff
changeset
|
629 |
log_info("done"); |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
630 |
} |