-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
179 lines (144 loc) · 4.3 KB
/
test.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
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "matching.h"
static struct matching_ctx ctx;
static char ctx_linebuffer[256];
static unsigned int match_count;
static unsigned int match_count_ko;
static const char input_data[] = "\r\nOK\r\nKO\r\nOK\r\nKO\r\nOK\r\nHI\r\n";
static void matching_cb_match(struct matching_ctx *ctx)
{
(void)ctx;
match_count++;
}
static void matching_cb_match_ko(struct matching_ctx *ctx)
{
(void)ctx;
match_count_ko++;
}
static void matching_cb_match_rickroll(struct matching_ctx *ctx)
{
(void)ctx;
assert(0 == strcmp("gonna give you up\r\n", ctx->match.str));
assert(0 == strcmp("+NEVER:gonna give you up\r\n", (const char *)ctx->linebuffer.data));
}
static void matching_cb_nsmi(struct matching_ctx *ctx)
{
(void)ctx;
assert(0 == strcmp("SENT\r\n", ctx->match.str));
}
static const struct matching_item matching_items[] = {
{ MATCHING_ITEM_STR("HI"), 0, NULL },
{ MATCHING_ITEM_STR("OK"), 0, matching_cb_match },
{ MATCHING_ITEM_STR("KO"), MATCHING_ITEM_FLAG_CB_ON_MATCH | MATCHING_ITEM_FLAG_CB_ON_RESET, matching_cb_match_ko },
{ MATCHING_ITEM_STR("+NEVER:"), 0, matching_cb_match_rickroll },
{ (const char *)"+NSMI:\0ERROR", 6, 0, matching_cb_nsmi }
};
static enum matching_item_states matching_items_state[sizeof(matching_items)/sizeof(matching_items[0])];
void prepare_ctx(void)
{
memset(&ctx, 0, sizeof(struct matching_ctx));
match_count = 0;
match_count_ko = 0;
ctx.cfg.flags = MATCHING_ITEM_FLAG_CB_ON_RESET;
ctx.cfg.reset_chars = "\n";
za_buffer_init(&ctx.linebuffer, ctx_linebuffer, sizeof(ctx_linebuffer));
ctx.items.list = matching_items;
ctx.items.n = sizeof(matching_items)/sizeof(matching_items[0]);
ctx.items.state = matching_items_state;
matching_init(&ctx);
}
void test_matching_feed_invalid(void)
{
matching_feed(NULL, input_data[0], true);
}
/* Count amount of callbacks calls */
void test_matching_count_cb_calls(void)
{
prepare_ctx();
for (int i = 0; i < strlen(input_data); i++) {
matching_feed(&ctx, input_data[i], true);
}
assert(3U == match_count);
assert(4U == match_count_ko);
}
/* Feed more than the linebuffer without match, check for crash */
void test_matching_check_linebuffer_overflow(void)
{
prepare_ctx();
for (int i = 0; i < sizeof(ctx_linebuffer) + 1; i++) {
matching_feed(&ctx, 0x55, true);
}
/* Expect we are at the end of the linebuffer with matching */
assert(ctx.linebuffer.used == ctx.linebuffer.size - 1);
}
/* No linebuffer, check if we match */
void test_matching_linebuffer_null(void)
{
prepare_ctx();
ctx.linebuffer.data = NULL;
for (int i = 0; i < strlen(input_data); i++) {
matching_feed(&ctx, input_data[i], true);
}
assert(3U == match_count);
assert(4U == match_count_ko);
}
/* Count amount of callbacks calls */
void test_matching_multi_chunk_ok_test(void)
{
const char chunk1[] = "Lorem ipsum, lorem ipsum\r\nO";
const char chunk2[] = "K\r\nmust match..\r\n";
prepare_ctx();
for (int i = 0; i < strlen(chunk1); i++) {
matching_feed(&ctx, chunk1[i], true);
}
for (int i = 0; i < strlen(chunk2); i++) {
matching_feed(&ctx, chunk2[i], true);
}
assert(1U == match_count);
assert(0U == match_count_ko);
}
void test_matching_rickroll(void)
{
const char chunk1[] = "\r\n\r\n+NEVER";
const char chunk2[] = ":gonna give you ";
const char chunk3[] = "up\r\n\r\nKO\r\nLALALA\r\n";
prepare_ctx();
assert(0U == match_count);
assert(0U == match_count_ko);
for (int i = 0; i < strlen(chunk1); i++) {
matching_feed(&ctx, chunk1[i], true);
}
assert(0U == match_count);
assert(0U == match_count_ko);
for (int i = 0; i < strlen(chunk2); i++) {
matching_feed(&ctx, chunk2[i], true);
}
assert(0U == match_count);
assert(0U == match_count_ko);
for (int i = 0; i < strlen(chunk3); i++) {
matching_feed(&ctx, chunk3[i], true);
}
assert(0U == match_count);
assert(2U == match_count_ko);
}
void test_matching_nsmi(void)
{
const char chunk1[] = "\r\nOK\r\n\r\n+NSMI:SENT\r\n\r\nOK\r\n";
prepare_ctx();
for (int i = 0; i < strlen(chunk1); i++) {
matching_feed(&ctx, chunk1[i], true);
}
}
int main(void)
{
test_matching_feed_invalid();
test_matching_count_cb_calls();
test_matching_check_linebuffer_overflow();
test_matching_linebuffer_null();
test_matching_multi_chunk_ok_test();
test_matching_rickroll();
test_matching_nsmi();
}