// The lock screen's opener against the core's own boxes: every vector the // core sealed (`crates/cotton-core/src/chat/vectors.json`) opens here to its // payload and its sender, and a wrong key or one changed byte opens nothing. // The alert says who and what from the box the push carries, and keeps the // push's words when it carries none or one that does not open. import CottonMailbox import Foundation import Testing private struct Vectors: Decodable { struct Vector: Decodable { var recipient_secret: String var sender_secret: String var sender_public: String var payload: String var box: String } var vectors: [Vector] } private func vectors() throws -> [Vectors.Vector] { let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().appendingPathComponent("../../../../crates/cotton-core/src/chat/vectors.json").standardized return try JSONDecoder().decode(Vectors.self, from: Data(contentsOf: root)).vectors } private func base64url(_ data: Data) -> String { data.base64EncodedString().replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "") } @Suite struct ChatBoxTests { // RULE: the extension opens exactly what the core seals (`c1:`), to the same payload and sender. @Test func everyCoreVectorOpens() throws { let all = try vectors() #expect(!all.isEmpty) for vector in all { let opened = try #require(ChatBox.open(vector.box, privateKey: vector.recipient_secret)) #expect(String(decoding: opened.payload, as: UTF8.self) == vector.payload) #expect(base64url(opened.sender) == vector.sender_public) } } // RULE: a box opens for its recipient alone, and not once a byte has moved. @Test func aWrongKeyOrAChangedByteOpensNothing() throws { let vector = try #require(try vectors().first) #expect(ChatBox.open(vector.box, privateKey: vector.sender_secret) == nil) var bytes = Array(vector.box) let at = bytes.count - 5 bytes[at] = bytes[at] == "A" ? "B" : "A" #expect(ChatBox.open(String(bytes), privateKey: vector.recipient_secret) == nil) #expect(ChatBox.open("c2:" + vector.box.dropFirst(3), privateKey: vector.recipient_secret) == nil) } // RULE: an accepted person's message pushed with its box is titled with the name this phone knows them by, and says its words. @Test func anAlertSaysWhoAndWhat() throws { let vector = try #require(try vectors().first) let words = ChatAlert.words(box: vector.box, privateKey: vector.recipient_secret, names: ["+15550100001": "Asha Example"]) #expect(words == ChatAlertWords(title: "Asha Example", body: "hello", from: "+15550100001")) #expect(words?.conversation == "+15550100001", "a one-to-one is filed under its number") let unnamed = ChatAlert.words(box: vector.box, privateKey: vector.recipient_secret, names: [:]) #expect(unnamed?.title == "+15550100001") } // RULE: a push with no box (a stranger's request, a box too big to push) keeps its words, and so does a box that does not open for this phone. @Test func noBoxOrAForeignBoxKeepsThePush() throws { let vector = try #require(try vectors().first) #expect(ChatAlert.words(box: nil, privateKey: vector.recipient_secret, names: [:]) == nil) #expect(ChatAlert.words(box: vector.box, privateKey: vector.sender_secret, names: [:]) == nil) #expect(ChatAlert.words(box: "c1:AAAA", privateKey: vector.recipient_secret, names: [:]) == nil) } // RULE: the names kept for the extension merge, skip a blank, and go at sign-out. @Test func namesAreKeptAndForgotten() throws { let suite = "cotton.chat.tests.\(UUID().uuidString)" let defaults = try #require(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite) } ChatNames.remember(["+15550100001": "Asha Example", "+15550100003": " "], in: defaults) ChatNames.remember(["+15550100004": "Ravi Example"], in: defaults) #expect(ChatNames.read(from: defaults) == ["+15550100001": "Asha Example", "+15550100004": "Ravi Example"]) ChatNames.forget(in: defaults) #expect(ChatNames.read(from: defaults).isEmpty) } // RULE: the threads kept for the extension merge by number, skip a blank, and go at sign-out. @Test func threadsAreKeptAndForgotten() throws { let suite = "cotton.chat.tests.\(UUID().uuidString)" let defaults = try #require(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite) } ChatThreads.remember(["+15550100001": "t-one", "+15550100003": ""], in: defaults) ChatThreads.remember(["+15550100001": "t-one", "+15550100004": "t-four"], in: defaults) #expect(ChatThreads.read(from: defaults) == ["+15550100001": "t-one", "+15550100004": "t-four"]) ChatThreads.forget(in: defaults) #expect(ChatThreads.read(from: defaults).isEmpty) } }