a0868cd3b7
Implement cel_serial_list_ports/cel_serial_free_ports with platform backends: Windows reads HKLM\HARDWARE\DEVICEMAP\ SERIALCOMM (fast, single registry read), POSIX scans /dev for ttyUSB*/ttyACM*. telemetry tool gains --list, --port, and --baudrate flags; baud rate was previously hardcoded to 400000. Rename the tool_telemetry CMake target to telemetry. Fix test_free_ports_zero_count, which passed a stack array to cel_serial_free_ports (which calls free() on it), corrupting the heap.
108 lines
3.1 KiB
C
108 lines
3.1 KiB
C
#include <stdlib.h>
|
|
|
|
#include "unity.h"
|
|
#include "celrs/serial.h"
|
|
#include "Mocklog_write.h"
|
|
|
|
void setUp(void) { Mocklog_write_Init(); }
|
|
void tearDown(void) { Mocklog_write_Verify(); Mocklog_write_Destroy(); }
|
|
|
|
void test_open_valid_path(void) {
|
|
cel_serial_port* port = cel_serial_open("COM3", 400000);
|
|
TEST_ASSERT_NOT_NULL(port);
|
|
cel_serial_close(port);
|
|
}
|
|
|
|
void test_open_null_path(void) {
|
|
TEST_ASSERT_NULL(cel_serial_open(NULL, 400000));
|
|
}
|
|
|
|
void test_open_preserves_path(void) {
|
|
cel_serial_port* port = cel_serial_open("/dev/ttyUSB0", 400000);
|
|
TEST_ASSERT_NOT_NULL(port);
|
|
/* path is stored internally; verify by roundtrip behavior */
|
|
cel_serial_close(port);
|
|
}
|
|
|
|
void test_close_null(void) {
|
|
/* Should not crash */
|
|
cel_serial_close(NULL);
|
|
}
|
|
|
|
void test_read_returns_zero_stub(void) {
|
|
cel_serial_port* port = cel_serial_open("COM3", 400000);
|
|
TEST_ASSERT_NOT_NULL(port);
|
|
uint8_t buf[16];
|
|
/* Stub implementation returns 0 */
|
|
size_t n = cel_serial_read(port, buf, sizeof(buf), 100);
|
|
TEST_ASSERT_EQUAL_UINT(0, n);
|
|
cel_serial_close(port);
|
|
}
|
|
|
|
void test_write_returns_zero_stub(void) {
|
|
cel_serial_port* port = cel_serial_open("COM3", 400000);
|
|
TEST_ASSERT_NOT_NULL(port);
|
|
uint8_t buf[4] = {0xC8, 0x10, 0x80, 0x03};
|
|
/* Stub implementation returns 0 */
|
|
size_t n = cel_serial_write(port, buf, sizeof(buf));
|
|
TEST_ASSERT_EQUAL_UINT(0, n);
|
|
cel_serial_close(port);
|
|
}
|
|
|
|
void test_flush_no_crash(void) {
|
|
cel_serial_port* port = cel_serial_open("COM3", 400000);
|
|
TEST_ASSERT_NOT_NULL(port);
|
|
cel_serial_flush(port); /* should not crash */
|
|
cel_serial_close(port);
|
|
}
|
|
|
|
void test_flush_null(void) {
|
|
cel_serial_flush(NULL); /* should not crash */
|
|
}
|
|
|
|
void test_list_ports_null_out(void) {
|
|
TEST_ASSERT_EQUAL_INT(-1, cel_serial_list_ports(NULL, 16));
|
|
}
|
|
|
|
void test_list_ports_runs_without_crash(void) {
|
|
char** ports = NULL;
|
|
int count = cel_serial_list_ports(&ports, 16);
|
|
/* count >= 0 is valid (may be 0 if no ports available) */
|
|
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, count);
|
|
cel_serial_free_ports(ports, count);
|
|
}
|
|
|
|
void test_list_ports_zero_max_uses_default(void) {
|
|
char** ports = NULL;
|
|
int count = cel_serial_list_ports(&ports, 0);
|
|
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, count);
|
|
cel_serial_free_ports(ports, count);
|
|
}
|
|
|
|
void test_free_ports_null(void) {
|
|
cel_serial_free_ports(NULL, 0); /* should not crash */
|
|
}
|
|
|
|
void test_free_ports_zero_count(void) {
|
|
char** empty = (char**)calloc(1, sizeof(char*));
|
|
cel_serial_free_ports(empty, 0); /* should not crash */
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_open_valid_path);
|
|
RUN_TEST(test_open_null_path);
|
|
RUN_TEST(test_open_preserves_path);
|
|
RUN_TEST(test_close_null);
|
|
RUN_TEST(test_read_returns_zero_stub);
|
|
RUN_TEST(test_write_returns_zero_stub);
|
|
RUN_TEST(test_flush_no_crash);
|
|
RUN_TEST(test_flush_null);
|
|
RUN_TEST(test_list_ports_null_out);
|
|
RUN_TEST(test_list_ports_runs_without_crash);
|
|
RUN_TEST(test_list_ports_zero_max_uses_default);
|
|
RUN_TEST(test_free_ports_null);
|
|
RUN_TEST(test_free_ports_zero_count);
|
|
return UNITY_END();
|
|
}
|