extern_interface/tests/handler/test_command.cpp

71 lines
2.6 KiB
C++
Raw Normal View History

#include "setup.inc"
TEST_CASE(test_send)
{
char command_data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
exint_send(ET_TYPE_COMMAND, 6, command_data);
auto command_frame = g_upperhost_mock_transport->get_sent_frame();
assert(command_frame);
assert_eq(ComFrame_TYPE(command_frame.get()), byteswaps(COM_FRAME_TYPE_REQUEST));
assert_eq(ComFrame_Length(command_frame.get(), true), 6 + sizeof(ComFrameHeader) + 2);
assert_mem_eq(ComFrame_PAYLOAD(command_frame), command_data, 6);
END_TEST;
}
TEST_CASE(test_resend)
{
char command_data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
exint_send(ET_TYPE_COMMAND, 6, command_data);
// send first command frame
auto command_frame = g_upperhost_mock_transport->get_sent_frame();
assert(command_frame);
assert_eq(ComFrame_TYPE(command_frame.get()), byteswaps(COM_FRAME_TYPE_REQUEST));
assert_eq(ComFrame_Length(command_frame.get(), true), 6 + sizeof(ComFrameHeader) + 2);
assert_mem_eq(ComFrame_PAYLOAD(command_frame), command_data, 6);
// resend frame
command_frame = g_upperhost_mock_transport->get_sent_frame();
assert(command_frame);
assert_eq(ComFrame_TYPE(command_frame.get()), byteswaps(COM_FRAME_TYPE_REQUEST));
assert_eq(ComFrame_Length(command_frame.get(), true), 6 + sizeof(ComFrameHeader) + 2);
assert_mem_eq(ComFrame_PAYLOAD(command_frame), command_data, 6);\
// resend frame again
command_frame = g_upperhost_mock_transport->get_sent_frame();
assert(command_frame);
assert_eq(ComFrame_TYPE(command_frame.get()), byteswaps(COM_FRAME_TYPE_REQUEST));
assert_eq(ComFrame_Length(command_frame.get(), true), 6 + sizeof(ComFrameHeader) + 2);
assert_mem_eq(ComFrame_PAYLOAD(command_frame), command_data, 6);
// responce
uint8_t payload[] = {0xAA, 0xAA};
g_upperhost_mock_transport->set_received_frame(
ComFrame_New(COM_FRAME_ADDRESS_VOIX, COM_FRAME_TYPE_REQUEST, payload, sizeof(payload), false)
);
try {
g_upperhost_mock_transport->get_sent_frame();
} catch (const QueueTimeout&) {
END_TEST;
}
printf("bad resend frame");
return 1;
}
TEST_CASE(test_recv) {
uint8_t command[] = { 0x00, 0x01, 0x02, 0x03, 0xAA, 0xAA};
auto command_msg = ComFrame_New(COM_FRAME_ADDRESS_VOIX, COM_FRAME_TYPE_COMMAND, command, sizeof(command), false);
g_upperhost_mock_transport->set_received_frame(command_msg);
uint32_t tp;
size_t len;
void* pack;
std::tie(tp, len, pack) = g_data_queue.Pop(std::chrono::milliseconds(1000));
assert_eq(tp, ET_TYPE_COMMAND);
assert_eq(len, sizeof(command));
assert_mem_eq(command, pack, len);
END_TEST;
}