MYCryptoTest.m
changeset 8 4c0eafa7b233
parent 5 b2e360b78189
child 11 3568d5fd4b6a
     1.1 --- a/MYCryptoTest.m	Thu Apr 09 22:27:51 2009 -0700
     1.2 +++ b/MYCryptoTest.m	Sun Apr 12 22:02:20 2009 -0700
     1.3 @@ -11,6 +11,7 @@
     1.4  #import "MYKeychain.h"
     1.5  #import "MYDigest.h"
     1.6  #import "MYIdentity.h"
     1.7 +#import "MYCrypto+Cocoa.h"
     1.8  #import "MYCrypto_Private.h"
     1.9  
    1.10  
    1.11 @@ -78,7 +79,7 @@
    1.12      Log(@"Enumerator = %@", e);
    1.13      CAssert(e);
    1.14      for (MYIdentity *ident in e) {
    1.15 -        Log(@"Found %@ -- name=%@, emails=(%@), key=%@",
    1.16 +        Log(@"Found %@\n\tcommonName=%@\n\temails=(%@)\n\tkey=%@",
    1.17              ident, ident.commonName, 
    1.18  #if TARGET_OS_IPHONE
    1.19              nil,
    1.20 @@ -172,33 +173,63 @@
    1.21  #pragma mark KEY-PAIRS:
    1.22  
    1.23  
    1.24 -TestCase(MYPrivateKey) {
    1.25 -    RequireTestCase(MYKeychain);
    1.26 -    
    1.27 -    Log(@"Generating key pair...");
    1.28 -    MYPrivateKey *pair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: 512];
    1.29 -    Log(@"...created { %@ , %@ }.", pair, pair.publicKey);
    1.30 +static void TestUseKeyPair(MYPrivateKey *pair) {
    1.31 +    Log(@"---- TestUseKeyPair { %@ , %@ }.", pair, pair.publicKey);
    1.32      CAssert(pair);
    1.33      CAssert(pair.keyRef);
    1.34      MYPublicKey *publicKey = pair.publicKey;
    1.35      CAssert(publicKey.keyRef);
    1.36      
    1.37 +    NSData *pubKeyData = publicKey.keyData;
    1.38 +    Log(@"Public key = %@ (%u bytes)",pubKeyData,pubKeyData.length);
    1.39 +    CAssert(pubKeyData);
    1.40 +    
    1.41 +    MYSHA1Digest *pubKeyDigest = publicKey.publicKeyDigest;
    1.42 +    Log(@"Public key digest = %@",pubKeyDigest);
    1.43 +    CAssertEqual(pair.publicKeyDigest, pubKeyDigest);
    1.44 +    
    1.45 +    Log(@"SHA1 of pub key = %@", pubKeyData.my_SHA1Digest.asData);
    1.46 +    
    1.47 +    // Let's sign data:
    1.48 +    NSData *data = [@"This is a test. This is only a test!" dataUsingEncoding: NSUTF8StringEncoding];
    1.49 +    NSData *sig = [pair signData: data];
    1.50 +    Log(@"Signature = %@ (%u bytes)",sig,sig.length);
    1.51 +    CAssert(sig);
    1.52 +    CAssert( [publicKey verifySignature: sig ofData: data] );
    1.53 +    
    1.54 +    // Now let's encrypt...
    1.55 +    NSData *crypted = [publicKey encryptData: data];
    1.56 +    Log(@"Encrypted = %@ (%u bytes)",crypted,crypted.length);
    1.57 +    CAssert(crypted);
    1.58 +    CAssertEqual([pair decryptData: crypted], data);
    1.59 +    Log(@"Verified decryption.");
    1.60 +    
    1.61 +    // Test creating a standalone public key:
    1.62 +    MYPublicKey *pub = [[MYPublicKey alloc] initWithKeyRef: publicKey.keyRef];
    1.63 +    CAssert( [pub verifySignature: sig ofData: data] );
    1.64 +    Log(@"Verified signature.");
    1.65 +    
    1.66 +    // Test creating a public key from data:
    1.67 +    Log(@"Reconstituting public key from data...");
    1.68 +    pub = [[MYPublicKey alloc] initWithKeyData: pubKeyData];
    1.69 +    CAssert(pub);
    1.70 +    CAssertEqual(pub.keyData, pubKeyData);
    1.71 +    CAssertEqual(pub.publicKeyDigest, pubKeyDigest);
    1.72 +    CAssert( [pub verifySignature: sig ofData: data] );
    1.73 +    Log(@"Verified signature from reconstituted key.");
    1.74 +}
    1.75 +
    1.76 +
    1.77 +TestCase(MYGenerateKeyPair) {
    1.78 +    RequireTestCase(MYKeychain);
    1.79 +    
    1.80 +    Log(@"Generating key pair...");
    1.81 +    MYPrivateKey *pair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: 512];
    1.82 +    MYPublicKey *publicKey = pair.publicKey;
    1.83 +    Log(@"...created { %@ , %@ }.", pair, publicKey);
    1.84 +    
    1.85      @try{
    1.86 -        NSData *pubKeyData = publicKey.keyData;
    1.87 -        Log(@"Public key = %@ (%u bytes)",pubKeyData,pubKeyData.length);
    1.88 -        CAssert(pubKeyData);
    1.89 -        
    1.90 -        MYSHA1Digest *pubKeyDigest = publicKey.publicKeyDigest;
    1.91 -        Log(@"Public key digest = %@",pubKeyDigest);
    1.92 -        CAssertEqual(pair.publicKeyDigest, pubKeyDigest);
    1.93 -        
    1.94 -        Log(@"SHA1 of pub key = %@", pubKeyData.my_SHA1Digest.asData);
    1.95 -        
    1.96 -        NSData *data = [@"This is a test. This is only a test!" dataUsingEncoding: NSUTF8StringEncoding];
    1.97 -        NSData *sig = [pair signData: data];
    1.98 -        Log(@"Signature = %@ (%u bytes)",sig,sig.length);
    1.99 -        CAssert(sig);
   1.100 -        CAssert( [publicKey verifySignature: sig ofData: data] );
   1.101 +        TestUseKeyPair(pair);
   1.102          
   1.103          [pair setName: @"Test KeyPair Label"];
   1.104          CAssertEqual(pair.name, @"Test KeyPair Label");
   1.105 @@ -212,28 +243,6 @@
   1.106          CAssertEqual(pair.alias, @"TestCase@mooseyard.com");
   1.107          CAssertEqual(publicKey.alias, @"TestCase@mooseyard.com");
   1.108          
   1.109 -        // Test creating a standalone public key:
   1.110 -        MYPublicKey *pub = [[MYPublicKey alloc] initWithKeyRef: publicKey.keyRef];
   1.111 -        CAssert( [pub verifySignature: sig ofData: data] );
   1.112 -        Log(@"Verified signature.");
   1.113 -
   1.114 -        // Test creating a public key from data:
   1.115 -        Log(@"Reconstituting public key from data...");
   1.116 -        pub = [[MYPublicKey alloc] initWithKeyData: pubKeyData];
   1.117 -        CAssert(pub);
   1.118 -        CAssertEqual(pub.keyData, pubKeyData);
   1.119 -        CAssertEqual(pub.publicKeyDigest, pubKeyDigest);
   1.120 -        CAssert( [pub verifySignature: sig ofData: data] );
   1.121 -        Log(@"Verified signature from reconstituted key.");
   1.122 -                
   1.123 -        // Now let's encrypt...
   1.124 -        NSData *crypted = [pub encryptData: data];
   1.125 -        Log(@"Encrypted = %@ (%u bytes)",crypted,crypted.length);
   1.126 -        CAssert(crypted);
   1.127 -        
   1.128 -        CAssertEqual([pair decryptData: crypted], data);
   1.129 -        Log(@"Verified decryption.");
   1.130 -        
   1.131          CAssert([pair removeFromKeychain]);
   1.132          Log(@"Removed key-pair.");
   1.133          pair = nil;
   1.134 @@ -249,6 +258,24 @@
   1.135  }
   1.136  
   1.137  
   1.138 +TestCase(MYUseIdentity) {
   1.139 +    MYIdentity *me = nil;//[MYIdentity preferredIdentityForName: @"MYCryptoTest"];
   1.140 +    if (!me) {
   1.141 +        NSArray *idents = [[[MYKeychain allKeychains] enumerateIdentities] allObjects];
   1.142 +        SFChooseIdentityPanel *panel = [SFChooseIdentityPanel sharedChooseIdentityPanel];
   1.143 +        [panel setAlternateButtonTitle: @"Cancel"];
   1.144 +        if ([panel my_runModalForIdentities: idents 
   1.145 +                                    message: @"Choose an identity for the MYEncoder test case:"]
   1.146 +            != NSOKButton) {
   1.147 +            [NSException raise: NSGenericException format: @"User canceled"];
   1.148 +        }
   1.149 +        me = [panel my_identity];
   1.150 +        [me makePreferredIdentityForName: @"MYCryptoTest"];
   1.151 +    }
   1.152 +    CAssert(me,@"No default identity has been set up in the Keychain");
   1.153 +    TestUseKeyPair(me.privateKey);
   1.154 +}
   1.155 +
   1.156  
   1.157  #pragma mark -
   1.158  #pragma mark KEYPAIR EXPORT: