Skip to content

Commit

Permalink
[Dvaas]: Create function to retag packets and update the tag for enti…
Browse files Browse the repository at this point in the history
…re PacketTestVector.
  • Loading branch information
VSuryaprasad-HCL committed Nov 19, 2024
1 parent bb8e983 commit 76f1109
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 30 deletions.
12 changes: 6 additions & 6 deletions dvaas/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ cc_library(
"//gutil:proto",
"//gutil:status",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi/packetlib",
"//p4_pdpi/packetlib:packetlib_cc_proto",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest",
"@com_google_protobuf//:protobuf",
"@com_googlesource_code_re2//:re2",
],
)
Expand Down Expand Up @@ -251,8 +247,12 @@ cc_test(
srcs = ["test_vector_test.cc"],
deps = [
":test_vector",
":test_vector_cc_proto",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi/packetlib",
"//p4_pdpi/packetlib:packetlib_cc_proto",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
78 changes: 64 additions & 14 deletions dvaas/test_vector.cc
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "dvaas/test_vector.h"

#include <ostream>
#include <string>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "dvaas/test_vector.pb.h"
#include "gutil/proto.h"
#include "gutil/status.h"
#include "p4_pdpi/packetlib/packetlib.h"
#include "p4_pdpi/packetlib/packetlib.pb.h"
#include "re2/re2.h"

Expand Down Expand Up @@ -53,4 +43,64 @@ std::ostream& operator<<(std::ostream& os, const SwitchOutput& output) {
return os << output.DebugString();
}

absl::Status UpdateTestTag(packetlib::Packet& packet, int new_tag) {
// Make a new input packet with updated payload.
std::string new_payload = packet.payload();
if (!RE2::Replace(&new_payload, *kTestPacketIdRegexp,
MakeTestPacketTagFromUniqueId(new_tag))) {
return gutil::InvalidArgumentErrorBuilder()
<< "Test packets must contain a tag of the form '"
<< kTestPacketIdRegexp->pattern()
<< "' in their payload, but the given packet with payload '"
<< packet.payload() << "' does not:\n"
<< gutil::PrintTextProto(packet);
}
packet.set_payload(new_payload);
bool status;
ASSIGN_OR_RETURN(status, PadPacketToMinimumSize(packet),
_.LogError() << "Failed to pad packet for tag: " << new_tag);
ASSIGN_OR_RETURN(status, UpdateAllComputedFields(packet),
_.LogError()
<< "Failed to update payload for tag: " << new_tag);

return absl::OkStatus();
}

// Returns a serialization of the given `packet` as a hexstring.
absl::StatusOr<std::string> SerializeAsHexString(
const packetlib::Packet& packet) {
ASSIGN_OR_RETURN(std::string serialized_packet,
packetlib::RawSerializePacket(packet),
_ << " where packet = " << packet.DebugString());
return absl::BytesToHexString(serialized_packet);
}

absl::Status UpdateTestTag(PacketTestVector& packet_test_vector, int new_tag) {
// Updates the payload of the SwitchInput.
dvaas::Packet& input_packet =
*packet_test_vector.mutable_input()->mutable_packet();
RETURN_IF_ERROR(UpdateTestTag(*input_packet.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string input_packet_updated_hexstr,
SerializeAsHexString(input_packet.parsed()));
input_packet.set_hex(input_packet_updated_hexstr);

// Update the payload of the SwitchOutput.
for (SwitchOutput& output_packet :
*packet_test_vector.mutable_acceptable_outputs()) {
for (dvaas::Packet& packet_out : *output_packet.mutable_packets()) {
RETURN_IF_ERROR(UpdateTestTag(*packet_out.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string packet_out_updated_hexstr,
SerializeAsHexString(packet_out.parsed()));
packet_out.set_hex(packet_out_updated_hexstr);
}
for (dvaas::PacketIn& packet_in : *output_packet.mutable_packet_ins()) {
RETURN_IF_ERROR(UpdateTestTag(*packet_in.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string packet_in_updated_hexstr,
SerializeAsHexString(packet_in.parsed()));
packet_in.set_hex(packet_in_updated_hexstr);
}
}
return absl::OkStatus();
}

} // namespace dvaas
19 changes: 10 additions & 9 deletions dvaas/test_vector.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PINS_TESTS_FORWARDING_TEST_VECTOR_H_
#define PINS_TESTS_FORWARDING_TEST_VECTOR_H_
#ifndef PINS_INFRA_TESTS_FORWARDING_TEST_VECTOR_H_
#define PINS_INFRA_TESTS_FORWARDING_TEST_VECTOR_H_

#include <ostream>
#include <string>
#include <vector>

#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include "dvaas/test_vector.pb.h"
#include "google/protobuf/descriptor.h"
#include "p4_pdpi/ir.pb.h"
#include "re2/re2.h"

namespace dvaas {

Expand All @@ -47,8 +43,13 @@ absl::StatusOr<int> ExtractTestPacketTag(const packetlib::Packet& packet);
// Needed to make gUnit produce human-readable output in open source.
std::ostream& operator<<(std::ostream& os, const SwitchOutput& output);

// Updates the test tag (to `new_tag`) and all computed fields of all packets
// (input, acceptable outputs) in the given `packet_test_vectr`. Returns an
// error if the packets are not already tagged.
absl::Status UpdateTestTag(PacketTestVector& packet_test_vector, int new_tag);

using PacketTestVectorById = absl::btree_map<int, PacketTestVector>;

} // namespace dvaas

#endif // PINS_TESTS_FORWARDING_TEST_VECTOR_H_
#endif // PINS_INFRA_TESTS_FORWARDING_TEST_VECTOR_H_
Loading

0 comments on commit 76f1109

Please sign in to comment.