-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #714 sort recipient keys depending on expire date
- Loading branch information
Showing
3 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ class RecipientTests: XCTestCase { | |
|
||
func testRecipientWithRevokedKey() { | ||
let keyDetails = generateKey(expiration: nil, revoked: true) | ||
let recipient = RecipientWithPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
|
||
XCTAssertEqual(recipient.keyState, .revoked) | ||
} | ||
|
@@ -23,37 +23,60 @@ class RecipientTests: XCTestCase { | |
let expiration = Date().timeIntervalSince1970 - 60 * 60 | ||
let keyDetails = generateKey(expiration: Int(expiration), revoked: false) | ||
|
||
let recipient = RecipientWithPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
XCTAssertEqual(recipient.keyState, .expired) | ||
} | ||
|
||
func testRecipientWithValidKey() { | ||
let expiration = Date().timeIntervalSince1970 + 60 * 60 | ||
let keyDetails = generateKey(expiration: Int(expiration), revoked: false) | ||
let recipient = RecipientWithPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails]) | ||
XCTAssertEqual(recipient.keyState, .active) | ||
|
||
let keyDetails2 = generateKey(expiration: nil, revoked: false) | ||
let recipient2 = RecipientWithPubKeys(email: "[email protected]", keyDetails: [keyDetails2]) | ||
let recipient2 = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails2]) | ||
XCTAssertEqual(recipient2.keyState, .active) | ||
} | ||
|
||
func testRecipientWithoutPubKey() { | ||
let recipient = RecipientWithPubKeys(email: "[email protected]", keyDetails: []) | ||
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: []) | ||
XCTAssertEqual(recipient.keyState, .empty) | ||
} | ||
|
||
func testRecipientKeysOrder() { | ||
let revokedKey = generateKey(expiration: Int(Date().timeIntervalSince1970 + 60 * 60), revoked: true) | ||
|
||
let activeKey1 = generateKey(expiration: Int(Date().timeIntervalSince1970 + 60 * 60)) | ||
let activeKey2 = generateKey(expiration: Int(Date().timeIntervalSince1970 + 48 * 60 * 60)) | ||
let activeKey3 = generateKey(expiration: Int(Date().timeIntervalSince1970 + 24 * 60 * 60)) | ||
|
||
let nonExpiringKey = generateKey(expiration: nil) | ||
let expiredKey = generateKey(expiration: Int(Date().timeIntervalSince1970 - 60 * 60)) | ||
let oldExpiredKey = generateKey(expiration: Int(Date().timeIntervalSince1970 - 24 * 60 * 60)) | ||
|
||
let keyDetails = [revokedKey, oldExpiredKey, activeKey1, expiredKey, activeKey2, nonExpiringKey, activeKey3] | ||
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", | ||
keyDetails: keyDetails) | ||
|
||
XCTAssertEqual(recipient.pubKeys[0].fingerprint, nonExpiringKey.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[1].fingerprint, activeKey2.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[2].fingerprint, activeKey3.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[3].fingerprint, activeKey1.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[4].fingerprint, expiredKey.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[5].fingerprint, oldExpiredKey.primaryFingerprint) | ||
XCTAssertEqual(recipient.pubKeys[6].fingerprint, revokedKey.primaryFingerprint) | ||
} | ||
} | ||
|
||
extension RecipientTests { | ||
private func generateKey(expiration: Int?, revoked: Bool) -> KeyDetails { | ||
private func generateKey(expiration: Int?, revoked: Bool = false) -> KeyDetails { | ||
KeyDetails( | ||
public: "Public part", | ||
private: nil, | ||
isFullyDecrypted: false, | ||
isFullyEncrypted: false, | ||
ids: [ | ||
KeyId(longid: "longid", fingerprint: "fingerprint") | ||
], | ||
ids: [KeyId(longid: randomString(length: 16), | ||
fingerprint: randomString(length: 16))], | ||
created: 1, | ||
lastModified: nil, | ||
expiration: expiration, | ||
|
@@ -62,4 +85,9 @@ extension RecipientTests { | |
revoked: revoked | ||
) | ||
} | ||
|
||
private func randomString(length: Int) -> String { | ||
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
return String((0..<length).map { _ in letters.randomElement()! }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters