Coverage Report

Created: 2025-12-31 07:33

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
3.83k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24
3.83k
    FuzzedDataProvider fdp(data, size);
25
3.83k
    bool checkOnly = fdp.ConsumeBool();
26
27
3.83k
    AgentSimpleParser simpleParser;
28
3.83k
    CtrlAgentCfgContextPtr ctxPtr(new CtrlAgentCfgContext());
29
3.83k
    ParserContext ctx;
30
3.83k
    ElementPtr elem;
31
32
    // Generate random parsing mode
33
3.83k
    const uint8_t mode = fdp.ConsumeIntegralInRange<uint8_t>(0, 2);
34
3.83k
    ParserContext::ParserType type = ParserContext::PARSER_JSON;
35
3.83k
    if (mode == 1) {
36
614
        type = ParserContext::PARSER_AGENT;
37
3.22k
    } else if (mode == 2) {
38
1.24k
        type = ParserContext::PARSER_SUB_AGENT;
39
1.24k
    }
40
41
3.83k
    const std::string payload = fdp.ConsumeRemainingBytesAsString();
42
43
    // Target context parseString
44
3.83k
    try {
45
3.83k
        ctx.parseString(payload, type);
46
3.83k
    } catch (const isc::Exception&) {
47
        // Slient exceptions
48
3.27k
    }
49
50
    // Parse payload to JSON
51
3.83k
    try {
52
3.83k
        elem = Element::fromJSON(payload);
53
3.83k
    } catch (...) {
54
        // If failed to parse the payload, early exit
55
2.35k
        return 0;
56
2.35k
    }
57
58
    // Target SimpleParser
59
1.47k
    try {
60
1.47k
        AgentSimpleParser::setAllDefaults(elem);
61
1.47k
        simpleParser.checkTlsSetup(elem);
62
1.47k
        simpleParser.parse(ctxPtr, elem, checkOnly);
63
1.47k
    } catch (const isc::Exception&) {
64
        // Slient exceptions
65
1.24k
    }
66
67
1.47k
    return 0;
68
1.47k
}