-
Notifications
You must be signed in to change notification settings - Fork 1
test: improve test quality #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| import Foundation | ||
| @testable import OpenTDFKit | ||
| import XCTest | ||
|
|
||
| final class BinaryParserTests: XCTestCase { | ||
| func testInvalidMagicNumberThrows() { | ||
| let data = Data([0x00, 0x00, 0x4C]) | ||
| let parser = BinaryParser(data: data) | ||
|
|
||
| XCTAssertThrowsError(try parser.parseHeader()) { error in | ||
| guard let parsingError = error as? ParsingError, | ||
| case .invalidMagicNumber = parsingError | ||
| else { | ||
| XCTFail("Expected ParsingError.invalidMagicNumber, got \(error)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testInvalidVersionThrows() { | ||
| // Valid magic number ("L1") followed by unsupported v13 (0x4D) | ||
| let data = Data([0x4C, 0x31, 0x4D]) | ||
| let parser = BinaryParser(data: data) | ||
|
|
||
| XCTAssertThrowsError(try parser.parseHeader()) { error in | ||
| guard let parsingError = error as? ParsingError, | ||
| case .invalidVersion = parsingError | ||
| else { | ||
| XCTFail("Expected ParsingError.invalidVersion, got \(error)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testTruncatedHeaderThrowsInvalidFormat() { | ||
| // Magic + version only; missing KAS locator, ECC mode, payload config, policy, and ephemeral key | ||
| let data = Data([0x4C, 0x31, 0x4C]) | ||
| let parser = BinaryParser(data: data) | ||
|
|
||
| XCTAssertThrowsError(try parser.parseHeader()) { error in | ||
| guard let parsingError = error as? ParsingError, | ||
| case .invalidFormat = parsingError | ||
| else { | ||
| XCTFail("Expected ParsingError.invalidFormat, got \(error)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testMissingEphemeralKeyThrowsInvalidFormat() { | ||
| // Valid magic + version + valid KAS locator + valid ECC mode + valid payload config + remote policy | ||
| var data = Data() | ||
| data.append(Header.magicNumber) | ||
| data.append(Header.versionV12) | ||
| data.append(ResourceLocator(protocolEnum: .http, body: "kas.example.com")!.toData()) | ||
| data.append(PolicyBindingConfig(ecdsaBinding: false, curve: .secp256r1).toData()) | ||
| data.append(SignatureAndPayloadConfig(signed: false, signatureCurve: nil, payloadCipher: .aes256GCM128).toData()) | ||
| data.append(Policy.PolicyType.remote.rawValue) | ||
| data.append(ResourceLocator(protocolEnum: .http, body: "kas.example.com/policy")!.toData()) | ||
| data.append(Data(count: 8)) // placeholder GMAC binding | ||
| // Intentionally omit the ephemeral public key | ||
|
|
||
| let parser = BinaryParser(data: data) | ||
| XCTAssertThrowsError(try parser.parseHeader()) { error in | ||
| guard let parsingError = error as? ParsingError, | ||
| case .invalidFormat = parsingError | ||
| else { | ||
| XCTFail("Expected ParsingError.invalidFormat, got \(error)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testMalformedResourceLocatorThrows() { | ||
| var data = Data() | ||
| data.append(Header.magicNumber) | ||
| data.append(Header.versionV12) | ||
| // Protocol byte with invalid protocol value (0xFF) and zero-length body | ||
| data.append(contentsOf: [0xFF, 0x00]) | ||
|
|
||
| let parser = BinaryParser(data: data) | ||
| XCTAssertThrowsError(try parser.parseHeader()) { error in | ||
| guard let parsingError = error as? ParsingError, | ||
| case .invalidFormat = parsingError | ||
| else { | ||
| XCTFail("Expected ParsingError.invalidFormat, got \(error)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,91 @@ | ||
| import CryptoKit | ||
| import Foundation | ||
| @testable import OpenTDFKit | ||
|
|
||
| extension CryptoHelper { | ||
| /// Combined operation that keeps sensitive types within the actor | ||
| struct EncryptionResult { | ||
| let gmacTag: Data | ||
| let nonce: Data | ||
| let ciphertext: Data | ||
| let tag: Data | ||
| } | ||
|
|
||
| func deriveKeysAndEncrypt( | ||
| keyPair: EphemeralKeyPair, | ||
| recipientPublicKey: Data, | ||
| plaintext: Data, | ||
| policyBody: Data, | ||
| ) throws -> EncryptionResult { | ||
| // Derive shared secret | ||
| guard let sharedSecret = try deriveSharedSecret( | ||
| keyPair: keyPair, | ||
| recipientPublicKey: recipientPublicKey, | ||
| ) else { | ||
| throw CryptoHelperError.keyDerivationFailed | ||
| } | ||
|
|
||
| // Derive symmetric key | ||
| let symmetricKey = deriveSymmetricKey( | ||
| sharedSecret: sharedSecret, | ||
| salt: CryptoConstants.hkdfSalt, | ||
| info: CryptoConstants.hkdfInfoEncryption, | ||
| outputByteCount: CryptoConstants.symmetricKeyByteCount, | ||
| ) | ||
|
|
||
| // Create GMAC binding | ||
| let gmacTag = try createGMACBinding( | ||
| policyBody: policyBody, | ||
| symmetricKey: symmetricKey, | ||
| ) | ||
|
|
||
| // Generate nonce | ||
| let nonce = try generateNonce() | ||
|
|
||
| // Encrypt payload | ||
| let (ciphertext, tag) = try encryptPayload( | ||
| plaintext: plaintext, | ||
| symmetricKey: symmetricKey, | ||
| nonce: nonce, | ||
| ) | ||
|
|
||
| return EncryptionResult( | ||
| gmacTag: gmacTag, | ||
| nonce: nonce, | ||
| ciphertext: ciphertext, | ||
| tag: tag, | ||
| ) | ||
| } | ||
|
|
||
| func decryptWithDerivedKeys( | ||
| keyPair: EphemeralKeyPair, | ||
| recipientPublicKey: Data, | ||
| ciphertext: Data, | ||
| nonce: Data, | ||
| tag: Data, | ||
| ) throws -> Data { | ||
| // Derive shared secret | ||
| guard let sharedSecret = try deriveSharedSecret( | ||
| keyPair: keyPair, | ||
| recipientPublicKey: recipientPublicKey, | ||
| ) else { | ||
| throw CryptoHelperError.keyDerivationFailed | ||
| } | ||
|
|
||
| // Derive symmetric key | ||
| let symmetricKey = deriveSymmetricKey( | ||
| sharedSecret: sharedSecret, | ||
| salt: CryptoConstants.hkdfSalt, | ||
| info: CryptoConstants.hkdfInfoEncryption, | ||
| outputByteCount: CryptoConstants.symmetricKeyByteCount, | ||
| ) | ||
|
|
||
| // Decrypt | ||
| return try decryptPayload( | ||
| ciphertext: ciphertext, | ||
| symmetricKey: symmetricKey, | ||
| nonce: nonce, | ||
| tag: tag, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.