Skip to content

feat: validate source address on ControllerApplication construction - #83

Open
RaulSMS wants to merge 1 commit into
masterfrom
fix/56-ca-constructor-address-validation
Open

feat: validate source address on ControllerApplication construction#83
RaulSMS wants to merge 1 commit into
masterfrom
fix/56-ca-constructor-address-validation

Conversation

@RaulSMS

@RaulSMS RaulSMS commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Summary

ControllerApplication.__init__ accepted any device_address_preferred without validating it was a claimable source address (0..253) — the only existing check lived inline in _begin_address_claim (the J1939-81 claim path), so an invalid address passed at construction would silently sit in an invalid state until the first claim attempt.

  • Extracted the existing bounds check (0..253; NULL=254/GLOBAL=255 excluded) into a shared ControllerApplication._check_source_address() static method.
  • Constructor now validates device_address_preferred (when not None) and raises ValueError with an explicit message stating the valid range, per the issue's acceptance criteria.
  • _begin_address_claim() now reuses the same shared check, preserving its existing behavior (log a warning and return False) for the async runtime claim path — no behavior change there.

Fixes #56

Test plan

  • Added tests in test/test_ca.py: min valid (0), max valid (253), None (no preferred address), and invalid boundaries (-1, 254 NULL, 255 GLOBAL) all raise/pass as expected.
  • pytest test/test_ca.py -q — 28 passed
  • pytest . --pyargs -q — 533 passed
  • ruff check — clean
  • pyright — 0 errors

ControllerApplication previously accepted any device_address_preferred
at construction time without checking it was a claimable source
address (0..253), only validating later when an address change was
requested via the J1939-81 claim procedure. An invalid address passed
at construction would silently sit in an invalid state until the first
claim attempt.

Extract the existing 0..253 bounds check into a shared
_check_source_address() static method, reused by both the constructor
(raises ValueError for None-safe, out-of-range addresses) and
_begin_address_claim() (preserves its existing warn-and-return-False
behavior for the async claim path).

Fixes #56

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

Check provided address on construction of controller application object

3 participants