-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit-tests.c
198 lines (147 loc) · 3.57 KB
/
unit-tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef CONFIG_H
#include "config.h"
#endif
#include <alloca.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "base64.h"
#include "vink-internal.h"
#include "vink.h"
static int ok = 1;
#define EXPECT(a) \
do \
{ \
if(!(a)) \
{ \
fprintf(stderr, "%s:%d: %s failed\n", __PRETTY_FUNCTION__, __LINE__, #a); \
ok = 0; \
} \
} \
while(0)
static int
buffer_write(const void* data, size_t size, void* arg)
{
struct VINK_buffer *buf = arg;
ARRAY_ADD_SEVERAL(buf, data, size);
return ARRAY_RESULT(buf);
}
int
myrand ()
{
static unsigned long next = 1;
next = next * 1103515245 + 12345;
return ((unsigned) (next / 65536) % 32768);
}
static void
t0x0000_base64_decode()
{
char input_buf[257];
char decoded_buf[257];
char *coded_buf;
size_t i, len, decoded_len, iteration;
for (iteration = 0; iteration <= 256; ++iteration)
{
len = iteration;
for (i = 0; i < len; ++i)
input_buf[i] = myrand ();
coded_buf = base64_encode (input_buf, len);
if (!coded_buf)
continue;
decoded_len = base64_decode (decoded_buf, coded_buf, strlen (coded_buf));
EXPECT (decoded_len == len);
EXPECT (!memcmp (input_buf, decoded_buf, len));
decoded_len = base64_decode (decoded_buf, coded_buf, 0);
EXPECT (decoded_len == len);
EXPECT (!memcmp (input_buf, decoded_buf, len));
free (coded_buf);
}
}
static void
t0x0001_base64_decode()
{
EXPECT (-1 == base64_decode (0, "%", 0));
}
static void
t0x0002_base64_decode()
{
char buf[4];
EXPECT (4 == base64_decode (buf, " a G V z d A = = ", 0));
EXPECT (!memcmp (buf, "hest", 4));
}
static void
t0x0000_xmpp_parse_jid()
{
char* input;
struct vink_xmpp_jid result;
int ret;
input = strdupa("example.org");
ret = vink_xmpp_parse_jid(&result, input);
EXPECT(ret == 0);
EXPECT(result.node == 0);
EXPECT(!strcmp(result.domain, "example.org"));
EXPECT(result.resource == 0);
}
static void
t0x0001_xmpp_parse_jid()
{
char* input;
struct vink_xmpp_jid result;
int ret;
input = strdupa("test@example.org");
ret = vink_xmpp_parse_jid(&result, input);
EXPECT(ret == 0);
EXPECT(!strcmp(result.node, "test"));
EXPECT(!strcmp(result.domain, "example.org"));
EXPECT(result.resource == 0);
}
static void
t0x0002_xmpp_parse_jid()
{
char* input;
struct vink_xmpp_jid result;
int ret;
input = strdupa("test@example.org/resource");
ret = vink_xmpp_parse_jid(&result, input);
EXPECT(ret == 0);
EXPECT(!strcmp(result.node, "test"));
EXPECT(!strcmp(result.domain, "example.org"));
EXPECT(!strcmp(result.resource, "resource"));
}
static void
t0x0000_xmpp_init()
{
struct vink_xmpp_state *state;
struct VINK_buffer buffer;
ARRAY_INIT(&buffer);
state = vink_xmpp_state_init(buffer_write, "example.org",
VINK_CLIENT, &buffer);
}
void
signhandler(int signal)
{
fprintf(stderr, "Signal handler called (%d)\n", signal);
exit(EXIT_FAILURE);
}
int
main(int argc, char** argv)
{
signal(SIGSEGV, signhandler);
if (-1 == vink_init (SRCDIR "/unit-tests.conf", VINK_CLIENT, VINK_API_VERSION))
{
fprintf (stderr, "vink_init failed: %s\n", vink_last_error());
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
t0x0000_base64_decode();
t0x0001_base64_decode();
t0x0002_base64_decode();
t0x0000_xmpp_parse_jid();
t0x0001_xmpp_parse_jid();
t0x0002_xmpp_parse_jid();
t0x0000_xmpp_init();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}