Skip to content

Commit 39ebf82

Browse files
authored
Refactor read/write mock (#315)
1 parent 7675013 commit 39ebf82

File tree

12 files changed

+155
-170
lines changed

12 files changed

+155
-170
lines changed

test/libp2p/basic/message_read_writer_test.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <gtest/gtest.h>
1010
#include <libp2p/multi/uvarint.hpp>
1111
#include "mock/libp2p/connection/layer_connection_mock.hpp"
12+
#include "testutil/expect_read.hpp"
13+
#include "testutil/expect_write.hpp"
1214
#include "testutil/gmock_actions.hpp"
1315

1416
using namespace libp2p;
@@ -41,17 +43,10 @@ class MessageReadWriterTest : public testing::Test {
4143
bool operation_completed_ = false;
4244
};
4345

44-
ACTION_P(ReadPut, buf) {
45-
ASSERT_GE(arg0.size(), buf.size());
46-
std::copy(buf.begin(), buf.end(), arg0.begin());
47-
arg2(buf.size());
48-
}
49-
5046
TEST_F(MessageReadWriterTest, Read) {
51-
EXPECT_CALL(*conn_mock_, read(_, 1, _))
52-
.WillOnce(ReadPut(len_varint_.toBytes()));
53-
EXPECT_CALL(*conn_mock_, read(_, kMsgLength, _))
54-
.WillOnce(ReadPut(msg_bytes_));
47+
EXPECT_CALL_READ(*conn_mock_)
48+
.WILL_READ(len_varint_.toBytes())
49+
.WILL_READ(msg_bytes_);
5550

5651
msg_rw_->read([this](auto &&res) {
5752
ASSERT_TRUE(res);
@@ -62,18 +57,8 @@ TEST_F(MessageReadWriterTest, Read) {
6257
ASSERT_TRUE(operation_completed_);
6358
}
6459

65-
ACTION_P2(CheckWrite, buf, varint) {
66-
ASSERT_EQ(arg0.size(), buf.size());
67-
68-
for (auto i = 0u; i < varint.size(); ++i) {
69-
ASSERT_EQ(arg0[i], varint.toBytes()[i]);
70-
}
71-
arg2(buf.size());
72-
}
73-
7460
TEST_F(MessageReadWriterTest, Write) {
75-
EXPECT_CALL(*conn_mock_, writeSome(_, kMsgLength + 1, _))
76-
.WillOnce(CheckWrite(msg_with_varint_bytes_, len_varint_));
61+
EXPECT_CALL_WRITE(*conn_mock_).WILL_WRITE(msg_with_varint_bytes_);
7762

7863
msg_rw_->write(msg_bytes_, [this](auto &&res) {
7964
ASSERT_TRUE(res);

test/libp2p/connection/security_conn/plaintext_connection_test.cpp

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <qtils/test/outcome.hpp>
1212
#include "mock/libp2p/connection/layer_connection_mock.hpp"
1313
#include "mock/libp2p/crypto/key_marshaller_mock.hpp"
14+
#include "testutil/expect_read.hpp"
15+
#include "testutil/expect_write.hpp"
1416
#include "testutil/gmock_actions.hpp"
1517

1618
using namespace libp2p::connection;
@@ -120,63 +122,29 @@ TEST_F(PlaintextConnectionTest, RemoteMultiaddr) {
120122
*/
121123
TEST_F(PlaintextConnectionTest, Read) {
122124
const int size = 100;
123-
EXPECT_CALL(*connection_, read(_, _, _)).WillOnce(AsioSuccess(size));
125+
EXPECT_CALL_READ(*connection_).WILL_READ_SIZE(size);
124126
auto buf = std::make_shared<std::vector<uint8_t>>(size, 0);
125127
secure_connection_->read(*buf, size, [size, buf](auto &&res) {
126128
ASSERT_OUTCOME_SUCCESS(res);
127129
ASSERT_EQ(res.value(), size);
128130
});
129131
}
130132

131-
/**
132-
* @given plaintext secure connection
133-
* @when invoking readSome method of the connection
134-
* @then method behaves as expected
135-
*/
136-
TEST_F(PlaintextConnectionTest, ReadSome) {
137-
const int size = 100;
138-
const int smaller = 50;
139-
EXPECT_CALL(*connection_, readSome(_, _, _))
140-
.WillOnce(AsioSuccess(smaller /* less than 100 */));
141-
auto buf = std::make_shared<std::vector<uint8_t>>(size, 0);
142-
secure_connection_->readSome(*buf, smaller, [smaller, buf](auto &&res) {
143-
ASSERT_OUTCOME_SUCCESS(res);
144-
ASSERT_EQ(res.value(), smaller);
145-
});
146-
}
147-
148133
/**
149134
* @given plaintext secure connection
150135
* @when invoking write method of the connection
151136
* @then method behaves as expected
152137
*/
153138
TEST_F(PlaintextConnectionTest, Write) {
154139
const int size = 100;
155-
EXPECT_CALL(*connection_, writeSome(_, _, _)).WillOnce(AsioSuccess(size));
140+
EXPECT_CALL_WRITE(*connection_).WILL_WRITE_SIZE(size);
156141
auto buf = std::make_shared<std::vector<uint8_t>>(size, 0);
157142
libp2p::writeReturnSize(secure_connection_, *buf, [size, buf](auto &&res) {
158143
ASSERT_OUTCOME_SUCCESS(res);
159144
ASSERT_EQ(res.value(), size);
160145
});
161146
}
162147

163-
/**
164-
* @given plaintext secure connection
165-
* @when invoking writeSome method of the connection
166-
* @then method behaves as expected
167-
*/
168-
TEST_F(PlaintextConnectionTest, WriteSome) {
169-
const int size = 100;
170-
const int smaller = 50;
171-
EXPECT_CALL(*connection_, writeSome(_, _, _))
172-
.WillOnce(AsioSuccess(smaller /* less than 100 */));
173-
auto buf = std::make_shared<std::vector<uint8_t>>(size, 0);
174-
secure_connection_->writeSome(*buf, smaller, [smaller, buf](auto &&res) {
175-
ASSERT_OUTCOME_SUCCESS(res);
176-
ASSERT_EQ(res.value(), smaller);
177-
});
178-
}
179-
180148
/**
181149
* @given plaintext secure connection
182150
* @when invoking isClosed method of the connection

test/libp2p/protocol/echo_test.cpp

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
#include <gtest/gtest.h>
88
#include <libp2p/protocol/echo.hpp>
9+
#include <qtils/bytestr.hpp>
910
#include <qtils/test/outcome.hpp>
1011
#include "mock/libp2p/connection/stream_mock.hpp"
12+
#include "testutil/expect_read.hpp"
13+
#include "testutil/expect_write.hpp"
1114
#include "testutil/gmock_actions.hpp"
1215
#include "testutil/prepare_loggers.hpp"
1316

@@ -17,30 +20,8 @@ using ::testing::_;
1720
using ::testing::Return;
1821
using std::string_literals::operator""s;
1922

20-
// set what we read
21-
ACTION_P(SetReadMsg, msg) {
22-
std::copy(msg.begin(), msg.end(), arg0.begin());
23-
arg2(msg.size());
24-
}
25-
26-
// assert that we write the same as we read
27-
ACTION_P(WriteMsgAssertEqual, msg) {
28-
std::string sub;
29-
30-
if (*arg0.begin() == 0) {
31-
// EOF
32-
return;
33-
}
34-
35-
auto begin = arg0.begin();
36-
auto end = arg0.begin();
37-
38-
std::advance(end, msg.size());
39-
std::copy(begin, end, std::back_inserter(sub));
40-
EXPECT_EQ(sub, msg);
41-
42-
arg2(msg.size());
43-
}
23+
const auto msg = "hello"s;
24+
const auto msg_bytes = qtils::str2byte(msg);
4425

4526
/**
4627
* @given Stream
@@ -52,7 +33,6 @@ TEST(EchoTest, Server) {
5233

5334
Echo echo;
5435
auto stream = std::make_shared<connection::StreamMock>();
55-
auto msg = "hello"s;
5636

5737
EXPECT_CALL(*stream, isClosedForRead())
5838
.WillOnce(Return(false))
@@ -63,8 +43,8 @@ TEST(EchoTest, Server) {
6343
EXPECT_CALL(*stream, close(_))
6444
.WillOnce(Arg0CallbackWithArg(outcome::success()));
6545

66-
EXPECT_CALL(*stream, readSome(_, _, _)).WillOnce(SetReadMsg(msg));
67-
EXPECT_CALL(*stream, writeSome(_, _, _)).WillOnce(WriteMsgAssertEqual(msg));
46+
EXPECT_CALL_READ(*stream).WILL_READ(msg_bytes);
47+
EXPECT_CALL_WRITE(*stream).WILL_WRITE(msg_bytes);
6848

6949
echo.handle(StreamAndProtocol{stream, {}});
7050
}
@@ -74,15 +54,14 @@ TEST(EchoTest, Server) {
7454
* @when client writes string "hello" to the Stream
7555
* @then client reads back the same string
7656
*/
77-
TEST(EchoTest, DISABLED_Client) {
57+
TEST(EchoTest, Client) {
7858
Echo echo;
7959
auto stream = std::make_shared<connection::StreamMock>();
80-
auto msg = "hello"s;
8160

8261
EXPECT_CALL(*stream, isClosedForWrite()).WillOnce(Return(false));
8362

84-
EXPECT_CALL(*stream, writeSome(_, _, _)).WillOnce(WriteMsgAssertEqual(msg));
85-
EXPECT_CALL(*stream, readSome(_, _, _)).WillOnce(WriteMsgAssertEqual(msg));
63+
EXPECT_CALL_READ(*stream).WILL_READ(msg_bytes).WILL_READ_ERROR();
64+
EXPECT_CALL_WRITE(*stream).WILL_WRITE(msg_bytes);
8665

8766
bool executed = false;
8867

test/libp2p/protocol/identify_delta_test.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mock/libp2p/network/connection_manager_mock.hpp"
2020
#include "mock/libp2p/peer/peer_repository_mock.hpp"
2121
#include "mock/libp2p/peer/protocol_repository_mock.hpp"
22+
#include "testutil/expect_read.hpp"
2223
#include "testutil/gmock_actions.hpp"
2324
#include "testutil/prepare_loggers.hpp"
2425

@@ -154,12 +155,9 @@ ACTION_P(ReadPut, buf) {
154155
*/
155156
TEST_F(IdentifyDeltaTest, Receive) {
156157
// handle
157-
EXPECT_CALL(*stream_, read(_, 1, _))
158-
.WillOnce(ReadPut(std::span(msg_added_rm_protos_bytes_.data(), 1)));
159-
EXPECT_CALL(*stream_, read(_, added_rm_proto_len_.toUInt64(), _))
160-
.WillOnce(ReadPut(std::span(
161-
msg_added_rm_protos_bytes_.data() + added_proto_len_.size(),
162-
msg_added_rm_protos_bytes_.size() - added_proto_len_.size())));
158+
EXPECT_CALL_READ(*stream_)
159+
.WILL_READ(BytesOut(msg_added_rm_protos_bytes_).first(1))
160+
.WILL_READ(BytesOut(msg_added_rm_protos_bytes_).subspan(1));
163161

164162
// deltaReceived
165163
EXPECT_CALL(*stream_, remotePeerId())

test/libp2p/protocol/identify_test.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "mock/libp2p/peer/key_repository_mock.hpp"
2525
#include "mock/libp2p/peer/peer_repository_mock.hpp"
2626
#include "mock/libp2p/peer/protocol_repository_mock.hpp"
27+
#include "testutil/expect_read.hpp"
28+
#include "testutil/expect_write.hpp"
2729
#include "testutil/prepare_loggers.hpp"
2830

2931
using namespace libp2p;
@@ -184,10 +186,7 @@ TEST_F(IdentifyTest, Send) {
184186
EXPECT_CALL(host_, getLibp2pClientVersion()).WillOnce(Return(kClientVersion));
185187

186188
// handle Identify request and check it
187-
EXPECT_CALL(*stream_, writeSome(_, _, _))
188-
.WillOnce(Success(
189-
BytesIn(identify_pb_msg_bytes_.data(), identify_pb_msg_bytes_.size()),
190-
outcome::success(identify_pb_msg_bytes_.size())));
189+
EXPECT_CALL_WRITE(*stream_).WILL_WRITE(identify_pb_msg_bytes_);
191190

192191
identify_->handle(StreamAndProtocol{stream_, {}});
193192
}
@@ -215,12 +214,9 @@ TEST_F(IdentifyTest, Receive) {
215214
newStream(kRemotePeerInfo, StreamProtocols{kIdentifyProto}, _))
216215
.WillOnce(InvokeArgument<2>(StreamAndProtocol{stream_, kIdentifyProto}));
217216

218-
EXPECT_CALL(*stream_, read(_, 1, _))
219-
.WillOnce(ReadPut(std::span(identify_pb_msg_bytes_.data(), 1)));
220-
EXPECT_CALL(*stream_, read(_, pb_msg_len_varint_->toUInt64(), _))
221-
.WillOnce(ReadPut(std::span(
222-
identify_pb_msg_bytes_.data() + pb_msg_len_varint_->size(),
223-
identify_pb_msg_bytes_.size() - pb_msg_len_varint_->size())));
217+
EXPECT_CALL_READ(*stream_)
218+
.WILL_READ(BytesOut(identify_pb_msg_bytes_).first(1))
219+
.WILL_READ(BytesOut(identify_pb_msg_bytes_).subspan(1));
224220

225221
EXPECT_CALL(*stream_, remotePeerId())
226222
.Times(2)

test/libp2p/protocol/ping_test.cpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "mock/libp2p/crypto/random_generator_mock.hpp"
2323
#include "mock/libp2p/host/host_mock.hpp"
2424
#include "mock/libp2p/peer/peer_repository_mock.hpp"
25+
#include "testutil/expect_read.hpp"
26+
#include "testutil/expect_write.hpp"
2527

2628
using libp2p::basic::Scheduler;
2729
using libp2p::basic::SchedulerMock;
@@ -97,18 +99,9 @@ ACTION_P(ReadPut, buf) {
9799
* writes it back
98100
*/
99101
TEST_F(PingTest, PingServer) {
100-
EXPECT_CALL(*stream_, read(_, kPingMsgSize, _))
101-
.WillOnce(ReadPut(buffer_))
102-
.WillOnce( // no second read
103-
InvokeArgument<2>(std::error_code{}));
104-
105-
auto if_eq_buf = [&](BytesIn actual) {
106-
auto expected = BytesIn(buffer_);
107-
return std::equal(
108-
actual.begin(), actual.end(), expected.begin(), expected.end());
109-
};
110-
EXPECT_CALL(*stream_, writeSome(Truly(if_eq_buf), kPingMsgSize, _))
111-
.WillOnce(InvokeArgument<2>(buffer_.size()));
102+
EXPECT_CALL_READ(*stream_).WILL_READ(buffer_).WILL_READ_ERROR();
103+
104+
EXPECT_CALL_WRITE(*stream_).WILL_WRITE(buffer_);
112105

113106
EXPECT_CALL(*stream_, isClosedForWrite()).WillOnce(Return(false));
114107
EXPECT_CALL(*stream_, isClosedForRead())
@@ -135,17 +128,9 @@ TEST_F(PingTest, PingClient) {
135128
EXPECT_CALL(*rand_gen_, randomBytes(kPingMsgSize))
136129
.Times(2)
137130
.WillRepeatedly(Return(buffer_));
138-
auto if_eq_buf = [&](BytesIn actual) {
139-
auto expected = BytesIn(buffer_);
140-
return std::equal(
141-
actual.begin(), actual.end(), expected.begin(), expected.end());
142-
};
143-
EXPECT_CALL(*stream_, writeSome(Truly(if_eq_buf), kPingMsgSize, _))
144-
.WillOnce(InvokeArgument<2>(buffer_.size()))
145-
.WillOnce( // no second write
146-
InvokeArgument<2>(
147-
make_error_code(boost::asio::error::invalid_argument)));
148-
EXPECT_CALL(*stream_, read(_, kPingMsgSize, _)).WillOnce(ReadPut(buffer_));
131+
132+
EXPECT_CALL_WRITE(*stream_).WILL_WRITE(buffer_).WILL_WRITE_ERROR();
133+
EXPECT_CALL_READ(*stream_).WILL_READ(buffer_);
149134

150135
EXPECT_CALL(*stream_, isClosedForWrite())
151136
.Times(2)
@@ -175,12 +160,7 @@ TEST_F(PingTest, PingClientTimeoutExpired) {
175160
.WillOnce(InvokeArgument<2>(StreamAndProtocol{stream_, kPingProto}));
176161

177162
EXPECT_CALL(*rand_gen_, randomBytes(kPingMsgSize)).WillOnce(Return(buffer_));
178-
auto if_eq_buf = [&](BytesIn actual) {
179-
auto expected = BytesIn(buffer_);
180-
return std::equal(
181-
actual.begin(), actual.end(), expected.begin(), expected.end());
182-
};
183-
EXPECT_CALL(*stream_, writeSome(Truly(if_eq_buf), kPingMsgSize, _));
163+
EXPECT_CALL_WRITE(*stream_).WILL_WRITE(buffer_);
184164

185165
EXPECT_CALL(*stream_, isClosedForWrite()).WillOnce(Return(false));
186166

test/libp2p/security/plaintext_adaptor_test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ class PlaintextAdaptorTest : public testing::Test {
4444
key_marshaller = std::make_shared<KeyMarshallerMock>();
4545
adaptor = std::make_shared<Plaintext>(marshaller, idmgr, key_marshaller);
4646

47-
ON_CALL(*conn, read(_, _, _)).WillByDefault(Arg2CallbackWithArg(5));
48-
ON_CALL(*conn, writeSome(_, _, _)).WillByDefault(Arg2CallbackWithArg(5));
49-
5047
ON_CALL(*idmgr, getKeyPair()).WillByDefault(ReturnRef(local_keypair));
5148
ON_CALL(*idmgr, getId()).WillByDefault(ReturnRef(local_pid));
5249
ON_CALL(*marshaller, marshal(_))

test/mock/libp2p/connection/layer_connection_mock.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77
#pragma once
88

99
#include <libp2p/connection/layer_connection.hpp>
10+
#include <libp2p/basic/read_return_size.hpp>
1011

1112
#include <gmock/gmock.h>
1213

1314
namespace libp2p::connection {
1415

15-
class LayerConnectionMock : public virtual LayerConnection {
16+
class LayerConnectionMock : public std::enable_shared_from_this<LayerConnectionMock>, public virtual LayerConnection {
1617
public:
1718
~LayerConnectionMock() override = default;
1819

1920
MOCK_CONST_METHOD0(isClosed, bool(void));
2021

2122
MOCK_METHOD0(close, outcome::result<void>());
2223

23-
MOCK_METHOD3(read, void(BytesOut, size_t, Reader::ReadCallbackFunc));
24+
void read(BytesOut out,
25+
size_t ambigous_size,
26+
Reader::ReadCallbackFunc cb) override {
27+
ASSERT_EQ(out.size(), ambigous_size);
28+
readReturnSize(shared_from_this(), out, cb);
29+
}
2430
MOCK_METHOD3(readSome, void(BytesOut, size_t, Reader::ReadCallbackFunc));
2531
MOCK_METHOD3(writeSome, void(BytesIn, size_t, Writer::WriteCallbackFunc));
2632
MOCK_METHOD2(deferReadCallback,

0 commit comments

Comments
 (0)