Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if(ENABLE_FUZZ_TARGETS)
set(fuzz_targets lys_parse_mem lyd_parse_mem_xml lyd_parse_mem_json lyd_parse_lyb lyd_parse_schema_mount_xml yang_parse_module)
set(fuzz_targets lys_parse_mem lyd_parse_mem_xml lyd_parse_mem_json lyd_parse_lyb lyd_parse_schema_mount_xml yang_parse_module lyd_cbor_roundtrip)

if(FUZZER STREQUAL "AFL")
foreach(target_name IN LISTS fuzz_targets)
Expand Down
1 change: 1 addition & 0 deletions tests/fuzz/corpus/lyd_cbor_roundtrip/seed01.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<c xmlns="urn:tests:fuzz-cbor"><s>hello</s><i>-9223372036854775808</i><u>18446744073709551615</u></c>
1 change: 1 addition & 0 deletions tests/fuzz/corpus/lyd_cbor_roundtrip/seed02.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<c xmlns="urn:tests:fuzz-cbor"><b>true</b><f>3.142</f><ll>-1</ll><ll>32767</ll></c>
1 change: 1 addition & 0 deletions tests/fuzz/corpus/lyd_cbor_roundtrip/seed03.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<c xmlns="urn:tests:fuzz-cbor"><l><k>k1</k><v>AAECAP0=</v></l><l><k>k2</k><v>//8=</v></l><empty/></c>
89 changes: 89 additions & 0 deletions tests/fuzz/lyd_cbor_roundtrip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @file lyd_cbor_roundtrip.c
* @author OSS-Fuzz coverage pipeline
* @brief fuzzing harness for the CBOR instance data format round trip.
*
* The fuzzed input is parsed as XML instance data against a fixed schema with
* diverse node types, printed as YANG-CBOR (src/printer_cbor.c) and re-parsed
* from the printed bytes (src/parser_cbor.c, src/lcbor.c), covering the CBOR
* printer and deserializer on printer-valid structures. Requires the build to
* have CBOR support enabled (libcbor present, ENABLE_CBOR_SUPPORT defined).
*
* Copyright (c) 2026 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "libyang.h"
#include "tests_config.h"

static const char *schema =
"module fuzz-cbor {namespace urn:tests:fuzz-cbor;prefix x;"
"container c {"
"leaf s {type string;}"
"leaf i {type int64;}"
"leaf u {type uint64;}"
"leaf b {type boolean;}"
"leaf f {type decimal64 {fraction-digits 3;}}"
"leaf-list ll {type int16;}"
"list l {key k; leaf k {type string;} leaf v {type binary;}}"
"leaf empty {type empty;}"
"}}";

int LLVMFuzzerTestOneInput(uint8_t const *buf, size_t len)
{
struct ly_ctx *ctx = NULL;
struct lyd_node *tree = NULL;
struct lyd_node *tree2 = NULL;
static bool log = false;
char *data = NULL;
char *cbor = NULL;
LY_ERR err;

if (!log) {
ly_log_options(0);
log = true;
}

err = ly_ctx_new(TESTS_SRC "/../modules", 0, &ctx);
if (err != LY_SUCCESS) {
return 0;
}

if (lys_parse_mem(ctx, schema, LYS_IN_YANG, NULL) != LY_SUCCESS) {
goto cleanup;
}

data = malloc(len + 1);
if (data == NULL) {
goto cleanup;
}
memcpy(data, buf, len);
data[len] = 0;

if (lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree) != LY_SUCCESS) {
goto cleanup;
}

if (lyd_print_mem(&cbor, tree, LYD_CBOR, 0) != LY_SUCCESS) {
goto cleanup;
}

lyd_parse_data_mem(ctx, cbor, LYD_CBOR, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree2);

cleanup:
free(data);
free(cbor);
lyd_free_all(tree);
lyd_free_all(tree2);
ly_ctx_destroy(ctx);
return 0;
}
26 changes: 26 additions & 0 deletions tests/fuzz/lyd_cbor_roundtrip.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"<c xmlns=\"urn:tests:fuzz-cbor\">"
"</c>"
"<s>"
"</s>"
"<i>"
"</i>"
"<u>"
"</u>"
"<b>"
"</b>"
"<f>"
"</f>"
"<ll>"
"</ll>"
"<l>"
"</l>"
"<k>"
"</k>"
"<v>"
"</v>"
"<empty/>"
"true"
"false"
"-9223372036854775808"
"18446744073709551615"
"xmlns=\"urn:tests:fuzz-cbor\""
Loading