Skip to content

Commit

Permalink
issue #714 pubkey sort pr updates (#791)
Browse files Browse the repository at this point in the history
* issue #714 pr updates

* issue #714 update pub keys sorting

Co-authored-by: Tom <[email protected]>
  • Loading branch information
sosnovsky and flowcrypt-machine-user authored Oct 26, 2021
1 parent 7d8f694 commit d7a6586
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ extension RecipientWithSortedPubKeys {

private var sortedPubKeys: [PubKey] {
_pubKeys
.sorted(by: {
guard !$0.isRevoked else { return false }

guard let expire1 = $0.expiresOn,
let expire2 = $1.expiresOn,
!$1.isRevoked
else { return true }

.sorted(by: { key1, key2 in
// check if key1 is revoked
guard !key1.isRevoked else { return false }
// check if key2 is revoked
guard !key2.isRevoked else { return true }
// check if key1 never expires
guard let expire1 = key1.expiresOn else { return true }
// check if key2 never expires
guard let expire2 = key2.expiresOn else { return false }
// compare expire dates
return expire1 > expire2
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ class RecipientTests: XCTestCase {
private let calendar = Calendar.current

func testRecipientWithRevokedKey() {
let keyDetails = generateKey(expiration: nil, revoked: true)
let keyDetails = createFakeKeyDetails(expiration: nil, revoked: true)
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails])

XCTAssertEqual(recipient.keyState, .revoked)
}

func testRecipientWithExpiredKey() {
let expiration = Date().timeIntervalSince1970 - 60 * 60
let keyDetails = generateKey(expiration: Int(expiration), revoked: false)
let keyDetails = createFakeKeyDetails(expiration: Int(expiration), revoked: false)

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 keyDetails = createFakeKeyDetails(expiration: Int(expiration), revoked: false)
let recipient = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails])
XCTAssertEqual(recipient.keyState, .active)

let keyDetails2 = generateKey(expiration: nil, revoked: false)
let keyDetails2 = createFakeKeyDetails(expiration: nil, revoked: false)
let recipient2 = RecipientWithSortedPubKeys(email: "[email protected]", keyDetails: [keyDetails2])
XCTAssertEqual(recipient2.keyState, .active)
}
Expand All @@ -45,23 +45,23 @@ class RecipientTests: XCTestCase {

func testRecipientKeysOrder() {
let now = Int(Date().timeIntervalSince1970)
let revokedKey = generateKey(expiration: now + 60 * 60, revoked: true)
let revokedKey = createFakeKeyDetails(expiration: now + 1 * 3600, revoked: true)

let activeKey1 = generateKey(expiration: now + 60 * 60)
let activeKey2 = generateKey(expiration: now + 48 * 60 * 60)
let activeKey3 = generateKey(expiration: now + 24 * 60 * 60)
let activeKey1 = createFakeKeyDetails(expiration: now + 1 * 3600)
let activeKey2 = createFakeKeyDetails(expiration: now + 2 * 3600)
let activeKey3 = createFakeKeyDetails(expiration: now + 3 * 3600)

let nonExpiringKey = generateKey(expiration: nil)
let expiredKey = generateKey(expiration: now - 60 * 60)
let oldExpiredKey = generateKey(expiration: now - 24 * 60 * 60)
let nonExpiringKey = createFakeKeyDetails(expiration: nil)
let expiredKey = createFakeKeyDetails(expiration: now - 1 * 3600)
let oldExpiredKey = createFakeKeyDetails(expiration: now - 2 * 3600)

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[1].fingerprint, activeKey3.primaryFingerprint)
XCTAssertEqual(recipient.pubKeys[2].fingerprint, activeKey2.primaryFingerprint)
XCTAssertEqual(recipient.pubKeys[3].fingerprint, activeKey1.primaryFingerprint)
XCTAssertEqual(recipient.pubKeys[4].fingerprint, expiredKey.primaryFingerprint)
XCTAssertEqual(recipient.pubKeys[5].fingerprint, oldExpiredKey.primaryFingerprint)
Expand All @@ -70,14 +70,14 @@ class RecipientTests: XCTestCase {
}

extension RecipientTests {
private func generateKey(expiration: Int?, revoked: Bool = false) -> KeyDetails {
private func createFakeKeyDetails(expiration: Int?, revoked: Bool = false) -> KeyDetails {
KeyDetails(
public: "Public part",
private: nil,
isFullyDecrypted: false,
isFullyEncrypted: false,
ids: [KeyId(longid: randomString(length: 16),
fingerprint: randomString(length: 16))],
ids: [KeyId(longid: randomString(length: 40),
fingerprint: randomString(length: 40))],
created: 1,
lastModified: nil,
expiration: expiration,
Expand Down

0 comments on commit d7a6586

Please sign in to comment.