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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "test_fuzz_isis_tlv_tests.h"
#include <zebra.h>
#include "memory.h"
#include "sbuf.h"
#include "stream.h"
#include "frrevent.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_tlvs.h"
#include "test_common.h"
#define TEST_STREAM_SIZE 1500
static bool atexit_registered;
static void show_meminfo_at_exit(void)
{
log_memstats(stderr, "isis fuzztest");
}
static int comp_line(const void *p1, const void *p2)
{
return strcmp(*(char * const *)p1, *(char * const *)p2);
}
static char *sortlines(char *in)
{
size_t line_count = 1;
size_t rv_len = strlen(in) + 1;
size_t rv_pos = 0;
char *rv = XMALLOC(MTYPE_TMP, rv_len);
for (char *c = in; *c; c++) {
if (*c == '\n')
line_count++;
}
if (line_count == 1) {
strncpy(rv, in, rv_len);
return rv;
}
char **lines = XCALLOC(MTYPE_TMP, sizeof(char *)*line_count);
char *saveptr = NULL;
size_t i = 0;
for (char *line = strtok_r(in, "\n", &saveptr); line;
line = strtok_r(NULL, "\n", &saveptr)) {
lines[i++] = line;
assert(i <= line_count);
}
line_count = i;
qsort(lines, line_count, sizeof(char *), comp_line);
for (i = 0; i < line_count; i++) {
int printf_rv = snprintf(rv + rv_pos, rv_len - rv_pos, "%s\n", lines[i]);
assert(printf_rv >= 0);
rv_pos += printf_rv;
}
XFREE(MTYPE_TMP, lines);
return rv;
}
static int test(FILE *input, FILE *output)
{
struct stream *s = stream_new(TEST_STREAM_SIZE);
char buf[TEST_STREAM_SIZE];
size_t bytes_read = 0;
if (!atexit_registered) {
atexit(show_meminfo_at_exit);
atexit_registered = true;
}
while (STREAM_WRITEABLE(s) && !feof(input)) {
bytes_read = fread(buf, 1, STREAM_WRITEABLE(s), input);
if (bytes_read == 0)
break;
stream_put(s, buf, bytes_read);
}
if (bytes_read && !feof(input)) {
fprintf(output, "Too much input data.\n");
stream_free(s);
return 1;
}
stream_set_getp(s, 0);
struct isis_tlvs *tlvs;
const char *log;
int rv = isis_unpack_tlvs(STREAM_READABLE(s), s, &tlvs, &log);
if (rv) {
fprintf(output, "Could not unpack TLVs:\n%s\n", log);
isis_free_tlvs(tlvs);
stream_free(s);
return 2;
}
fprintf(output, "Unpack log:\n%s", log);
const char *s_tlvs = isis_format_tlvs(tlvs, NULL);
fprintf(output, "Unpacked TLVs:\n%s", s_tlvs);
struct isis_item *orig_auth = tlvs->isis_auth.head;
tlvs->isis_auth.head = NULL;
s_tlvs = isis_format_tlvs(tlvs, NULL);
struct isis_tlvs *tlv_copy = isis_copy_tlvs(tlvs);
tlvs->isis_auth.head = orig_auth;
isis_free_tlvs(tlvs);
struct stream *s2 = stream_new(TEST_STREAM_SIZE);
if (isis_pack_tlvs(tlv_copy, s2, (size_t)-1, false, false)) {
fprintf(output, "Could not pack TLVs.\n");
assert(0);
}
stream_set_getp(s2, 0);
rv = isis_unpack_tlvs(STREAM_READABLE(s2), s2, &tlvs, &log);
if (rv) {
fprintf(output, "Could not unpack own TLVs:\n%s\n", log);
assert(0);
}
char *orig_tlvs = XSTRDUP(MTYPE_TMP, s_tlvs);
s_tlvs = isis_format_tlvs(tlvs, NULL);
if (strcmp(orig_tlvs, s_tlvs)) {
fprintf(output,
"Deserialized and Serialized LSP seem to differ.\n");
fprintf(output, "Re-Unpacked TLVs:\n%s", s_tlvs);
assert(0);
}
isis_free_tlvs(tlv_copy);
stream_free(s);
stream_free(s2);
struct list *fragments = isis_fragment_tlvs(tlvs, 550);
isis_free_tlvs(tlvs);
if (!fragments) {
XFREE(MTYPE_TMP, orig_tlvs);
return 0;
}
s = stream_new(550);
struct sbuf fragment_format;
sbuf_init(&fragment_format, NULL, 0);
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(fragments, node, tlvs)) {
stream_reset(s);
int rv = isis_pack_tlvs(tlvs, s, (size_t)-1, false, false);
if (rv) {
fprintf(output, "Could not pack fragment, too large.\n");
assert(0);
}
sbuf_push(&fragment_format, 0, "%s", isis_format_tlvs(tlvs, NULL));
isis_free_tlvs(tlvs);
}
list_delete(&fragments);
stream_free(s);
char *fragment_content = sortlines((char *)sbuf_buf(&fragment_format));
sbuf_free(&fragment_format);
char *orig_tlv_content = sortlines(orig_tlvs);
XFREE(MTYPE_TMP, orig_tlvs);
if (strcmp(fragment_content, orig_tlv_content)) {
fprintf(output, "Fragmented and unfragmented LSP seem to differ.\n");
fprintf(output, "Original:\n%s\nFragmented:\n%s\n",
orig_tlv_content, fragment_content);
assert(0);
}
XFREE(MTYPE_TMP, fragment_content);
XFREE(MTYPE_TMP, orig_tlv_content);
return 0;
}
|