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