-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test Hmac (wip commit, rewrite this message)
- Loading branch information
Showing
4 changed files
with
51 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
@testable import TutanotaSharedFramework | ||
|
||
class HmacTest: XCTestCase { | ||
private func prepareTestData() async -> (IosNativeCryptoFacade, ((DataWrapper, DataWrapper), DataWrapper)) { | ||
let facade = IosNativeCryptoFacade() | ||
let rawKey = secureRandomBytes(ofLength: 32) | ||
let rawData = secureRandomBytes(ofLength: 256) | ||
let key = DataWrapper(data: Data(bytes: rawKey, count: rawKey.count)) | ||
let data = DataWrapper(data: Data(bytes: rawData, count: rawData.count)) | ||
let computedTag = await facade.hmacSha256(key, data) | ||
// Swift does not want tuples to exceed two items. So we respect that. | ||
// Feel free to define a struct for this if these nested tuples bother you. | ||
return (facade, ((key, data), computedTag)) | ||
} | ||
func testRoundTrip() async throws { | ||
let (facade, ((key, data), computedTag)) = await prepareTestData() | ||
try await facade.verifyHmacSha256(key, data, computedTag) | ||
} | ||
func testBadKey() async throws { | ||
let (facade, ((key, data), computedTag)) = await prepareTestData() | ||
let rawBadKey = secureRandomBytes(ofLength: 32) | ||
let badKey = DataWrapper(data: Data(bytes: rawBadKey, count: rawBadKey.count)) | ||
await TUTAssertThrowsErrorAsync(try await facade.verifyHmacSha256(badKey, data, computedTag)) | ||
} | ||
func testBadData() async throws { | ||
let (facade, ((key, data), computedTag)) = await prepareTestData() | ||
let rawBadData = secureRandomBytes(ofLength: 256) | ||
let badData = DataWrapper(data: Data(bytes: rawBadData, count: rawBadData.count)) | ||
await TUTAssertThrowsErrorAsync(try await facade.verifyHmacSha256(key, badData, computedTag)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import XCTest | ||
|
||
func TUTAssertThrowsErrorAsync<T>( | ||
_ expression: @autoclosure () async throws -> T, | ||
_ message: @autoclosure () -> String = "", | ||
file: StaticString = #filePath, | ||
line: UInt = #line, | ||
_ errorHandler: (_ error: any Error) -> Void = { _ in } | ||
) async { | ||
do { | ||
_ = try await expression() | ||
XCTFail(message(), file: file, line: line) | ||
} catch { errorHandler(error) } | ||
} |