src.dualinventive.com/fw/dncm/libdi/tests/rpc_lowlevel_encode.cpp

80 lines
2.2 KiB
C++

#include <di/rpc/lowlevel.h>
#include <gtest/gtest.h>
#include "tests/buffer.h"
extern "C" {
#include "rpc/lowlevel.c"
}
#define TEST_DI_RPC_LL_MSG_DATA_SIZE 64
DI_BUFFER_DECL(buf1, TEST_DI_RPC_LL_MSG_DATA_SIZE);
DI_BUFFER_DECL(buf2, TEST_DI_RPC_LL_MSG_DATA_SIZE);
/**
* Test encode handshake for device uid with just-fit di_buffer with
* size: DI_RPC_LL_MSG_HDR_SIZE + DI_RPC_LL_MSG_HANDSHAKE_DEVICE_UID_SIZE
*/
TEST(rpc_ll_encode, handshake_strict_buf)
{
const char uid[33] = "deadbeefcafebabedeadbeefcafebabe";
di_buffer_init(&buf1, buf1_data, sizeof(buf1_data));
di_buffer_flush(&buf1);
buf1.size = DI_RPC_LL_MSG_HDR_SIZE + DI_RPC_LL_MSG_HANDSHAKE_DEVICE_UID_SIZE;
ASSERT_EQ(DNOK, di_rpc_ll_encode_handshake(&buf1, uid));
ASSERT_EQ(DI_RPC_LL_MSG_HDR_SIZE + DI_RPC_LL_MSG_HANDSHAKE_DEVICE_UID_SIZE, buf1.used);
ASSERT_STREQ(uid, reinterpret_cast<const char *>(&buf1_data[DI_RPC_LL_MSG_HDR_SIZE]));
buf1.size = sizeof(buf1_data);
test_buffer_dump(&buf1);
}
/**
* Encode a time request lowlevel message and check if it is correct written
* to the buffer.
* - Checks if the written buffer size is DI_RPC_LL_MSG_HDR_SIZE
*/
TEST(rpc_ll_encode, time_request)
{
// const char exp[] = "DJR\x00\x00\x40\x00\x08";
const char exp[] = "DJR\x40\x00\x06";
di_buffer_init(&buf1, buf1_data, sizeof(buf1_data));
di_buffer_flush(&buf1);
ASSERT_EQ(DNOK, di_rpc_ll_encode_time_request(&buf1));
EXPECT_EQ(DI_RPC_LL_MSG_HDR_SIZE, buf1.used);
EXPECT_STREQ(exp, (const char *)&buf1_data);
test_buffer_dump(&buf1);
}
/**
* Encode msg in src di_buffer into dst and write header
* Check if the encode returns ok and if the dst buffer is written correctly
*/
TEST(rpc_ll_encode, DNOK)
{
// static const uint8_t expect[] = "DJR\x00\x00\x10\x00\x0a\x13\x37";
static const uint8_t expect[] = "DJR\x10\x00\x08\x13\x37";
/* Initialize */
di_buffer_init(&buf1, buf1_data, sizeof(buf1_data));
di_buffer_flush(&buf1);
di_buffer_init(&buf2, buf2_data, sizeof(buf2_data));
di_buffer_flush(&buf2);
/* Write string into dst buffer, set the buf2.used size */
di_buffer_memcpy(&buf2, 0, "\x13\x37", 2);
buf2.used = 2;
EXPECT_EQ(DNOK, di_rpc_ll_encode(&buf1, &buf2, DI_RPC_LL_MSG_TYPE_PLAIN));
EXPECT_EQ(0, memcmp(expect, buf1.data, sizeof(expect) - 1));
test_buffer_dump(&buf1);
}