5 #include "test_fuzz_isis_tlv_tests.h"
14 #include "isisd/isis_circuit.h"
15 #include "isisd/isis_tlvs.h"
17 #define TEST_STREAM_SIZE 1500
19 struct thread_master
*master
;
20 int isis_sock_init(struct isis_circuit
*circuit
);
21 int isis_sock_init(struct isis_circuit
*circuit
)
26 struct zebra_privs_t isisd_privs
;
28 static bool atexit_registered
;
30 static void show_meminfo_at_exit(void)
32 log_memstats(stderr
, "isis fuzztest");
35 static int comp_line(const void *p1
, const void *p2
)
37 return strcmp(*(char * const *)p1
, *(char * const *)p2
);
40 static char *sortlines(char *in
)
42 size_t line_count
= 1;
43 size_t rv_len
= strlen(in
) + 1;
45 char *rv
= XMALLOC(MTYPE_TMP
, rv_len
);
47 for (char *c
= in
; *c
; c
++) {
52 if (line_count
== 1) {
53 strncpy(rv
, in
, rv_len
);
57 char **lines
= XCALLOC(MTYPE_TMP
, sizeof(char *)*line_count
);
61 for (char *line
= strtok_r(in
, "\n", &saveptr
); line
;
62 line
= strtok_r(NULL
, "\n", &saveptr
)) {
64 assert(i
<= line_count
);
69 qsort(lines
, line_count
, sizeof(char *), comp_line
);
71 for (i
= 0; i
< line_count
; i
++) {
72 int printf_rv
= snprintf(rv
+ rv_pos
, rv_len
- rv_pos
, "%s\n", lines
[i
]);
73 assert(printf_rv
>= 0);
77 XFREE(MTYPE_TMP
, lines
);
81 static int test(FILE *input
, FILE *output
)
83 struct stream
*s
= stream_new(TEST_STREAM_SIZE
);
84 char buf
[TEST_STREAM_SIZE
];
85 size_t bytes_read
= 0;
87 if (!atexit_registered
) {
88 atexit(show_meminfo_at_exit
);
89 atexit_registered
= true;
92 while (STREAM_WRITEABLE(s
) && !feof(input
)) {
93 bytes_read
= fread(buf
, 1, STREAM_WRITEABLE(s
), input
);
96 stream_put(s
, buf
, bytes_read
);
99 if (bytes_read
&& !feof(input
)) {
100 fprintf(output
, "Too much input data.\n");
105 stream_set_getp(s
, 0);
106 struct isis_tlvs
*tlvs
;
108 int rv
= isis_unpack_tlvs(STREAM_READABLE(s
), s
, &tlvs
, &log
);
111 fprintf(output
, "Could not unpack TLVs:\n%s\n", log
);
112 isis_free_tlvs(tlvs
);
117 fprintf(output
, "Unpack log:\n%s", log
);
118 const char *s_tlvs
= isis_format_tlvs(tlvs
);
119 fprintf(output
, "Unpacked TLVs:\n%s", s_tlvs
);
121 struct isis_item
*orig_auth
= tlvs
->isis_auth
.head
;
122 tlvs
->isis_auth
.head
= NULL
;
123 s_tlvs
= isis_format_tlvs(tlvs
);
124 struct isis_tlvs
*tlv_copy
= isis_copy_tlvs(tlvs
);
125 tlvs
->isis_auth
.head
= orig_auth
;
126 isis_free_tlvs(tlvs
);
128 struct stream
*s2
= stream_new(TEST_STREAM_SIZE
);
130 if (isis_pack_tlvs(tlv_copy
, s2
, (size_t)-1, false, false)) {
131 fprintf(output
, "Could not pack TLVs.\n");
135 stream_set_getp(s2
, 0);
136 rv
= isis_unpack_tlvs(STREAM_READABLE(s2
), s2
, &tlvs
, &log
);
138 fprintf(output
, "Could not unpack own TLVs:\n%s\n", log
);
142 char *orig_tlvs
= XSTRDUP(MTYPE_TMP
, s_tlvs
);
143 s_tlvs
= isis_format_tlvs(tlvs
);
145 if (strcmp(orig_tlvs
, s_tlvs
)) {
147 "Deserialized and Serialized LSP seem to differ.\n");
148 fprintf(output
, "Re-Unpacked TLVs:\n%s", s_tlvs
);
152 isis_free_tlvs(tlv_copy
);
156 struct list
*fragments
= isis_fragment_tlvs(tlvs
, 550);
157 isis_free_tlvs(tlvs
);
159 XFREE(MTYPE_TMP
, orig_tlvs
);
165 struct sbuf fragment_format
;
166 sbuf_init(&fragment_format
, NULL
, 0);
168 struct listnode
*node
;
169 for (ALL_LIST_ELEMENTS_RO(fragments
, node
, tlvs
)) {
171 int rv
= isis_pack_tlvs(tlvs
, s
, (size_t)-1, false, false);
173 fprintf(output
, "Could not pack fragment, too large.\n");
176 sbuf_push(&fragment_format
, 0, "%s", isis_format_tlvs(tlvs
));
177 isis_free_tlvs(tlvs
);
179 list_delete(&fragments
);
182 char *fragment_content
= sortlines((char *)sbuf_buf(&fragment_format
));
183 sbuf_free(&fragment_format
);
184 char *orig_tlv_content
= sortlines(orig_tlvs
);
185 XFREE(MTYPE_TMP
, orig_tlvs
);
187 if (strcmp(fragment_content
, orig_tlv_content
)) {
188 fprintf(output
, "Fragmented and unfragmented LSP seem to differ.\n");
189 fprintf(output
, "Original:\n%s\nFragmented:\n%s\n",
190 orig_tlv_content
, fragment_content
);
194 XFREE(MTYPE_TMP
, fragment_content
);
195 XFREE(MTYPE_TMP
, orig_tlv_content
);