Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/helium/syncable_primitive_serializer.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "blimp/helium/version_vector.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 14 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite .h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace helium { | |
| 18 namespace { | |
| 19 | |
| 20 class SyncablePrimitiveSerializerTest : public testing::Test { | |
| 21 public: | |
| 22 SyncablePrimitiveSerializerTest() {} | |
| 23 ~SyncablePrimitiveSerializerTest() override = default; | |
| 24 | |
| 25 protected: | |
| 26 template <class T> | |
| 27 void SerializeAndDeserialize(const std::vector<T>& input) { | |
| 28 std::string changeset; | |
| 29 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); | |
| 30 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); | |
| 31 | |
| 32 for (size_t i = 0; i < input.size(); ++i) { | |
| 33 SyncablePrimitiveSerializer::Serialize(input[i], &output_stream); | |
| 34 } | |
| 35 | |
| 36 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), | |
| 37 changeset.size()); | |
| 38 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); | |
| 39 | |
| 40 for (size_t i = 0; i < input.size(); ++i) { | |
| 41 CheckCorrectDeserialization<T>(&input_stream, input[i]); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 template <class T> | |
| 46 void CheckCorrectDeserialization( | |
| 47 google::protobuf::io::CodedInputStream* input_stream, | |
| 48 T expected) { | |
| 49 T output; | |
| 50 EXPECT_TRUE( | |
| 51 SyncablePrimitiveSerializer::Deserialize(input_stream, &output)); | |
| 52 EXPECT_EQ(expected, output); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 DISALLOW_COPY_AND_ASSIGN(SyncablePrimitiveSerializerTest); | |
| 57 }; | |
| 58 | |
| 59 template <> | |
| 60 void SyncablePrimitiveSerializerTest::CheckCorrectDeserialization< | |
| 61 VersionVector>(google::protobuf::io::CodedInputStream* input_stream, | |
| 62 VersionVector expected) { | |
| 63 VersionVector output; | |
| 64 EXPECT_TRUE(SyncablePrimitiveSerializer::Deserialize(input_stream, &output)); | |
| 65 EXPECT_EQ(VersionVector::Comparison::EqualTo, expected.CompareTo(output)); | |
| 66 } | |
| 67 | |
| 68 TEST_F(SyncablePrimitiveSerializerTest, SerializesMixedTypes) { | |
| 69 std::string changeset; | |
| 70 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); | |
| 71 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); | |
| 72 | |
| 73 SyncablePrimitiveSerializer::Serialize(345, &output_stream); | |
| 74 SyncablePrimitiveSerializer::Serialize("some string", &output_stream); | |
| 75 SyncablePrimitiveSerializer::Serialize(VersionVector(12, 13), &output_stream); | |
| 76 | |
| 77 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), | |
| 78 changeset.size()); | |
| 79 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); | |
| 80 | |
| 81 CheckCorrectDeserialization<int32_t>(&input_stream, 345); | |
| 82 CheckCorrectDeserialization<std::string>(&input_stream, "some string"); | |
| 83 CheckCorrectDeserialization<VersionVector>(&input_stream, | |
| 84 VersionVector(12, 13)); | |
| 85 } | |
| 86 | |
| 87 TEST_F(SyncablePrimitiveSerializerTest, SerializesInt32) { | |
| 88 SerializeAndDeserialize<int32_t>({123, 0, -456, 987}); | |
| 89 } | |
| 90 | |
| 91 TEST_F(SyncablePrimitiveSerializerTest, SerializesString) { | |
| 92 SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"}); | |
| 93 } | |
| 94 | |
| 95 TEST_F(SyncablePrimitiveSerializerTest, | |
| 96 FailsToDeserializeStringGivenOutrageousLength) { | |
| 97 std::string changeset; | |
| 98 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); | |
| 99 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); | |
| 100 output_stream.WriteVarint32(20 << 20); // 20MB | |
|
Kevin M
2016/10/20 00:38:25
"20MB, equal to kMaxStringLength"
steimel
2016/10/20 00:57:38
Done, but with "greater than", since kMax is 10MB
| |
| 101 output_stream.WriteString("This string is 20MB, I swear!"); | |
| 102 | |
| 103 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), | |
| 104 changeset.size()); | |
| 105 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); | |
| 106 | |
| 107 std::string output; | |
| 108 EXPECT_FALSE( | |
| 109 SyncablePrimitiveSerializer::Deserialize(&input_stream, &output)); | |
| 110 } | |
| 111 | |
| 112 TEST_F(SyncablePrimitiveSerializerTest, SerializesVersionVector) { | |
| 113 SerializeAndDeserialize<VersionVector>( | |
| 114 {VersionVector(0, 0), VersionVector(3, 1), VersionVector(0, 4), | |
| 115 VersionVector(3, 0)}); | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 } // namespace helium | |
| 120 } // namespace blimp | |
| OLD | NEW |