Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: blimp/helium/coded_value_serializer_unittest.cc

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Add comment on max string length constant Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/helium/coded_value_serializer.cc ('k') | blimp/helium/lww_register.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/coded_value_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 CodedValueSerializerTest : public testing::Test {
21 public:
22 CodedValueSerializerTest() {}
23 ~CodedValueSerializerTest() 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 CodedValueSerializer::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(CodedValueSerializer::Deserialize(input_stream, &output));
51 EXPECT_EQ(expected, output);
52 }
53
54 private:
55 DISALLOW_COPY_AND_ASSIGN(CodedValueSerializerTest);
56 };
57
58 template <>
59 void CodedValueSerializerTest::CheckCorrectDeserialization<VersionVector>(
60 google::protobuf::io::CodedInputStream* input_stream,
61 VersionVector expected) {
62 VersionVector output;
63 EXPECT_TRUE(CodedValueSerializer::Deserialize(input_stream, &output));
64 EXPECT_EQ(VersionVector::Comparison::EqualTo, expected.CompareTo(output));
65 }
66
67 TEST_F(CodedValueSerializerTest, SerializesMixedTypes) {
68 std::string changeset;
69 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
70 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
71
72 CodedValueSerializer::Serialize(345, &output_stream);
73 CodedValueSerializer::Serialize("some string", &output_stream);
74 CodedValueSerializer::Serialize(VersionVector(12, 13), &output_stream);
75
76 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
77 changeset.size());
78 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
79
80 CheckCorrectDeserialization<int32_t>(&input_stream, 345);
81 CheckCorrectDeserialization<std::string>(&input_stream, "some string");
82 CheckCorrectDeserialization<VersionVector>(&input_stream,
83 VersionVector(12, 13));
84 }
85
86 TEST_F(CodedValueSerializerTest, SerializesInt32) {
87 SerializeAndDeserialize<int32_t>({123, 0, -456, 987});
88 }
89
90 TEST_F(CodedValueSerializerTest, SerializesString) {
91 SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"});
92 }
93
94 TEST_F(CodedValueSerializerTest,
95 FailsToDeserializeStringGivenOutrageousLength) {
96 std::string changeset;
97 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
98 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
99 output_stream.WriteVarint32(20 << 20); // 20MB, greater than kMaxStringLength
100 output_stream.WriteString("This string is 20MB, I swear!");
101
102 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
103 changeset.size());
104 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
105
106 std::string output;
107 EXPECT_FALSE(CodedValueSerializer::Deserialize(&input_stream, &output));
108 }
109
110 TEST_F(CodedValueSerializerTest, SerializesVersionVector) {
111 SerializeAndDeserialize<VersionVector>(
112 {VersionVector(0, 0), VersionVector(3, 1), VersionVector(0, 4),
113 VersionVector(3, 0)});
114 }
115
116 } // namespace
117 } // namespace helium
118 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/helium/coded_value_serializer.cc ('k') | blimp/helium/lww_register.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698