blob: 9c3de92e19c83b8e1055cf5a714c84af7784c68c [file]
// Protocol Buffers - Google's data interchange format
// Copyright 2026 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/strings/string_view.h"
#include "rust/test/shared/utf8/feature_verify.pb.h"
#include "rust/test/shared/utf8/no_features_proto2.pb.h"
#include "rust/test/shared/utf8/no_features_proto3.pb.h"
namespace {
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::Not;
// We use 0b1000_0000, since 0b1XXX_XXXX in UTF-8 denotes a byte 2-4, but never
// the first byte.
constexpr char kInvalidUtf8[] = "\x80";
TEST(Utf8Test, TestProto2) {
utf8::NoFeaturesProto2 no_features_proto2;
// No error on setter.
no_features_proto2.set_my_field(kInvalidUtf8);
EXPECT_THAT(no_features_proto2.my_field(),
Eq(absl::string_view(kInvalidUtf8)));
// No error on serialization.
std::string serialized_nonutf8 = no_features_proto2.SerializeAsString();
EXPECT_THAT(serialized_nonutf8, Not(IsEmpty()));
// No error on parsing.
utf8::NoFeaturesProto2 parsed;
EXPECT_THAT(parsed.ParseFromString(serialized_nonutf8), Eq(true));
}
TEST(Utf8Test, TestProto3) {
utf8::NoFeaturesProto3 no_features_proto3;
// No error on setter.
no_features_proto3.set_my_field(kInvalidUtf8);
EXPECT_THAT(no_features_proto3.my_field(),
Eq(absl::string_view(kInvalidUtf8)));
// No error on serialization.
std::string serialized_nonutf8 = no_features_proto3.SerializeAsString();
EXPECT_THAT(serialized_nonutf8, Not(IsEmpty()));
// Error on parsing.
utf8::NoFeaturesProto3 parsed;
EXPECT_THAT(parsed.ParseFromString(serialized_nonutf8), Eq(false));
}
TEST(Utf8Test, TestEditionsVerify) {
utf8::Verify verify;
// No error on setter.
verify.set_my_field(kInvalidUtf8);
EXPECT_THAT(verify.my_field(), Eq(absl::string_view(kInvalidUtf8)));
// No error on serialization.
std::string serialized_nonutf8 = verify.SerializeAsString();
EXPECT_THAT(serialized_nonutf8, Not(IsEmpty()));
// Error on parsing.
utf8::Verify parsed;
EXPECT_THAT(parsed.ParseFromString(serialized_nonutf8), Eq(false));
}
} // namespace