Skip to content

Commit

Permalink
Remove outdated conformance/fuzz tests.
Browse files Browse the repository at this point in the history
These are very old unit-tests that appear to have been written to provide some conformance and fuzzing capabilities before we had more comprehensive solutions.  Today, they should be already covered by our conformance tests and fuzz tests and create unnecessary noise.

The goal of this change is to remove the checked-in binary golden data that we no longer know how to regenerate.  There is a lot of nearby code that could likely also be cleaned up in a follow-up

PiperOrigin-RevId: 667604310
  • Loading branch information
mkruskal-google authored and copybara-github committed Aug 26, 2024
1 parent beb4bdb commit 0d9baf3
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.google.protobuf.CodedOutputStream.OutOfSpaceException;
import protobuf_unittest.UnittestProto.SparseEnumMessage;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestPackedTypes;
import protobuf_unittest.UnittestProto.TestSparseEnum;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -402,47 +401,6 @@ public void computeTagSize() {
assertThat(CodedOutputStream.computeTagSize((1 << 30) + 1)).isEqualTo(1);
}

/** Tests writing a whole message with every field type. */
@Test
public void testWriteWholeMessage() throws Exception {
final byte[] expectedBytes = TestUtil.getGoldenMessage().toByteArray();
TestAllTypes message = TestUtil.getAllSet();

for (OutputType outputType : OutputType.values()) {
Coder coder = outputType.newCoder(message.getSerializedSize());
message.writeTo(coder.stream());
coder.stream().flush();
byte[] rawBytes = coder.toByteArray();
assertEqualBytes(outputType, expectedBytes, rawBytes);
}

// Try different block sizes.
for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
Coder coder = OutputType.STREAM.newCoder(blockSize);
message.writeTo(coder.stream());
coder.stream().flush();
assertEqualBytes(OutputType.STREAM, expectedBytes, coder.toByteArray());
}
}

/**
* Tests writing a whole message with every packed field type. Ensures the wire format of packed
* fields is compatible with C++.
*/
@Test
public void testWriteWholePackedFieldsMessage() throws Exception {
byte[] expectedBytes = TestUtil.getGoldenPackedFieldsMessage().toByteArray();
TestPackedTypes message = TestUtil.getPackedSet();

for (OutputType outputType : OutputType.values()) {
Coder coder = outputType.newCoder(message.getSerializedSize());
message.writeTo(coder.stream());
coder.stream().flush();
byte[] rawBytes = coder.toByteArray();
assertEqualBytes(outputType, expectedBytes, rawBytes);
}
}

/**
* Test writing a message containing a negative enum value. This used to fail because the size was
* not properly computed as a sign-extended varint.
Expand Down
47 changes: 0 additions & 47 deletions java/core/src/test/java/com/google/protobuf/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@
import protobuf_unittest.UnittestProto.TestUnpackedTypes;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -3819,53 +3818,7 @@ public static ByteString readBytesFromFile(String filename) {
throw new IllegalArgumentException("Couldn't read file: " + fullPath.getPath(), e);
}
}
// END FULL-RUNTIME

private static ByteString readBytesFromResource(String name) {
try {
InputStream in = TestUtil.class.getResourceAsStream(name);
if (in == null) { //
throw new RuntimeException("Tests data file " + name + " is missing.");
}
return ByteString.readFrom(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Get the bytes of the "golden message". This is a serialized TestAllTypes with all fields set as
* they would be by {@link #setAllFields(TestAllTypes.Builder)}, but it is loaded from a file on
* disk rather than generated dynamically. The file is actually generated by C++ code, so testing
* against it verifies compatibility with C++.
*/
public static ByteString getGoldenMessage() {
if (goldenMessage == null) {
goldenMessage =
readBytesFromResource("/google/protobuf/testdata/golden_message_oneof_implemented");
}
return goldenMessage;
}

private static ByteString goldenMessage = null;

/**
* Get the bytes of the "golden packed fields message". This is a serialized TestPackedTypes with
* all fields set as they would be by {@link #setPackedFields(TestPackedTypes.Builder)}, but it is
* loaded from a file on disk rather than generated dynamically. The file is actually generated by
* C++ code, so testing against it verifies compatibility with C++.
*/
public static ByteString getGoldenPackedFieldsMessage() {
if (goldenPackedFieldsMessage == null) {
goldenPackedFieldsMessage =
readBytesFromResource("/google/protobuf/testdata/golden_packed_fields_message");
}
return goldenPackedFieldsMessage;
}

private static ByteString goldenPackedFieldsMessage = null;

// BEGIN FULL-RUNTIME
/**
* Mock implementation of {@link GeneratedMessage.BuilderParent} for testing.
*
Expand Down
76 changes: 3 additions & 73 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,48 +65,6 @@ def testBadUtf8String(self, message_module):
message_module.TestAllTypes.FromString(bad_utf8_data)
self.assertIn('TestAllTypes.optional_string', str(context.exception))

def testGoldenMessage(self, message_module):
# Proto3 doesn't have the "default_foo" members or foreign enums,
# and doesn't preserve unknown fields, so for proto3 we use a golden
# message that doesn't have these fields set.
if message_module is unittest_pb2:
golden_data = test_util.GoldenFileData('golden_message_oneof_implemented')
else:
golden_data = test_util.GoldenFileData('golden_message_proto3')

golden_message = message_module.TestAllTypes()
golden_message.ParseFromString(golden_data)
if message_module is unittest_pb2:
test_util.ExpectAllFieldsSet(self, golden_message)
self.assertEqual(golden_data, golden_message.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())

def testGoldenMessageBytearray(self, message_module):
# bytearray was broken, test that it works again
if message_module is unittest_pb2:
golden_data = test_util.GoldenFileData('golden_message_oneof_implemented')
else:
golden_data = test_util.GoldenFileData('golden_message_proto3')

golden_message = message_module.TestAllTypes()
golden_message.ParseFromString(bytearray(golden_data))
if message_module is unittest_pb2:
test_util.ExpectAllFieldsSet(self, golden_message)
self.assertEqual(golden_data, golden_message.SerializeToString())

def testGoldenPackedMessage(self, message_module):
golden_data = test_util.GoldenFileData('golden_packed_fields_message')
golden_message = message_module.TestPackedTypes()
parsed_bytes = golden_message.ParseFromString(golden_data)
all_set = message_module.TestPackedTypes()
test_util.SetAllPackedFields(all_set)
self.assertEqual(parsed_bytes, len(golden_data))
self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, all_set.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())

def testParseErrors(self, message_module):
msg = message_module.TestAllTypes()
self.assertRaises(TypeError, msg.FromString, 0)
Expand Down Expand Up @@ -162,7 +120,9 @@ def __bool__(self):
golden_message.SerializeToString(deterministic=BadArg())

def testPickleSupport(self, message_module):
golden_data = test_util.GoldenFileData('golden_message')
golden_message = message_module.TestAllTypes()
test_util.SetAllFields(golden_message)
golden_data = golden_message.SerializeToString()
golden_message = message_module.TestAllTypes()
golden_message.ParseFromString(golden_data)
pickled_message = pickle.dumps(golden_message)
Expand Down Expand Up @@ -1537,36 +1497,6 @@ def testCopyFromAllPackedExtensions(self):
all_set.Extensions[unittest_pb2.packed_float_extension].extend([61.0, 71.0])
self.assertNotEqual(all_set, copy)

def testGoldenExtensions(self):
golden_data = test_util.GoldenFileData('golden_message')
golden_message = unittest_pb2.TestAllExtensions()
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(all_set)
self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, golden_message.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_message, golden_copy)
# Depend on a specific serialization order for extensions is not
# reasonable to guarantee.
if api_implementation.Type() != 'upb':
self.assertEqual(golden_data, golden_copy.SerializeToString())

def testGoldenPackedExtensions(self):
golden_data = test_util.GoldenFileData('golden_packed_fields_message')
golden_message = unittest_pb2.TestPackedExtensions()
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestPackedExtensions()
test_util.SetAllPackedExtensions(all_set)
self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, all_set.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_message, golden_copy)
# Depend on a specific serialization order for extensions is not
# reasonable to guarantee.
if api_implementation.Type() != 'upb':
self.assertEqual(golden_data, golden_copy.SerializeToString())

def testPickleIncompleteProto(self):
golden_message = unittest_pb2.TestRequired(a=1)
pickled_message = pickle.dumps(golden_message)
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,6 @@ cc_test(
"message_unittest.inc",
"message_unittest_legacy_apis.inc",
],
data = [":testdata"],
deps = [
":arena",
":cc_lite_test_protos",
Expand Down Expand Up @@ -1648,7 +1647,6 @@ cc_test(
"edition_message_unittest.cc",
"message_unittest.inc",
],
data = [":testdata"],
deps = [
":arena",
":cc_test_protos",
Expand Down
40 changes: 0 additions & 40 deletions src/google/protobuf/arena_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,6 @@

namespace google {
namespace protobuf {

template <typename T, bool use_arena>
void TestParseCorruptedString(const T& message) {
int success_count = 0;
std::string s;
{
// Map order is not deterministic. To make the test deterministic we want
// to serialize the proto deterministically.
io::StringOutputStream output(&s);
io::CodedOutputStream out(&output);
out.SetSerializationDeterministic(true);
message.SerializePartialToCodedStream(&out);
}
#if defined(PROTOBUF_ASAN) || defined(PROTOBUF_TSAN) || defined(PROTOBUF_MSAN)
// Make the test smaller in sanitizer mode.
const int kMaxIters = 200;
#else
const int kMaxIters = 900;
#endif
const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters;
const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2;
for (int i = start; i < s.size(); i += stride) {
for (int c = 1 + (i % 17); c < 256; c += 2 * c + (i & 3)) {
s[i] ^= c;
Arena arena;
T* message = Arena::Create<T>(use_arena ? &arena : nullptr);
if (message->ParseFromString(s)) {
++success_count;
}
if (!use_arena) {
delete message;
}
s[i] ^= c; // Restore s to its original state.
}
}
// This next line is a low bar. But getting through the test without crashing
// due to use-after-free or other bugs is a big part of what we're checking.
ABSL_CHECK_GT(success_count, 0);
}

namespace internal {

struct ArenaTestPeer {
Expand Down
7 changes: 0 additions & 7 deletions src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1607,13 +1607,6 @@ TEST(ArenaTest, NoHeapAllocationsTest) {
arena.Reset();
}

TEST(ArenaTest, ParseCorruptedString) {
TestAllTypes message;
TestUtil::SetAllFields(&message);
TestParseCorruptedString<TestAllTypes, true>(message);
TestParseCorruptedString<TestAllTypes, false>(message);
}

#if PROTOBUF_RTTI
// Test construction on an arena via generic MessageLite interface. We should be
// able to successfully deserialize on the arena without incurring heap
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ cc_test(
"//src/google/protobuf:cc_test_protos",
"//src/google/protobuf:port",
"//src/google/protobuf:test_textproto",
"//src/google/protobuf:test_util",
"//src/google/protobuf:test_util2",
"//src/google/protobuf/compiler/cpp:names",
"//src/google/protobuf/io",
Expand Down
28 changes: 20 additions & 8 deletions src/google/protobuf/compiler/command_line_interface_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "google/protobuf/compiler/mock_code_generator.h"
#include "google/protobuf/compiler/plugin.pb.h"
#include "google/protobuf/test_textproto.h"
#include "google/protobuf/test_util.h"
#include "google/protobuf/test_util2.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
Expand Down Expand Up @@ -4202,7 +4203,16 @@ class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> {
std::string unittest_proto_descriptor_set_filename_;
};

static void WriteGoldenMessage(const std::string& filename) {
protobuf_unittest::TestAllTypes message;
TestUtil::SetAllFields(&message);
std::string golden = message.SerializeAsString();
ABSL_CHECK_OK(File::SetContents(filename, golden, true));
}

TEST_P(EncodeDecodeTest, Encode) {
std::string golden_path = absl::StrCat(TestTempDir(), "/golden_message");
WriteGoldenMessage(golden_path);
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"));
Expand All @@ -4212,14 +4222,14 @@ TEST_P(EncodeDecodeTest, Encode) {
}
EXPECT_TRUE(
Run(absl::StrCat(args, " --encode=protobuf_unittest.TestAllTypes")));
ExpectStdoutMatchesBinaryFile(TestUtil::GetTestDataPath(
"google/protobuf/testdata/golden_message_oneof_implemented"));
ExpectStdoutMatchesBinaryFile(golden_path);
ExpectStderrMatchesText("");
}

TEST_P(EncodeDecodeTest, Decode) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"google/protobuf/testdata/golden_message_oneof_implemented"));
std::string golden_path = absl::StrCat(TestTempDir(), "/golden_message");
WriteGoldenMessage(golden_path);
RedirectStdinFromFile(golden_path);
EXPECT_TRUE(
Run("google/protobuf/unittest.proto"
" --decode=protobuf_unittest.TestAllTypes"));
Expand Down Expand Up @@ -4272,6 +4282,8 @@ TEST_P(EncodeDecodeTest, ProtoParseError) {
}

TEST_P(EncodeDecodeTest, EncodeDeterministicOutput) {
std::string golden_path = absl::StrCat(TestTempDir(), "/golden_message");
WriteGoldenMessage(golden_path);
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"));
Expand All @@ -4281,14 +4293,14 @@ TEST_P(EncodeDecodeTest, EncodeDeterministicOutput) {
}
EXPECT_TRUE(Run(absl::StrCat(
args, " --encode=protobuf_unittest.TestAllTypes --deterministic_output")));
ExpectStdoutMatchesBinaryFile(TestUtil::GetTestDataPath(
"google/protobuf/testdata/golden_message_oneof_implemented"));
ExpectStdoutMatchesBinaryFile(golden_path);
ExpectStderrMatchesText("");
}

TEST_P(EncodeDecodeTest, DecodeDeterministicOutput) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"google/protobuf/testdata/golden_message_oneof_implemented"));
std::string golden_path = absl::StrCat(TestTempDir(), "/golden_message");
WriteGoldenMessage(golden_path);
RedirectStdinFromFile(golden_path);
EXPECT_FALSE(
Run("google/protobuf/unittest.proto"
" --decode=protobuf_unittest.TestAllTypes --deterministic_output"));
Expand Down
4 changes: 1 addition & 3 deletions src/google/protobuf/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ cc_test(
"zero_copy_stream_unittest.cc",
],
copts = COPTS,
data = [
"//src/google/protobuf:testdata",
],
deps = [
":gzip_stream",
":io",
Expand All @@ -204,6 +201,7 @@ cc_test(
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf:test_util",
"//src/google/protobuf:test_util2",
"//src/google/protobuf/stubs",
"//src/google/protobuf/testing",
Expand Down
Loading

0 comments on commit 0d9baf3

Please sign in to comment.