blob: fac9fb481038069b054d88b8b4741c2d5de7f062 [file] [view]
# Starnix C++ Syscall Tests Agent Guide
This guide covers C++ Linux syscall tests in `//src/starnix/tests/syscalls/cpp/`,
building on [`//AGENTS.md`](../../../../../AGENTS.md) and
[`//src/starnix/AGENTS.md`](../../../AGENTS.md).
## Reuse `test_helper.h` and `:test_utils`
Before writing test infrastructure or boilerplate, **read
[`test_helper.h`](test_helper.h) directly** and reuse its existing helpers via:
```cpp
#include "src/starnix/tests/syscalls/cpp/test_helper.h"
```
Every `syscall_test(...)` target in `BUILD.gn` automatically links `:test_utils`
(see `syscall_test.gni`), so `test_helper.h` and the companion headers below are
already available without extra `BUILD.gn` deps.
**Do not hand-roll** custom versions of infrastructure already provided by
`test_helper.h`, including:
- Syscall error/errno helpers, `fit::result` unwrap helpers, or `EINTR` retry
loops
- `fork()`/`clone()` wrappers, child reaping, or `/proc/<pid>/stat` polling
- Pipes, `eventfd` semaphores, or `socketpair()` wrappers for cross-process or
cross-thread synchronization
- Temporary files, temporary directories (`TEST_TMPDIR`), temporary symlinks, or
recursive unmount/cleanup routines
- RAII wrappers for `mmap`, `mount`, `sigaction`, `sigprocmask`, sysctl files,
loop devices, or `uinput` devices
- `/proc` parsers (`maps`, `smaps`, `status`, `mountinfo`), netlink message
encoders, or memory-access/segfault probes
Some older tests in this directory still contain hand-rolled setup code; do not
copy those patterns into new or edited tests. If you need reusable test
infrastructure that does not exist yet, add it to `test_helper.h`/
`test_helper.cc` with unit coverage in `test_helper_test.cc` instead of
duplicating it across test files.
Also check the focused companion headers in `:test_utils` before writing custom
matchers or fixtures (read each header directly for current APIs):
- [`syscall_matchers.h`](syscall_matchers.h): GMock matchers for raw syscall
return values and `fit::result<int, ...>` (prefer these over manual `errno`
assertions so failures print `strerror(errno)` automatically).
- [`capabilities_helper.h`](capabilities_helper.h): Thread/file capability and
unprivileged-user helpers.
- [`proc_test_base.h`](proc_test_base.h): Base GTest fixture for procfs tests.
When `/proc` is absent, `ProcTestBase` mounts procfs at a temporary directory
rather than `/proc`, so callers must use `proc_path()` instead of hardcoding
`/proc/...`.
- Subsystem-specific headers when testing those areas:
`fault_test.h`/`fault_test_suite.h`, `binder_helper.h` (and `binder/`), and
`io_uring_helper.h`.
## Test Pitfalls & Conventions
- **Record known Starnix divergences in `expectations/*.json5`**: Tests should
generally assert Linux-conformant behavior unconditionally and track expected
Starnix failures in `expectations/*.json5` with a
`TODO(https://fxbug.dev/...)` comment rather than branching on `IsStarnix()`.
Keep `IsStarnix()` usage minimal (e.g., paired as `!IsStarnix()` with runtime
capability/feature probes, or narrowly isolating an unavoidable bug-tracked
divergence).
- **Do not silently skip privileged tests on Starnix**: Avoid unconditional
`EPERM` skips (such as `SAFE_SYSCALL_SKIP_ON_EPERM`, which skips even on
Starnix; see `TODO(https://fxbug.dev/317285180)` in `test_helper.h`). For new
tests, gate privileged operations on explicit capability probes
(`HasCapability(...)` / `HasSysAdmin()`) paired with `!IsStarnix()` so Starnix
fails loudly instead of silently skipping.
- **Do not rely on `GTEST_SKIP()` inside forked or cloned child lambdas**:
`GTEST_SKIP()`-emitting helpers leave `testing::Test::HasFailure()` false, so
a child spawned via `ForkHelper` or `CloneHelper` exits `0` and silently
passes rather than skipping or failing.
- **Avoid wall-clock sleeps (`usleep` or short `poll` timeouts) for thread
synchronization or timestamp advancement**: Under CI, AEMU virtualization, and
sanitizer load, fixed sleeps race scheduler delays (silently bypassing
blocking/wakeup paths under test) or wake before coarse kernel jiffy ticks
update `atime`/`mtime`. Synchronize with child/background threads using
blocking kernel primitives or process-state polling helpers in
`test_helper.h`, and backdate file timestamps deterministically with
`utimensat()` before operations expected to advance `atime`/`mtime`.