Coverage Report

Created: 2026-02-14 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea-fuzzer/fuzz_agent.cc
Line
Count
Source
1
// Copyright (C) 2025 Ada Logcis Ltd.
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
////////////////////////////////////////////////////////////////////////////////
7
#include "config.h"
8
#include <fuzzer/FuzzedDataProvider.h>
9
10
#include <agent/parser_context.h>
11
#include <agent/simple_parser.h>
12
#include <agent/ca_cfg_mgr.h>
13
14
#include <cc/data.h>
15
#include <exceptions/exceptions.h>
16
17
#include <string>
18
#include <memory>
19
20
using namespace isc::agent;
21
using namespace isc::data;
22
23
5.68k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24
5.68k
    FuzzedDataProvider fdp(data, size);
25
5.68k
    bool checkOnly = fdp.ConsumeBool();
26
27
5.68k
    AgentSimpleParser simpleParser;
28
5.68k
    CtrlAgentCfgContextPtr ctxPtr(new CtrlAgentCfgContext());
29
5.68k
    ParserContext ctx;
30
5.68k
    ElementPtr elem;
31
32
    // Generate random parsing mode
33
5.68k
    const uint8_t mode = fdp.ConsumeIntegralInRange<uint8_t>(0, 2);
34
5.68k
    ParserContext::ParserType type = ParserContext::PARSER_JSON;
35
5.68k
    if (mode == 1) {
36
1.04k
        type = ParserContext::PARSER_AGENT;
37
4.63k
    } else if (mode == 2) {
38
1.85k
        type = ParserContext::PARSER_SUB_AGENT;
39
1.85k
    }
40
41
5.68k
    const std::string payload = fdp.ConsumeRemainingBytesAsString();
42
43
    // Target context parseString
44
5.68k
    try {
45
5.68k
        ctx.parseString(payload, type);
46
5.68k
    } catch (const isc::Exception&) {
47
        // Slient exceptions
48
5.06k
    }
49
50
    // Parse payload to JSON
51
5.68k
    try {
52
5.68k
        elem = Element::fromJSON(payload);
53
5.68k
    } catch (...) {
54
        // If failed to parse the payload, early exit
55
3.43k
        return 0;
56
3.43k
    }
57
58
    // Target SimpleParser
59
2.24k
    try {
60
2.24k
        AgentSimpleParser::setAllDefaults(elem);
61
2.24k
        simpleParser.checkTlsSetup(elem);
62
2.24k
        simpleParser.parse(ctxPtr, elem, checkOnly);
63
2.24k
    } catch (const isc::Exception&) {
64
        // Slient exceptions
65
1.87k
    }
66
67
2.24k
    return 0;
68
2.24k
}