| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // Cross-implementation gate for the per-viewer names blob (#577): opens the |
| 5 | // web-sealed fixture (emitted by test/nicknames.test.mjs from crypto.js |
| 6 | // sealForPub) and round-trips a native seal — so the CryptoKit seal/open pair |
| 7 | // can never drift from the web's wire or JSON shape. Runs with `swift test` |
| 8 | // (no simulator), like ECIESVectorTests. |
| 9 | final class NicknamesVectorTests: XCTestCase { |
| 10 | private struct Vector: Decodable { |
| 11 | let privPkcs8B64: String |
| 12 | let pubRawB64: String |
| 13 | let sealedB64: String |
| 14 | let expectedNames: [String: NicknameEntry] |
| 15 | } |
| 16 | |
| 17 | private func vector() throws -> Vector { |
| 18 | let url = try XCTUnwrap( |
| 19 | Bundle.module.url(forResource: "Fixtures/nicknames-vector", withExtension: "json"), |
| 20 | "fixture missing — regenerate with: node test/nicknames.test.mjs --emit") |
| 21 | return try JSONDecoder().decode(Vector.self, from: Data(contentsOf: url)) |
| 22 | } |
| 23 | |
| 24 | func testOpensWebSealedBlob() throws { |
| 25 | let v = try vector() |
| 26 | let names = try Nicknames.open(blobB64: v.sealedB64, privPkcs8B64: v.privPkcs8B64) |
| 27 | XCTAssertEqual(names, v.expectedNames) |
| 28 | } |
| 29 | |
| 30 | func testNativeSealRoundTrips() throws { |
| 31 | let v = try vector() |
| 32 | let sealed = try Nicknames.seal(v.expectedNames, recipientPubB64: v.pubRawB64) |
| 33 | let reopened = try Nicknames.open(blobB64: sealed, privPkcs8B64: v.privPkcs8B64) |
| 34 | XCTAssertEqual(reopened, v.expectedNames) |
| 35 | } |
| 36 | |
| 37 | func testWrongKeyCannotOpen() throws { |
| 38 | let v = try vector() |
| 39 | let stranger = ECIES.generateUserKeypair() |
| 40 | XCTAssertThrowsError(try Nicknames.open(blobB64: v.sealedB64, privPkcs8B64: stranger.priv)) |
| 41 | } |
| 42 | } |
| 43 | |