extern_interface/tests/test_host_com.cpp

109 lines
3.5 KiB
C++

#include <thread>
#include <pty.h>
#include <fcntl.h>
#include <unistd.h>
#include "comframe.h"
#include "telemetry.h"
#include "dataqueue.hpp"
#include "logging/interface.h"
#include "transport/base.hpp"
#include "exint/protocol.hpp"
#include "exint/detail.hpp"
#include "c_testcase.h"
class MockTransport: public Transport {
public:
void send_backend() override {}
void receive_backend() override {}
Transport::FrameType get_sent_frame() {
return super::send_que.Pop().first; // std::chrono::seconds(1)
}
void set_received_frame(ComFrame* frame) {
super::recv_que.Push(std::make_pair(std::shared_ptr<ComFrame>(frame, ComFrame_Del), nullptr));
}
private:
typedef Transport super;
};
static int g_telemetry_request_count = 0;
static DataQueue<std::tuple<uint32_t, size_t, void*>> g_data_queue;
static MockTransport* g_upperhost_mock_transport;
static MockTransport* g_telemetry_mock_transport;
void exint_data_callback(uint32_t type, size_t size, void* data) {
if (type == ET_TYPE_TELEMETRY_REQUEST) {
g_telemetry_request_count++;
memset(data, 0, size);
((TelemetryRequestData*)data)->app_ver_high = 1;
((TelemetryRequestData*)data)->app_ver_low = 2;
} else {
g_data_queue.Push(std::make_tuple(type, size, data));
}
}
SETUP {
g_upperhost_mock_transport = new MockTransport();
g_telemetry_mock_transport = new MockTransport();
exint_setup(std::unique_ptr<Transport>(g_upperhost_mock_transport), std::unique_ptr<Transport>(g_telemetry_mock_transport), exint_data_callback);
g_telemetry_request_count = 0;
log_set_level(LOG_LEVEL_DEBUG);
return 0;
}
TEARDOWN {
exint_finialize();
g_data_queue.Clear();
return 0;
}
TEST_CASE(test_init) {
END_TEST;
}
TEST_CASE(test_host_telemetry) {
auto request_msg = NewTelemetryRequestMsg(COM_FRAME_ADDRESS_VOIX, false);
g_telemetry_mock_transport->set_received_frame(request_msg);
auto telemetry_reply = g_telemetry_mock_transport->get_sent_frame();
assert(telemetry_reply);
assert_eq(g_telemetry_request_count, 1);
TelemetryData* telemetry_data = (TelemetryData*)ComFrame_PAYLOAD(telemetry_reply);
assert_eq(ComFrame_TYPE(telemetry_reply.get()), byteswaps(COM_FRAME_TYPE_TELEMETRY_ANSWER));
assert_eq(ComFrame_Length(telemetry_reply.get(), true), sizeof(ComFrameHeader) + sizeof(TelemetryData) + sizeof(uint16_t));
assert_eq(telemetry_data->application_version_high, 1);
assert_eq(telemetry_data->application_version_low, 2);
END_TEST;
}
TEST_CASE(test_callback) {
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;
}
TEST_CASE(test_send_command) {
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;
}