/src/opendnp3/cpp/tests/fuzz/fuzzoutstation.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 | | |
22 | | #include <exe4cpp/MockExecutor.h> |
23 | | |
24 | | #include <dnp3mocks/MockCommandHandler.h> |
25 | | #include <dnp3mocks/MockLowerLayer.h> |
26 | | #include <dnp3mocks/MockOutstationApplication.h> |
27 | | |
28 | | #include <outstation/Database.h> |
29 | | #include <outstation/OutstationContext.h> |
30 | | |
31 | | #include "NullLogHandler.h" |
32 | | |
33 | | #include <functional> |
34 | | |
35 | | class OutstationTestObject |
36 | | { |
37 | | public: |
38 | | OutstationTestObject(const opendnp3::OutstationConfig& config, |
39 | | const opendnp3::DatabaseConfig& database = opendnp3::DatabaseConfig(10)) |
40 | 9.44k | : logger(), |
41 | 9.44k | exe(std::make_shared<exe4cpp::MockExecutor>()), |
42 | 9.44k | lower(std::make_shared<MockLowerLayer>()), |
43 | 9.44k | cmdHandler(std::make_shared<MockCommandHandler>(opendnp3::CommandStatus::SUCCESS)), |
44 | 9.44k | application(std::make_shared<MockOutstationApplication>()), |
45 | 9.44k | context(opendnp3::Addresses(), config, database, logger.get_logger(), exe, lower, cmdHandler, application) |
46 | 9.44k | { |
47 | 9.44k | lower->SetUpperLayer(context); |
48 | 9.44k | } |
49 | | |
50 | | size_t SendToOutstation(const ser4cpp::rseq_t& buffer) |
51 | 9.44k | { |
52 | 9.44k | context.OnReceive(opendnp3::Message(opendnp3::Addresses(), buffer)); |
53 | 9.44k | return exe->run_many(); |
54 | 9.44k | } |
55 | | |
56 | | size_t LowerLayerUp() |
57 | 9.44k | { |
58 | 9.44k | context.OnLowerLayerUp(); |
59 | 9.44k | return exe->run_many(); |
60 | 9.44k | } |
61 | | |
62 | | private: |
63 | | const NullLogger logger; |
64 | | const std::shared_ptr<exe4cpp::MockExecutor> exe; |
65 | | const std::shared_ptr<MockLowerLayer> lower; |
66 | | const std::shared_ptr<MockCommandHandler> cmdHandler; |
67 | | const std::shared_ptr<MockOutstationApplication> application; |
68 | | opendnp3::OContext context; |
69 | | }; |
70 | | |
71 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) |
72 | 9.44k | { |
73 | 9.44k | ser4cpp::rseq_t buffer(Data, Size); |
74 | | |
75 | 9.44k | opendnp3::OutstationConfig config; |
76 | 9.44k | OutstationTestObject t(config); |
77 | 9.44k | t.LowerLayerUp(); |
78 | 9.44k | t.SendToOutstation(buffer); |
79 | 9.44k | return 0; |
80 | 9.44k | } |