• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium OS 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 <brillo/streams/stream_utils.h>
6 
7 #include <limits>
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include <base/bind.h>
13 #include <brillo/message_loops/fake_message_loop.h>
14 #include <brillo/message_loops/message_loop.h>
15 #include <brillo/streams/mock_stream.h>
16 #include <brillo/streams/stream_errors.h>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 using testing::InSequence;
21 using testing::StrictMock;
22 using testing::_;
23 
ACTION_TEMPLATE(InvokeAsyncCallback,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (size))24 ACTION_TEMPLATE(InvokeAsyncCallback,
25                 HAS_1_TEMPLATE_PARAMS(int, k),
26                 AND_1_VALUE_PARAMS(size)) {
27   brillo::MessageLoop::current()->PostTask(
28       FROM_HERE, base::BindOnce(std::get<k>(args), size));
29   return true;
30 }
31 
ACTION_TEMPLATE(InvokeAsyncCallback,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())32 ACTION_TEMPLATE(InvokeAsyncCallback,
33                 HAS_1_TEMPLATE_PARAMS(int, k),
34                 AND_0_VALUE_PARAMS()) {
35   brillo::MessageLoop::current()->PostTask(FROM_HERE, std::get<k>(args));
36   return true;
37 }
38 
ACTION_TEMPLATE(InvokeAsyncErrorCallback,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (code))39 ACTION_TEMPLATE(InvokeAsyncErrorCallback,
40                 HAS_1_TEMPLATE_PARAMS(int, k),
41                 AND_1_VALUE_PARAMS(code)) {
42   brillo::ErrorPtr error;
43   brillo::Error::AddTo(&error, FROM_HERE, "test", code, "message");
44   brillo::MessageLoop::current()->PostTask(
45       FROM_HERE, base::BindOnce(std::get<k>(args),
46                                 base::Owned(error.release())));
47   return true;
48 }
49 
50 namespace brillo {
51 
TEST(StreamUtils,ErrorStreamClosed)52 TEST(StreamUtils, ErrorStreamClosed) {
53   ErrorPtr error;
54   EXPECT_FALSE(stream_utils::ErrorStreamClosed(FROM_HERE, &error));
55   EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
56   EXPECT_EQ(errors::stream::kStreamClosed, error->GetCode());
57   EXPECT_EQ("Stream is closed", error->GetMessage());
58 }
59 
TEST(StreamUtils,ErrorOperationNotSupported)60 TEST(StreamUtils, ErrorOperationNotSupported) {
61   ErrorPtr error;
62   EXPECT_FALSE(stream_utils::ErrorOperationNotSupported(FROM_HERE, &error));
63   EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
64   EXPECT_EQ(errors::stream::kOperationNotSupported, error->GetCode());
65   EXPECT_EQ("Stream operation not supported", error->GetMessage());
66 }
67 
TEST(StreamUtils,ErrorReadPastEndOfStream)68 TEST(StreamUtils, ErrorReadPastEndOfStream) {
69   ErrorPtr error;
70   EXPECT_FALSE(stream_utils::ErrorReadPastEndOfStream(FROM_HERE, &error));
71   EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
72   EXPECT_EQ(errors::stream::kPartialData, error->GetCode());
73   EXPECT_EQ("Reading past the end of stream", error->GetMessage());
74 }
75 
TEST(StreamUtils,CheckInt64Overflow)76 TEST(StreamUtils, CheckInt64Overflow) {
77   const int64_t max_int64 = std::numeric_limits<int64_t>::max();
78   const uint64_t max_uint64 = std::numeric_limits<uint64_t>::max();
79   EXPECT_TRUE(stream_utils::CheckInt64Overflow(FROM_HERE, 0, 0, nullptr));
80   EXPECT_TRUE(stream_utils::CheckInt64Overflow(
81       FROM_HERE, 0, max_int64, nullptr));
82   EXPECT_TRUE(stream_utils::CheckInt64Overflow(
83       FROM_HERE, max_int64, 0, nullptr));
84   EXPECT_TRUE(stream_utils::CheckInt64Overflow(FROM_HERE, 100, -90, nullptr));
85   EXPECT_TRUE(stream_utils::CheckInt64Overflow(
86       FROM_HERE, 1000, -1000, nullptr));
87 
88   ErrorPtr error;
89   EXPECT_FALSE(stream_utils::CheckInt64Overflow(FROM_HERE, 100, -101, &error));
90   EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
91   EXPECT_EQ(errors::stream::kInvalidParameter, error->GetCode());
92   EXPECT_EQ("The stream offset value is out of range", error->GetMessage());
93 
94   EXPECT_FALSE(stream_utils::CheckInt64Overflow(
95       FROM_HERE, max_int64, 1, nullptr));
96   EXPECT_FALSE(stream_utils::CheckInt64Overflow(
97       FROM_HERE, max_uint64, 0, nullptr));
98   EXPECT_FALSE(stream_utils::CheckInt64Overflow(
99       FROM_HERE, max_uint64, max_int64, nullptr));
100 }
101 
TEST(StreamUtils,CalculateStreamPosition)102 TEST(StreamUtils, CalculateStreamPosition) {
103   using Whence = Stream::Whence;
104   const uint64_t current_pos = 1234;
105   const uint64_t end_pos = 2000;
106   uint64_t pos = 0;
107 
108   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
109       FROM_HERE, 0, Whence::FROM_BEGIN, current_pos, end_pos, &pos, nullptr));
110   EXPECT_EQ(0u, pos);
111 
112   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
113       FROM_HERE, 0, Whence::FROM_CURRENT, current_pos, end_pos, &pos, nullptr));
114   EXPECT_EQ(current_pos, pos);
115 
116   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
117       FROM_HERE, 0, Whence::FROM_END, current_pos, end_pos, &pos, nullptr));
118   EXPECT_EQ(end_pos, pos);
119 
120   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
121       FROM_HERE, 10, Whence::FROM_BEGIN, current_pos, end_pos, &pos, nullptr));
122   EXPECT_EQ(10u, pos);
123 
124   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
125       FROM_HERE, 10, Whence::FROM_CURRENT, current_pos, end_pos, &pos,
126       nullptr));
127   EXPECT_EQ(current_pos + 10, pos);
128 
129   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
130       FROM_HERE, 10, Whence::FROM_END, current_pos, end_pos, &pos, nullptr));
131   EXPECT_EQ(end_pos + 10, pos);
132 
133   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
134       FROM_HERE, -10, Whence::FROM_CURRENT, current_pos, end_pos, &pos,
135       nullptr));
136   EXPECT_EQ(current_pos - 10, pos);
137 
138   EXPECT_TRUE(stream_utils::CalculateStreamPosition(
139       FROM_HERE, -10, Whence::FROM_END, current_pos, end_pos, &pos, nullptr));
140   EXPECT_EQ(end_pos - 10, pos);
141 
142   ErrorPtr error;
143   EXPECT_FALSE(stream_utils::CalculateStreamPosition(
144       FROM_HERE, -1, Whence::FROM_BEGIN, current_pos, end_pos, &pos, &error));
145   EXPECT_EQ(errors::stream::kInvalidParameter, error->GetCode());
146   EXPECT_EQ("The stream offset value is out of range", error->GetMessage());
147 
148   EXPECT_FALSE(stream_utils::CalculateStreamPosition(
149       FROM_HERE, -1001, Whence::FROM_CURRENT, 1000, end_pos, &pos, nullptr));
150 
151   const uint64_t max_int64 = std::numeric_limits<int64_t>::max();
152   EXPECT_FALSE(stream_utils::CalculateStreamPosition(
153       FROM_HERE, 1, Whence::FROM_CURRENT, max_int64, end_pos, &pos, nullptr));
154 }
155 
156 class CopyStreamDataTest : public testing::Test {
157  public:
SetUp()158   void SetUp() override {
159     fake_loop_.SetAsCurrent();
160     in_stream_.reset(new StrictMock<MockStream>{});
161     out_stream_.reset(new StrictMock<MockStream>{});
162   }
163 
164   FakeMessageLoop fake_loop_{nullptr};
165   std::unique_ptr<StrictMock<MockStream>> in_stream_;
166   std::unique_ptr<StrictMock<MockStream>> out_stream_;
167   bool succeeded_{false};
168   bool failed_{false};
169 
OnSuccess(uint64_t expected,StreamPtr,StreamPtr,uint64_t copied)170   void OnSuccess(uint64_t expected,
171                  StreamPtr /* in_stream */,
172                  StreamPtr /* out_stream */,
173                  uint64_t copied) {
174     EXPECT_EQ(expected, copied);
175     succeeded_ = true;
176   }
177 
OnError(const std::string & expected_error,StreamPtr,StreamPtr,const Error * error)178   void OnError(const std::string& expected_error,
179                StreamPtr /* in_stream */,
180                StreamPtr /* out_stream */,
181                const Error* error) {
182     EXPECT_EQ(expected_error, error->GetCode());
183     failed_ = true;
184   }
185 
ExpectSuccess()186   void ExpectSuccess() {
187     EXPECT_TRUE(succeeded_);
188     EXPECT_FALSE(failed_);
189   }
190 
ExpectFailure()191   void ExpectFailure() {
192     EXPECT_FALSE(succeeded_);
193     EXPECT_TRUE(failed_);
194   }
195 };
196 
TEST_F(CopyStreamDataTest,CopyAllAtOnce)197 TEST_F(CopyStreamDataTest, CopyAllAtOnce) {
198   {
199     InSequence seq;
200     EXPECT_CALL(*in_stream_, ReadAsync(_, 100, _, _, _))
201         .WillOnce(InvokeAsyncCallback<2>(100));
202     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 100, _, _, _))
203         .WillOnce(InvokeAsyncCallback<2>());
204   }
205   stream_utils::CopyData(
206       std::move(in_stream_), std::move(out_stream_), 100, 4096,
207       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 100),
208       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this), ""));
209   fake_loop_.Run();
210   ExpectSuccess();
211 }
212 
TEST_F(CopyStreamDataTest,CopyInBlocks)213 TEST_F(CopyStreamDataTest, CopyInBlocks) {
214   {
215     InSequence seq;
216     EXPECT_CALL(*in_stream_, ReadAsync(_, 100, _, _, _))
217         .WillOnce(InvokeAsyncCallback<2>(60));
218     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 60, _, _, _))
219         .WillOnce(InvokeAsyncCallback<2>());
220     EXPECT_CALL(*in_stream_, ReadAsync(_, 40, _, _, _))
221         .WillOnce(InvokeAsyncCallback<2>(40));
222     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 40, _, _, _))
223         .WillOnce(InvokeAsyncCallback<2>());
224   }
225   stream_utils::CopyData(
226       std::move(in_stream_), std::move(out_stream_), 100, 4096,
227       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 100),
228       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this), ""));
229   fake_loop_.Run();
230   ExpectSuccess();
231 }
232 
TEST_F(CopyStreamDataTest,CopyTillEndOfStream)233 TEST_F(CopyStreamDataTest, CopyTillEndOfStream) {
234   {
235     InSequence seq;
236     EXPECT_CALL(*in_stream_, ReadAsync(_, 100, _, _, _))
237         .WillOnce(InvokeAsyncCallback<2>(60));
238     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 60, _, _, _))
239         .WillOnce(InvokeAsyncCallback<2>());
240     EXPECT_CALL(*in_stream_, ReadAsync(_, 40, _, _, _))
241         .WillOnce(InvokeAsyncCallback<2>(0));
242   }
243   stream_utils::CopyData(
244       std::move(in_stream_), std::move(out_stream_), 100, 4096,
245       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 60),
246       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this), ""));
247   fake_loop_.Run();
248   ExpectSuccess();
249 }
250 
TEST_F(CopyStreamDataTest,CopyInSmallBlocks)251 TEST_F(CopyStreamDataTest, CopyInSmallBlocks) {
252   {
253     InSequence seq;
254     EXPECT_CALL(*in_stream_, ReadAsync(_, 60, _, _, _))
255         .WillOnce(InvokeAsyncCallback<2>(60));
256     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 60, _, _, _))
257         .WillOnce(InvokeAsyncCallback<2>());
258     EXPECT_CALL(*in_stream_, ReadAsync(_, 40, _, _, _))
259         .WillOnce(InvokeAsyncCallback<2>(40));
260     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 40, _, _, _))
261         .WillOnce(InvokeAsyncCallback<2>());
262   }
263   stream_utils::CopyData(
264       std::move(in_stream_), std::move(out_stream_), 100, 60,
265       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 100),
266       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this), ""));
267   fake_loop_.Run();
268   ExpectSuccess();
269 }
270 
TEST_F(CopyStreamDataTest,ErrorRead)271 TEST_F(CopyStreamDataTest, ErrorRead) {
272   {
273     InSequence seq;
274     EXPECT_CALL(*in_stream_, ReadAsync(_, 60, _, _, _))
275         .WillOnce(InvokeAsyncErrorCallback<3>("read"));
276   }
277   stream_utils::CopyData(
278       std::move(in_stream_), std::move(out_stream_), 100, 60,
279       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 0),
280       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this), "read"));
281   fake_loop_.Run();
282   ExpectFailure();
283 }
284 
TEST_F(CopyStreamDataTest,ErrorWrite)285 TEST_F(CopyStreamDataTest, ErrorWrite) {
286   {
287     InSequence seq;
288     EXPECT_CALL(*in_stream_, ReadAsync(_, 60, _, _, _))
289         .WillOnce(InvokeAsyncCallback<2>(60));
290     EXPECT_CALL(*out_stream_, WriteAllAsync(_, 60, _, _, _))
291         .WillOnce(InvokeAsyncErrorCallback<3>("write"));
292   }
293   stream_utils::CopyData(
294       std::move(in_stream_), std::move(out_stream_), 100, 60,
295       base::Bind(&CopyStreamDataTest::OnSuccess, base::Unretained(this), 0),
296       base::Bind(&CopyStreamDataTest::OnError, base::Unretained(this),
297                  "write"));
298   fake_loop_.Run();
299   ExpectFailure();
300 }
301 
302 }  // namespace brillo
303