| 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 "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| 12 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" |
| 13 |
| 14 namespace blimp { |
| 15 namespace helium { |
| 16 namespace { |
| 17 |
| 18 class SyncablePrimitiveSerializerTest : public testing::Test { |
| 19 protected: |
| 20 template <class T> |
| 21 void SerializeAndDeserialize(const std::vector<T>& input) { |
| 22 std::string changeset; |
| 23 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); |
| 24 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); |
| 25 |
| 26 for (size_t i = 0; i < input.size(); ++i) { |
| 27 SyncablePrimitiveSerializer::Serialize(input[i], &output_stream); |
| 28 } |
| 29 |
| 30 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), |
| 31 changeset.size()); |
| 32 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); |
| 33 |
| 34 for (size_t i = 0; i < input.size(); ++i) { |
| 35 T output; |
| 36 EXPECT_TRUE( |
| 37 SyncablePrimitiveSerializer::Deserialize(&input_stream, &output)); |
| 38 EXPECT_EQ(input[i], output); |
| 39 } |
| 40 } |
| 41 }; |
| 42 |
| 43 TEST_F(SyncablePrimitiveSerializerTest, SerializesInt32) { |
| 44 SerializeAndDeserialize<int32_t>({123, 0, -456, 987}); |
| 45 } |
| 46 |
| 47 TEST_F(SyncablePrimitiveSerializerTest, SerializesString) { |
| 48 SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"}); |
| 49 } |
| 50 |
| 51 TEST_F(SyncablePrimitiveSerializerTest, |
| 52 FailsToDeserializeStringGivenOutrageousLength) { |
| 53 std::string changeset; |
| 54 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); |
| 55 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); |
| 56 output_stream.WriteVarint32(20 << 20); // 20MB |
| 57 output_stream.WriteString("This string is 20MB, I swear!"); |
| 58 |
| 59 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), |
| 60 changeset.size()); |
| 61 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); |
| 62 |
| 63 std::string output; |
| 64 EXPECT_FALSE( |
| 65 SyncablePrimitiveSerializer::Deserialize(&input_stream, &output)); |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 } // namespace helium |
| 70 } // namespace blimp |
| OLD | NEW |