|
| 1 | +// UserEntityPublicKeyTests.swift |
| 2 | +// MeshtasticTests |
| 3 | + |
| 4 | +import Testing |
| 5 | +import Foundation |
| 6 | +@testable import Meshtastic |
| 7 | + |
| 8 | +/// Covers `UserEntity.applyInboundPublicKey(_:nodeNum:)` — the first-wins public-key handling shared |
| 9 | +/// by the three inbound ingestion paths (UpdateSwiftData NodeInfo + User, MeshPackets NodeInfo). |
| 10 | +@Suite("UserEntity.applyInboundPublicKey") |
| 11 | +@MainActor |
| 12 | +struct UserEntityApplyInboundPublicKeyTests { |
| 13 | + |
| 14 | + private let keyA = Data([0x01, 0x02, 0x03, 0x04]) |
| 15 | + private let keyB = Data([0xAA, 0xBB, 0xCC, 0xDD]) |
| 16 | + |
| 17 | + @Test("stores the first non-empty key when none is on file") |
| 18 | + func storesFirstKey() { |
| 19 | + let user = UserEntity() |
| 20 | + let outcome = user.applyInboundPublicKey(keyA, nodeNum: 42) |
| 21 | + |
| 22 | + #expect(outcome == .stored) |
| 23 | + #expect(user.publicKey == keyA) |
| 24 | + #expect(user.pkiEncrypted == true) |
| 25 | + #expect(user.keyMatch == true) // untouched — no mismatch |
| 26 | + #expect(user.newPublicKey == nil) |
| 27 | + } |
| 28 | + |
| 29 | + @Test("treats a non-nil but empty stored key as no key and stores") |
| 30 | + func emptyStoredKeyIsTreatedAsNoKey() { |
| 31 | + let user = UserEntity() |
| 32 | + user.publicKey = Data() // non-nil but empty — must NOT count as a stored key |
| 33 | + let outcome = user.applyInboundPublicKey(keyA, nodeNum: 42) |
| 34 | + |
| 35 | + #expect(outcome == .stored) |
| 36 | + #expect(user.publicKey == keyA) |
| 37 | + } |
| 38 | + |
| 39 | + @Test("ignores an empty inbound key") |
| 40 | + func ignoresEmptyInboundKey() { |
| 41 | + let user = UserEntity() |
| 42 | + user.publicKey = keyA |
| 43 | + user.pkiEncrypted = true |
| 44 | + let outcome = user.applyInboundPublicKey(Data(), nodeNum: 42) |
| 45 | + |
| 46 | + #expect(outcome == .ignoredEmpty) |
| 47 | + #expect(user.publicKey == keyA) // unchanged |
| 48 | + #expect(user.keyMatch == true) |
| 49 | + } |
| 50 | + |
| 51 | + @Test("a matching inbound key is a no-op") |
| 52 | + func matchingKeyIsNoOp() { |
| 53 | + let user = UserEntity() |
| 54 | + user.publicKey = keyA |
| 55 | + user.pkiEncrypted = true |
| 56 | + let outcome = user.applyInboundPublicKey(keyA, nodeNum: 42) |
| 57 | + |
| 58 | + #expect(outcome == .matched) |
| 59 | + #expect(user.publicKey == keyA) |
| 60 | + #expect(user.keyMatch == true) |
| 61 | + #expect(user.newPublicKey == nil) |
| 62 | + } |
| 63 | + |
| 64 | + @Test("a different inbound key is refused and surfaced to the UI (key-substitution attempt)") |
| 65 | + func mismatchKeepsStoredKeyAndFlagsUI() { |
| 66 | + let user = UserEntity() |
| 67 | + user.publicKey = keyA |
| 68 | + user.pkiEncrypted = true |
| 69 | + let outcome = user.applyInboundPublicKey(keyB, nodeNum: 42) |
| 70 | + |
| 71 | + #expect(outcome == .mismatch) |
| 72 | + #expect(user.publicKey == keyA) // first-wins: stored key stands |
| 73 | + #expect(user.keyMatch == false) // drives the red key.slash indicator |
| 74 | + #expect(user.newPublicKey == keyB) // records the rejected key |
| 75 | + } |
| 76 | +} |
0 commit comments