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

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

Issue 2602103002: Delete blimp/helium and remove references to it from dependent targets (Closed)
Patch Set: . Created 3 years, 11 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/stream_helpers.cc ('k') | blimp/helium/sync_manager.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/stream_helpers.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10
11 #include "base/macros.h"
12 #include "blimp/helium/version_vector.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
15 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite .h"
16
17 namespace blimp {
18 namespace helium {
19 namespace {
20
21 class StreamHelpersTest : public testing::Test {
22 public:
23 StreamHelpersTest() {}
24 ~StreamHelpersTest() override = default;
25
26 protected:
27 template <class T>
28 void SerializeAndDeserialize(const std::vector<T>& input) {
29 std::string changeset;
30 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
31 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
32
33 for (size_t i = 0; i < input.size(); ++i) {
34 WriteToStream(input[i], &output_stream);
35 }
36
37 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
38 changeset.size());
39 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
40
41 for (size_t i = 0; i < input.size(); ++i) {
42 CheckCorrectDeserialization<T>(&input_stream, input[i]);
43 }
44 }
45
46 template <class T>
47 void CheckCorrectDeserialization(
48 google::protobuf::io::CodedInputStream* input_stream,
49 T expected) {
50 T output;
51 EXPECT_TRUE(ReadFromStream(input_stream, &output));
52 EXPECT_EQ(expected, output);
53 }
54
55 private:
56 DISALLOW_COPY_AND_ASSIGN(StreamHelpersTest);
57 };
58
59 template <>
60 void StreamHelpersTest::CheckCorrectDeserialization<VersionVector>(
61 google::protobuf::io::CodedInputStream* input_stream,
62 VersionVector expected) {
63 VersionVector output;
64 EXPECT_TRUE(ReadFromStream(input_stream, &output));
65 EXPECT_EQ(VersionVector::Comparison::EqualTo, expected.CompareTo(output));
66 }
67
68 TEST_F(StreamHelpersTest, 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 WriteToStream(345, &output_stream);
74 WriteToStream("some string", &output_stream);
75 WriteToStream(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(StreamHelpersTest, Int32) {
88 SerializeAndDeserialize<int32_t>({123, 0, -456, 987});
89 }
90
91 TEST_F(StreamHelpersTest, String) {
92 SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"});
93 }
94
95 TEST_F(StreamHelpersTest, UInt64) {
96 SerializeAndDeserialize<uint64_t>({123llu, 456llu, 1llu << 63});
97 }
98
99 TEST_F(StreamHelpersTest, MapWithTwoKeys) {
100 std::map<int, int> expected = {{1, 2}, {3, 4}};
101
102 std::string changeset;
103 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
104 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
105 WriteToStream(expected, &output_stream);
106 output_stream.Trim();
107
108 std::map<int, int> actual;
109 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
110 changeset.size());
111 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
112 ASSERT_TRUE(ReadFromStream(&input_stream, &actual));
113
114 EXPECT_EQ(2, actual[1]);
115 EXPECT_EQ(4, actual[3]);
116
117 EXPECT_EQ(changeset.size(),
118 static_cast<size_t>(input_stream.CurrentPosition()));
119 }
120
121 TEST_F(StreamHelpersTest, InvalidMapWithDupeKeys) {
122 std::string changeset;
123 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
124 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
125 output_stream.WriteVarint32(2);
126 output_stream.WriteVarint32(1);
127 output_stream.WriteVarint32(2);
128 output_stream.WriteVarint32(1);
129 output_stream.WriteVarint32(4);
130
131 std::map<int, int> actual;
132 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
133 changeset.size());
134 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
135 ASSERT_FALSE(ReadFromStream(&input_stream, &actual));
136 }
137
138 TEST_F(StreamHelpersTest, EmptyMap) {
139 std::string changeset;
140 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
141 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
142 WriteToStream(std::map<int, int>(), &output_stream);
143
144 std::map<int, int> actual;
145 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
146 changeset.size());
147 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
148 ASSERT_TRUE(ReadFromStream(&input_stream, &actual));
149
150 EXPECT_TRUE(actual.empty());
151 }
152
153 TEST_F(StreamHelpersTest, FailsToDeserializeStringGivenOutrageousLength) {
154 std::string changeset;
155 google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
156 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
157 output_stream.WriteVarint32(20 << 20); // 20MB, greater than kMaxStringLength
158 output_stream.WriteString("This string is 20MB, I swear!");
159
160 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), 1);
161 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
162
163 std::string output;
164 EXPECT_FALSE(ReadFromStream(&input_stream, &output));
165 }
166
167 TEST_F(StreamHelpersTest, SerializesVersionVector) {
168 SerializeAndDeserialize<VersionVector>(
169 {VersionVector(0, 0), VersionVector(3, 1), VersionVector(0, 4),
170 VersionVector(3, 0)});
171 }
172
173 } // namespace
174 } // namespace helium
175 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/helium/stream_helpers.cc ('k') | blimp/helium/sync_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698