Add unit coverage for core and bare-metal HALs#24
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive testing suite, including unit tests for JSON processing and session management, as well as HAL-level tests for bare-metal UART and socket implementations. The review feedback highlights several opportunities to improve test robustness, specifically by clearing global HAL pointers at the end of tests to prevent dangling references and addressing a potential buffer overflow during socket data reception.
| ASSERT_EQ(strcmp(line, "hello"), 0, "bare-metal UART should strip CRLF"); | ||
| ASSERT_EQ(ec_io_write("ok"), 0, "bare-metal UART should write"); | ||
| ASSERT_EQ(strcmp(uart.output, "ok"), 0, "write should reach HAL"); | ||
| return 1; |
There was a problem hiding this comment.
The UART HAL and context pointers are stored in global variables (s_uart_hal and s_uart_ctx) but point to local stack variables within this test. To prevent dangling pointers that could cause issues if more tests are added or reordered, these globals should be cleared before the function returns.
ec_io_uart_set_hal(NULL);
s_uart_ctx = NULL;
return 1;| ASSERT_EQ(line[0], '\0', "truncated line should clear output buffer"); | ||
| ASSERT_STR(uart.output, "input truncated", | ||
| "truncation warning should be written through HAL"); | ||
| return 1; |
| int n = ec_socket_recv(sock, buf, sizeof(buf), 100); | ||
| ASSERT(n > 0, "recv should delegate to HAL"); | ||
| buf[n] = '\0'; |
There was a problem hiding this comment.
Potential buffer overflow: if ec_socket_recv returns the full size of the buffer (32), the subsequent null-termination buf[n] = '\0' will write one byte past the end of the array. You should pass sizeof(buf) - 1 to the receive function to ensure space for the terminator.
| int n = ec_socket_recv(sock, buf, sizeof(buf), 100); | |
| ASSERT(n > 0, "recv should delegate to HAL"); | |
| buf[n] = '\0'; | |
| int n = ec_socket_recv(sock, buf, sizeof(buf) - 1, 100); | |
| ASSERT(n > 0, "recv should delegate to HAL"); | |
| buf[n] = '\0'; |
| ASSERT(ec_socket_connect("after-close.example.test", 80, 0) != 0, | ||
| "connect should work again after close"); | ||
| ec_socket_close(sock); | ||
| return 1; |
Summary
Verification