| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef UNITTEST_GUNIT_XPLUGIN_XCL_PROTOCOL_T_H_ |
| 27 |
#define UNITTEST_GUNIT_XPLUGIN_XCL_PROTOCOL_T_H_ |
| 28 |
|
| 29 |
#include <gmock/gmock.h> |
| 30 |
#include <gtest/gtest.h> |
| 31 |
#include <algorithm> |
| 32 |
#include <array> |
| 33 |
#include <cstdint> |
| 34 |
#include <memory> |
| 35 |
#include <string> |
| 36 |
#include <utility> |
| 37 |
|
| 38 |
#include "errmsg.h" |
| 39 |
#include "my_compiler.h" |
| 40 |
#include "my_inttypes.h" |
| 41 |
#include "plugin/x/client/context/xconnection_config.h" |
| 42 |
#include "plugin/x/client/context/xssl_config.h" |
| 43 |
#include "plugin/x/client/xprotocol_impl.h" |
| 44 |
#include "unittest/gunit/xplugin/xcl/message_helpers.h" |
| 45 |
#include "unittest/gunit/xplugin/xcl/mock/connection.h" |
| 46 |
#include "unittest/gunit/xplugin/xcl/mock/connection_state.h" |
| 47 |
#include "unittest/gunit/xplugin/xcl/mock/factory.h" |
| 48 |
#include "unittest/gunit/xplugin/xcl/mock/message_handler.h" |
| 49 |
#include "unittest/gunit/xplugin/xcl/mock/query_result.h" |
| 50 |
|
| 51 |
namespace xcl { |
| 52 |
namespace test { |
| 53 |
|
| 54 |
using ::testing::_; |
| 55 |
using ::testing::InSequence; |
| 56 |
using ::testing::Invoke; |
| 57 |
using ::testing::Ref; |
| 58 |
using ::testing::Return; |
| 59 |
using ::testing::ReturnRef; |
| 60 |
using ::testing::StrictMock; |
| 61 |
using ::testing::Test; |
| 62 |
|
| 63 |
class Xcl_protocol_impl_tests : public Test { |
| 64 |
protected: |
| 65 |
std::array<uint8_t, 4> extract_header_from_message(const uchar *data) { |
| 66 |
std::array<uint8_t, 4> header = {{data[0], data[1], data[2], data[3]}}; |
| 67 |
|
| 68 |
#ifdef WORDS_BIGENDIAN |
| 69 |
std::swap(header[0], header[3]); |
| 70 |
std::swap(header[1], header[2]); |
| 71 |
#endif |
| 72 |
return header; |
| 73 |
} |
| 74 |
|
| 75 |
public: |
| 76 |
void SetUp() override { |
| 77 |
m_mock_connection = new StrictMock<mock::XConnection>(); |
| 78 |
|
| 79 |
EXPECT_CALL(m_mock_factory, create_connection_raw(_)) |
| 80 |
.WillOnce(Return(m_mock_connection)); |
| 81 |
|
| 82 |
m_sut.reset(new Protocol_impl(m_context, &m_mock_factory)); |
| 83 |
} |
| 84 |
|
| 85 |
XQuery_result *expect_factory_new_result() { |
| 86 |
mock::XQuery_result *result = new mock::XQuery_result(); |
| 87 |
static XQuery_result::Metadata metadata; |
| 88 |
|
| 89 |
EXPECT_CALL(m_mock_factory, create_result_raw(_, _, _)) |
| 90 |
.WillOnce(Return(result)); |
| 91 |
EXPECT_CALL(*result, get_metadata(_)).WillOnce(ReturnRef(metadata)); |
| 92 |
|
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
void expect_write_header(const XProtocol::Client_message_type_id id, |
| 97 |
const uint32_t expected_payload_size, |
| 98 |
const int32_t error_code = 0) { |
| 99 |
EXPECT_CALL(*m_mock_connection, write(_, 5)) |
| 100 |
.WillOnce(Invoke([this, id, expected_payload_size, error_code]( |
| 101 |
const uchar *data, const std::size_t size |
| 102 |
[[maybe_unused]]) -> XError { |
| 103 |
EXPECT_EQ(data[4], id); |
| 104 |
auto header = this->extract_header_from_message(data); |
| 105 |
|
| 106 |
EXPECT_EQ(expected_payload_size, |
| 107 |
*reinterpret_cast<int32_t *>(header.data()) - 1); |
| 108 |
return XError{error_code, ""}; |
| 109 |
})); |
| 110 |
} |
| 111 |
|
| 112 |
template <typename Message_type> |
| 113 |
void expect_write_message_without_payload( |
| 114 |
const Message_from_str<Message_type> &message, |
| 115 |
const int32_t error_code = 0) { |
| 116 |
const Message_type &message_base = message; |
| 117 |
|
| 118 |
expect_write_message_without_payload(message_base, error_code); |
| 119 |
} |
| 120 |
|
| 121 |
template <typename Message_type> |
| 122 |
void expect_write_message_without_payload( |
| 123 |
const Message_type &message, const int32_t expected_error_code = 0) { |
| 124 |
auto message_binary = Message_encoder::encode(message); |
| 125 |
expect_write_header(Client_message<Message_type>::get_id(), |
| 126 |
static_cast<uint32>(message_binary.size()), |
| 127 |
expected_error_code); |
| 128 |
} |
| 129 |
|
| 130 |
template <typename Message_type> |
| 131 |
void expect_write_message(const Message_from_str<Message_type> &message, |
| 132 |
const int32_t error_code = 0) { |
| 133 |
const Message_type &message_base = message; |
| 134 |
|
| 135 |
expect_write_message(message_base, error_code); |
| 136 |
} |
| 137 |
|
| 138 |
template <typename Message_type> |
| 139 |
void expect_write_message(const Message_type &message, |
| 140 |
const int32_t error_code = 0) { |
| 141 |
auto message_binary = Message_encoder::encode(message); |
| 142 |
|
| 143 |
const uint8_t payload_size = message_binary.size(); |
| 144 |
const auto id = Client_message<Message_type>::get_id(); |
| 145 |
|
| 146 |
expect_write_payload(id, payload_size, error_code); |
| 147 |
} |
| 148 |
|
| 149 |
void expect_write_payload(const uint8_t id, const uint32_t payload_size, |
| 150 |
const int32 error_code = 0) { |
| 151 |
const std::uint8_t header_size = 5; |
| 152 |
|
| 153 |
EXPECT_CALL(*m_mock_connection, write(_, header_size + payload_size)) |
| 154 |
.WillOnce(Invoke([this, id, payload_size, error_code]( |
| 155 |
const uchar *data, const std::size_t size |
| 156 |
[[maybe_unused]]) -> XError { |
| 157 |
EXPECT_EQ(data[4], id); |
| 158 |
auto header = this->extract_header_from_message(data); |
| 159 |
|
| 160 |
EXPECT_EQ(payload_size, |
| 161 |
*reinterpret_cast<int32_t *>(header.data()) - 1); |
| 162 |
return XError{error_code, ""}; |
| 163 |
})); |
| 164 |
} |
| 165 |
|
| 166 |
template <typename Message_type_id> |
| 167 |
void expect_read_header(const Message_type_id id, const int32_t payload_size, |
| 168 |
const int32_t error_code = 0) { |
| 169 |
const int32_t expected_header_size = 5; |
| 170 |
|
| 171 |
EXPECT_CALL(*m_mock_connection, read(_, expected_header_size)) |
| 172 |
.WillOnce(Invoke([id, payload_size, error_code]( |
| 173 |
uchar *data, const std::size_t data_length |
| 174 |
[[maybe_unused]]) -> XError { |
| 175 |
|
| 176 |
*reinterpret_cast<int32_t *>(data) = 1 + payload_size; |
| 177 |
|
| 178 |
#ifdef WORDS_BIGENDIAN |
| 179 |
std::swap(data[0], data[3]); |
| 180 |
std::swap(data[1], data[2]); |
| 181 |
#endif |
| 182 |
|
| 183 |
data[4] = static_cast<uchar>(id); |
| 184 |
|
| 185 |
return XError{error_code, ""}; |
| 186 |
})); |
| 187 |
} |
| 188 |
|
| 189 |
template <typename Message_type> |
| 190 |
void expect_read_message_without_payload(const Message_type &message |
| 191 |
[[maybe_unused]], |
| 192 |
const int32_t error_code = 0) { |
| 193 |
expect_read_header(Server_message<Message_type>::get_id(), 0, error_code); |
| 194 |
} |
| 195 |
|
| 196 |
template <typename Message_type> |
| 197 |
void expect_read_message(const Message_from_str<Message_type> &message) { |
| 198 |
const Message_type &message_base = message; |
| 199 |
|
| 200 |
expect_read_message(message_base); |
| 201 |
} |
| 202 |
|
| 203 |
template <typename Message_type> |
| 204 |
void expect_read_message(const Message_type &message) { |
| 205 |
const std::string message_payload = Message_encoder::encode(message); |
| 206 |
|
| 207 |
expect_read_header(Server_message<Message_type>::get_id(), |
| 208 |
static_cast<int32>(message_payload.size())); |
| 209 |
|
| 210 |
EXPECT_CALL(*m_mock_connection, read(_, message_payload.size())) |
| 211 |
.WillOnce( |
| 212 |
Invoke([message_payload](uchar *data, const std::size_t data_length |
| 213 |
[[maybe_unused]]) -> XError { |
| 214 |
std::copy(message_payload.begin(), message_payload.end(), data); |
| 215 |
return {}; |
| 216 |
})); |
| 217 |
} |
| 218 |
|
| 219 |
template <typename Message_type> |
| 220 |
void assert_read_message(const Message_type &message) { |
| 221 |
XProtocol::Server_message_type_id out_id; |
| 222 |
XError out_error; |
| 223 |
|
| 224 |
expect_read_message(message); |
| 225 |
|
| 226 |
auto result = m_sut->recv_single_message(&out_id, &out_error); |
| 227 |
ASSERT_FALSE(out_error); |
| 228 |
ASSERT_TRUE(result.get()); |
| 229 |
ASSERT_EQ(message.GetTypeName(), result->GetTypeName()); |
| 230 |
ASSERT_EQ(Message_compare<Message_type>(message), *result); |
| 231 |
} |
| 232 |
|
| 233 |
StrictMock<mock::XConnection> *m_mock_connection; |
| 234 |
StrictMock<mock::Protocol_factory> m_mock_factory; |
| 235 |
StrictMock<mock::Message_handlers> m_mock_handlers; |
| 236 |
|
| 237 |
std::shared_ptr<Context> m_context{std::make_shared<Context>()}; |
| 238 |
|
| 239 |
std::shared_ptr<Protocol_impl> m_sut; |
| 240 |
}; |
| 241 |
|
| 242 |
template <typename T> |
| 243 |
class Xcl_protocol_impl_tests_with_msg : public Xcl_protocol_impl_tests { |
| 244 |
public: |
| 245 |
using Msg = T; |
| 246 |
using Msg_ptr = std::unique_ptr<Msg>; |
| 247 |
using Msg_descriptor = Client_message<T>; |
| 248 |
using Type = Xcl_protocol_impl_tests_with_msg<T>; |
| 249 |
|
| 250 |
public: |
| 251 |
Msg_ptr m_message; |
| 252 |
|
| 253 |
void setup_required_fields_in_message() { |
| 254 |
m_message.reset(new Msg(Msg_descriptor::make_required())); |
| 255 |
} |
| 256 |
|
| 257 |
XError assert_write_size(const uchar *data, const uint32_t size) { |
| 258 |
EXPECT_EQ(Msg_descriptor::get_id(), data[4]); |
| 259 |
auto header = extract_header_from_message(data); |
| 260 |
|
| 261 |
const uint8_t expected_header_size = 5; |
| 262 |
const uint32_t payload_size = |
| 263 |
*reinterpret_cast<uint32_t *>(header.data()) - 1; |
| 264 |
EXPECT_EQ(size, (payload_size + expected_header_size)); |
| 265 |
|
| 266 |
return {}; |
| 267 |
} |
| 268 |
|
| 269 |
void assert_multiple_handlers(const Handler_result first_handler_consumed) { |
| 270 |
StrictMock<mock::Message_handlers> mock_handlers[2]; |
| 271 |
|
| 272 |
this->setup_required_fields_in_message(); |
| 273 |
|
| 274 |
const auto id1 = this->m_sut->add_send_message_handler( |
| 275 |
this->m_mock_handlers.get_mock_lambda_send_message_handler()); |
| 276 |
const auto id2 = this->m_sut->add_send_message_handler( |
| 277 |
mock_handlers[0].get_mock_lambda_send_message_handler()); |
| 278 |
const auto id3 = this->m_sut->add_send_message_handler( |
| 279 |
mock_handlers[1].get_mock_lambda_send_message_handler()); |
| 280 |
|
| 281 |
{ |
| 282 |
InSequence s; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
EXPECT_CALL(mock_handlers[1], |
| 287 |
send_message_handler(_, Msg_descriptor::get_id(), |
| 288 |
Ref(*this->m_message.get()))) |
| 289 |
.WillOnce(Return(first_handler_consumed)); |
| 290 |
EXPECT_CALL(mock_handlers[0], |
| 291 |
send_message_handler(_, Msg_descriptor::get_id(), |
| 292 |
Ref(*this->m_message.get()))) |
| 293 |
.WillOnce(Return(Handler_result::Continue)); |
| 294 |
EXPECT_CALL(this->m_mock_handlers, |
| 295 |
send_message_handler(_, Msg_descriptor::get_id(), |
| 296 |
Ref(*this->m_message.get()))) |
| 297 |
.WillOnce(Return(Handler_result::Continue)); |
| 298 |
|
| 299 |
EXPECT_CALL(*this->m_mock_connection, write(_, _)) |
| 300 |
.WillOnce(Invoke( |
| 301 |
[this](const uchar *data, const std::size_t size) -> XError { |
| 302 |
return this->assert_write_size(data, size); |
| 303 |
})); |
| 304 |
} |
| 305 |
|
| 306 |
auto error = this->m_sut->send(*this->m_message.get()); |
| 307 |
|
| 308 |
ASSERT_FALSE(error); |
| 309 |
|
| 310 |
this->m_sut->remove_send_message_handler(id1); |
| 311 |
this->m_sut->remove_send_message_handler(id2); |
| 312 |
this->m_sut->remove_send_message_handler(id3); |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
#endif |