| Index: blimp/helium/syncable_primitive_serializer_unittest.cc
|
| diff --git a/blimp/helium/syncable_primitive_serializer_unittest.cc b/blimp/helium/syncable_primitive_serializer_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bfbffe9ca90911e67539d48cd2dd002705a84a62
|
| --- /dev/null
|
| +++ b/blimp/helium/syncable_primitive_serializer_unittest.cc
|
| @@ -0,0 +1,70 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "blimp/helium/syncable_primitive_serializer.h"
|
| +
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
|
| +#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h"
|
| +
|
| +namespace blimp {
|
| +namespace helium {
|
| +namespace {
|
| +
|
| +class SyncablePrimitiveSerializerTest : public testing::Test {
|
| + protected:
|
| + template <class T>
|
| + void SerializeAndDeserialize(const std::vector<T>& input) {
|
| + std::string changeset;
|
| + google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
|
| + google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
|
| +
|
| + for (size_t i = 0; i < input.size(); ++i) {
|
| + SyncablePrimitiveSerializer::Serialize(input[i], &output_stream);
|
| + }
|
| +
|
| + google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
|
| + changeset.size());
|
| + google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
|
| +
|
| + for (size_t i = 0; i < input.size(); ++i) {
|
| + T output;
|
| + EXPECT_TRUE(
|
| + SyncablePrimitiveSerializer::Deserialize(&input_stream, &output));
|
| + EXPECT_EQ(input[i], output);
|
| + }
|
| + }
|
| +};
|
| +
|
| +TEST_F(SyncablePrimitiveSerializerTest, SerializesInt32) {
|
| + SerializeAndDeserialize<int32_t>({123, 0, -456, 987});
|
| +}
|
| +
|
| +TEST_F(SyncablePrimitiveSerializerTest, SerializesString) {
|
| + SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"});
|
| +}
|
| +
|
| +TEST_F(SyncablePrimitiveSerializerTest,
|
| + FailsToDeserializeStringGivenOutrageousLength) {
|
| + std::string changeset;
|
| + google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
|
| + google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
|
| + output_stream.WriteVarint32(20 << 20); // 20MB
|
| + output_stream.WriteString("This string is 20MB, I swear!");
|
| +
|
| + google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
|
| + changeset.size());
|
| + google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
|
| +
|
| + std::string output;
|
| + EXPECT_FALSE(
|
| + SyncablePrimitiveSerializer::Deserialize(&input_stream, &output));
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace helium
|
| +} // namespace blimp
|
|
|