krz/keycask

Password manager: Swift core library, CLI for macOS/Linux/Windows, iOS/macOS app. cli password-manager swift

Tests/KeycaskCoreTests/VaultCodecTests.swift

v0.1.0
keycask/Tests/KeycaskCoreTests/VaultCodecTests.swift history · blame · raw

52 lines · 1940 bytes

 1import Foundation
 2import Testing
 3
 4@testable import KeycaskCore
 5
 6@Suite struct VaultCodecTests {
 7    @Test func roundTripsAndIsDeterministic() throws {
 8        var v = Vault()
 9        try v.add(
10            Entry(
11                id: EntryID("aaaa2222")!, name: "gh", username: "cmc", password: "p",
12                url: "https://github.com", notes: "n", tags: ["dev"],
13                now: Date(timeIntervalSince1970: 1_700_000_000)))
14        let a = try VaultCodec.encode(v)
15        let b = try VaultCodec.encode(v)
16        #expect(a == b)
17        #expect(try VaultCodec.decode(a) == v)
18    }
19
20    @Test func datesAreISO8601WholeSeconds() throws {
21        var v = Vault()
22        try v.add(
23            Entry(
24                id: EntryID("aaaa2222")!, name: "gh", password: "p",
25                now: Date(timeIntervalSince1970: 1_700_000_000)))
26        let text = String(decoding: try VaultCodec.encode(v), as: UTF8.self)
27        #expect(text.contains("\"created\":\"2023-11-14T22:13:20Z\""))
28    }
29
30    @Test func keysAreSorted() throws {
31        var v = Vault()
32        try v.add(Entry(id: EntryID("aaaa2222")!, name: "gh", password: "p"))
33        let text = String(decoding: try VaultCodec.encode(v), as: UTF8.self)
34        let created = text.range(of: "\"created\"")!.lowerBound
35        let id = text.range(of: "\"id\"")!.lowerBound
36        let updated = text.range(of: "\"updated\"")!.lowerBound
37        #expect(created < id && id < updated)
38    }
39
40    @Test func garbageIsCorrupt() {
41        #expect(throws: KeycaskError.self) { try VaultCodec.decode(Data("nope".utf8)) }
42        do {
43            _ = try VaultCodec.decode(Data("{\"entries\":[{\"id\":1}]}".utf8))
44            Issue.record("expected corrupt")
45        } catch let e as KeycaskError {
46            #expect(e.exitCode == 1)
47            #expect(e.message.hasPrefix("vault is corrupt:"))
48        } catch {
49            Issue.record("wrong error \(error)")
50        }
51    }
52}