Coverage Report

Created: 2025-08-25 06:35

/src/opendnp3/cpp/tests/fuzz/fuzzmaster.cpp
Line
Count
Source
1
/*
2
 * Copyright 2013-2022 Step Function I/O, LLC
3
 *
4
 * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O
5
 * LLC (https://stepfunc.io) under one or more contributor license agreements.
6
 * See the NOTICE file distributed with this work for additional information
7
 * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license
8
 * this file to you under the Apache License, Version 2.0 (the "License"); you
9
 * may not use this file except in compliance with the License. You may obtain
10
 * a copy of the License at:
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
#include <opendnp3/logging/LogLevels.h>
21
#include <opendnp3/master/DefaultMasterApplication.h>
22
23
#include <exe4cpp/MockExecutor.h>
24
25
#include <dnp3mocks/MockLowerLayer.h>
26
#include <dnp3mocks/MockMasterApplication.h>
27
#include <dnp3mocks/MockSOEHandler.h>
28
29
#include <master/MasterContext.h>
30
#include <master/MasterSchedulerBackend.h>
31
32
#include "NullLogHandler.h"
33
34
class MasterTestObject
35
{
36
public:
37
    MasterTestObject(const opendnp3::MasterParams& params)
38
6.13k
        : addresses(),
39
6.13k
          logger(),
40
6.13k
          exe(std::make_shared<exe4cpp::MockExecutor>()),
41
6.13k
          meas(std::make_shared<MockSOEHandler>()),
42
6.13k
          lower(std::make_shared<MockLowerLayer>()),
43
6.13k
          application(std::make_shared<MockMasterApplication>()),
44
6.13k
          scheduler(std::make_shared<opendnp3::MasterSchedulerBackend>(exe)),
45
6.13k
          context(opendnp3::MContext::Create(addresses, logger.get_logger(), exe, lower, meas, application, this->scheduler, params))
46
6.13k
    {
47
6.13k
        lower->SetUpperLayer(*context);
48
6.13k
    }
49
50
    ~MasterTestObject()
51
6.13k
    {
52
6.13k
        this->scheduler->Shutdown();
53
6.13k
    }
54
55
    size_t SendToMaster(const ser4cpp::rseq_t& buffer)
56
6.13k
    {
57
6.13k
        context->OnReceive(opendnp3::Message(this->addresses.Reverse(), buffer));
58
6.13k
        return exe->run_many();
59
6.13k
    }
60
61
    size_t LowerLayerUp()
62
6.13k
    {
63
6.13k
        context->OnLowerLayerUp();
64
6.13k
        return exe->run_many();
65
6.13k
    }
66
67
private:
68
    const opendnp3::Addresses addresses;
69
70
    const NullLogger logger;
71
    const std::shared_ptr<exe4cpp::MockExecutor> exe;
72
    const std::shared_ptr<MockSOEHandler> meas;
73
    const std::shared_ptr<MockLowerLayer> lower;
74
    const std::shared_ptr<MockMasterApplication> application;
75
    const std::shared_ptr<opendnp3::IMasterScheduler> scheduler;
76
    const std::shared_ptr<opendnp3::MContext> context;
77
};
78
79
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
80
6.13k
{
81
6.13k
    ser4cpp::rseq_t buffer(Data, Size);
82
83
6.13k
    opendnp3::MasterParams config;
84
6.13k
    MasterTestObject t(config);
85
6.13k
    t.LowerLayerUp();
86
6.13k
    t.SendToMaster(buffer);
87
6.13k
    return 0;
88
6.13k
}