Skip to content

Commit

Permalink
[gutil] Disallow empty semantic version string. (sonic-net#181)
Browse files Browse the repository at this point in the history
Co-authored-by: smolkaj <[email protected]>
Co-authored-by: kishanps <[email protected]>
  • Loading branch information
3 people authored Jun 7, 2024
1 parent 46ab725 commit aa191e2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 0 additions & 2 deletions gutil/version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ absl::StatusOr<Version> ParseVersion(absl::string_view version_string) {
if (!RE2::FullMatch(version_string, *kSemanticVersionRegex,
&version.major_version, &version.minor_version,
&version.patch_version)) {
// TODO: Remove this hack once all P4Infos have versions.
if (version_string.empty()) return Version{0, 0, 0};
return gutil::InvalidArgumentErrorBuilder()
<< "unable to parse '" << version_string
<< "' as a semantic version string; expected string of the form "
Expand Down
24 changes: 11 additions & 13 deletions gutil/version_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

namespace gutil {
namespace {
using gutil::IsOkAndHolds;
using testing::Eq;

using ::gutil::IsOkAndHolds;
using ::gutil::StatusIs;
using ::testing::Eq;

bool RoundTrips(const Version& version) {
absl::StatusOr<Version> roundtripped_version =
Expand All @@ -39,17 +41,18 @@ TEST(ParseVersionTest, PositiveExamples) {

TEST(ParseVersionTest, NegativeExamples) {
EXPECT_THAT(ParseVersion("100"),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion("1.1"),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion("1.1.1."),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion("1.1.1.1"),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion("1.1.1,0"),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion("hello"),
gutil::StatusIs(absl::StatusCode::kInvalidArgument));
StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(ParseVersion(""), StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST(VersionTest, VersionToStringAndParseVersionRoundTrip) {
Expand All @@ -66,10 +69,5 @@ TEST(ComparisonTest, OrderingIsLexicographic) {
EXPECT_GT((Version{10, 0, 0}), (Version{2, 0, 0}));
}

// TODO: Remove this test once all P4Infos have versions.
TEST(ParseVersionTest, EmptyStringForBackwardsCompatibility) {
EXPECT_THAT(ParseVersion(""), IsOkAndHolds(Eq(Version{0, 0, 0})));
}

} // namespace
} // namespace gutil
8 changes: 4 additions & 4 deletions sai_p4/instantiations/google/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ cc_library(
":clos_stage",
":instantiations",
":unioned_p4info_embed",
"//p4_pdpi:ir_cc_proto",
"//sai_p4/tools:p4info_tools",
"@com_github_google_glog//:glog",
"@com_google_protobuf//:protobuf",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"//p4_pdpi:ir_cc_proto",
"//sai_p4/tools:p4info_tools",
"@com_google_protobuf//:protobuf",
],
)

Expand Down

0 comments on commit aa191e2

Please sign in to comment.