Skip to content

Commit

Permalink
Add isMainKeyInMemory variable in Keymaker
Browse files Browse the repository at this point in the history
  • Loading branch information
victor committed Oct 11, 2023
2 parents d2adaa0 + e17c0d5 commit 450c5d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions libraries/Keymaker/Sources/Keymaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public class Keymaker: NSObject {
}
}
}

public var isMainKeyInMemory: Bool {
_mainKey != nil
}

// accessor for stored value; if stored value is nill - calls provokeMainKeyObtention() method

Expand Down
22 changes: 15 additions & 7 deletions libraries/Keymaker/Tests/KeymakerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import XCTest
@testable import ProtonCoreKeymaker

class KeymakerTests: XCTestCase {

var sut: Keymaker!
var keychainMock: KeychainMock!
var protectorMock: ProtectionStrategyMock!
var keymaker: Keymaker!

override func setUp() async throws {
keychainMock = KeychainMock(service: "whatever", accessGroup: "whatever")
keymaker = Keymaker(autolocker: nil, keychain: keychainMock)
sut = Keymaker(autolocker: nil, keychain: keychainMock)
protectorMock = ProtectionStrategyMock()
ProtectionStrategyMock.underlyingKeychainLabel = "whatever"
protectorMock.keychain = keychainMock
Expand All @@ -40,7 +39,7 @@ class KeymakerTests: XCTestCase {
}

override func tearDown() async throws {
keymaker = nil
sut = nil
try await super.tearDown()
}

Expand All @@ -53,7 +52,7 @@ class KeymakerTests: XCTestCase {
}

do {
try await keymaker.verify(protector: protectorMock)
try await sut.verify(protector: protectorMock)
} catch {
XCTFail("Verification failed with: \(error)")
}
Expand All @@ -63,7 +62,7 @@ class KeymakerTests: XCTestCase {
keychainMock.dataForKeyClosure = { _ in return nil }

do {
try await keymaker.verify(protector: protectorMock)
try await sut.verify(protector: protectorMock)
XCTFail("Verification should throw error.")
} catch {
guard let err = error as? Keymaker.Errors, err == .cypherBitsIsNil else {
Expand All @@ -81,7 +80,7 @@ class KeymakerTests: XCTestCase {
}

do {
try await keymaker.verify(protector: protectorMock)
try await sut.verify(protector: protectorMock)
XCTFail("Verification should throw error.")
} catch {
guard let err = error as? Keymaker.Errors, err == .cypherBitsIsNil else {
Expand All @@ -90,4 +89,13 @@ class KeymakerTests: XCTestCase {
}
}
}

func testIsMainKeyInMemory_whenItIsInMemory_itShouldReturnTrue() {
sut.forceInjectMainKey(NoneProtection.generateRandomValue(length: 32))
XCTAssertTrue(sut.isMainKeyInMemory)
}

func testIsMainKeyInMemory_whenItIsNotInMemory_itShouldReturnTrue() {
XCTAssertFalse(sut.isMainKeyInMemory)
}
}

0 comments on commit 450c5d1

Please sign in to comment.