-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArmoredTests.swift
93 lines (79 loc) · 3.9 KB
/
ArmoredTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// ArmoredTests.swift
// ProtonCore-Crypto-Tests - Created on 07/15/22.
//
// Copyright (c) 2022 Proton Technologies AG
//
// This file is part of Proton Technologies AG and ProtonCore.
//
// ProtonCore is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonCore is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
import XCTest
import ProtonCoreCryptoGoInterface
import ProtonCoreCrypto
class ArmoredTests: CryptoTestBase {
func testArmoredKey() {
XCTAssertNotEqual("\(ArmoredKey.self)", "\(String.self)")
XCTAssertNotEqual("\(ArmoredMessage.self)", "\(String.self)")
XCTAssertNotEqual("\(ArmoredSignature.self)", "\(String.self)")
XCTAssertEqual("\(ArmoredKey.self)", "\(Armored<ArmoredType.Key>.self)")
XCTAssertEqual("\(ArmoredMessage.self)", "\(Armored<ArmoredType.Message>.self)")
XCTAssertEqual("\(ArmoredSignature.self)", "\(Armored<ArmoredType.Signature>.self)")
let check = "Test"
let armoredKey: ArmoredKey = ArmoredKey.init(value: check)
XCTAssertEqual(check, armoredKey.value)
}
func testUnArmoredKey() {
XCTAssertNotEqual("\(UnArmoredKey.self)", "\(Data.self)")
XCTAssertEqual("\(UnArmoredKey.self)", "\(UnArmored<UnArmoredType.Key>.self)")
let check = random(length: 32)
let unarmored: UnArmoredKey = UnArmoredKey.init(value: check)
XCTAssertEqual(check, unarmored.value)
}
func testArmoredExtensionSplit() {
let testMessage = content(of: "testdata_pgp_message")
let armoredMessage = ArmoredMessage.init(value: testMessage)
XCTAssertNoThrow( try armoredMessage.split() )
}
func testExtensionEncryptPrivateKey() {
let privKey = self.content(of: "user_a_privatekey")
let privKeyPassphrase = self.content(of: "user_a_privatekey_passphrase")
let clearText = "testing Armored<Key> extension. encrypt clear text. no signature."
do {
let armoredKey = ArmoredKey.init(value: privKey)
let armoredMessage: ArmoredMessage = try armoredKey.encrypt(clearText: clearText)
let decryptionKey = DecryptionKey.init(privateKey: armoredKey,
passphrase: Passphrase.init(value: privKeyPassphrase))
let check: String = try Decryptor.decrypt(decryptionKeys: [decryptionKey], encrypted: armoredMessage)
XCTAssertEqual(check, clearText)
} catch {
XCTFail("Should not happen: \(error)")
}
}
func testExtensionEncryptPublicKey() {
let privKey = self.content(of: "user_a_privatekey")
let privKeyPassphrase = self.content(of: "user_a_privatekey_passphrase")
let clearText = "testing Armored<Key> extension. encrypt clear text. no signature."
let pubKey = privKey.publicKey
do {
let armoredKey = ArmoredKey.init(value: pubKey)
let armoredMessage: ArmoredMessage = try armoredKey.encrypt(clearText: clearText)
let decryptionKey = DecryptionKey.init(privateKey: ArmoredKey.init(value: privKey),
passphrase: Passphrase.init(value: privKeyPassphrase))
let check: String = try Decryptor.decrypt(decryptionKeys: [decryptionKey], encrypted: armoredMessage)
XCTAssertEqual(check, clearText)
} catch {
XCTFail("Should not happen: \(error)")
}
}
}