-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFlowCryptCoreTests.swift
More file actions
607 lines (556 loc) · 24.4 KB
/
Copy pathFlowCryptCoreTests.swift
File metadata and controls
607 lines (556 loc) · 24.4 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//
// FlowCryptCoreTests.swift
//
// Created by luke on 21/7/2019.
// Copyright © 2017-present FlowCrypt a. s. All rights reserved.
//
@testable import FlowCrypt
import FlowCryptCommon
import XCTest
final class FlowCryptCoreTests: XCTestCase {
let core: Core = .shared
private func testPerformance(maxDuration: Double, repeats: Int = 5, testBlock: () async throws -> Void) async {
var durations: [Double] = []
for _ in 1 ... repeats {
let timer = TestTimer()
timer.start()
do {
try await testBlock()
} catch {}
timer.stop()
durations.append(timer.durationMs)
}
let average = durations.reduce(0, +) / Double(durations.count)
XCTAssertLessThan(average, maxDuration)
}
// the tests below
func testVersions() async throws {
let r = try await core.version()
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "[unknown version]"
XCTAssertEqual(r.app_version, "iOS \(appVersion)")
}
func testGenerateKey() async throws {
let r = try await core.generateKey(
passphrase: "some pass phrase test",
variant: KeyVariant.curve25519,
userIds: [UserId(email: "first@domain.com", name: "First")]
)
XCTAssertNotNil(r.key.private)
XCTAssertEqual(r.key.isFullyDecrypted, false)
XCTAssertEqual(r.key.isFullyEncrypted, true)
XCTAssertNotNil(r.key.private!.range(of: "-----BEGIN PGP PRIVATE KEY BLOCK-----"))
XCTAssertNotNil(r.key.public.range(of: "-----BEGIN PGP PUBLIC KEY BLOCK-----"))
XCTAssertEqual(r.key.ids.count, 2)
}
func testZxcvbnStrengthBarWeak() async throws {
let r = try await core.zxcvbnStrengthBar(passPhrase: "nothing much")
XCTAssertEqual(r.word.word, CoreRes.ZxcvbnStrengthBar.WordDetails.Word.weak)
XCTAssertEqual(r.word.pass, false)
XCTAssertEqual(r.word.color, CoreRes.ZxcvbnStrengthBar.WordDetails.Color.red)
XCTAssertEqual(r.word.bar, 10)
XCTAssertEqual(r.time, "less than a second")
}
func testZxcvbnStrengthBarStrong() async throws {
let r = try await core.zxcvbnStrengthBar(passPhrase: "this one is seriously over the top strong pwd")
XCTAssertEqual(r.word.word, CoreRes.ZxcvbnStrengthBar.WordDetails.Word.perfect)
XCTAssertEqual(r.word.pass, true)
XCTAssertEqual(r.word.color, CoreRes.ZxcvbnStrengthBar.WordDetails.Color.green)
XCTAssertEqual(r.word.bar, 100)
XCTAssertEqual(r.time, "millennia")
}
func testParseKeys() async throws {
let r = try await core.parseKeys(armoredOrBinary: TestData.k0.public.data(using: .utf8)! + [10] + TestData.k1.private.data(using: .utf8)!)
XCTAssertEqual(r.format, CoreRes.ParseKeys.Format.armored)
XCTAssertEqual(r.keyDetails.count, 2)
// k0 k is public
let k0 = r.keyDetails[0]
XCTAssertNil(k0.private)
XCTAssertNil(k0.isFullyDecrypted)
XCTAssertNil(k0.isFullyEncrypted)
XCTAssertEqual(k0.lastModified, 1_543_925_225)
XCTAssertNil(k0.expiration)
// k1 is private
let k1 = r.keyDetails[1]
XCTAssertNotNil(k1.private)
XCTAssertEqual(k1.isFullyDecrypted, false)
XCTAssertEqual(k1.isFullyEncrypted, true)
XCTAssertEqual(k1.lastModified, 1_563_630_809)
XCTAssertNil(k1.expiration)
// todo - could test user ids
}
func testDecryptKeyWithCorrectPassPhrase() async throws {
let decryptKeyRes = try await core.decryptKey(armoredPrv: TestData.k0.private, passphrase: TestData.k0.passphrase!)
XCTAssertNotNil(decryptKeyRes.decryptedKey)
// make sure indeed decrypted
let parseKeyRes = try await core.parseKeys(armoredOrBinary: decryptKeyRes.decryptedKey.data(using: .utf8)!)
XCTAssertEqual(parseKeyRes.keyDetails[0].isFullyDecrypted, true)
XCTAssertEqual(parseKeyRes.keyDetails[0].isFullyEncrypted, false)
}
func testDecryptKeyWithWrongPassPhrase() async {
do {
_ = try await core.decryptKey(armoredPrv: TestData.k0.private, passphrase: "wrong")
XCTFail("Should have thrown above")
} catch {
Logger.logDebug("catched \(error)")
return
}
XCTFail("Should have thrown above")
}
func testComposeEmailPlain() async throws {
let msg = SendableMsg(
text: "this is the message",
html: "this is the message",
to: ["email@hello.com"],
cc: [],
bcc: [],
from: "sender@hello.com",
subject: "subj",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: nil,
signingPrv: nil,
password: nil
)
let r = try await core.composeEmail(msg: msg, fmt: .plain)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
XCTAssertNil(mime.range(of: "-----BEGIN PGP MESSAGE-----")) // not encrypted
XCTAssertNotNil(mime.range(of: msg.text)) // plain text visible
XCTAssertNotNil(mime.range(of: "Subject: \(msg.subject)")) // has mime Subject header
XCTAssertNil(mime.range(of: "In-Reply-To")) // Not a reply
}
func testComposeEmailEncryptInline() async throws {
let msg = SendableMsg(
text: "this is the message",
html: "this is the message",
to: ["email@hello.com"],
cc: [],
bcc: [],
from: "sender@hello.com",
subject: "subj",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [TestData.k0.public, TestData.k1.public],
signingPrv: nil,
password: nil
)
let r = try await self.core.composeEmail(msg: msg, fmt: .encryptInline)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
XCTAssertNotNil(mime.range(of: "-----BEGIN PGP MESSAGE-----")) // encrypted
XCTAssertNil(mime.range(of: msg.text)) // plain text not visible
XCTAssertNotNil(mime.range(of: "Subject: \(msg.subject)")) // has mime Subject header
XCTAssertNil(mime.range(of: "In-Reply-To")) // Not a reply
}
func testComposeEmailInlineWithAttachment() async throws {
let initialFileName = "data.txt"
let urlPath = URL(fileURLWithPath: Bundle(for: type(of: self))
.path(forResource: "data", ofType: "txt")!)
let fileData = try! Data(contentsOf: urlPath, options: .dataReadingMapped)
let attachment = SendableMsg.Attachment(
name: initialFileName, type: "text/plain",
base64: fileData.base64EncodedString()
)
let msg = SendableMsg(
text: "this is the message",
html: "this is the message",
to: ["email@hello.com"], cc: [], bcc: [],
from: "sender@hello.com",
subject: "subj",
replyToMsgId: nil,
inReplyTo: nil,
atts: [attachment],
pubKeys: [TestData.k0.public, TestData.k1.public],
signingPrv: nil,
password: nil
)
let r = try await core.composeEmail(msg: msg, fmt: .encryptInline)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
XCTAssertNil(mime.range(of: msg.text)) // text encrypted
XCTAssertNotNil(mime.range(of: "Content-Type: application/pgp-encrypted")) // encrypted
XCTAssertNotNil(mime.range(of: "name=\(attachment.name)")) // attachment
XCTAssertNotNil(mime.range(of: "Subject: \(msg.subject)")) // has mime Subject header
}
func testComposeEmailWithSigningKey() async throws {
// arrange
let signingKey = TestData.k0
let msg = SendableMsg(
text: "this is the message",
html: nil,
to: ["email@hello.com"], cc: [], bcc: [],
from: "sender@hello.com",
subject: "Signed email",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [TestData.k0.public],
signingPrv: signingKey,
password: nil
)
// act
let r = try await core.composeEmail(msg: msg, fmt: .encryptInline)
// assert
let decrypted = try await core.parseDecryptMsg(
encrypted: r.mimeEncoded,
keys: [signingKey],
msgPwd: nil,
isMime: true,
verificationPubKeys: [TestData.k0.public]
)
guard let verifyResult = decrypted.blocks.first?.verifyRes else {
XCTFail("verify result expected")
return
}
XCTAssertTrue(verifyResult.match ?? false)
XCTAssertEqual(verifyResult.signer, "063635B3E33EB14C")
}
func testComposeEmailWithSigningKeyWithoutVerificationKey() async throws {
// arrange
let signingKey = TestData.k0
let msg = SendableMsg(
text: "this is the message",
html: nil,
to: ["email@hello.com"], cc: [], bcc: [],
from: "sender@hello.com",
subject: "Signed email",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [TestData.k0.public],
signingPrv: signingKey,
password: nil
)
// act
let r = try await core.composeEmail(msg: msg, fmt: .encryptInline)
// assert
let decrypted = try await core.parseDecryptMsg(
encrypted: r.mimeEncoded,
keys: [signingKey],
msgPwd: nil,
isMime: true,
verificationPubKeys: []
)
guard let verifyResult = decrypted.blocks.first?.verifyRes else {
XCTFail("verify result expected")
return
}
XCTAssertNil(verifyResult.match)
XCTAssertEqual(verifyResult.signer, "063635B3E33EB14C")
}
func testEndToEnd() async throws {
let passphrase = "some pass phrase test"
let email = "e2e@domain.com"
let text = "this is the encrypted e2e content"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end")]
)
let msg = SendableMsg(
text: text,
html: text,
to: [email],
cc: [],
bcc: [],
from: email,
subject: "e2e subj",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [generateKeyRes.key.public],
signingPrv: nil,
password: nil
)
let mime = try await core.composeEmail(msg: msg, fmt: .encryptInline)
let keys = try [Keypair(generateKeyRes.key, passPhrase: passphrase, source: "test")]
let decrypted = try await core.parseDecryptMsg(encrypted: mime.mimeEncoded, keys: keys, msgPwd: nil, isMime: true, verificationPubKeys: [])
XCTAssertEqual(decrypted.text, text)
XCTAssertEqual(decrypted.replyType, ReplyType.encrypted)
XCTAssertEqual(decrypted.blocks.count, 1)
let b = decrypted.blocks[0]
XCTAssertNil(b.keyDetails) // should only be present on pubkey blocks
XCTAssertNil(b.decryptErr) // was supposed to be a success
XCTAssertEqual(b.type, MsgBlock.BlockType.plainHtml)
XCTAssertNotNil(b.content.range(of: text)) // original text contained within the formatted html block
}
// Regression test for https://github.com/FlowCrypt/flowcrypt-ios/issues/630
// Verifies that emoji / non-BMP Unicode scalars survive the full
// compose -> encrypt -> parseDecryptMsg round-trip through the JS core
// (WKWebView bridge). If this passes, any user-visible emoji rendering
// issue is located above the Core layer (i.e. in ThreadDetailWebNode /
// WKWebView HTML rendering).
func testEndToEndWithEmoji() async throws {
let passphrase = "some pass phrase test"
let email = "e2e-emoji@domain.com"
// Mix BMP emoji, supplementary-plane emoji (surrogate pair in UTF-16),
// ZWJ sequence, combining mark, and Chinese character.
let text = "Hello 😀 🙂 🔐 👩💻 é 汉"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end emoji")]
)
let msg = SendableMsg(
text: text,
html: text,
to: [email],
cc: [],
bcc: [],
from: email,
subject: "emoji subj 😀",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [generateKeyRes.key.public],
signingPrv: nil,
password: nil
)
let mime = try await core.composeEmail(msg: msg, fmt: .encryptInline)
let keys = try [Keypair(generateKeyRes.key, passPhrase: passphrase, source: "test")]
let decrypted = try await core.parseDecryptMsg(
encrypted: mime.mimeEncoded,
keys: keys,
msgPwd: nil,
isMime: true,
verificationPubKeys: []
)
XCTAssertEqual(decrypted.replyType, ReplyType.encrypted)
// The plain text field must match byte-for-byte after round-trip.
XCTAssertEqual(decrypted.text, text)
XCTAssertEqual(decrypted.blocks.count, 1)
let b = decrypted.blocks[0]
XCTAssertEqual(b.type, MsgBlock.BlockType.plainHtml)
XCTAssertNil(b.decryptErr)
// Each emoji/unicode scalar must appear intact inside the rendered html block.
for scalar in ["😀", "🙂", "🔐", "👩💻", "é", "汉"] {
XCTAssertNotNil(
b.content.range(of: scalar),
"expected \(scalar) to survive round-trip inside rendered html block"
)
}
}
func testDecryptErrMismatch() async throws {
let key = TestData.k0
let r = try await core.parseDecryptMsg(encrypted: TestData.mismatchEncryptedMsg.data(using: .utf8)!, keys: [key], msgPwd: nil, isMime: false, verificationPubKeys: [])
let decrypted = r
XCTAssertEqual(decrypted.text, "")
XCTAssertEqual(decrypted.replyType, ReplyType.plain) // replies to errors should be plain
XCTAssertEqual(decrypted.blocks.count, 2)
let contentBlock = decrypted.blocks[0]
XCTAssertEqual(contentBlock.type, MsgBlock.BlockType.plainHtml)
XCTAssertNotNil(contentBlock.content.range(of: "<body></body>")) // formatted content is empty
let decryptErrBlock = decrypted.blocks[1]
XCTAssertEqual(decryptErrBlock.type, MsgBlock.BlockType.decryptErr)
XCTAssertNotNil(decryptErrBlock.decryptErr)
let e = decryptErrBlock.decryptErr!
XCTAssertEqual(e.error.type, DecryptErr.ErrorType.keyMismatch)
}
func testEncryptFile() async throws {
// Given
let initialFileName = "data.txt"
let urlPath = URL(fileURLWithPath: Bundle(for: type(of: self))
.path(forResource: "data", ofType: "txt")!)
let fileData = try! Data(contentsOf: urlPath, options: .dataReadingMapped)
let passphrase = "some pass phrase test"
let email = "e2e@domain.com"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end")]
)
// When
let encrypted = try await core.encrypt(
file: fileData,
name: initialFileName,
pubKeys: [generateKeyRes.key.public]
)
let decrypted = try await core.decryptFile(
encrypted: encrypted,
keys: [Keypair(generateKeyRes.key, passPhrase: passphrase, source: "test")],
msgPwd: nil
)
// Then
XCTAssertTrue(decrypted.decryptSuccess?.data == fileData)
XCTAssertTrue(decrypted.decryptSuccess?.data.toStr() == fileData.toStr())
XCTAssertTrue(decrypted.decryptSuccess?.name == initialFileName)
}
func testDecryptNotEncryptedFile() async throws {
// Given
let urlPath = URL(fileURLWithPath: Bundle(for: type(of: self))
.path(forResource: "data", ofType: "txt")!)
let fileData = try! Data(contentsOf: urlPath, options: .dataReadingMapped)
let passphrase = "some pass phrase test"
let email = "e2e@domain.com"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end")]
)
// When
let decryptRes = try await core.decryptFile(
encrypted: fileData,
keys: [Keypair(generateKeyRes.key, passPhrase: passphrase, source: "test")],
msgPwd: nil
)
// Then
XCTAssertEqual(decryptRes.decryptErr?.error.type, DecryptErr.ErrorType.format)
}
func testDecryptWithNoKeys() async throws {
// Given
let initialFileName = "data.txt"
let urlPath = URL(fileURLWithPath: Bundle(for: type(of: self))
.path(forResource: "data", ofType: "txt")!)
let fileData = try! Data(contentsOf: urlPath, options: .dataReadingMapped)
let passphrase = "some pass phrase test"
let email = "e2e@domain.com"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end")]
)
let k = generateKeyRes.key
let encryptedFile = try await core.encrypt(
file: fileData,
name: initialFileName,
pubKeys: [k.public]
)
let decryptResult = try await core.decryptFile(
encrypted: encryptedFile,
keys: [],
msgPwd: nil
)
XCTAssertEqual(decryptResult.decryptErr!.error.type, DecryptErr.ErrorType.keyMismatch)
}
func testRsaPerformance() async throws {
// Test decrypt key
await testPerformance(maxDuration: 600) {
_ = try await core.decryptKey(armoredPrv: TestData.k3rsa4096.private, passphrase: TestData.k3rsa4096.passphrase!)
}
// Test encrypt key
let decryptKeyRes = try await core.decryptKey(armoredPrv: TestData.k3rsa4096.private, passphrase: TestData.k3rsa4096.passphrase!)
await testPerformance(maxDuration: 1200) {
_ = try await core.encryptKey(armoredPrv: decryptKeyRes.decryptedKey, passphrase: TestData.k3rsa4096.passphrase!)
}
// Test verify key
await testPerformance(maxDuration: 300) {
try await core.verifyKey(armoredPrv: TestData.k3rsa4096.private)
}
// Test encrypt message
await testPerformance(maxDuration: 300) {
_ = try await core.encrypt(
data: "Test email message".data(),
pubKeys: [TestData.k3rsa4096.public],
password: nil
)
}
// Test decrypt message
let encrypted = try await core.encrypt(
data: "Test email message".data(),
pubKeys: [TestData.k3rsa4096.public],
password: nil
)
await testPerformance(maxDuration: 120) {
_ = try await core.parseDecryptMsg(
encrypted: encrypted,
keys: [TestData.k3rsa4096],
msgPwd: nil,
isMime: true,
verificationPubKeys: [TestData.k3rsa4096.public]
)
}
// Test sign message
let msg = SendableMsg(
text: "this is the message",
html: nil,
to: ["email@recipient.test"],
cc: [],
bcc: [],
from: "sender@sender.test",
subject: "Signed email",
replyToMsgId: nil,
inReplyTo: nil,
atts: [],
pubKeys: [TestData.k3rsa4096.public],
signingPrv: TestData.k3rsa4096,
password: nil
)
await testPerformance(maxDuration: 1200) {
_ = try await core.composeEmail(msg: msg, fmt: .encryptInline)
}
}
func testDecryptEncryptedFile() async throws {
// Given
let initialFileName = "data.txt"
let urlPath = URL(fileURLWithPath: Bundle(for: type(of: self))
.path(forResource: "data", ofType: "txt")!)
let fileData = try! Data(contentsOf: urlPath, options: .dataReadingMapped)
let passphrase = "some pass phrase test"
let email = "e2e@domain.com"
let generateKeyRes = try await core.generateKey(
passphrase: passphrase,
variant: KeyVariant.curve25519,
userIds: [UserId(email: email, name: "End to end")]
)
// When
let encrypted = try await core.encrypt(
file: fileData,
name: initialFileName,
pubKeys: [generateKeyRes.key.public]
)
let decrypted = try await core.decryptFile(
encrypted: encrypted,
keys: [Keypair(generateKeyRes.key, passPhrase: passphrase, source: "test")],
msgPwd: nil
)
// Then
XCTAssertEqual(decrypted.decryptSuccess?.name, initialFileName)
XCTAssertEqual(decrypted.decryptSuccess?.data.count, fileData.count)
}
func testException() async throws {
do {
_ = try await core.decryptKey(armoredPrv: "not really a key", passphrase: "whatnot")
XCTFail("Should have thrown above")
} catch let CoreError.exception(message) {
XCTAssertNotNil(message.range(of: "Error: Misformed armored text"))
}
}
// This test always passes, even wrongly, on simulators running on a mac with 2 or fewer cores.
// Behaves meaningfully on real iPhone or simulator on a mac with many cores
func testCoreResponseCorrectnessUnderConcurrency() async throws {
// given: a bunch of keys
let pp = "this particular pass phrase is long enough"
let k0 = try await core.generateKey(passphrase: pp, variant: KeyVariant.curve25519, userIds: [UserId(email: "k0@concurrent.test", name: "k0")])
let k1 = try await core.generateKey(passphrase: pp, variant: KeyVariant.curve25519, userIds: [UserId(email: "k1@concurrent.test", name: "k1")])
let k2 = try await core.generateKey(passphrase: pp, variant: KeyVariant.curve25519, userIds: [UserId(email: "k2@concurrent.test", name: "k2")])
let k3 = try await core.generateKey(passphrase: pp, variant: KeyVariant.curve25519, userIds: [UserId(email: "k3@concurrent.test", name: "k3")])
let k4 = try await core.generateKey(passphrase: pp, variant: KeyVariant.curve25519, userIds: [UserId(email: "k4@concurrent.test", name: "k4")])
// when: keys are parsed concurrently
async let p0prv = try await core.parseKeys(armoredOrBinary: k0.key.private!.data())
async let p0pub = try await core.parseKeys(armoredOrBinary: k0.key.public.data())
async let p1prv = try await core.parseKeys(armoredOrBinary: k1.key.private!.data())
async let p1pub = try await core.parseKeys(armoredOrBinary: k1.key.public.data())
async let p2prv = try await core.parseKeys(armoredOrBinary: k2.key.private!.data())
async let p2pub = try await core.parseKeys(armoredOrBinary: k2.key.public.data())
async let p3prv = try await core.parseKeys(armoredOrBinary: k3.key.private!.data())
async let p3pub = try await core.parseKeys(armoredOrBinary: k3.key.public.data())
async let p4prv = try await core.parseKeys(armoredOrBinary: k4.key.private!.data())
async let p4pub = try await core.parseKeys(armoredOrBinary: k4.key.public.data())
let prvs = try await [p0prv, p1prv, p2prv, p3prv, p4prv]
let pubs = try await [p0pub, p1pub, p2pub, p3pub, p4pub]
// then: parse results are not mixed up
for (i, parsed) in prvs.enumerated() {
XCTAssertEqual(
parsed.keyDetails.first!.pgpUserEmails.first!,
"k\(i)@concurrent.test"
)
XCTAssertNotNil(parsed.keyDetails.first?.private)
}
for (i, parsed) in pubs.enumerated() {
XCTAssertEqual(
parsed.keyDetails.first!.pgpUserEmails.first!,
"k\(i)@concurrent.test"
)
XCTAssertNil(parsed.keyDetails.first?.private)
}
}
}