blob: 75cbc93472ab0406161b1f6872af80ef917011dc [file] [log] [blame]
Juraj Vijtiukc496e6f2020-06-30 16:15:01 +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
53 LY_ERR err;
54
55 if (!log) {
56 ly_log_options(0);
57 log = true;
58 }
59
60 err = ly_ctx_new(NULL, 0, &ctx);
61 if (err != LY_SUCCESS) {
62 fprintf(stderr, "Failed to create context\n");
63 exit(EXIT_FAILURE);
64 }
65
66 lys_parse_mem(ctx, schema_a, LYS_IN_YANG);
67 lys_parse_mem(ctx, schema_b, LYS_IN_YANG);
68
69 data = malloc(len + 1);
70 if (data == NULL) {
71 return 0;
72 }
73 memcpy(data, buf, len);
74 data[len] = 0;
75
76 lyd_parse_mem(ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY);
77 ly_ctx_destroy(ctx, NULL);
78
79 free(data);
80
81 return 0;
82}