P4C
The P4 Compiler
Loading...
Searching...
No Matches
protobuf.h
1/*
2 * SPDX-FileCopyrightText: 2023 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_BACKEND_PROTOBUF_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_BACKEND_PROTOBUF_H_
9
10#include <cstddef>
11#include <optional>
12#include <string>
13#include <vector>
14
15#include <inja/inja.hpp>
16
17#include "backends/p4tools/common/control_plane/p4info_map.h"
18#include "control-plane/p4RuntimeSerializer.h"
19#include "ir/ir.h"
20#include "lib/cstring.h"
21
22#include "backends/p4tools/modules/testgen/lib/test_spec.h"
23#include "backends/p4tools/modules/testgen/targets/bmv2/test_backend/common.h"
24
26
27using P4::ControlPlaneAPI::p4rt_id_t;
28
29struct ProtobufTest : public AbstractTest {
30 private:
32 std::string formattedTest_;
33
34 public:
35 explicit ProtobufTest(std::string formattedTest) : formattedTest_(std::move(formattedTest)) {}
36
38 [[nodiscard]] const std::string &getFormattedTest() const { return formattedTest_; }
39
40 DECLARE_TYPEINFO(ProtobufTest);
41};
42
44class Protobuf : public Bmv2TestFramework {
45 public:
46 explicit Protobuf(const TestBackendConfiguration &testBackendConfiguration,
47 P4::P4RuntimeAPI p4RuntimeApi);
48
49 void writeTestToFile(const TestSpec *testSpec, cstring selectedBranches, size_t testId,
50 float currentCoverage) override;
51
52 AbstractTestReferenceOrError produceTest(const TestSpec *testSpec, cstring selectedBranches,
53 size_t testIdx, float currentCoverage) override;
54
55 private:
58 P4::P4RuntimeAPI p4RuntimeApi;
59
62
63 inja::json getControlPlane(const TestSpec *testSpec) const override;
64
65 [[nodiscard]] inja::json getSend(const TestSpec *testSpec) const override;
66
67 [[nodiscard]] inja::json getExpectedPacket(const TestSpec *testSpec) const override;
68
70 static std::string formatHexExprWithSep(const IR::Expression *expr);
71
74 [[nodiscard]] std::optional<p4rt_id_t> lookupP4RuntimeId(cstring controlPlaneName) const;
75
78 void emitPreamble(const std::string &preamble);
79
85 inja::json produceTestCase(const TestSpec *testSpec, cstring selectedBranches, size_t testId,
86 float currentCoverage) const;
87
89 static std::string getTestCaseTemplate();
90
94 [[nodiscard]] inja::json getControlPlaneForTable(cstring tableName, cstring actionName,
95 const TableMatchMap &matches,
96 const std::vector<ActionArg> &args) const;
97};
98
99} // namespace P4::P4Tools::P4Testgen::Bmv2
100
101#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_BACKEND_PROTOBUF_H_ */
Definition p4info_map.h:43
AbstractTestReferenceOrError produceTest(const TestSpec *testSpec, cstring selectedBranches, size_t testIdx, float currentCoverage) override
Definition protobuf.cpp:246
void writeTestToFile(const TestSpec *testSpec, cstring selectedBranches, size_t testId, float currentCoverage) override
Definition protobuf.cpp:231
Definition lib/test_spec.h:303
Definition cstring.h:85
Inja.
Definition targets/bmv2/cmd_stepper.cpp:37
Definition p4RuntimeSerializer.h:48
Type definitions for abstract tests.
Definition test_framework.h:35
const std::string & getFormattedTest() const
Definition protobuf.h:38
Definition test_backend_configuration.h:22