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