Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor tests to use nserror and added more tests

  • Loading branch information...
commit b2704208672746c80b34d345940137804041b3e9 1 parent ea79144
@cvasilak cvasilak authored
Showing with 61 additions and 14 deletions.
  1. +26 −3 crypto-sdkTests/AGCryptoBoxSpec.m
  2. +35 −11 crypto-sdkTests/AGSecretBoxSpec.m
View
29 crypto-sdkTests/AGCryptoBoxSpec.m
@@ -27,6 +27,7 @@
NSString * const ALICE_PUBLIC_KEY = @"8520F0098930A754748B7DDCB43EF75A0DBF3A0D26381AF4EBA4A98EAA9B4E6A";
NSString * const ALICE_PRIVATE_KEY = @"77076D0A7318A57D3C16C17251B26645DF4C2F87EBC0992AB177FBA51DB92C2A";
+ NSString * const ALICE_CORRUPTED_PRIVATE_KEY = @"1207640A7318B57D3C16C17251B26645DF4C2F87EBC0992AB177FBA51DB92C2A";
NSString * const BOB_PRIVATE_KEY = @"5DAB087E624A8A4B79E17F8B83800EE66F3BB1292618B6FD1C2F8B27FF88E0EB";
NSString * const BOB_PUBLIC_KEY = @"DE9EDB7D7B7DC1B4D35B61C2ECE435373F8343C85B78674DADFC7E146F882B4F";
@@ -94,7 +95,7 @@
NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
AGCryptoBox *cryptoBox = [[AGCryptoBox alloc] initWithKey:alicePublicKey privateKey:bobPrivateKey];
- NSData *cipherText = [cryptoBox encrypt:message nonce:nonce];
+ NSData *cipherText = [cryptoBox encrypt:message nonce:nonce error:nil];
[[[AGUtil hexString:cipherText] should] equal:BOX_CIPHERTEXT];
});
@@ -108,13 +109,35 @@
NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
AGCryptoBox *cryptoBox = [[AGCryptoBox alloc] initWithKey:alicePublicKey privateKey:bobPrivateKey];
- NSData *cipherText = [cryptoBox encrypt:message nonce:nonce];
+ NSData *cipherText = [cryptoBox encrypt:message nonce:nonce error:nil];
//Create a new box to test end to end asymmetric encryption
AGCryptoBox *pandora = [[AGCryptoBox alloc] initWithKey:bobPublicKey privateKey:alicePrivateKey];
- NSData *plainText = [pandora decrypt:cipherText nonce:nonce];
+ NSData *plainText = [pandora decrypt:cipherText nonce:nonce error:nil];
[[[AGUtil hexString:plainText] should] equal:BOX_MESSAGE];
});
+
+ it(@"should reject to decrypt with corrupted key", ^{
+ NSData *alicePublicKey = [AGUtil hexStringToBytes:ALICE_PUBLIC_KEY];
+ NSData *alicePrivateKey = [AGUtil hexStringToBytes:ALICE_CORRUPTED_PRIVATE_KEY]; // corrupted key
+ NSData *bobPrivateKey = [AGUtil hexStringToBytes:BOB_PRIVATE_KEY];
+ NSData *bobPublicKey = [AGUtil hexStringToBytes:BOB_PUBLIC_KEY];
+
+ NSData *nonce = [AGUtil hexStringToBytes:BOX_NONCE];
+ NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
+
+ AGCryptoBox *cryptoBox = [[AGCryptoBox alloc] initWithKey:alicePublicKey privateKey:bobPrivateKey];
+ NSData *cipherText = [cryptoBox encrypt:message nonce:nonce error:nil];
+
+ //Create a new box to test end to end asymmetric encryption
+ AGCryptoBox *pandora = [[AGCryptoBox alloc] initWithKey:bobPublicKey privateKey:alicePrivateKey];
+
+ NSError *error;
+ NSData __unused *plainText = [pandora decrypt:cipherText nonce:nonce error:&error];
+
+ [error shouldNotBeNil];
+ [[theValue(error.code) should] equal:theValue(AGCryptoFailedToDecryptError)];
+ });
});
});
View
46 crypto-sdkTests/AGSecretBoxSpec.m
@@ -28,6 +28,7 @@
context(@"Symmetric encryption", ^{
NSString * const BOB_SECRET_KEY = @"5DAB087E624A8A4B79E17F8B83800EE66F3BB1292618B6FD1C2F8B27FF88E0EB";
+ NSString * const BOB_CORRUPTED_SECRET_KEY = @"1FAC087E124A8A4B79E17F8B83800EE66F3BB1292618B6FD1C2F8B27FF88E0EB";
NSString * const BOX_NONCE = @"69696EE955B62B73CD62BDA875FC73D68219E0036B7A0B37";
@@ -67,12 +68,12 @@
NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
secretBox = [[AGSecretBox alloc] initWithKey:key];
-
- NSData *cipherText = [secretBox encrypt:message nonce:nonce];
-
+
+ NSData *cipherText = [secretBox encrypt:message nonce:nonce error:nil];
+
//Create a new box to test end to end symmetric encryption
AGSecretBox *pandora = [[AGSecretBox alloc] initWithKey:key];
- NSData *plainText = [pandora decrypt:cipherText nonce:nonce];
+ NSData *plainText = [pandora decrypt:cipherText nonce:nonce error:nil];
[[[AGUtil hexString:plainText] should] equal:BOX_MESSAGE];
});
@@ -83,7 +84,7 @@
NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
secretBox = [[AGSecretBox alloc] initWithKey:key];
- NSData *cipherText = [secretBox encrypt:message nonce:nonce];
+ NSData *cipherText = [secretBox encrypt:message nonce:nonce error:nil];
[[[AGUtil hexString:cipherText] should] equal:BOX_CIPHERTEXT];
});
@@ -95,11 +96,11 @@
secretBox = [[AGSecretBox alloc] initWithKey:key];
- NSData *cipherText = [secretBox encrypt:message nonce:nonce];
+ NSData *cipherText = [secretBox encrypt:message nonce:nonce error:nil];
//Create a new box to test end to end symmetric encryption
AGSecretBox *pandora = [[AGSecretBox alloc] initWithKey:key];
- NSData *plainText = [pandora decrypt:cipherText nonce:nonce];
+ NSData *plainText = [pandora decrypt:cipherText nonce:nonce error:nil];
[[[AGUtil hexString:plainText] should] equal:BOX_MESSAGE];
});
@@ -111,17 +112,40 @@
secretBox = [[AGSecretBox alloc] initWithKey:key];
- NSData *cipherText = [secretBox encrypt:message nonce:nonce];
+ NSData *cipherText = [secretBox encrypt:message nonce:nonce error:nil];
// corrupt ciphertext
NSMutableData *corupted_cipherText = [NSMutableData dataWithData:cipherText];
[corupted_cipherText resetBytesInRange:NSMakeRange(0,5)];
AGSecretBox *pandora = [[AGSecretBox alloc] initWithKey:key];
+
+ NSError *error;
+ NSData __unused *plainText = [pandora decrypt:corupted_cipherText nonce:nonce error:&error];
+
+ [error shouldNotBeNil];
+ [[theValue(error.code) should] equal:theValue(AGCryptoFailedToDecryptError)];
+ });
- [[theBlock(^{
- NSData __unused *plainText = [pandora decrypt:corupted_cipherText nonce:nonce];
- }) should] raise];
+ it(@"should reject to decrypt corrupted key", ^{
+ NSData *key = [AGUtil hexStringToBytes:BOB_SECRET_KEY];
+
+ NSData *nonce = [AGUtil hexStringToBytes:BOX_NONCE];
+ NSData *message = [AGUtil hexStringToBytes:BOX_MESSAGE];
+
+ secretBox = [[AGSecretBox alloc] initWithKey:key];
+
+ NSData *cipherText = [secretBox encrypt:message nonce:nonce error:nil];
+
+ // initialize with corrupted key
+ NSData *corruptedkey = [AGUtil hexStringToBytes:BOB_CORRUPTED_SECRET_KEY];
+ AGSecretBox *pandora = [[AGSecretBox alloc] initWithKey:corruptedkey];
+
+ NSError *error;
+ NSData __unused *plainText = [pandora decrypt:cipherText nonce:nonce error:&error];
+
+ [error shouldNotBeNil];
+ [[theValue(error.code) should] equal:theValue(AGCryptoFailedToDecryptError)];
});
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.