Skip to content
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

[77] Improve test suite for RSAEncrypter/Decrypter #81

Merged
merged 32 commits into from
Jul 30, 2018
Merged
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bc7bbdf
[JOSE-80] Add shared user data introduced Xcode 9.3 to gitignore
mohemian-92817281 Apr 11, 2018
9c0c504
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 May 3, 2018
7d00453
[JOSE-80] Add failure test case for decrypting empty data
mohemian-92817281 May 3, 2018
9567ae8
[JOSE-80] Better assertions for failure case
mohemian-92817281 May 3, 2018
2265f17
[JOSE-80] privateKey2048 -> privateKeyAlice2048
mohemian-92817281 May 3, 2018
a87570c
[JOSE-80] publicKey2048 -> publicKeyAlice2048
mohemian-92817281 May 3, 2018
bcec1c5
[JOSE-80] Add Bob’s 2048 bit private key
mohemian-92817281 May 3, 2018
17ffa68
[JOSE-80] Add encryption test for Bob’s key
mohemian-92817281 May 3, 2018
f7350e5
[JOSE-80] keyPair2048 -> keyPairAlice2048
mohemian-92817281 May 3, 2018
9650165
[JOSE-80] Read Bob’s keypair from plist file
mohemian-92817281 May 3, 2018
79582f8
[JOSE-80] cipherTextBase64URL -> cipherTextWithAliceKeyBase64URL
mohemian-92817281 May 3, 2018
c660a0c
[JOSE-80] Load Bob’s key data; update cipher texts; add Bob decryptio…
mohemian-92817281 May 3, 2018
62f222d
[JOSE-80] Add test for encrypting the same data twice
mohemian-92817281 May 3, 2018
40ce19b
[JOSE-80] Add test to check if Alice’s and Bob’s cipher texts differ
mohemian-92817281 May 3, 2018
a7292e5
[JOSE-80] Add equality for decryption error
mohemian-92817281 May 3, 2018
ee5323a
[JOSE-80] Add default decryption error
mohemian-92817281 May 3, 2018
2d4f09f
[JOSE-80] Decrypting with the wrong key should throw an error
mohemian-92817281 May 7, 2018
8c58365
[JOSE-80] Update Bob’s cipher text
mohemian-92817281 May 7, 2018
f36fdb2
[JOSE-80] Fix Base64 variable names
mohemian-92817281 May 14, 2018
eeecbde
[JOSE-80] Use multiline literals for cipher texts
mohemian-92817281 May 14, 2018
9d675a3
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 18, 2018
03adc40
[JOSE-80] Re-generate Alice’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
25fe769
[JOSE-80] Re-generate Bob’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
ba2dac1
[JOSE-80] Better comments
mohemian-92817281 Jul 26, 2018
3d374e6
[JOSE-80] Remove nonsensical empty string test
mohemian-92817281 Jul 26, 2018
6a8a3c8
[JOSE-80] Rename TestKey.plist -> TestKeys.plist
mohemian-92817281 Jul 26, 2018
09b1963
[JOSE-80] Add decrypter test for zero-length data
mohemian-92817281 Jul 26, 2018
5a75cf4
[JOSE-80] Add tests for encrypting message of length `mLen` and `mLen…
mohemian-92817281 Jul 30, 2018
b221486
[JOSE-80] Better naming for zero length decryption test
mohemian-92817281 Jul 30, 2018
130d902
[JOSE-80] Add decryption tests for `len=k`, `len=k-1`, `len=k+1`
mohemian-92817281 Jul 30, 2018
f307b16
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 30, 2018
fdbfc93
[JOSE-80] Use Alice’s key for new tests
mohemian-92817281 Jul 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions Tests/RSADecrypterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,24 @@ import XCTest
@testable import JOSESwift

class RSADecrypterTests: CryptoTestCase {
let cipherTextWithAliceKeyBase64URL = "RHLdaxC52ve0icBLGh9Xn_jNoV3uJlEuLRer8_7OOqY2QCGdWPb8OiuxxjEep8okfvlrw_eheEll6v8xdjpzP2zopVEBoNZ9J8BDKL1Y-tTi176jlGXBXKJdRitsp14sVHx7x-dW9FjiOHEn_CSm3bM2THAJOEtjjeF6vRvxpGxm1KruTbtZ37VTl-vM-e4STda5dCanRPKFemDPYQvjUAF_wuE-P-UeE0fpxDceuCRN3C-H8LL9TIrSETRV_CzPVf6Ki9zJpaZQAnqZh5ix0KhaHFMawV7TJetcKPbCzEHbxAF5ib16mxKFc5Th3QFS3eRKYxjaEdciXWVMCrGfXQ"
let cipherTextWithBobKeyBase64URL = "TwSS87gjm4cbFbda5klqW_Io2oCoo2zAxWJaU6L1jAeGEoL6u1Gw7vPdEzGGNvUliqsdBBCe8yQhnft9nqIZ9CD7enfVz6tFWvEJuVZbhy0eZad_LLFBFm8eDEgmlw7n7nAZxeww0WloffMmK6GPUE5SjQSWjCRAYA9xwacbxM8shH6hIwpelwV3f-QJr6M_hoAVkiqZJ2tFfiEciPhVVuqNZxxudMxaNj-bWfSipwh2T11kxYQD3EdVR4MDkyxz2JTUQc20OIf_F4WwQXJlbVNLTs4miDKHmw7Ddd45D2DJs_nH-bMQK8b6iH3nke15arP23rozuagF4SHhenApPw"
let cipherTextWithAliceKeyEmptyStringBase64URL = "hyOA/tmOclFkj31UPrRb1EnaRMhR5VZg5TrUyfLMtCUlh3grAva0+sSjqt6zSlWK06A6zUieV69aLRbJ0ZactTTqX2CFrhiZ5nUXhzuUya83VKBI0xrGkpQ8u1y2Iqgb+gbWsFJdJ41cSpZXRpc16Hhd3klTp7YydYZQUG//PLM5bn359kqpT8meJdGqTceehVxmdqTpVwukh/uqLOE8CBCrT7D/2t18mzApGpm/Su4bVbZggJ5g9MRPSnwgq1GjKNcMa1PKd+/OWB/rIeDmorT8dLrusGeLbwFCj1HEz4z5izamiBiyPh96G0m4ZhPtVhR4Fo3ARj9C037GroDQ2w=="

let cipherTextWithAliceKeyBase64 = """
RHLdaxC52ve0icBLGh9Xn_jNoV3uJlEuLRer8_7OOqY2QCGdWPb8OiuxxjEep8okfvlrw_eheEll6v8xdjpzP2zopVEBoNZ9J8BDKL1Y-tTi176jlGXBXKJ\
dRitsp14sVHx7x-dW9FjiOHEn_CSm3bM2THAJOEtjjeF6vRvxpGxm1KruTbtZ37VTl-vM-e4STda5dCanRPKFemDPYQvjUAF_wuE-P-UeE0fpxDceuCRN3C\
-H8LL9TIrSETRV_CzPVf6Ki9zJpaZQAnqZh5ix0KhaHFMawV7TJetcKPbCzEHbxAF5ib16mxKFc5Th3QFS3eRKYxjaEdciXWVMCrGfXQ
"""

let cipherTextWithBobKeyBase64 = """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, now this is a little confusing, why is cipherTextWithBobKeyBase64 and cipherTextWithAliceKeyBase64base64URL andcipherTextWithAliceKeyEmptyStringBase64` "only" base64?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn, you're right. I'll clean this up asap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's base64 encoded now.

printf "The true sign of intelligence is not knowledge but imagination." | openssl rsautl -encrypt -pubin -inkey alice.pub.pem -out >(base64)

TwSS87gjm4cbFbda5klqW_Io2oCoo2zAxWJaU6L1jAeGEoL6u1Gw7vPdEzGGNvUliqsdBBCe8yQhnft9nqIZ9CD7enfVz6tFWvEJuVZbhy0eZad_LLFBFm8\
eDEgmlw7n7nAZxeww0WloffMmK6GPUE5SjQSWjCRAYA9xwacbxM8shH6hIwpelwV3f-QJr6M_hoAVkiqZJ2tFfiEciPhVVuqNZxxudMxaNj-bWfSipwh2T1\
1kxYQD3EdVR4MDkyxz2JTUQc20OIf_F4WwQXJlbVNLTs4miDKHmw7Ddd45D2DJs_nH-bMQK8b6iH3nke15arP23rozuagF4SHhenApPw
"""

let cipherTextWithAliceKeyEmptyStringBase64 = """
hyOA/tmOclFkj31UPrRb1EnaRMhR5VZg5TrUyfLMtCUlh3grAva0+sSjqt6zSlWK06A6zUieV69aLRbJ0ZactTTqX2CFrhiZ5nUXhzuUya83VKBI0xrGkpQ\
8u1y2Iqgb+gbWsFJdJ41cSpZXRpc16Hhd3klTp7YydYZQUG//PLM5bn359kqpT8meJdGqTceehVxmdqTpVwukh/uqLOE8CBCrT7D/2t18mzApGpm/Su4bVb\
ZggJ5g9MRPSnwgq1GjKNcMa1PKd+/OWB/rIeDmorT8dLrusGeLbwFCj1HEz4z5izamiBiyPh96G0m4ZhPtVhR4Fo3ARj9C037GroDQ2w==
"""

let defaultDecryptionError = RSAError.decryptingFailed(description: "The operation couldn’t be completed. (OSStatus error -50 - RSAdecrypt wrong input (err -1))")

Expand All @@ -46,7 +61,7 @@ class RSADecrypterTests: CryptoTestCase {
}

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)
let plainText = try! decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyBase64URL)!)
let plainText = try! decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyBase64)!)

XCTAssertEqual(plainText, message.data(using: .utf8))
}
Expand All @@ -58,7 +73,7 @@ class RSADecrypterTests: CryptoTestCase {
}

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyBob2048!)
let plainText = try! decrypter.decrypt(Data(base64URLEncoded: cipherTextWithBobKeyBase64URL)!)
let plainText = try! decrypter.decrypt(Data(base64URLEncoded: cipherTextWithBobKeyBase64)!)

XCTAssertEqual(plainText, message.data(using: .utf8))
}
Expand All @@ -72,7 +87,7 @@ class RSADecrypterTests: CryptoTestCase {
let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyBob2048!)

// Decrypting with the wrong key should throw an error
XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyBase64URL)!)) { (error: Error) in
XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyBase64)!)) { (error: Error) in
XCTAssertEqual(error as! RSAError, defaultDecryptionError)
}
}
Expand All @@ -86,7 +101,7 @@ class RSADecrypterTests: CryptoTestCase {
let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)

// Decrypting with the wrong key should throw an error
XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithBobKeyBase64URL)!)) { (error: Error) in
XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithBobKeyBase64)!)) { (error: Error) in
XCTAssertEqual(error as! RSAError, defaultDecryptionError)
}
}
Expand All @@ -111,7 +126,7 @@ class RSADecrypterTests: CryptoTestCase {

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)

XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyEmptyStringBase64URL)!)) { (error: Error) in
XCTAssertThrowsError(try decrypter.decrypt(Data(base64URLEncoded: cipherTextWithAliceKeyEmptyStringBase64)!)) { (error: Error) in
XCTAssertEqual(error as! RSAError, defaultDecryptionError)
}
}
Expand Down