Commit graph

20 commits

Author SHA1 Message Date
Matthew Wolter
2bf3f1b9dd fix: BATTERY handler drops frames shorter than 3 bytes (level field guard)
A BATTERY frame with len(data) < 3 caused dbuf.read(2) to return short
bytes; int.from_bytes(b"", ...) silently yielded 0, propagating a bogus
level=0 to HA sensors.  Same silent-zero class as N07 (storage fields).

Option B: early-return with debug log, matching the NEW-C pattern for
STATUS_RESPONSE.  No BATTERY event is dispatched for malformed frames.

Not in the original forensics report — discovered during G1 N07 work and
logged in issues_log.md.  Resolved here because no later branch touches
this handler.

Files changed:
- src/meshcore/reader.py: add `if len(data) < 3: return` guard before
  the level read in the BATTERY branch
- tests/unit/test_reader.py: add test_g1_battery_too_short_for_level —
  sends a 1-byte frame (type only), asserts no BATTERY event dispatched
  and debug log emitted
2026-04-11 19:24:26 -07:00
Matthew Wolter
f3973151d6 G1: add verification tests for F06, N07, NEW-C, R02
Adds the four unit tests required by proposal §4.1 verification:

(a) test_g1_handle_rx_malformed_frame_logged_and_swallowed — F06.
    Sends a 4-byte CHANNEL_MSG_RECV_V3 frame missing the channel_idx
    byte. The handler raises IndexError on dbuf.read(1)[0]; the F06
    umbrella must catch it, log "handle_rx parse error" with a
    traceback, and return cleanly without dispatching any event.

(b) test_g1_battery_short_frame_omits_storage_fields — N07.
    Sends a 3-byte BATTERY frame (type + 2-byte level only). Verifies
    that exactly one BATTERY event is dispatched, the payload contains
    `level` but does NOT contain `used_kb` or `total_kb`. Pre-fix the
    `len(data) > 3` gate would have produced both fields with bogus
    silent zeros.

(c) test_g1_status_response_short_frame_skipped — NEW-C.
    Sends a 30-byte STATUS_RESPONSE push frame (well below the 60-byte
    minimum). Verifies that no STATUS_RESPONSE event is dispatched and
    the new "STATUS_RESPONSE push frame too short" debug log fires.

(d) test_g1_parse_packet_payload_txt_type_decodes_high_bits — R02.
    Sets up a synthetic channel with a known 16-byte AES key, encrypts
    a 16-byte plaintext where byte 4 = (5 << 2) | 1 = 0x15, builds the
    full pkt_payload (chan_hash + cipher_mac + ciphertext), wraps it in
    a minimal route header, calls parsePacketPayload directly, and
    asserts log_data["txt_type"] == 5 and log_data["attempt"] == 1.
    Pre-R02-fix `uncrypted[4:4]` was the empty slice, so txt_type was
    always 0 — this test would have failed with txt_type=0.

Adds a small _CapturingDispatcher helper class. Adds imports for
logging at the top of the file. The existing test_binary_response is
left untouched.

File: tests/unit/test_reader.py
2026-04-11 18:39:01 -07:00
Alex Wolden
ed96df197a Fix 16 failing unit tests to match current source behavior
- Update mock dispatcher to use subscribe-before-send pattern matching
  the rewritten CommandHandler.send() method
- Use 32-byte pubkeys in tests for commands that now require
  prefix_length=32 (login, logout, statusreq, reset_path, share/export/remove contact)
- Fix send_trace test path format to match flags=1 (2-byte path hashes)
- Update LPP current test to expect signed wrap for values > 32.767
- Fix BinaryReqType import (moved from meshcore.parsing to meshcore.packets)
- Fix register_binary_request call signature (added pubkey_prefix param)
- Update timeout test to expect 'no_event_received' instead of 'timeout'
2026-04-05 18:38:16 -07:00
fdlamotte
52ad5c201c
Merge pull request #67 from jkingsman/respect-found-idx
Use the frame start once we've found it
2026-03-22 12:48:11 -04:00
Jack Kingsman
4df3655752
Use the frame start once we've found it 2026-03-21 21:08:04 -07:00
Jack Kingsman
3ad77d364d
Fix three byte path packets 2026-03-18 17:31:17 -07:00
jkingsman
1ea32885a3 Add typing to send_chan_message with test 2025-12-23 18:40:59 -08:00
agessaman
e0f71482c6 Add private key export support
- Add PRIVATE_KEY and DISABLED event types
- Add packet parsing for private key export responses
- Add export_private_key() method to DeviceCommands
- Add comprehensive unit tests
- Add BLE private key export example
- Update documentation with security notes
2025-10-12 18:23:32 -07:00
Copilot
29003b94dc
Implement BLE PIN pairing support for enhanced security
* Implement BLE pin pairing support with comprehensive tests and documentation
2025-09-24 00:21:30 +02:00
Alex Wolden
ccb1d6eb9e Revert "Refactor command system to be queue based"
This reverts commit 28957a4b60.
2025-09-04 15:08:08 -07:00
fdlamotte
4ce3a6fd9a
Merge branch 'main' into feature/refactor 2025-08-06 10:56:24 +02:00
Alex Wolden
43e2cfc724 timing and test fixes 2025-08-05 23:08:17 -07:00
Alex Wolden
968e42c6c8 Add testing workflow 2025-08-05 13:21:30 -07:00
Ventz Petkov
f4d3be1360 Fix: Improved BLE Connection Logic on macOS 2025-08-05 15:52:44 -04:00
Ventz Petkov
36727f4ea3 feat: Refactor binary commands and apply BLE fixes
Refactored the BinaryCommandHandler to align with the other command handlers, inheriting from CommandHandlerBase. This resolves an AttributeError and simplifies the command structure. Moved binary_commands.py into the commands module. Applied fixes to the BLE connection handler based on feedback, improving reliability on macOS and ensuring the device address is correctly handled.
2025-08-05 15:31:54 -04:00
Ventz Petkov
c19fd166f8 Fix: Improved BLE Connection Logic on macOS 2025-08-05 07:50:59 -04:00
Alex Wolden
cca0ca90e9 Add channel commands and fix a lint error 2025-06-01 20:31:37 -07:00
Alex Wolden
6fbf15885d Change contract for commands to return full event 2025-04-14 11:23:10 -07:00
Alex Wolden
52553a41bd Handle error events properly in commands 2025-04-14 09:03:56 -07:00
Alex Wolden
3f7155d913 Added in some unit tests 2025-04-13 22:55:39 -07:00