Skip to content

Commit 92f6433

Browse files
test: complete tx coverage and use canard user_context
1 parent 54b8ab9 commit 92f6433

File tree

4 files changed

+771
-63
lines changed

4 files changed

+771
-63
lines changed

libcanard/canard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ struct canard_t
470470
uint_least8_t p2p_transfer_id[CANARD_NODE_ID_CAPACITY];
471471

472472
const canard_vtable_t* vtable;
473+
474+
void* user_context;
473475
};
474476

475477
/// The TX queue is shared between all redundant interfaces with deduplication (each frame is enqueued only once).

tests/src/test_api_tx.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
11
// This software is distributed under the terms of the MIT License.
22
// Copyright (c) OpenCyphal Development Team.
33

4-
#include <canard.h>
4+
#include "helpers.h"
55
#include <unity.h>
66
#include <cstdint>
77
#include <cstdlib>
88
#include <memory>
99

10+
// Simple allocator that always fails.
11+
static void* dummy_alloc_mem(const canard_mem_t mem, const size_t size) { return dummy_alloc(mem.context, size); }
12+
static void dummy_free_mem(const canard_mem_t mem, const size_t size, void* const pointer)
13+
{
14+
dummy_free(mem.context, size, pointer);
15+
}
16+
17+
static void test_canard_publish_validation(void)
18+
{
19+
canard_t self = {};
20+
21+
// Invalid interface bitmap.
22+
const canard_bytes_chain_t payload = { .bytes = { .size = 0, .data = NULL }, .next = NULL };
23+
TEST_ASSERT_FALSE(canard_publish(&self, 0, 0, canard_prio_nominal, 0, 0, payload, CANARD_USER_CONTEXT_NULL, false));
24+
25+
// Invalid payload.
26+
const canard_bytes_chain_t bad_payload = { .bytes = { .size = 1, .data = NULL }, .next = NULL };
27+
TEST_ASSERT_FALSE(
28+
canard_publish(&self, 0, 1, canard_prio_nominal, 0, 0, bad_payload, CANARD_USER_CONTEXT_NULL, false));
29+
}
30+
31+
static void test_canard_publish_oom(void)
32+
{
33+
canard_mem_vtable_t vtable = {};
34+
vtable.free = dummy_free_mem;
35+
vtable.alloc = dummy_alloc_mem;
36+
canard_t self = {};
37+
self.mem.tx_transfer = canard_mem_t{ &vtable, NULL };
38+
self.mem.tx_frame = canard_mem_t{ &vtable, NULL };
39+
40+
// Allocation failure in txfer_new should return false.
41+
const canard_bytes_chain_t payload = { .bytes = { .size = 0, .data = NULL }, .next = NULL };
42+
TEST_ASSERT_FALSE(canard_publish(&self, 0, 1, canard_prio_nominal, 0, 0, payload, CANARD_USER_CONTEXT_NULL, false));
43+
}
44+
45+
static void test_canard_0v1_publish_requires_node_id(void)
46+
{
47+
canard_t self = {};
48+
49+
// Node-ID zero should reject the request.
50+
const canard_bytes_chain_t payload = { .bytes = { .size = 0, .data = NULL }, .next = NULL };
51+
TEST_ASSERT_FALSE(canard_0v1_publish(&self, 0, 1, canard_prio_nominal, 1, 0xFFFF, 0, payload));
52+
}
53+
1054
extern "C" void setUp() {}
1155
extern "C" void tearDown() {}
1256

1357
int main()
1458
{
1559
UNITY_BEGIN();
1660

17-
// TODO add tests
61+
// canard_publish/canard_0v1_publish validation.
62+
RUN_TEST(test_canard_publish_validation);
63+
RUN_TEST(test_canard_publish_oom);
64+
RUN_TEST(test_canard_0v1_publish_requires_node_id);
1865

1966
return UNITY_END();
2067
}

0 commit comments

Comments
 (0)