add fuzzer and cram based unit tests
[project/ubus.git] / tests / fuzz / test-fuzz.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <limits.h>
5
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
8
9 #include "ubusmsg.h"
10 #include "libubus.h"
11 #include "libubus-internal.h"
12
13 static void _ubus_validate_hdr(const uint8_t *data, size_t size)
14 {
15 if (size > sizeof(struct ubus_msghdr))
16 return;
17
18 ubus_validate_hdr((struct ubus_msghdr *) data);
19 }
20
21 static void _ubus_parse_msg(const uint8_t *data, size_t size)
22 {
23 struct blob_attr *attr = (struct blob_attr *) data;
24
25 if (size < sizeof(struct blob_attr *))
26 return;
27
28 if (blob_pad_len(attr) > UBUS_MAX_MSGLEN)
29 return;
30
31 ubus_parse_msg(attr);
32 }
33
34 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
35 {
36 _ubus_validate_hdr(data, size);
37 _ubus_parse_msg(data, size);
38
39 return 0;
40 }