blob: 0acad349b77a40c7f61aeb5396046cea7dbf5fe4 [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{
Michal Vaskob9878502022-11-11 10:00:05 +01009 struct ly_ctx *ctx = NULL;
10 static bool log = false;
11 const char *schema_a =
12 "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;"
13 "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;}"
14 "identity fast-ethernet {base ethernet;}}";
15 const char *schema_b =
16 "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}"
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020017 "feature f; identity gigabit-ethernet { base defs:ethernet;}"
18 "container cont {leaf leaftarget {type empty;}"
19 "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}}"
20 "leaf-list leaflisttarget {type uint8; max-elements 5;}}"
21 "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}"
22 "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}"
23 "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}"
24 "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}"
25 "leaf-list leaflisttarget {type string;}"
26 "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}"
27 "leaf binary-norestr {type binary;}"
28 "leaf int8 {type int8 {range 10..20;}}"
29 "leaf uint8 {type uint8 {range 150..200;}}"
30 "leaf int16 {type int16 {range -20..-10;}}"
31 "leaf uint16 {type uint16 {range 150..200;}}"
32 "leaf int32 {type int32;}"
33 "leaf uint32 {type uint32;}"
34 "leaf int64 {type int64;}"
35 "leaf uint64 {type uint64;}"
36 "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}"
37 "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}"
38 "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}"
39 "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}"
40 "leaf str {type string {length 8..10; pattern '[a-z ]*';}}"
41 "leaf str-norestr {type string;}"
42 "leaf str-utf8 {type string{length 2..5; pattern '€*';}}"
43 "leaf bool {type boolean;}"
44 "leaf empty {type empty;}"
45 "leaf ident {type identityref {base defs:interface-type;}}"
Michal Vaskob9878502022-11-11 10:00:05 +010046 "leaf inst {type instance-identifier {require-instance true;}}"
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020047 "leaf inst-noreq {type instance-identifier {require-instance false;}}"
48 "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}"
49 "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}"
50 "leaf un1 {type union {"
51 "type leafref {path /int8; require-instance true;}"
52 "type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }"
53 "type string {length 1..20;}}}}";
Michal Vaskob9878502022-11-11 10:00:05 +010054 char *data = NULL;
55 struct lyd_node *tree = NULL;
56 LY_ERR err;
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020057
Michal Vaskob9878502022-11-11 10:00:05 +010058 if (!log) {
59 ly_log_options(0);
60 log = true;
61 }
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020062
Michal Vaskob9878502022-11-11 10:00:05 +010063 err = ly_ctx_new(NULL, 0, &ctx);
64 if (err != LY_SUCCESS) {
65 fprintf(stderr, "Failed to create context\n");
66 exit(EXIT_FAILURE);
67 }
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020068
Michal Vaskob9878502022-11-11 10:00:05 +010069 lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL);
70 lys_parse_mem(ctx, schema_b, LYS_IN_YANG, NULL);
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020071
Michal Vaskob9878502022-11-11 10:00:05 +010072 data = malloc(len + 1);
73 if (data == NULL) {
74 return 0;
75 }
76 memcpy(data, buf, len);
77 data[len] = 0;
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020078
Michal Vaskob9878502022-11-11 10:00:05 +010079 lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree);
80 lyd_free_all(tree);
81 ly_ctx_destroy(ctx);
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020082
Michal Vaskob9878502022-11-11 10:00:05 +010083 free(data);
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020084
Michal Vaskob9878502022-11-11 10:00:05 +010085 return 0;
Juraj Vijtiuka1c0c702020-10-02 09:55:48 +020086}