| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // Drift gate for the native ECIES wrap/unwrap: asserts Crypto.swift opens the |
| 5 | // committed cross-implementation fixture (produced by the web crypto — regenerate |
| 6 | // with `node test/threadkey.test.mjs --emit`). A failure means the native ECIES |
| 7 | // has drifted from the web's wire format. |
| 8 | final class ECIESVectorTests: XCTestCase { |
| 9 | struct Vector: Decodable { |
| 10 | let privPkcs8B64: String |
| 11 | let pubRawB64: String |
| 12 | let wrappedB64: String |
| 13 | let expectedThreadKeyB64: String |
| 14 | } |
| 15 | |
| 16 | func testUnwrapMatchesWebVector() throws { |
| 17 | let v = try fixture(Vector.self, "thread-key-vector") |
| 18 | let expected = try XCTUnwrap(Data(base64urlEncoded: v.expectedThreadKeyB64), |
| 19 | "fixture expectedThreadKeyB64 is not valid base64url") |
| 20 | let got = try ECIES.unwrap(wrappedB64: v.wrappedB64, privPkcs8B64: v.privPkcs8B64) |
| 21 | XCTAssertEqual(got, expected, "unwrapped key != expected — native ECIES drifted from crypto.js") |
| 22 | |
| 23 | // Wrap round-trip: wrap a known key to the fixture's public key, then |
| 24 | // unwrap with its private key. Validates ECIES.wrap (used when starting a |
| 25 | // chat) produces a blob the unwrap path — and thus the web — can open. |
| 26 | let fresh = try ECIES.wrap(rawKey: expected, recipientPubB64: v.pubRawB64) |
| 27 | let back = try ECIES.unwrap(wrappedB64: fresh, privPkcs8B64: v.privPkcs8B64) |
| 28 | XCTAssertEqual(back, expected, "wrap→unwrap round-trip mismatch") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Decode a JSON fixture bundled with the test target. |
| 33 | func fixture<T: Decodable>(_ type: T.Type, _ name: String) throws -> T { |
| 34 | let url = try XCTUnwrap(Bundle.module.url(forResource: "Fixtures/\(name)", withExtension: "json"), |
| 35 | "missing fixture \(name).json") |
| 36 | return try JSONDecoder().decode(T.self, from: Data(contentsOf: url)) |
| 37 | } |
| 38 | |