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