Do not directly compare byte outputs of serialized Protocol Buffers.
This CL replaces direct comparison of serialized Protocol Buffer messages with
::testing::EqualsProto. This removes the dependency on the exact format of
serialized messages. For example, instead of string-comparison of two serialized
Protocol Buffer messages, it uses ::testing::EqualsProto against two Protocol
Buffer messages.
=======================================================
EXPECT_EQ(a.SerializeAsString(), b.SerializeAsString()); // Before.
EXPECT_THAT(a, ::testing::EqualsProto(b)); // After.
EXPECT_EQ("thisismyserializedproto", b.SerializeAsString()); // Before
EXPECT_THAT("thisismyserializedproto",
::testing::proto::WhenDeserialized(::testing::EqualsProto(b))); // After
=======================================================
Directly comparing serialized Protocol Buffer messages are not reliable because
Protocol Buffer serialization may vary across software versions, schema changes,
languages, and even within the same program. Using ::testing::EqualsProto
makes the tests more robust. Also, this change ensures that the affected tests
continue to work with any future implementation change in Protocol Buffer
serialization.
PiperOrigin-RevId: 240628307
Change-Id: I7dfd4e1e0b79dedb84603824d3da187fda2e67a9
diff --git a/quic/core/crypto/quic_crypto_server_config_test.cc b/quic/core/crypto/quic_crypto_server_config_test.cc
index 7116993..2de5b7a 100644
--- a/quic/core/crypto/quic_crypto_server_config_test.cc
+++ b/quic/core/crypto/quic_crypto_server_config_test.cc
@@ -9,6 +9,7 @@
#include <memory>
#include <string>
+#include "testing/gmock/include/gmock/gmock.h"
#include "net/third_party/quiche/src/quic/core/crypto/cert_compressor.h"
#include "net/third_party/quiche/src/quic/core/crypto/chacha20_poly1305_encrypter.h"
#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h"
@@ -25,6 +26,8 @@
namespace quic {
namespace test {
+using ::testing::EqualsProto;
+using ::testing::Not;
class QuicCryptoServerConfigTest : public QuicTest {};
@@ -259,12 +262,12 @@
NewSourceAddressToken(kPrimary, ip4_, &cached_network_params_input);
CachedNetworkParameters cached_network_params_output;
- EXPECT_NE(cached_network_params_output.SerializeAsString(),
- cached_network_params_input.SerializeAsString());
+ EXPECT_THAT(cached_network_params_output,
+ Not(EqualsProto(cached_network_params_input)));
ValidateSourceAddressTokens(kPrimary, token4_with_cached_network_params, ip4_,
&cached_network_params_output);
- EXPECT_EQ(cached_network_params_output.SerializeAsString(),
- cached_network_params_input.SerializeAsString());
+ EXPECT_THAT(cached_network_params_output,
+ EqualsProto(cached_network_params_input));
}
// Test the ability for a source address token to be valid for multiple