Skip to content

Add unit coverage for core and bare-metal HALs#24

Merged
DemonGiggle merged 1 commit into
refactor/baremetal-platformfrom
test/improve-coverage
May 16, 2026
Merged

Add unit coverage for core and bare-metal HALs#24
DemonGiggle merged 1 commit into
refactor/baremetal-platformfrom
test/improve-coverage

Conversation

@DemonGiggle
Copy link
Copy Markdown
Owner

Summary

  • add unit tests for JSON writer/parser behavior and session message history/view semantics
  • add bare-metal HAL tests for UART line handling, truncation warnings, socket delegation, and single-socket lifecycle
  • include the new unit and bare-metal HAL tests in POSIX and host-sim CI jobs

Verification

  • cmake -S . -B build-posix -DEC_PLATFORM=POSIX -DEC_ENABLE_TLS=OFF
  • cmake --build build-posix
  • ctest --test-dir build-posix --output-on-failure
  • cmake -S . -B build-host-sim -DEC_PLATFORM=POSIX -DEC_HOST_SIM=ON -DEC_ENABLE_TLS=OFF
  • cmake --build build-host-sim
  • ctest --test-dir build-host-sim --output-on-failure
  • cmake -S . -B build-baremetal -DEC_PLATFORM=BAREMETAL -DEC_ENABLE_TLS=OFF
  • cmake --build build-baremetal
  • cmake -S . -B build-baremetal-tls -DEC_PLATFORM=BAREMETAL -DEC_ENABLE_TLS=ON
  • cmake --build build-baremetal-tls

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous test, the global HAL and context pointers should be cleared to avoid leaving the test suite in an inconsistent state with dangling pointers.

    ec_io_uart_set_hal(NULL);
    s_uart_ctx = NULL;
    return 1;

Comment on lines +170 to +172
int n = ec_socket_recv(sock, buf, sizeof(buf), 100);
ASSERT(n > 0, "recv should delegate to HAL");
buf[n] = '\0';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The socket HAL pointer should be cleared before the test returns to prevent other tests from accidentally using a dangling pointer to this function's stack.

    ec_socket_baremetal_set_hal(NULL);
    return 1;

@DemonGiggle DemonGiggle merged commit 1e7f39e into refactor/baremetal-platform May 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant