Issue 87 execute tests with GitHub ci (#234)
* Enable lcov coverage in unit testing via cmake. * fix pipeline build error * add compile options for unit test to silence some warnings * remove all BAC_TEST unit tests in src/bacnet/ folder. They are now in test/bacnet/ folders using ztest. * removed key.c - only used for unit test. * produce XML test result output for parsing * produce junit XML test result output * change lint workflow to quality * update readme badge for quality results Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
@@ -2620,952 +2620,3 @@ const char *bvlc_result_code_name(uint16_t result_code)
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "ctest.h"
|
||||
|
||||
static void test_BVLC_Address(Test *pTest,
|
||||
BACNET_IP_ADDRESS *bip_address_1,
|
||||
BACNET_IP_ADDRESS *bip_address_2)
|
||||
{
|
||||
ct_test(pTest, !bvlc_address_different(bip_address_1, bip_address_2));
|
||||
}
|
||||
|
||||
static void test_BVLC_Broadcast_Distribution_Mask(Test *pTest,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK *bd_mask_1,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK *bd_mask_2)
|
||||
{
|
||||
ct_test(pTest,
|
||||
!bvlc_broadcast_distribution_mask_different(bd_mask_1, bd_mask_2));
|
||||
}
|
||||
|
||||
static void test_BVLC_Broadcast_Distribution_Table_Entry(Test *pTest,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *bdt_entry_1,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *bdt_entry_2)
|
||||
{
|
||||
if (bdt_entry_1 && bdt_entry_2) {
|
||||
ct_test(pTest, bdt_entry_1->valid == bdt_entry_2->valid);
|
||||
test_BVLC_Address(
|
||||
pTest, &bdt_entry_1->dest_address, &bdt_entry_2->dest_address);
|
||||
test_BVLC_Broadcast_Distribution_Mask(
|
||||
pTest, &bdt_entry_1->broadcast_mask, &bdt_entry_2->broadcast_mask);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void test_BVLC_Foreign_Device_Table_Entry(Test *pTest,
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY *fdt_entry_1,
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY *fdt_entry_2)
|
||||
{
|
||||
if (fdt_entry_1 && fdt_entry_2) {
|
||||
ct_test(pTest, fdt_entry_1->valid == fdt_entry_2->valid);
|
||||
test_BVLC_Address(
|
||||
pTest, &fdt_entry_1->dest_address, &fdt_entry_2->dest_address);
|
||||
ct_test(pTest, fdt_entry_1->ttl_seconds == fdt_entry_2->ttl_seconds);
|
||||
ct_test(pTest,
|
||||
fdt_entry_1->ttl_seconds_remaining ==
|
||||
fdt_entry_2->ttl_seconds_remaining);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int test_BVLC_Header(Test *pTest,
|
||||
uint8_t *pdu,
|
||||
uint16_t pdu_len,
|
||||
uint8_t *message_type,
|
||||
uint16_t *message_length)
|
||||
|
||||
{
|
||||
int bytes_consumed = 0;
|
||||
int len = 0;
|
||||
|
||||
if (pdu && message_type && message_length) {
|
||||
len = bvlc_decode_header(pdu, pdu_len, message_type, message_length);
|
||||
ct_test(pTest, len == 4);
|
||||
bytes_consumed = len;
|
||||
}
|
||||
|
||||
return bytes_consumed;
|
||||
}
|
||||
|
||||
static void test_BVLC_Result_Code(Test *pTest, uint16_t result_code)
|
||||
{
|
||||
uint8_t pdu[50] = { 0 };
|
||||
uint16_t test_result_code = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
|
||||
len = bvlc_encode_result(pdu, sizeof(pdu), result_code);
|
||||
ct_test(pTest, len == 6);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_RESULT);
|
||||
ct_test(pTest, length == 6);
|
||||
test_len += bvlc_decode_result(&pdu[4], length - 4, &test_result_code);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, result_code == test_result_code);
|
||||
}
|
||||
|
||||
static void test_BVLC_Result(Test *pTest)
|
||||
{
|
||||
uint16_t result_code[] = { BVLC_RESULT_SUCCESSFUL_COMPLETION,
|
||||
BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK,
|
||||
BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK,
|
||||
BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK,
|
||||
BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK,
|
||||
BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK,
|
||||
BVLC_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK };
|
||||
unsigned int i = 0;
|
||||
size_t result_code_max = sizeof(result_code) / sizeof(result_code[0]);
|
||||
|
||||
for (i = 0; i < result_code_max; i++) {
|
||||
test_BVLC_Result_Code(pTest, result_code[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Original_Unicast_NPDU_Message(
|
||||
Test *pTest, uint8_t *npdu, uint16_t npdu_len)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc_encode_original_unicast(pdu, sizeof(pdu), npdu, npdu_len);
|
||||
msg_len = 4 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_ORIGINAL_UNICAST_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_original_unicast(
|
||||
&pdu[4], length - 4, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Original_Unicast_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC_Original_Unicast_NPDU_Message(pTest, npdu, npdu_len);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Original_Unicast_NPDU_Message(pTest, npdu, npdu_len);
|
||||
}
|
||||
|
||||
static void test_BVLC_Original_Broadcast_NPDU_Message(
|
||||
Test *pTest, uint8_t *npdu, uint16_t npdu_len)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc_encode_original_broadcast(pdu, sizeof(pdu), npdu, npdu_len);
|
||||
msg_len = 4 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_ORIGINAL_BROADCAST_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_original_broadcast(
|
||||
&pdu[4], length - 4, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Original_Broadcast_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC_Original_Broadcast_NPDU_Message(pTest, npdu, npdu_len);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Original_Broadcast_NPDU_Message(pTest, npdu, npdu_len);
|
||||
}
|
||||
|
||||
static void test_BVLC_Forwarded_NPDU_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
BACNET_IP_ADDRESS *bip_address)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[75] = { 0 };
|
||||
BACNET_IP_ADDRESS test_bip_address = { 0 };
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc_encode_forwarded_npdu(
|
||||
pdu, sizeof(pdu), bip_address, npdu, npdu_len);
|
||||
msg_len = 1 + 1 + 2 + BIP_ADDRESS_MAX + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_FORWARDED_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_forwarded_npdu(&pdu[4], length - 4,
|
||||
&test_bip_address, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
test_BVLC_Address(pTest, bip_address, &test_bip_address);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Forwarded_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
BACNET_IP_ADDRESS bip_address = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC_Forwarded_NPDU_Message(pTest, npdu, npdu_len, &bip_address);
|
||||
for (i = 0; i < sizeof(bip_address.address); i++) {
|
||||
bip_address.address[i] = i;
|
||||
}
|
||||
bip_address.port = 47808;
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Forwarded_NPDU_Message(pTest, npdu, npdu_len, &bip_address);
|
||||
}
|
||||
|
||||
static void test_BVLC_Register_Foreign_Device_Message(
|
||||
Test *pTest, uint16_t ttl_seconds)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_ttl_seconds = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 6;
|
||||
|
||||
len = bvlc_encode_register_foreign_device(pdu, sizeof(pdu), ttl_seconds);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_REGISTER_FOREIGN_DEVICE);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_register_foreign_device(
|
||||
&pdu[4], length - 4, &test_ttl_seconds);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, ttl_seconds == test_ttl_seconds);
|
||||
}
|
||||
|
||||
static void test_BVLC_Register_Foreign_Device(Test *pTest)
|
||||
{
|
||||
uint16_t ttl_seconds = 0;
|
||||
|
||||
test_BVLC_Register_Foreign_Device_Message(pTest, ttl_seconds);
|
||||
ttl_seconds = 600;
|
||||
test_BVLC_Register_Foreign_Device_Message(pTest, ttl_seconds);
|
||||
}
|
||||
|
||||
static void test_BVLC_Delete_Foreign_Device_Message(
|
||||
Test *pTest, BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY *fdt_entry)
|
||||
{
|
||||
uint8_t pdu[64] = { 0 };
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY test_fdt_entry = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 0x000A;
|
||||
|
||||
len = bvlc_encode_delete_foreign_device(
|
||||
pdu, sizeof(pdu), &fdt_entry->dest_address);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_delete_foreign_device(
|
||||
&pdu[4], length - 4, &test_fdt_entry.dest_address);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
if (msg_len != test_len) {
|
||||
printf("msg:%u test:%u\n", msg_len, test_len);
|
||||
}
|
||||
test_BVLC_Address(
|
||||
pTest, &fdt_entry->dest_address, &test_fdt_entry.dest_address);
|
||||
}
|
||||
|
||||
static void test_BVLC_Delete_Foreign_Device(Test *pTest)
|
||||
{
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY fdt_entry = { 0 };
|
||||
unsigned int i = 0;
|
||||
|
||||
/* test with zeros */
|
||||
test_BVLC_Delete_Foreign_Device_Message(pTest, &fdt_entry);
|
||||
/* test with valid values */
|
||||
for (i = 0; i < sizeof(fdt_entry.dest_address.address); i++) {
|
||||
fdt_entry.dest_address.address[i] = i;
|
||||
}
|
||||
fdt_entry.dest_address.port = 47808;
|
||||
fdt_entry.ttl_seconds = 600;
|
||||
fdt_entry.ttl_seconds_remaining = 42;
|
||||
fdt_entry.next = NULL;
|
||||
test_BVLC_Delete_Foreign_Device_Message(pTest, &fdt_entry);
|
||||
}
|
||||
|
||||
static void test_BVLC_Secure_BVLL_Message(
|
||||
Test *pTest, uint8_t *sbuf, uint16_t sbuf_len)
|
||||
{
|
||||
uint8_t test_sbuf[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_sbuf_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc_encode_secure_bvll(pdu, sizeof(pdu), sbuf, sbuf_len);
|
||||
msg_len = 1 + 1 + 2 + sbuf_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_SECURE_BVLL);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_secure_bvll(
|
||||
&pdu[4], length - 4, test_sbuf, sizeof(test_sbuf), &test_sbuf_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, sbuf_len == test_sbuf_len);
|
||||
for (i = 0; i < sbuf_len; i++) {
|
||||
ct_test(pTest, sbuf[i] == test_sbuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Secure_BVLL(Test *pTest)
|
||||
{
|
||||
uint8_t sbuf[50] = { 0 };
|
||||
uint16_t sbuf_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC_Secure_BVLL_Message(pTest, sbuf, sbuf_len);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(sbuf); i++) {
|
||||
sbuf[i] = i;
|
||||
}
|
||||
sbuf_len = sizeof(sbuf);
|
||||
test_BVLC_Secure_BVLL_Message(pTest, sbuf, sbuf_len);
|
||||
}
|
||||
|
||||
static void test_BVLC_Read_Broadcast_Distribution_Table_Message(Test *pTest)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
|
||||
len = bvlc_encode_read_broadcast_distribution_table(pdu, sizeof(pdu));
|
||||
msg_len = 1 + 1 + 2;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_READ_BROADCAST_DIST_TABLE);
|
||||
ct_test(pTest, length == msg_len);
|
||||
}
|
||||
|
||||
static void test_BVLC_Distribute_Broadcast_To_Network_Message(
|
||||
Test *pTest, uint8_t *npdu, uint16_t npdu_len)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc_encode_distribute_broadcast_to_network(
|
||||
pdu, sizeof(pdu), npdu, npdu_len);
|
||||
msg_len = 4 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_distribute_broadcast_to_network(
|
||||
&pdu[4], length - 4, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Distribute_Broadcast_To_Network(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC_Distribute_Broadcast_To_Network_Message(pTest, npdu, npdu_len);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Distribute_Broadcast_To_Network_Message(pTest, npdu, npdu_len);
|
||||
}
|
||||
|
||||
static void test_BVLC_Write_Broadcast_Distribution_Table_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *bdt_list)
|
||||
{
|
||||
uint8_t pdu[480] = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *test_bdt_list = NULL;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
|
||||
count = bvlc_broadcast_distribution_table_valid_count(bdt_list);
|
||||
test_bdt_list =
|
||||
calloc(count, sizeof(BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY));
|
||||
bvlc_broadcast_distribution_table_link_array(test_bdt_list, count);
|
||||
/* encode the message */
|
||||
len = bvlc_encode_write_broadcast_distribution_table(
|
||||
pdu, sizeof(pdu), bdt_list);
|
||||
msg_len = 4 + (count * BACNET_IP_BDT_ENTRY_SIZE);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_write_broadcast_distribution_table(
|
||||
&pdu[4], length - 4, test_bdt_list);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
for (i = 0; i < count; i++) {
|
||||
test_BVLC_Broadcast_Distribution_Table_Entry(
|
||||
pTest, &bdt_list[i], &test_bdt_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Broadcast_Distribution_Table_Encode(Test *pTest)
|
||||
{
|
||||
uint8_t apdu[480] = { 0 };
|
||||
uint16_t apdu_len = 0;
|
||||
uint16_t test_apdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
uint16_t test_count = 0;
|
||||
bool status = false;
|
||||
BACNET_ERROR_CODE error_code;
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_list[5] = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_entry = { 0 };
|
||||
BACNET_IP_ADDRESS dest_address = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK broadcast_mask = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY test_bdt_list[5] = { 0 };
|
||||
|
||||
/* configure a BDT entry */
|
||||
count = sizeof(bdt_list) / sizeof(bdt_list[0]);
|
||||
bvlc_broadcast_distribution_table_link_array(&bdt_list[0], count);
|
||||
for (i = 0; i < count; i++) {
|
||||
status = bvlc_address_port_from_ascii(
|
||||
&dest_address, "192.168.0.255", "0xBAC0");
|
||||
ct_test(pTest, status);
|
||||
dest_address.port += i;
|
||||
broadcast_mask.address[0] = 255;
|
||||
broadcast_mask.address[1] = 255;
|
||||
broadcast_mask.address[2] = 255;
|
||||
broadcast_mask.address[3] = 255;
|
||||
status = bvlc_broadcast_distribution_table_entry_set(
|
||||
&bdt_entry, &dest_address, &broadcast_mask);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_broadcast_distribution_table_entry_append(
|
||||
&bdt_list[0], &bdt_entry);
|
||||
ct_test(pTest, status);
|
||||
}
|
||||
test_count = bvlc_broadcast_distribution_table_count(&bdt_list[0]);
|
||||
if (test_count != count) {
|
||||
printf("size=%u count=%u\n", count, test_count);
|
||||
}
|
||||
ct_test(pTest, test_count == count);
|
||||
/* test the encode/decode pair */
|
||||
apdu_len = bvlc_broadcast_distribution_table_encode(&apdu[0],
|
||||
sizeof(apdu), &bdt_list[0]);
|
||||
test_count = sizeof(test_bdt_list) / sizeof(test_bdt_list[0]);
|
||||
bvlc_broadcast_distribution_table_link_array(&test_bdt_list[0], test_count);
|
||||
test_apdu_len = bvlc_broadcast_distribution_table_decode(&apdu[0],
|
||||
apdu_len, &error_code, &test_bdt_list[0]);
|
||||
ct_test(pTest, test_apdu_len == apdu_len);
|
||||
count = bvlc_broadcast_distribution_table_count(&test_bdt_list[0]);
|
||||
ct_test(pTest, test_count == count);
|
||||
for (i = 0; i < count; i++) {
|
||||
status = bvlc_broadcast_distribution_table_entry_different(
|
||||
&bdt_list[i], &test_bdt_list[i]);
|
||||
ct_test(pTest, !status);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Write_Broadcast_Distribution_Table(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[480] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
uint16_t test_count = 0;
|
||||
bool status = false;
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_list[5] = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_entry = { 0 };
|
||||
BACNET_IP_ADDRESS dest_address = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK broadcast_mask = { 0 };
|
||||
|
||||
/* configure a BDT entry */
|
||||
count = sizeof(bdt_list) / sizeof(bdt_list[0]);
|
||||
bvlc_broadcast_distribution_table_link_array(&bdt_list[0], count);
|
||||
for (i = 0; i < count; i++) {
|
||||
status = bvlc_address_port_from_ascii(
|
||||
&dest_address, "192.168.0.255", "0xBAC0");
|
||||
ct_test(pTest, status);
|
||||
dest_address.port += i;
|
||||
broadcast_mask.address[0] = 255;
|
||||
broadcast_mask.address[1] = 255;
|
||||
broadcast_mask.address[2] = 255;
|
||||
broadcast_mask.address[3] = 255;
|
||||
status = bvlc_broadcast_distribution_table_entry_set(
|
||||
&bdt_entry, &dest_address, &broadcast_mask);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_broadcast_distribution_table_entry_append(
|
||||
&bdt_list[0], &bdt_entry);
|
||||
ct_test(pTest, status);
|
||||
}
|
||||
test_count = bvlc_broadcast_distribution_table_count(&bdt_list[0]);
|
||||
if (test_count != count) {
|
||||
printf("size=%u count=%u\n", count, test_count);
|
||||
}
|
||||
ct_test(pTest, test_count == count);
|
||||
test_count = bvlc_broadcast_distribution_table_valid_count(&bdt_list[0]);
|
||||
ct_test(pTest, test_count == count);
|
||||
for (i = 1; i < 5; i++) {
|
||||
status = bvlc_broadcast_distribution_table_entry_different(
|
||||
&bdt_list[0], &bdt_list[i]);
|
||||
ct_test(pTest, status);
|
||||
}
|
||||
test_BVLC_Write_Broadcast_Distribution_Table_Message(
|
||||
pTest, npdu, npdu_len, &bdt_list[0]);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Write_Broadcast_Distribution_Table_Message(
|
||||
pTest, npdu, npdu_len, &bdt_list[0]);
|
||||
}
|
||||
|
||||
static void test_BVLC_Read_Broadcast_Distribution_Table_Ack_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *bdt_list)
|
||||
{
|
||||
uint8_t pdu[480] = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY *test_bdt_list = NULL;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
|
||||
count = bvlc_broadcast_distribution_table_valid_count(bdt_list);
|
||||
test_bdt_list =
|
||||
calloc(count, sizeof(BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY));
|
||||
bvlc_broadcast_distribution_table_link_array(test_bdt_list, count);
|
||||
/* encode the message */
|
||||
len = bvlc_encode_read_broadcast_distribution_table_ack(
|
||||
pdu, sizeof(pdu), bdt_list);
|
||||
msg_len = 4 + (count * BACNET_IP_BDT_ENTRY_SIZE);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_READ_BROADCAST_DIST_TABLE_ACK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_read_broadcast_distribution_table_ack(
|
||||
&pdu[4], length - 4, test_bdt_list);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
for (i = 0; i < count; i++) {
|
||||
test_BVLC_Broadcast_Distribution_Table_Entry(
|
||||
pTest, &bdt_list[i], &test_bdt_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Read_Broadcast_Distribution_Table_Ack(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[480] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
uint16_t test_count = 0;
|
||||
bool status = false;
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_list[5] = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_TABLE_ENTRY bdt_entry = { 0 };
|
||||
|
||||
/* configure a BDT entry */
|
||||
count = sizeof(bdt_list) / sizeof(bdt_list[0]);
|
||||
for (i = 0; i < count; i++) {
|
||||
status = bvlc_address_port_from_ascii(
|
||||
&bdt_entry.dest_address, "192.168.0.255", "0xBAC0");
|
||||
ct_test(pTest, status);
|
||||
bdt_entry.dest_address.port += i;
|
||||
bdt_entry.broadcast_mask.address[0] = 255;
|
||||
bdt_entry.broadcast_mask.address[1] = 255;
|
||||
bdt_entry.broadcast_mask.address[2] = 255;
|
||||
bdt_entry.broadcast_mask.address[3] = 255;
|
||||
status = bvlc_broadcast_distribution_table_entry_copy(
|
||||
&bdt_list[i], &bdt_entry);
|
||||
ct_test(pTest, status);
|
||||
bdt_list[i].valid = true;
|
||||
if (i > 0) {
|
||||
bdt_list[i - 1].next = &bdt_list[i];
|
||||
}
|
||||
bdt_list[i].next = NULL;
|
||||
}
|
||||
test_count = bvlc_broadcast_distribution_table_count(&bdt_list[0]);
|
||||
if (test_count != count) {
|
||||
printf("size=%u count=%u\n", count, test_count);
|
||||
}
|
||||
ct_test(pTest, test_count == count);
|
||||
test_count = bvlc_broadcast_distribution_table_valid_count(&bdt_list[0]);
|
||||
ct_test(pTest, test_count == count);
|
||||
bvlc_broadcast_distribution_table_entry_copy(&bdt_entry, &bdt_list[0]);
|
||||
status = bvlc_broadcast_distribution_table_entry_different(
|
||||
&bdt_entry, &bdt_list[0]);
|
||||
ct_test(pTest, !status);
|
||||
test_BVLC_Read_Broadcast_Distribution_Table_Ack_Message(
|
||||
pTest, npdu, npdu_len, &bdt_list[0]);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Read_Broadcast_Distribution_Table_Ack_Message(
|
||||
pTest, npdu, npdu_len, &bdt_list[0]);
|
||||
}
|
||||
|
||||
static void test_BVLC_Read_Foreign_Device_Table_Ack_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY *fdt_list)
|
||||
{
|
||||
uint8_t pdu[480] = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY *test_fdt_list = NULL;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
|
||||
count = bvlc_foreign_device_table_valid_count(fdt_list);
|
||||
test_fdt_list = calloc(count, sizeof(BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY));
|
||||
bvlc_foreign_device_table_link_array(test_fdt_list, count);
|
||||
/* encode the message */
|
||||
len = bvlc_encode_read_foreign_device_table_ack(pdu, sizeof(pdu), fdt_list);
|
||||
msg_len = 4 + (count * BACNET_IP_FDT_ENTRY_SIZE);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC_READ_FOREIGN_DEVICE_TABLE_ACK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc_decode_read_foreign_device_table_ack(
|
||||
&pdu[4], length - 4, test_fdt_list);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
for (i = 0; i < count; i++) {
|
||||
test_BVLC_Foreign_Device_Table_Entry(
|
||||
pTest, &fdt_list[i], &test_fdt_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Read_Foreign_Device_Table_Ack(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[480] = { 0 };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t count = 0;
|
||||
uint16_t test_count = 0;
|
||||
uint16_t test_port_start = 0xBAC1;
|
||||
bool status = false;
|
||||
BACNET_IP_FOREIGN_DEVICE_TABLE_ENTRY fdt_list[5] = { 0 };
|
||||
BACNET_IP_ADDRESS dest_address = { 0 };
|
||||
|
||||
status = bvlc_address_from_ascii(&dest_address, "192.168.0.1");
|
||||
ct_test(pTest, status);
|
||||
/* configure a FDT entry */
|
||||
count = sizeof(fdt_list) / sizeof(fdt_list[0]);
|
||||
bvlc_foreign_device_table_link_array(fdt_list, count);
|
||||
for (i = 0; i < count; i++) {
|
||||
dest_address.port = test_port_start + i;
|
||||
status = bvlc_foreign_device_table_entry_add(
|
||||
&fdt_list[0], &dest_address, 12345);
|
||||
ct_test(pTest, status);
|
||||
/* add again should only update TTL */
|
||||
status = bvlc_foreign_device_table_entry_add(
|
||||
&fdt_list[0], &dest_address, 12345);
|
||||
ct_test(pTest, status);
|
||||
}
|
||||
test_count = bvlc_foreign_device_table_count(fdt_list);
|
||||
ct_test(pTest, test_count == count);
|
||||
if (test_count != count) {
|
||||
printf("size=%u count=%u\n", count, test_count);
|
||||
}
|
||||
test_count = bvlc_foreign_device_table_valid_count(fdt_list);
|
||||
ct_test(pTest, test_count == count);
|
||||
test_BVLC_Read_Foreign_Device_Table_Ack_Message(
|
||||
pTest, npdu, npdu_len, fdt_list);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
test_BVLC_Read_Foreign_Device_Table_Ack_Message(
|
||||
pTest, npdu, npdu_len, fdt_list);
|
||||
/* cleanup */
|
||||
for (i = 0; i < count; i++) {
|
||||
dest_address.port = test_port_start + i;
|
||||
status =
|
||||
bvlc_foreign_device_table_entry_delete(&fdt_list[0], &dest_address);
|
||||
ct_test(pTest, status);
|
||||
}
|
||||
test_count = bvlc_foreign_device_table_valid_count(fdt_list);
|
||||
ct_test(pTest, test_count == 0);
|
||||
}
|
||||
|
||||
static void test_BVLC_Address_Copy(Test *pTest)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
BACNET_IP_ADDRESS src = { 0 };
|
||||
BACNET_IP_ADDRESS dst = { 0 };
|
||||
bool status = false;
|
||||
|
||||
/* test with zeros */
|
||||
status = bvlc_address_copy(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, !status);
|
||||
/* test with valid values */
|
||||
for (i = 0; i < sizeof(src.address); i++) {
|
||||
src.address[i] = 1 + i;
|
||||
}
|
||||
src.port = 47808;
|
||||
status = bvlc_address_copy(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, !status);
|
||||
/* test for different port */
|
||||
dst.port = 47809;
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
/* test for different address */
|
||||
dst.port = src.port;
|
||||
for (i = 0; i < sizeof(src.address); i++) {
|
||||
dst.address[i] = 0;
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
dst.address[i] = 1 + i;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC_Address_Get_Set(Test *pTest)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
BACNET_ADDRESS bsrc = { 0 };
|
||||
BACNET_IP_ADDRESS src = { 0 };
|
||||
BACNET_IP_ADDRESS dst = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK mask = { 0 };
|
||||
BACNET_IP_BROADCAST_DISTRIBUTION_MASK test_mask = { 0 };
|
||||
const uint32_t broadcast_mask = 0x12345678;
|
||||
uint32_t test_broadcast_mask = 0;
|
||||
uint8_t octet0 = 192;
|
||||
uint8_t octet1 = 168;
|
||||
uint8_t octet2 = 1;
|
||||
uint8_t octet3 = 255;
|
||||
uint8_t test_octet0 = 0;
|
||||
uint8_t test_octet1 = 0;
|
||||
uint8_t test_octet2 = 0;
|
||||
uint8_t test_octet3 = 0;
|
||||
const uint16_t dnet = 12345;
|
||||
uint16_t snet = 0;
|
||||
bool status = false;
|
||||
|
||||
for (i = 0; i < 255; i++) {
|
||||
octet0 = i;
|
||||
octet1 = i;
|
||||
octet2 = i;
|
||||
octet3 = i;
|
||||
status = bvlc_address_set(&src, octet0, octet1, octet2, octet3);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_get(
|
||||
&src, &test_octet0, &test_octet1, &test_octet2, &test_octet3);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, octet0 == test_octet0);
|
||||
ct_test(pTest, octet1 == test_octet1);
|
||||
ct_test(pTest, octet2 == test_octet2);
|
||||
ct_test(pTest, octet3 == test_octet3);
|
||||
}
|
||||
/* test the ASCII hex to address */
|
||||
/* test invalid */
|
||||
status = bvlc_address_from_ascii(&src, "256");
|
||||
ct_test(pTest, status == false);
|
||||
status = bvlc_address_from_ascii(&src, "192.168.0.1");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_set(&dst, 192, 168, 0, 1);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, status == false);
|
||||
/* test zero compression */
|
||||
status = bvlc_address_from_ascii(&src, "127...");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_set(&dst, 127, 0, 0, 0);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
if (status) {
|
||||
status = bvlc_address_get(
|
||||
&src, &test_octet0, &test_octet1, &test_octet2, &test_octet3);
|
||||
printf("src:%u.%u.%u.%u\n", (unsigned)test_octet0,
|
||||
(unsigned)test_octet1, (unsigned)test_octet2,
|
||||
(unsigned)test_octet3);
|
||||
status = bvlc_address_get(
|
||||
&dst, &test_octet0, &test_octet1, &test_octet2, &test_octet3);
|
||||
printf("dst:%u.%u.%u.%u\n", (unsigned)test_octet0,
|
||||
(unsigned)test_octet1, (unsigned)test_octet2,
|
||||
(unsigned)test_octet3);
|
||||
}
|
||||
ct_test(pTest, status == false);
|
||||
/* BACnet to IPv4 address conversions */
|
||||
status = bvlc_address_port_from_ascii(&src, "192.168.0.1", "0xBAC0");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_ip_address_to_bacnet_local(&bsrc, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_ip_address_from_bacnet_local(&dst, &bsrc);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_address_different(&dst, &src);
|
||||
ct_test(pTest, !status);
|
||||
status = bvlc_ip_address_to_bacnet_remote(&bsrc, dnet, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc_ip_address_from_bacnet_remote(&dst, &snet, &bsrc);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, snet == dnet);
|
||||
status = bvlc_ip_address_from_bacnet_remote(&dst, NULL, &bsrc);
|
||||
ct_test(pTest, status);
|
||||
/* Broadcast Distribution Mask conversions */
|
||||
status = bvlc_broadcast_distribution_mask_from_host(&mask, broadcast_mask);
|
||||
ct_test(pTest, status);
|
||||
status =
|
||||
bvlc_broadcast_distribution_mask_to_host(&test_broadcast_mask, &mask);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, test_broadcast_mask == broadcast_mask);
|
||||
octet0 = 0x12;
|
||||
octet1 = 0x34;
|
||||
octet2 = 0x56;
|
||||
octet3 = 0x78;
|
||||
bvlc_broadcast_distribution_mask_set(
|
||||
&test_mask, octet0, octet1, octet2, octet3);
|
||||
status = bvlc_broadcast_distribution_mask_different(&mask, &test_mask);
|
||||
ct_test(pTest, !status);
|
||||
bvlc_broadcast_distribution_mask_get(
|
||||
&test_mask, &test_octet0, &test_octet1, &test_octet2, &test_octet3);
|
||||
ct_test(pTest, octet0 == test_octet0);
|
||||
ct_test(pTest, octet1 == test_octet1);
|
||||
ct_test(pTest, octet2 == test_octet2);
|
||||
ct_test(pTest, octet3 == test_octet3);
|
||||
}
|
||||
|
||||
void test_BVLC(Test *pTest)
|
||||
{
|
||||
bool rc;
|
||||
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Result);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest,
|
||||
test_BVLC_Broadcast_Distribution_Table_Encode);
|
||||
assert(rc);
|
||||
rc =
|
||||
ct_addTestFunction(pTest, test_BVLC_Write_Broadcast_Distribution_Table);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(
|
||||
pTest, test_BVLC_Read_Broadcast_Distribution_Table_Message);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(
|
||||
pTest, test_BVLC_Read_Broadcast_Distribution_Table_Ack);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Forwarded_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Register_Foreign_Device);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Read_Foreign_Device_Table_Ack);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Delete_Foreign_Device);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Distribute_Broadcast_To_Network);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Original_Unicast_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Original_Broadcast_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Secure_BVLL);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Address_Copy);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC_Address_Get_Set);
|
||||
assert(rc);
|
||||
}
|
||||
|
||||
#ifdef TEST_BVLC
|
||||
int main(void)
|
||||
{
|
||||
Test *pTest;
|
||||
|
||||
pTest = ct_create("BACnet Virtual Link Control IP/v4", NULL);
|
||||
test_BVLC(pTest);
|
||||
/* configure output */
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
(void)ct_report(pTest);
|
||||
ct_destroy(pTest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* TEST_BBMD */
|
||||
#endif /* BAC_TEST */
|
||||
|
||||
@@ -511,12 +511,6 @@ extern "C" {
|
||||
BACNET_STACK_EXPORT
|
||||
const char *bvlc_result_code_name(uint16_t result_code);
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include "ctest.h"
|
||||
BACNET_STACK_EXPORT
|
||||
void test_BVLC(Test *pTest);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
@@ -1572,798 +1572,3 @@ int bvlc6_decode_distribute_broadcast_to_network(uint8_t *pdu,
|
||||
|
||||
return bytes_consumed;
|
||||
}
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "ctest.h"
|
||||
|
||||
static void test_BVLC6_Address(Test *pTest,
|
||||
BACNET_IP6_ADDRESS *bip6_address_1,
|
||||
BACNET_IP6_ADDRESS *bip6_address_2)
|
||||
{
|
||||
unsigned i = 0;
|
||||
|
||||
if (bip6_address_1 && bip6_address_2) {
|
||||
ct_test(pTest, bip6_address_1->port == bip6_address_2->port);
|
||||
for (i = 0; i < IP6_ADDRESS_MAX; i++) {
|
||||
ct_test(pTest,
|
||||
bip6_address_1->address[i] == bip6_address_2->address[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int test_BVLC6_Header(Test *pTest,
|
||||
uint8_t *pdu,
|
||||
uint16_t pdu_len,
|
||||
uint8_t *message_type,
|
||||
uint16_t *length)
|
||||
|
||||
{
|
||||
int bytes_consumed = 0;
|
||||
int len = 0;
|
||||
|
||||
if (pdu && message_type && length) {
|
||||
len = bvlc6_decode_header(pdu, pdu_len, message_type, length);
|
||||
ct_test(pTest, len == 4);
|
||||
bytes_consumed = len;
|
||||
}
|
||||
|
||||
return bytes_consumed;
|
||||
}
|
||||
|
||||
static void test_BVLC6_Result_Code(
|
||||
Test *pTest, uint32_t vmac, uint16_t result_code)
|
||||
{
|
||||
uint8_t pdu[50] = { 0 };
|
||||
uint32_t test_vmac = 0;
|
||||
uint16_t test_result_code = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
|
||||
len = bvlc6_encode_result(pdu, sizeof(pdu), vmac, result_code);
|
||||
ct_test(pTest, len == 9);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_RESULT);
|
||||
ct_test(pTest, length == 9);
|
||||
test_len +=
|
||||
bvlc6_decode_result(&pdu[4], length - 4, &test_vmac, &test_result_code);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, vmac == test_vmac);
|
||||
ct_test(pTest, result_code == test_result_code);
|
||||
len = bvlc6_encode_result(pdu, sizeof(pdu), 0xffffff + 1, result_code);
|
||||
ct_test(pTest, len == 0);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Result(Test *pTest)
|
||||
{
|
||||
uint32_t vmac = 0;
|
||||
uint16_t result_code[6] = { BVLC6_RESULT_SUCCESSFUL_COMPLETION,
|
||||
BVLC6_RESULT_ADDRESS_RESOLUTION_NAK,
|
||||
BVLC6_RESULT_VIRTUAL_ADDRESS_RESOLUTION_NAK,
|
||||
BVLC6_RESULT_REGISTER_FOREIGN_DEVICE_NAK,
|
||||
BVLC6_RESULT_DELETE_FOREIGN_DEVICE_NAK,
|
||||
BVLC6_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK };
|
||||
unsigned int i = 0;
|
||||
|
||||
vmac = 4194303;
|
||||
for (i = 0; i < 6; i++) {
|
||||
test_BVLC6_Result_Code(pTest, vmac, result_code[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Original_Unicast_NPDU_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
uint32_t vmac_src,
|
||||
uint32_t vmac_dst)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint32_t test_vmac_dst = 0;
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc6_encode_original_unicast(
|
||||
pdu, sizeof(pdu), vmac_src, vmac_dst, npdu, npdu_len);
|
||||
msg_len = 10 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_ORIGINAL_UNICAST_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len +=
|
||||
bvlc6_decode_original_unicast(&pdu[4], length - 4, &test_vmac_src,
|
||||
&test_vmac_dst, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, vmac_dst == test_vmac_dst);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Original_Unicast_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint32_t vmac_src = 0;
|
||||
uint32_t vmac_dst = 0;
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Original_Unicast_NPDU_Message(
|
||||
pTest, npdu, npdu_len, vmac_src, vmac_dst);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
vmac_src = 4194303;
|
||||
vmac_dst = 4194302;
|
||||
test_BVLC6_Original_Unicast_NPDU_Message(
|
||||
pTest, npdu, npdu_len, vmac_src, vmac_dst);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Original_Broadcast_NPDU_Message(
|
||||
Test *pTest, uint8_t *npdu, uint16_t npdu_len, uint32_t vmac)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac = 0;
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len =
|
||||
bvlc6_encode_original_broadcast(pdu, sizeof(pdu), vmac, npdu, npdu_len);
|
||||
msg_len = 7 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_ORIGINAL_BROADCAST_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_original_broadcast(&pdu[4], length - 4, &test_vmac,
|
||||
test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac == test_vmac);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Original_Broadcast_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint32_t vmac = 0;
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Original_Broadcast_NPDU_Message(pTest, npdu, npdu_len, vmac);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
vmac = 4194303;
|
||||
test_BVLC6_Original_Broadcast_NPDU_Message(pTest, npdu, npdu_len, vmac);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Resolution_Message(
|
||||
Test *pTest, uint32_t vmac_src, uint32_t vmac_target)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint32_t test_vmac_target = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 10;
|
||||
|
||||
len = bvlc6_encode_address_resolution(
|
||||
pdu, sizeof(pdu), vmac_src, vmac_target);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_ADDRESS_RESOLUTION);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_address_resolution(
|
||||
&pdu[4], length - 4, &test_vmac_src, &test_vmac_target);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, vmac_target == test_vmac_target);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Resolution(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
uint32_t vmac_target = 0;
|
||||
|
||||
test_BVLC6_Address_Resolution_Message(pTest, vmac_src, vmac_target);
|
||||
vmac_src = 4194303;
|
||||
vmac_target = 4194302;
|
||||
test_BVLC6_Address_Resolution_Message(pTest, vmac_src, vmac_target);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Forwarded_Address_Resolution_Message(Test *pTest,
|
||||
uint32_t vmac_src,
|
||||
uint32_t vmac_dst,
|
||||
BACNET_IP6_ADDRESS *bip6_address)
|
||||
{
|
||||
BACNET_IP6_ADDRESS test_bip6_address = { { 0 } };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint32_t test_vmac_dst = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 4 + 3 + 3 + BIP6_ADDRESS_MAX;
|
||||
|
||||
len = bvlc6_encode_forwarded_address_resolution(
|
||||
pdu, sizeof(pdu), vmac_src, vmac_dst, bip6_address);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_FORWARDED_ADDRESS_RESOLUTION);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_forwarded_address_resolution(&pdu[4], length - 4,
|
||||
&test_vmac_src, &test_vmac_dst, &test_bip6_address);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, vmac_dst == test_vmac_dst);
|
||||
test_BVLC6_Address(pTest, bip6_address, &test_bip6_address);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Forwarded_Address_Resolution(Test *pTest)
|
||||
{
|
||||
BACNET_IP6_ADDRESS bip6_address = { { 0 } };
|
||||
uint32_t vmac_src = 0;
|
||||
uint32_t vmac_target = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Forwarded_Address_Resolution_Message(
|
||||
pTest, vmac_src, vmac_target, &bip6_address);
|
||||
/* now with some address data */
|
||||
for (i = 0; i < sizeof(bip6_address.address); i++) {
|
||||
bip6_address.address[i] = i;
|
||||
}
|
||||
bip6_address.port = 47808;
|
||||
vmac_src = 4194303;
|
||||
vmac_target = 4194302;
|
||||
test_BVLC6_Forwarded_Address_Resolution_Message(
|
||||
pTest, vmac_src, vmac_target, &bip6_address);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Resolution_Ack_Message(
|
||||
Test *pTest, uint32_t vmac_src, uint32_t vmac_dst)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint32_t test_vmac_dst = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 10;
|
||||
|
||||
len = bvlc6_encode_address_resolution_ack(
|
||||
pdu, sizeof(pdu), vmac_src, vmac_dst);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_ADDRESS_RESOLUTION_ACK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_address_resolution_ack(
|
||||
&pdu[4], length - 4, &test_vmac_src, &test_vmac_dst);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, vmac_dst == test_vmac_dst);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Resolution_Ack(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
uint32_t vmac_dst = 0;
|
||||
|
||||
test_BVLC6_Address_Resolution_Ack_Message(pTest, vmac_src, vmac_dst);
|
||||
vmac_src = 4194303;
|
||||
vmac_dst = 4194302;
|
||||
test_BVLC6_Address_Resolution_Ack_Message(pTest, vmac_src, vmac_dst);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Virtual_Address_Resolution_Message(
|
||||
Test *pTest, uint32_t vmac_src)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 7;
|
||||
|
||||
len = bvlc6_encode_virtual_address_resolution(pdu, sizeof(pdu), vmac_src);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_VIRTUAL_ADDRESS_RESOLUTION);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_virtual_address_resolution(
|
||||
&pdu[4], length - 4, &test_vmac_src);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Virtual_Address_Resolution(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
|
||||
test_BVLC6_Virtual_Address_Resolution_Message(pTest, vmac_src);
|
||||
vmac_src = 0x1234;
|
||||
test_BVLC6_Virtual_Address_Resolution_Message(pTest, vmac_src);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Virtual_Address_Resolution_Ack_Message(
|
||||
Test *pTest, uint32_t vmac_src, uint32_t vmac_dst)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint32_t test_vmac_dst = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 10;
|
||||
|
||||
len = bvlc6_encode_virtual_address_resolution_ack(
|
||||
pdu, sizeof(pdu), vmac_src, vmac_dst);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_VIRTUAL_ADDRESS_RESOLUTION_ACK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_virtual_address_resolution_ack(
|
||||
&pdu[4], length - 4, &test_vmac_src, &test_vmac_dst);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, vmac_dst == test_vmac_dst);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Virtual_Address_Resolution_Ack(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
uint32_t vmac_dst = 0;
|
||||
|
||||
test_BVLC6_Virtual_Address_Resolution_Ack_Message(
|
||||
pTest, vmac_src, vmac_dst);
|
||||
vmac_src = 4194303;
|
||||
vmac_dst = 4194302;
|
||||
test_BVLC6_Virtual_Address_Resolution_Ack_Message(
|
||||
pTest, vmac_src, vmac_dst);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Forwarded_NPDU_Message(Test *pTest,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len,
|
||||
uint32_t vmac_src,
|
||||
BACNET_IP6_ADDRESS *bip6_address)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[75] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
BACNET_IP6_ADDRESS test_bip6_address = { { 0 } };
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc6_encode_forwarded_npdu(
|
||||
pdu, sizeof(pdu), vmac_src, bip6_address, npdu, npdu_len);
|
||||
msg_len = 1 + 1 + 2 + 3 + BIP6_ADDRESS_MAX + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_FORWARDED_NPDU);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_forwarded_npdu(&pdu[4], length - 4, &test_vmac_src,
|
||||
&test_bip6_address, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
test_BVLC6_Address(pTest, bip6_address, &test_bip6_address);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Forwarded_NPDU(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint32_t vmac_src = 0;
|
||||
BACNET_IP6_ADDRESS bip6_address = { { 0 } };
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Forwarded_NPDU_Message(
|
||||
pTest, npdu, npdu_len, vmac_src, &bip6_address);
|
||||
for (i = 0; i < sizeof(bip6_address.address); i++) {
|
||||
bip6_address.address[i] = i;
|
||||
}
|
||||
bip6_address.port = 47808;
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
vmac_src = 4194303;
|
||||
test_BVLC6_Forwarded_NPDU_Message(
|
||||
pTest, npdu, npdu_len, vmac_src, &bip6_address);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Register_Foreign_Device_Message(
|
||||
Test *pTest, uint32_t vmac_src, uint16_t ttl_seconds)
|
||||
{
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
uint16_t test_ttl_seconds = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 9;
|
||||
|
||||
len = bvlc6_encode_register_foreign_device(
|
||||
pdu, sizeof(pdu), vmac_src, ttl_seconds);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_REGISTER_FOREIGN_DEVICE);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_register_foreign_device(
|
||||
&pdu[4], length - 4, &test_vmac_src, &test_ttl_seconds);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
ct_test(pTest, ttl_seconds == test_ttl_seconds);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Register_Foreign_Device(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
uint16_t ttl_seconds = 0;
|
||||
|
||||
test_BVLC6_Register_Foreign_Device_Message(pTest, vmac_src, ttl_seconds);
|
||||
vmac_src = 4194303;
|
||||
ttl_seconds = 600;
|
||||
test_BVLC6_Register_Foreign_Device_Message(pTest, vmac_src, ttl_seconds);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Delete_Foreign_Device_Message(Test *pTest,
|
||||
uint32_t vmac_src,
|
||||
BACNET_IP6_FOREIGN_DEVICE_TABLE_ENTRY *fdt_entry)
|
||||
{
|
||||
uint8_t pdu[64] = { 0 };
|
||||
uint32_t test_vmac_src = 0;
|
||||
BACNET_IP6_FOREIGN_DEVICE_TABLE_ENTRY test_fdt_entry = { 0 };
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, test_len = 0;
|
||||
const int msg_len = 0x0019;
|
||||
|
||||
len = bvlc6_encode_delete_foreign_device(
|
||||
pdu, sizeof(pdu), vmac_src, &fdt_entry->bip6_address);
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_DELETE_FOREIGN_DEVICE);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_delete_foreign_device(
|
||||
&pdu[4], length - 4, &test_vmac_src, &test_fdt_entry.bip6_address);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac_src == test_vmac_src);
|
||||
test_BVLC6_Address(
|
||||
pTest, &fdt_entry->bip6_address, &test_fdt_entry.bip6_address);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Delete_Foreign_Device(Test *pTest)
|
||||
{
|
||||
uint32_t vmac_src = 0;
|
||||
BACNET_IP6_FOREIGN_DEVICE_TABLE_ENTRY fdt_entry = { 0 };
|
||||
unsigned int i = 0;
|
||||
|
||||
/* test with zeros */
|
||||
test_BVLC6_Delete_Foreign_Device_Message(pTest, vmac_src, &fdt_entry);
|
||||
/* test with valid values */
|
||||
vmac_src = 4194303;
|
||||
for (i = 0; i < sizeof(fdt_entry.bip6_address.address); i++) {
|
||||
fdt_entry.bip6_address.address[i] = i;
|
||||
}
|
||||
fdt_entry.bip6_address.port = 47808;
|
||||
fdt_entry.ttl_seconds = 600;
|
||||
fdt_entry.ttl_seconds_remaining = 42;
|
||||
fdt_entry.next = NULL;
|
||||
test_BVLC6_Delete_Foreign_Device_Message(pTest, vmac_src, &fdt_entry);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Secure_BVLL_Message(
|
||||
Test *pTest, uint8_t *sbuf, uint16_t sbuf_len)
|
||||
{
|
||||
uint8_t test_sbuf[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint16_t test_sbuf_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc6_encode_secure_bvll(pdu, sizeof(pdu), sbuf, sbuf_len);
|
||||
msg_len = 1 + 1 + 2 + sbuf_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_SECURE_BVLL);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_secure_bvll(
|
||||
&pdu[4], length - 4, test_sbuf, sizeof(test_sbuf), &test_sbuf_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, sbuf_len == test_sbuf_len);
|
||||
for (i = 0; i < sbuf_len; i++) {
|
||||
ct_test(pTest, sbuf[i] == test_sbuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Secure_BVLL(Test *pTest)
|
||||
{
|
||||
uint8_t sbuf[50] = { 0 };
|
||||
uint16_t sbuf_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Secure_BVLL_Message(pTest, sbuf, sbuf_len);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(sbuf); i++) {
|
||||
sbuf[i] = i;
|
||||
}
|
||||
sbuf_len = sizeof(sbuf);
|
||||
test_BVLC6_Secure_BVLL_Message(pTest, sbuf, sbuf_len);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Distribute_Broadcast_To_Network_Message(
|
||||
Test *pTest, uint8_t *npdu, uint16_t npdu_len, uint32_t vmac)
|
||||
{
|
||||
uint8_t test_npdu[50] = { 0 };
|
||||
uint8_t pdu[60] = { 0 };
|
||||
uint32_t test_vmac = 0;
|
||||
uint16_t test_npdu_len = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint16_t length = 0;
|
||||
int len = 0, msg_len = 0, test_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
len = bvlc6_encode_distribute_broadcast_to_network(
|
||||
pdu, sizeof(pdu), vmac, npdu, npdu_len);
|
||||
msg_len = 7 + npdu_len;
|
||||
ct_test(pTest, len == msg_len);
|
||||
test_len = test_BVLC6_Header(pTest, pdu, len, &message_type, &length);
|
||||
ct_test(pTest, test_len == 4);
|
||||
ct_test(pTest, message_type == BVLC6_DISTRIBUTE_BROADCAST_TO_NETWORK);
|
||||
ct_test(pTest, length == msg_len);
|
||||
test_len += bvlc6_decode_distribute_broadcast_to_network(&pdu[4],
|
||||
length - 4, &test_vmac, test_npdu, sizeof(test_npdu), &test_npdu_len);
|
||||
ct_test(pTest, len == test_len);
|
||||
ct_test(pTest, msg_len == test_len);
|
||||
ct_test(pTest, vmac == test_vmac);
|
||||
ct_test(pTest, npdu_len == test_npdu_len);
|
||||
for (i = 0; i < npdu_len; i++) {
|
||||
ct_test(pTest, npdu[i] == test_npdu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Distribute_Broadcast_To_Network(Test *pTest)
|
||||
{
|
||||
uint8_t npdu[50] = { 0 };
|
||||
uint32_t vmac = 0;
|
||||
uint16_t npdu_len = 0;
|
||||
uint16_t i = 0;
|
||||
|
||||
test_BVLC6_Distribute_Broadcast_To_Network_Message(
|
||||
pTest, npdu, npdu_len, vmac);
|
||||
/* now with some NPDU data */
|
||||
for (i = 0; i < sizeof(npdu); i++) {
|
||||
npdu[i] = i;
|
||||
}
|
||||
npdu_len = sizeof(npdu);
|
||||
vmac = 4194303;
|
||||
test_BVLC6_Distribute_Broadcast_To_Network_Message(
|
||||
pTest, npdu, npdu_len, vmac);
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Copy(Test *pTest)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
BACNET_IP6_ADDRESS src = { { 0 } };
|
||||
BACNET_IP6_ADDRESS dst = { { 0 } };
|
||||
bool status = false;
|
||||
|
||||
/* test with zeros */
|
||||
status = bvlc6_address_copy(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, !status);
|
||||
/* test with valid values */
|
||||
for (i = 0; i < sizeof(src.address); i++) {
|
||||
src.address[i] = 1 + i;
|
||||
}
|
||||
src.port = 47808;
|
||||
status = bvlc6_address_copy(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, !status);
|
||||
/* test for different port */
|
||||
dst.port = 47809;
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
/* test for different address */
|
||||
dst.port = src.port;
|
||||
for (i = 0; i < sizeof(src.address); i++) {
|
||||
dst.address[i] = 0;
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, status);
|
||||
dst.address[i] = 1 + i;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_BVLC6_Address_Get_Set(Test *pTest)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
BACNET_IP6_ADDRESS src = { { 0 } };
|
||||
BACNET_IP6_ADDRESS dst = { { 0 } };
|
||||
uint16_t group = 1;
|
||||
uint16_t test_group = 0;
|
||||
uint16_t hextet[8] = { 0 };
|
||||
bool status = false;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
status = bvlc6_address_set(&src, group, 0, 0, 0, 0, 0, 0, 0);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_get(
|
||||
&src, &test_group, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, group == test_group);
|
||||
group = group << 1;
|
||||
}
|
||||
/* test the ASCII hex to address */
|
||||
/* test too short */
|
||||
status = bvlc6_address_from_ascii(&src, "[1234:5678]");
|
||||
ct_test(pTest, status == false);
|
||||
status = bvlc6_address_from_ascii(
|
||||
&src, "[1234:5678:9ABC:DEF0:1234:5678:9ABC:DEF0]");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_set(
|
||||
&dst, 0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1234, 0x5678, 0x9ABC, 0xDEF0);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, status == false);
|
||||
/* test zero compression */
|
||||
status = bvlc6_address_from_ascii(&src, "[1234:5678:9ABC::5678:9ABC:DEF0]");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_set(
|
||||
&dst, 0x1234, 0x5678, 0x9ABC, 0x0000, 0x0000, 0x5678, 0x9ABC, 0xDEF0);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
if (status) {
|
||||
status = bvlc6_address_get(&src, &hextet[0], &hextet[1], &hextet[2],
|
||||
&hextet[3], &hextet[4], &hextet[5], &hextet[6], &hextet[7]);
|
||||
printf("src:[%X:%X:%X:%X:%X:%X:%X:%X]\n", hextet[0], hextet[1],
|
||||
hextet[2], hextet[3], hextet[4], hextet[5], hextet[6], hextet[7]);
|
||||
status = bvlc6_address_get(&dst, &hextet[0], &hextet[1], &hextet[2],
|
||||
&hextet[3], &hextet[4], &hextet[5], &hextet[6], &hextet[7]);
|
||||
printf("dst:[%X:%X:%X:%X:%X:%X:%X:%X]\n", hextet[0], hextet[1],
|
||||
hextet[2], hextet[3], hextet[4], hextet[5], hextet[6], hextet[7]);
|
||||
}
|
||||
ct_test(pTest, status == false);
|
||||
/* test some compressed 16-bit zero fields */
|
||||
status =
|
||||
bvlc6_address_from_ascii(&src, "[234:678:ABC:EF0:1234:5678:9ABC:DEF0]");
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_set(
|
||||
&dst, 0x0234, 0x0678, 0x0ABC, 0x0EF0, 0x1234, 0x5678, 0x9ABC, 0xDEF0);
|
||||
ct_test(pTest, status);
|
||||
status = bvlc6_address_different(&dst, &src);
|
||||
ct_test(pTest, status == false);
|
||||
}
|
||||
|
||||
static void test_BVLC6_VMAC_Address_Get_Set(Test *pTest)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
BACNET_ADDRESS addr;
|
||||
uint32_t device_id = 1;
|
||||
uint32_t test_device_id = 0;
|
||||
bool status = false;
|
||||
|
||||
for (i = 0; i < 24; i++) {
|
||||
status = bvlc6_vmac_address_set(&addr, device_id);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, addr.mac_len == 3);
|
||||
ct_test(pTest, addr.net == 0);
|
||||
ct_test(pTest, addr.len == 0);
|
||||
status = bvlc6_vmac_address_get(&addr, &test_device_id);
|
||||
ct_test(pTest, status);
|
||||
ct_test(pTest, device_id == test_device_id);
|
||||
device_id = device_id << 1;
|
||||
}
|
||||
}
|
||||
|
||||
void test_BVLC6(Test *pTest)
|
||||
{
|
||||
bool rc;
|
||||
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Result);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Original_Unicast_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Original_Broadcast_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Address_Resolution);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Forwarded_Address_Resolution);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Address_Resolution_Ack);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Virtual_Address_Resolution);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Virtual_Address_Resolution_Ack);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Forwarded_NPDU);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Register_Foreign_Device);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Delete_Foreign_Device);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Secure_BVLL);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Distribute_Broadcast_To_Network);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Address_Copy);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_Address_Get_Set);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, test_BVLC6_VMAC_Address_Get_Set);
|
||||
assert(rc);
|
||||
}
|
||||
|
||||
#ifdef TEST_BVLC6
|
||||
int main(void)
|
||||
{
|
||||
Test *pTest;
|
||||
|
||||
pTest = ct_create("BACnet Virtual Link Control IP/v6", NULL);
|
||||
test_BVLC6(pTest);
|
||||
/* configure output */
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
(void)ct_report(pTest);
|
||||
ct_destroy(pTest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* TEST_BBMD */
|
||||
#endif /* BAC_TEST */
|
||||
|
||||
@@ -421,13 +421,6 @@ extern "C" {
|
||||
uint16_t npdu_size,
|
||||
uint16_t * npdu_len);
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include "ctest.h"
|
||||
BACNET_STACK_EXPORT
|
||||
void test_BVLC6(
|
||||
Test * pTest);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
@@ -140,130 +140,3 @@ uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue)
|
||||
(crcLow >> 4) ^ (crcLow & 0x0f) ^ ((crcLow & 0x0f) << 7);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "ctest.h"
|
||||
#include "bacnet/bytes.h"
|
||||
|
||||
/* test from Annex G 1.0 of BACnet Standard */
|
||||
void testCRC8(Test *pTest)
|
||||
{
|
||||
uint8_t crc = 0xff; /* accumulates the crc value */
|
||||
uint8_t frame_crc; /* appended to the end of the frame */
|
||||
|
||||
crc = CRC_Calc_Header(0x00, crc);
|
||||
ct_test(pTest, crc == 0x55);
|
||||
crc = CRC_Calc_Header(0x10, crc);
|
||||
ct_test(pTest, crc == 0xC2);
|
||||
crc = CRC_Calc_Header(0x05, crc);
|
||||
ct_test(pTest, crc == 0xBC);
|
||||
crc = CRC_Calc_Header(0x00, crc);
|
||||
ct_test(pTest, crc == 0x95);
|
||||
crc = CRC_Calc_Header(0x00, crc);
|
||||
ct_test(pTest, crc == 0x73);
|
||||
/* send the ones complement of the CRC in place of */
|
||||
/* the CRC, and the resulting CRC will always equal 0x55. */
|
||||
frame_crc = ~crc;
|
||||
ct_test(pTest, frame_crc == 0x8C);
|
||||
/* use the ones complement value and the next to last CRC value */
|
||||
crc = CRC_Calc_Header(frame_crc, crc);
|
||||
ct_test(pTest, crc == 0x55);
|
||||
}
|
||||
|
||||
/* test from Annex G 2.0 of BACnet Standard */
|
||||
void testCRC16(Test *pTest)
|
||||
{
|
||||
uint16_t crc = 0xffff;
|
||||
uint16_t data_crc;
|
||||
|
||||
crc = CRC_Calc_Data(0x01, crc);
|
||||
ct_test(pTest, crc == 0x1E0E);
|
||||
crc = CRC_Calc_Data(0x22, crc);
|
||||
ct_test(pTest, crc == 0xEB70);
|
||||
crc = CRC_Calc_Data(0x30, crc);
|
||||
ct_test(pTest, crc == 0x42EF);
|
||||
/* send the ones complement of the CRC in place of */
|
||||
/* the CRC, and the resulting CRC will always equal 0xF0B8. */
|
||||
data_crc = ~crc;
|
||||
ct_test(pTest, data_crc == 0xBD10);
|
||||
crc = CRC_Calc_Data(LO_BYTE(data_crc), crc);
|
||||
ct_test(pTest, crc == 0x0F3A);
|
||||
crc = CRC_Calc_Data(HI_BYTE(data_crc), crc);
|
||||
ct_test(pTest, crc == 0xF0B8);
|
||||
}
|
||||
|
||||
void testCRC8CreateTable(Test *pTest)
|
||||
{
|
||||
uint8_t crc = 0xff; /* accumulates the crc value */
|
||||
int i;
|
||||
|
||||
(void)pTest;
|
||||
printf("static const uint8_t HeaderCRC[256] =\n");
|
||||
printf("{\n");
|
||||
printf(" ");
|
||||
for (i = 0; i < 256; i++) {
|
||||
crc = CRC_Calc_Header(i, 0);
|
||||
printf("0x%02x, ", crc);
|
||||
if (!((i + 1) % 8)) {
|
||||
printf("\n");
|
||||
if (i != 255) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("};\n");
|
||||
}
|
||||
|
||||
void testCRC16CreateTable(Test *pTest)
|
||||
{
|
||||
uint16_t crc;
|
||||
int i;
|
||||
|
||||
(void)pTest;
|
||||
printf("static const uint16_t DataCRC[256] =\n");
|
||||
printf("{\n");
|
||||
printf(" ");
|
||||
for (i = 0; i < 256; i++) {
|
||||
crc = CRC_Calc_Data(i, 0);
|
||||
printf("0x%04x, ", crc);
|
||||
if (!((i + 1) % 8)) {
|
||||
printf("\n");
|
||||
if (i != 255) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("};\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef TEST_CRC
|
||||
int main(void)
|
||||
{
|
||||
Test *pTest;
|
||||
bool rc;
|
||||
|
||||
pTest = ct_create("crc", NULL);
|
||||
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, testCRC8);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testCRC16);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testCRC8CreateTable);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testCRC16CreateTable);
|
||||
assert(rc);
|
||||
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
(void)ct_report(pTest);
|
||||
|
||||
ct_destroy(pTest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1251,487 +1251,3 @@ void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port)
|
||||
mstp_port->TokenCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "bacnet/basic/sys/ringbuf.h"
|
||||
#include "ctest.h"
|
||||
|
||||
static uint8_t RxBuffer[MAX_MPDU];
|
||||
static uint8_t TxBuffer[MAX_MPDU];
|
||||
/* test stub functions */
|
||||
void RS485_Send_Frame(
|
||||
volatile struct mstp_port_struct_t *mstp_port, /* port specific data */
|
||||
uint8_t *buffer, /* frame to send (up to 501 bytes of data) */
|
||||
uint16_t nbytes)
|
||||
{ /* number of bytes of data (up to 501) */
|
||||
(void)mstp_port;
|
||||
(void)buffer;
|
||||
(void)nbytes;
|
||||
}
|
||||
|
||||
#define RING_BUFFER_DATA_SIZE 1
|
||||
#define RING_BUFFER_SIZE MAX_MPDU
|
||||
static RING_BUFFER Test_Buffer;
|
||||
static uint8_t Test_Buffer_Data[RING_BUFFER_DATA_SIZE * RING_BUFFER_SIZE];
|
||||
static void Load_Input_Buffer(uint8_t *buffer, size_t len)
|
||||
{
|
||||
static bool initialized = false; /* tracks our init */
|
||||
if (!initialized) {
|
||||
initialized = true;
|
||||
Ringbuf_Init(&Test_Buffer, (char *)Test_Buffer_Data,
|
||||
RING_BUFFER_DATA_SIZE, RING_BUFFER_SIZE);
|
||||
}
|
||||
/* empty any the existing data */
|
||||
while (!Ringbuf_Empty(&Test_Buffer)) {
|
||||
(void)Ringbuf_Pop(&Test_Buffer, NULL);
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
while (len) {
|
||||
(void)Ringbuf_Put(&Test_Buffer, (char *)buffer);
|
||||
len--;
|
||||
buffer++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
|
||||
{ /* port specific data */
|
||||
char *data;
|
||||
if (!Ringbuf_Empty(&Test_Buffer) && mstp_port &&
|
||||
(mstp_port->DataAvailable == false)) {
|
||||
data = Ringbuf_Peek(&Test_Buffer);
|
||||
if (data) {
|
||||
mstp_port->DataRegister = *data;
|
||||
mstp_port->DataAvailable = true;
|
||||
}
|
||||
(void)Ringbuf_Pop(&Test_Buffer, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port)
|
||||
{
|
||||
return mstp_port->DataLength;
|
||||
}
|
||||
|
||||
/* for the MS/TP state machine to use for getting data to send */
|
||||
/* Return: amount of PDU data */
|
||||
uint16_t MSTP_Get_Send(
|
||||
volatile struct mstp_port_struct_t *mstp_port, unsigned timeout)
|
||||
{ /* milliseconds to wait for a packet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t MSTP_Get_Reply(
|
||||
volatile struct mstp_port_struct_t *mstp_port, unsigned timeout)
|
||||
{ /* milliseconds to wait for a packet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t SilenceTime = 0;
|
||||
static uint16_t Timer_Silence(void)
|
||||
{
|
||||
return SilenceTime;
|
||||
}
|
||||
|
||||
static void Timer_Silence_Reset(void)
|
||||
{
|
||||
SilenceTime = 0;
|
||||
}
|
||||
|
||||
void testReceiveNodeFSM(Test *pTest)
|
||||
{
|
||||
volatile struct mstp_port_struct_t mstp_port; /* port data */
|
||||
unsigned EventCount = 0; /* local counter */
|
||||
uint8_t my_mac = 0x05; /* local MAC address */
|
||||
uint8_t HeaderCRC = 0; /* for local CRC calculation */
|
||||
uint8_t FrameType = 0; /* type of packet that was sent */
|
||||
unsigned len; /* used for the size of the message packet */
|
||||
size_t i; /* used to loop through the message bytes */
|
||||
uint8_t buffer[MAX_MPDU] = { 0 };
|
||||
uint8_t data[MAX_PDU] = { 0 };
|
||||
mstp_port.InputBuffer = &RxBuffer[0];
|
||||
mstp_port.InputBufferSize = sizeof(RxBuffer);
|
||||
mstp_port.OutputBuffer = &TxBuffer[0];
|
||||
mstp_port.OutputBufferSize = sizeof(TxBuffer);
|
||||
mstp_port.SilenceTimer = Timer_Silence;
|
||||
mstp_port.SilenceTimerReset = Timer_Silence_Reset;
|
||||
mstp_port.This_Station = my_mac;
|
||||
mstp_port.Nmax_info_frames = 1;
|
||||
mstp_port.Nmax_master = 127;
|
||||
MSTP_Init(&mstp_port);
|
||||
/* check the receive error during idle */
|
||||
mstp_port.receive_state = MSTP_RECEIVE_STATE_IDLE;
|
||||
mstp_port.ReceiveError = true;
|
||||
SilenceTime = 255;
|
||||
mstp_port.EventCount = 0;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.ReceiveError == false);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for bad packet header */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x11;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for good packet header, but timeout */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
/* force the timeout */
|
||||
SilenceTime = Tframe_abort + 1;
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for good packet header preamble, but receive error */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
/* force the error */
|
||||
mstp_port.ReceiveError = true;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.ReceiveError == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for good packet header preamble1, but bad preamble2 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
/* no change of state if no data yet */
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
/* repeated preamble1 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
/* repeated preamble1 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
/* bad data */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x11;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.ReceiveError == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for good packet header preamble, but timeout in packet */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
/* preamble2 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0xFF;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 0);
|
||||
ct_test(pTest, mstp_port.HeaderCRC == 0xFF);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
/* force the timeout */
|
||||
SilenceTime = Tframe_abort + 1;
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
ct_test(pTest, mstp_port.ReceivedInvalidFrame == true);
|
||||
/* check for good packet header preamble, but error in packet */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
/* preamble2 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0xFF;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 0);
|
||||
ct_test(pTest, mstp_port.HeaderCRC == 0xFF);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
/* force the error */
|
||||
mstp_port.ReceiveError = true;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.ReceiveError == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* check for good packet header preamble */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x55;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
/* preamble2 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0xFF;
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 0);
|
||||
ct_test(pTest, mstp_port.HeaderCRC == 0xFF);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
/* no change of state if no data yet */
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
/* Data is received - index is incremented */
|
||||
/* FrameType */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = FRAME_TYPE_TOKEN;
|
||||
HeaderCRC = 0xFF;
|
||||
HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 1);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
ct_test(pTest, FrameType == FRAME_TYPE_TOKEN);
|
||||
/* Destination */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0x10;
|
||||
HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 2);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
ct_test(pTest, mstp_port.DestinationAddress == 0x10);
|
||||
/* Source */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = my_mac;
|
||||
HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 3);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
ct_test(pTest, mstp_port.SourceAddress == my_mac);
|
||||
/* Length1 = length*256 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0;
|
||||
HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 4);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
ct_test(pTest, mstp_port.DataLength == 0);
|
||||
/* Length2 */
|
||||
mstp_port.DataAvailable = true;
|
||||
mstp_port.DataRegister = 0;
|
||||
HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 5);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER);
|
||||
ct_test(pTest, mstp_port.DataLength == 0);
|
||||
/* HeaderCRC */
|
||||
mstp_port.DataAvailable = true;
|
||||
ct_test(pTest, HeaderCRC == 0x73); /* per Annex G example */
|
||||
mstp_port.DataRegister = ~HeaderCRC; /* one's compliment of CRC is sent */
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
ct_test(pTest, mstp_port.Index == 5);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
ct_test(pTest, mstp_port.HeaderCRC == 0x55);
|
||||
/* BadCRC in header check */
|
||||
mstp_port.ReceivedInvalidFrame = false;
|
||||
mstp_port.ReceivedValidFrame = false;
|
||||
len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN,
|
||||
0x10, /* destination */
|
||||
my_mac, /* source */
|
||||
NULL, /* data */
|
||||
0); /* data size */
|
||||
ct_test(pTest, len > 0);
|
||||
/* make the header CRC bad */
|
||||
buffer[7] = 0x00;
|
||||
Load_Input_Buffer(buffer, len);
|
||||
for (i = 0; i < len; i++) {
|
||||
RS485_Check_UART_Data(&mstp_port);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
}
|
||||
ct_test(pTest, mstp_port.ReceivedInvalidFrame == true);
|
||||
ct_test(pTest, mstp_port.ReceivedValidFrame == false);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* NoData for us */
|
||||
mstp_port.ReceivedInvalidFrame = false;
|
||||
mstp_port.ReceivedValidFrame = false;
|
||||
len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN,
|
||||
my_mac, /* destination */
|
||||
my_mac, /* source */
|
||||
NULL, /* data */
|
||||
0); /* data size */
|
||||
ct_test(pTest, len > 0);
|
||||
Load_Input_Buffer(buffer, len);
|
||||
for (i = 0; i < len; i++) {
|
||||
RS485_Check_UART_Data(&mstp_port);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
}
|
||||
ct_test(pTest, mstp_port.ReceivedInvalidFrame == false);
|
||||
ct_test(pTest, mstp_port.ReceivedValidFrame == true);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* FrameTooLong */
|
||||
mstp_port.ReceivedInvalidFrame = false;
|
||||
mstp_port.ReceivedValidFrame = false;
|
||||
len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN,
|
||||
my_mac, /* destination */
|
||||
my_mac, /* source */
|
||||
NULL, /* data */
|
||||
0); /* data size */
|
||||
ct_test(pTest, len > 0);
|
||||
/* make the header data length bad */
|
||||
buffer[5] = 0x02;
|
||||
Load_Input_Buffer(buffer, len);
|
||||
for (i = 0; i < len; i++) {
|
||||
RS485_Check_UART_Data(&mstp_port);
|
||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
ct_test(pTest, mstp_port.DataAvailable == false);
|
||||
ct_test(pTest, mstp_port.SilenceTimer() == 0);
|
||||
ct_test(pTest, mstp_port.EventCount == EventCount);
|
||||
}
|
||||
ct_test(pTest, mstp_port.ReceivedInvalidFrame == true);
|
||||
ct_test(pTest, mstp_port.ReceivedValidFrame == false);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
/* Data */
|
||||
mstp_port.ReceivedInvalidFrame = false;
|
||||
mstp_port.ReceivedValidFrame = false;
|
||||
memset(data, 0, sizeof(data));
|
||||
len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_PROPRIETARY_MIN,
|
||||
my_mac, my_mac, data, sizeof(data));
|
||||
ct_test(pTest, len > 0);
|
||||
Load_Input_Buffer(buffer, len);
|
||||
RS485_Check_UART_Data(&mstp_port);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
while (mstp_port.receive_state != MSTP_RECEIVE_STATE_IDLE) {
|
||||
RS485_Check_UART_Data(&mstp_port);
|
||||
MSTP_Receive_Frame_FSM(&mstp_port);
|
||||
}
|
||||
ct_test(pTest, mstp_port.DataLength == sizeof(data));
|
||||
ct_test(pTest, mstp_port.ReceivedInvalidFrame == false);
|
||||
ct_test(pTest, mstp_port.ReceivedValidFrame == true);
|
||||
ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE);
|
||||
return;
|
||||
}
|
||||
|
||||
void testMasterNodeFSM(Test *pTest)
|
||||
{
|
||||
volatile struct mstp_port_struct_t MSTP_Port; /* port data */
|
||||
uint8_t my_mac = 0x05; /* local MAC address */
|
||||
MSTP_Port.InputBuffer = &RxBuffer[0];
|
||||
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
|
||||
MSTP_Port.OutputBuffer = &TxBuffer[0];
|
||||
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
|
||||
MSTP_Port.This_Station = my_mac;
|
||||
MSTP_Port.Nmax_info_frames = 1;
|
||||
MSTP_Port.Nmax_master = 127;
|
||||
MSTP_Port.SilenceTimer = Timer_Silence;
|
||||
MSTP_Port.SilenceTimerReset = Timer_Silence_Reset;
|
||||
MSTP_Init(&MSTP_Port);
|
||||
ct_test(pTest, MSTP_Port.master_state == MSTP_MASTER_STATE_INITIALIZE);
|
||||
/* FIXME: write a unit test for the Master Node State Machine */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef TEST_MSTP
|
||||
int main(void)
|
||||
{
|
||||
Test *pTest;
|
||||
bool rc;
|
||||
pTest = ct_create("mstp", NULL);
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, testReceiveNodeFSM);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testMasterNodeFSM);
|
||||
assert(rc);
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
(void)ct_report(pTest);
|
||||
ct_destroy(pTest);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user