P4C
The P4 Compiler
Loading...
Searching...
No Matches
lib/test_backend.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_BACKEND_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_BACKEND_H_
9
10#include <cstdint>
11#include <functional>
12#include <optional>
13#include <vector>
14
15#include "backends/p4tools/common/lib/model.h"
16#include "backends/p4tools/common/lib/trace_event.h"
17#include "ir/ir.h"
18
19#include "backends/p4tools/modules/testgen/core/program_info.h"
20#include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h"
21#include "backends/p4tools/modules/testgen/lib/execution_state.h"
22#include "backends/p4tools/modules/testgen/lib/final_state.h"
23#include "backends/p4tools/modules/testgen/lib/test_framework.h"
24#include "backends/p4tools/modules/testgen/lib/test_spec.h"
25#include "backends/p4tools/modules/testgen/options.h"
26
27namespace P4::P4Tools::P4Testgen {
28
29class TestBackEnd {
30 private:
32 int64_t testCount = 0;
33
35 static const int64_t RESET_THRESHOLD = 10000;
36
38 std::reference_wrapper<const ProgramInfo> programInfo;
39
41 std::reference_wrapper<const TestBackendConfiguration> testBackendConfiguration;
42
43 protected:
46
50
52 int64_t maxTests;
53
55 float coverage = 0;
56
58 AbstractTestList tests;
59
60 explicit TestBackEnd(const ProgramInfo &programInfo,
61 const TestBackendConfiguration &testBackendConfiguration,
63
64 [[nodiscard]] bool needsToTerminate(int64_t testCount) const;
65
66 public:
67 TestBackEnd(const TestBackEnd &) = default;
68
69 TestBackEnd(TestBackEnd &&) = default;
70
71 TestBackEnd &operator=(const TestBackEnd &) = delete;
72
73 TestBackEnd &operator=(TestBackEnd &&) = delete;
74
75 virtual ~TestBackEnd() = default;
76
77 struct TestInfo {
80 const IR::Constant *inputPacket;
81
84
86 const IR::Constant *outputPacket;
87
90
92 const IR::Constant *packetTaintMask;
93
95 const std::vector<std::reference_wrapper<const TraceEvent>> programTraces;
96
98 bool packetIsDropped = false;
99 };
100
102 virtual const TestSpec *createTestSpec(const ExecutionState *executionState,
103 const Model *finalModel, const TestInfo &testInfo) = 0;
104
108 virtual bool printTestInfo(const ExecutionState *executionState, const TestInfo &testInfo,
109 const IR::Expression *outputPortExpr);
110
112 [[nodiscard]] std::optional<std::reference_wrapper<const FinalState>> computeConcolicVariables(
113 const FinalState &state) const;
114
118 const ExecutionState *executionState, const Model *finalModel,
119 const IR::Expression *outputPacketExpr, const IR::Expression *outputPortExpr,
120 const std::vector<std::reference_wrapper<const TraceEvent>> *programTraces);
121
123 virtual bool run(const FinalState &state);
124
126 [[nodiscard]] int64_t getTestCount() const;
127
129 [[nodiscard]] float getCoverage() const;
130
132 [[nodiscard]] const ProgramInfo &getProgramInfo() const;
133
135 [[nodiscard]] const TestBackendConfiguration &getTestBackendConfiguration() const;
136
139 [[nodiscard]] const AbstractTestList &getTests() const { return tests; }
140};
141
142} // namespace P4::P4Tools::P4Testgen
143
144#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_BACKEND_H_ */
Definition backends/p4tools/common/lib/model.h:24
Represents state of execution after having reached a program point.
Definition execution_state.h:40
Represents the final state after execution.
Definition final_state.h:25
Stores target-specific information about a P4 program.
Definition core/program_info.h:27
Definition symbolic_executor.h:27
int inputPort
The input port of the packet.
Definition lib/test_backend.h:83
const IR::Constant * packetTaintMask
The taint mask.
Definition lib/test_backend.h:92
const AbstractTestList & getTests() const
Definition lib/test_backend.h:139
int64_t getTestCount() const
Returns test count.
Definition lib/test_backend.cpp:274
TestFramework * testWriter
Writes the tests out to a file.
Definition lib/test_backend.h:45
float coverage
The accumulated coverage of all finished test cases. Number in range [0, 1].
Definition lib/test_backend.h:55
AbstractTestList tests
The list of tests accumulated in the test back end.
Definition lib/test_backend.h:58
int64_t maxTests
Test maximum number of tests that are to be produced.
Definition lib/test_backend.h:52
int outputPort
The output port of the packet.
Definition lib/test_backend.h:89
std::optional< std::reference_wrapper< const FinalState > > computeConcolicVariables(const FinalState &state) const
virtual TestInfo produceTestInfo(const ExecutionState *executionState, const Model *finalModel, const IR::Expression *outputPacketExpr, const IR::Expression *outputPortExpr, const std::vector< std::reference_wrapper< const TraceEvent > > *programTraces)
Definition lib/test_backend.cpp:189
const IR::Constant * outputPacket
The concrete value of the output packet as modified by the packet.
Definition lib/test_backend.h:86
const IR::Constant * inputPacket
Definition lib/test_backend.h:80
SymbolicExecutor & symbex
Definition lib/test_backend.h:49
virtual bool run(const FinalState &state)
The callback that is executed by the symbolic executor.
Definition lib/test_backend.cpp:47
const std::vector< std::reference_wrapper< const TraceEvent > > programTraces
The traces that have been collected during execution of this particular test path.
Definition lib/test_backend.h:95
const TestBackendConfiguration & getTestBackendConfiguration() const
Returns the configuration options for the test back end.
Definition lib/test_backend.cpp:280
virtual bool printTestInfo(const ExecutionState *executionState, const TestInfo &testInfo, const IR::Expression *outputPortExpr)
Definition lib/test_backend.cpp:227
virtual const TestSpec * createTestSpec(const ExecutionState *executionState, const Model *finalModel, const TestInfo &testInfo)=0
bool packetIsDropped
Indicates whether the packet is dropped.
Definition lib/test_backend.h:98
float getCoverage() const
Returns coverage achieved by all the processed tests.
Definition lib/test_backend.cpp:276
const ProgramInfo & getProgramInfo() const
Returns the program info.
Definition lib/test_backend.cpp:278
Definition lib/test_backend.h:77
Definition test_framework.h:60
Definition lib/test_spec.h:303
Definition test_backend_configuration.h:22