Skip to content

Add code coverage and ioctl unit testing#52

Merged
colemancda merged 29 commits into
masterfrom
feature/code-coverage
Jul 18, 2026
Merged

Add code coverage and ioctl unit testing#52
colemancda merged 29 commits into
masterfrom
feature/code-coverage

Conversation

@colemancda

@colemancda colemancda commented Jul 18, 2026

Copy link
Copy Markdown
Member

Summary

Adds code coverage measurement/enforcement and unit-testing scaffolding for the package's ioctl() calls, plus value-type tests — and fixes three real bugs the new tests exposed.

Coverage: 1.53% → 41.23% (949/2302 lines), 40 tests, all passing. Rebased on latest master.

Code coverage

  • Scripts/coverage.sh runs the suite with coverage, exports LCOV + Cobertura reports scoped to the package's own Sources/, and enforces a minimum line-coverage threshold (currently 1%, to be ratcheted up as the suite grows).
  • CI (swift.yml) runs the gate and publishes to GitHub Code Quality; replaces the retired maxep/spm-lcov-action step and refreshes the deprecated ubuntu-20.04 runner and upload-artifact@v3 action.

ioctl unit testing

Syscall mocking modeled on swift-system's MockingDriver:

  • Internal/Mocking.swift — thread-local MockingDriver that records a trace of syscalls, can force errnos (.always / .counted for error-path and EINTR-retry tests), and exposes an ioctlHandler closure to fake the kernel's out-parameter reply.
  • Internal/Syscalls.swiftsystem_ioctl shims that divert to the mock under ENABLE_MOCKING (debug only) and compile to the plain ioctl in release.
  • Internal/IOControl.swift — in-module SocketDescriptor.inputOutput overloads that route all existing ioctl call sites through the mockable seam without touching them.
  • ENABLE_MOCKING is defined for debug builds only, mirroring swift-system.

Bugs found and fixed by the new tests

  • HCIScan inquiry parsing used the wrong buffer offset — results were read at Swift MemoryLayout.size (9) instead of the C sizeof(struct hci_inquiry_req) (10) the kernel writes at, and the misaligned typed rebind traps at runtime. Rewritten with a properly aligned raw buffer, stride-based offsets, and loadUnaligned.
  • RFCOMMIO.GetDeviceList had the same ABI bugdev_info[] was read at offset 2 instead of the C flexible-array offset 4 (count padded to element alignment), with the same misalignment trap.
  • RFCOMMDevice copied the source address into destination (destination = cValue.source).

Tests

  • IOControlTests.swift — ioctl paths through the mock seam: request tracing, faked kernel replies for HCIGETDEVLIST, HCIGETDEVINFO, HCIINQUIRY (verifies marshalled request fields incl. GIAC lap), RFCOMM create/release/list/get-info, forced-errno, and interrupt-retry paths.
  • ValueTests.swift — pure value-type logic: AddressType, the full HCIError → Errno mapping, HostControllerIO/RFCOMMIO round trips + descriptions + Codable, HCIDeviceOptions, LinkMode, RFCOMMState, HCIBusType/HCIControllerType descriptions, HCIPacketType, BluetoothSocketProtocol, and socket-option encode/decode round trips (Security, RFCOMM LinkMode, ConnectionInfo).

All run without Bluetooth hardware, on Linux and Darwin.

Progress log

Milestone Coverage Tests
Initial ioctl mocking scaffolding 12.45% 9
Value-type + ioctl device/RFCOMM tests 26.42% 26
Inquiry/RFCOMM-list ioctls, socket options, ABI fixes 41.23% 40

Now fully covered: Error, AddressType, HCIBusType, HCIControllerType, HCIDeviceFlag, HCIDeviceOptions, RFCOMMFlag, RFCOMMLinkMode, RFCOMMGetDeviceInformation. Remaining gaps are the async HostController operations and L2CAP/socket plumbing that need a live HCI socket.

The toolchain is pinned to Swift 6.3.3 via .swift-version.

Includes the BitMaskOptionSet → native OptionSet migration (#53, merged into this branch).

@colemancda
colemancda force-pushed the feature/code-coverage branch from 860a46e to 631397f Compare July 18, 2026 02:59
@colemancda
colemancda merged commit f40288f into master Jul 18, 2026
1 check passed
@colemancda
colemancda deleted the feature/code-coverage branch July 18, 2026 04:09
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