|
| 1 | +// |
| 2 | +// VAPIDKeyTests.swift |
| 3 | +// swift-webpush |
| 4 | +// |
| 5 | +// Created by Dimitri Bouniol on 2024-12-22. |
| 6 | +// Copyright © 2024 Mochi Development, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Crypto |
| 10 | +import Foundation |
| 11 | +import Testing |
| 12 | +@testable import WebPush |
| 13 | +import WebPushTesting |
| 14 | + |
| 15 | +@Suite("VAPID Key Tests") struct VAPIDKeyTests { |
| 16 | + @Suite struct Initialization { |
| 17 | + @Test func createNew() { |
| 18 | + let key = VAPID.Key() |
| 19 | + #expect(!key.id.description.isEmpty) |
| 20 | + } |
| 21 | + |
| 22 | + @Test func privateKey() { |
| 23 | + let privateKey = P256.Signing.PrivateKey() |
| 24 | + let key = VAPID.Key(privateKey: privateKey) |
| 25 | + #expect(key.id.description == privateKey.publicKey.x963Representation.base64URLEncodedString()) |
| 26 | + } |
| 27 | + |
| 28 | + @Test func base64Representation() throws { |
| 29 | + let key = try VAPID.Key(base64URLEncoded: "6PSSAJiMj7uOvtE4ymNo5GWcZbT226c5KlV6c+8fx5g=") |
| 30 | + #expect(key.id.description == "BKO3ND8PZ4w3TMdjUE-VFLmwKoawWnfU_fHtp2G55mgOQdCY9sf2b9LjVbmItinpRPMC4qv_9GE9bSDYJ0jaErE") |
| 31 | + |
| 32 | + #expect(throws: Base64URLDecodingError()) { |
| 33 | + try VAPID.Key(base64URLEncoded: "()") |
| 34 | + } |
| 35 | + |
| 36 | + #expect(throws: CryptoKitError.self) { |
| 37 | + try VAPID.Key(base64URLEncoded: "AAAA") |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test func equality() throws { |
| 43 | + let key1 = VAPID.Key.mockedKey1 |
| 44 | + let key2 = VAPID.Key.mockedKey2 |
| 45 | + let key3 = VAPID.Key(privateKey: try .init(rawRepresentation: Data(base64URLEncoded: "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=")!)) |
| 46 | + |
| 47 | + #expect(key1 != key2) |
| 48 | + #expect(key1 == .mockedKey1) |
| 49 | + #expect(key1 == key3) |
| 50 | + #expect(key1.hashValue == key3.hashValue) |
| 51 | + } |
| 52 | + |
| 53 | + @Suite struct Coding { |
| 54 | + @Test func encoding() throws { |
| 55 | + #expect(String(decoding: try JSONEncoder().encode(VAPID.Key.mockedKey1), as: UTF8.self) == "\"FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=\"") |
| 56 | + } |
| 57 | + |
| 58 | + @Test func decoding() throws { |
| 59 | + #expect(try JSONDecoder().decode(VAPID.Key.self, from: Data("\"FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=\"".utf8)) == .mockedKey1) |
| 60 | + |
| 61 | + #expect(throws: DecodingError.self) { |
| 62 | + try JSONDecoder().decode(VAPID.Key.self, from: Data("{}".utf8)) |
| 63 | + } |
| 64 | + |
| 65 | + #expect(throws: DecodingError.self) { |
| 66 | + try JSONDecoder().decode(VAPID.Key.self, from: Data("\"()\"".utf8)) |
| 67 | + } |
| 68 | + |
| 69 | + #expect(throws: CryptoKitError.self) { |
| 70 | + try JSONDecoder().decode(VAPID.Key.self, from: Data("\"\"".utf8)) |
| 71 | + } |
| 72 | + |
| 73 | + #expect(throws: CryptoKitError.self) { |
| 74 | + try JSONDecoder().decode(VAPID.Key.self, from: Data("\"AAAA\"".utf8)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Suite struct Identification { |
| 80 | + @Test func comparable() { |
| 81 | + #expect([ |
| 82 | + VAPID.Key.ID.mockedKeyID1, |
| 83 | + VAPID.Key.ID.mockedKeyID2, |
| 84 | + VAPID.Key.ID.mockedKeyID3, |
| 85 | + VAPID.Key.ID.mockedKeyID4, |
| 86 | + ].sorted() == [ |
| 87 | + VAPID.Key.ID.mockedKeyID2, |
| 88 | + VAPID.Key.ID.mockedKeyID4, |
| 89 | + VAPID.Key.ID.mockedKeyID1, |
| 90 | + VAPID.Key.ID.mockedKeyID3, |
| 91 | + ]) |
| 92 | + } |
| 93 | + |
| 94 | + @Test func encoding() throws { |
| 95 | + #expect(String(decoding: try JSONEncoder().encode(VAPID.Key.ID.mockedKeyID1), as: UTF8.self) == "\"BLf3RZAljlexEovBgfZgFTjcEVUKBDr3lIH8quJioMdX4FweRdId_P72h613ptxtU-qSAyW3Tbt_3WgwGhOUxrs\"") |
| 96 | + } |
| 97 | + |
| 98 | + @Test func decoding() throws { |
| 99 | + #expect(try JSONDecoder().decode(VAPID.Key.ID.self, from: Data("\"BLf3RZAljlexEovBgfZgFTjcEVUKBDr3lIH8quJioMdX4FweRdId_P72h613ptxtU-qSAyW3Tbt_3WgwGhOUxrs\"".utf8)) == .mockedKeyID1) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments