/src/libyang/tests/fuzz/lyd_parse_mem_json.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include <stdbool.h> |
4 | | |
5 | | #include "libyang.h" |
6 | | |
7 | | int LLVMFuzzerTestOneInput(uint8_t const *buf, size_t len) |
8 | 18.9k | { |
9 | 18.9k | struct ly_ctx *ctx = NULL; |
10 | 18.9k | static bool log = false; |
11 | 18.9k | const char *schema_a = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;" |
12 | 18.9k | "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}}"; |
13 | 18.9k | const char *schema_b = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}" |
14 | 18.9k | "feature f; identity gigabit-ethernet { base defs:ethernet;}" |
15 | 18.9k | "container cont {leaf leaftarget {type empty;}" |
16 | 18.9k | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}}" |
17 | 18.9k | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
18 | 18.9k | "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}" |
19 | 18.9k | "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}" |
20 | 18.9k | "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}" |
21 | 18.9k | "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}" |
22 | 18.9k | "leaf-list leaflisttarget {type string;}" |
23 | 18.9k | "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}" |
24 | 18.9k | "leaf binary-norestr {type binary;}" |
25 | 18.9k | "leaf int8 {type int8 {range 10..20;}}" |
26 | 18.9k | "leaf uint8 {type uint8 {range 150..200;}}" |
27 | 18.9k | "leaf int16 {type int16 {range -20..-10;}}" |
28 | 18.9k | "leaf uint16 {type uint16 {range 150..200;}}" |
29 | 18.9k | "leaf int32 {type int32;}" |
30 | 18.9k | "leaf uint32 {type uint32;}" |
31 | 18.9k | "leaf int64 {type int64;}" |
32 | 18.9k | "leaf uint64 {type uint64;}" |
33 | 18.9k | "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}" |
34 | 18.9k | "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}" |
35 | 18.9k | "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}" |
36 | 18.9k | "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}" |
37 | 18.9k | "leaf str {type string {length 8..10; pattern '[a-z ]*';}}" |
38 | 18.9k | "leaf str-norestr {type string;}" |
39 | 18.9k | "leaf str-utf8 {type string{length 2..5; pattern '€*';}}" |
40 | 18.9k | "leaf bool {type boolean;}" |
41 | 18.9k | "leaf empty {type empty;}" |
42 | 18.9k | "leaf ident {type identityref {base defs:interface-type;}}" |
43 | 18.9k | "leaf inst {type instance-identifier {require-instance true;}}" |
44 | 18.9k | "leaf inst-noreq {type instance-identifier {require-instance false;}}" |
45 | 18.9k | "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}" |
46 | 18.9k | "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}" |
47 | 18.9k | "leaf un1 {type union {" |
48 | 18.9k | "type leafref {path /int8; require-instance true;}" |
49 | 18.9k | "type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }" |
50 | 18.9k | "type string {length 1..20;}}}}"; |
51 | 18.9k | char *data = NULL; |
52 | 18.9k | struct lyd_node *tree = NULL; |
53 | | |
54 | 18.9k | LY_ERR err; |
55 | | |
56 | 18.9k | if (!log) { |
57 | 3 | ly_log_options(0); |
58 | 3 | log = true; |
59 | 3 | } |
60 | | |
61 | 18.9k | err = ly_ctx_new(NULL, 0, &ctx); |
62 | 18.9k | if (err != LY_SUCCESS) { |
63 | 0 | fprintf(stderr, "Failed to create context\n"); |
64 | 0 | exit(EXIT_FAILURE); |
65 | 0 | } |
66 | | |
67 | 18.9k | lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL); |
68 | 18.9k | lys_parse_mem(ctx, schema_b, LYS_IN_YANG, NULL); |
69 | | |
70 | 18.9k | data = malloc(len + 1); |
71 | 18.9k | if (data == NULL) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | 18.9k | memcpy(data, buf, len); |
75 | 18.9k | data[len] = 0; |
76 | | |
77 | 18.9k | lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree); |
78 | 18.9k | lyd_free_all(tree); |
79 | 18.9k | ly_ctx_destroy(ctx); |
80 | | |
81 | 18.9k | free(data); |
82 | | |
83 | 18.9k | return 0; |
84 | 18.9k | } |