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