authenticationservices/authenticationserver/test/tauthkeys/step_encryptkey.cpp
changeset 102 deec7e509f66
parent 94 0e6c5a9328b5
child 108 ca9a0fc2f082
equal deleted inserted replaced
94:0e6c5a9328b5 102:deec7e509f66
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32file.h>
       
    20 #include "tauthkeysstep.h"
       
    21 
       
    22 using namespace AuthServer;
       
    23 
       
    24 CTStepEncryptKey::CTStepEncryptKey()
       
    25 	{
       
    26 	SetTestStepName(KTStepEncryptKey);
       
    27 	}
       
    28 
       
    29 TVerdict CTStepEncryptKey::doTestStepL()
       
    30 	{
       
    31 	if (TestStepResult() != EPass)
       
    32 		{
       
    33 		return TestStepResult();
       
    34 		}
       
    35 __UHEAP_MARK;		// Check for memory leaks
       
    36 
       
    37     TBool res = ETrue; 
       
    38     TBool tmp = ETrue; 
       
    39 	TInt  err = KErrNone;
       
    40  
       
    41 
       
    42 	_LIT8(KPluginData, "ABCDEFGHIJ");
       
    43 	_LIT8(KBadPluginData, "KLMNOPQRST");
       
    44 	_LIT(KPluginName, "PasswordPlugin");
       
    45 
       
    46     HBufC* pluginName = HBufC::NewLC(14);
       
    47 	
       
    48 	*pluginName = KPluginName;
       
    49 
       
    50 	CTransientKeyInfo* transInfo =
       
    51 	  CTransientKeyInfo::NewLC(9999);
       
    52 	  
       
    53 	CTransientKey* transient =
       
    54 		transInfo->CreateTransientKeyL(KPluginData);
       
    55 	CleanupStack::PushL(transient);
       
    56 
       
    57 	CProtectionKey* protection =
       
    58 		CProtectionKey::NewL(8);
       
    59 	CleanupStack::PushL(protection);
       
    60 		
       
    61 	CEncryptedProtectionKey* encrypted =
       
    62 		transient->EncryptL(*protection);
       
    63 	
       
    64 	transInfo->SetEncryptedProtectionKeyL(encrypted);
       
    65 	
       
    66 	// try to set another key 
       
    67 	TRAP(err, transInfo->SetEncryptedProtectionKeyL(encrypted));
       
    68 	TEST(tmp = (err == KErrAlreadyExists));
       
    69 	res = tmp && res;
       
    70 	
       
    71 	// check the encrypted key is different from the protection key
       
    72 	TEST(tmp = (encrypted->KeyData() != protection->KeyData()));
       
    73 	res = tmp && res;
       
    74 	
       
    75 	CProtectionKey* decrypted =
       
    76 		transient->DecryptL(*encrypted);
       
    77 	CleanupStack::PushL(decrypted);
       
    78 
       
    79 	// check that the decrypted key is the same as the protection key
       
    80 	TEST(tmp = (protection->KeyData() == decrypted->KeyData()));
       
    81 	res = tmp && res;
       
    82 	
       
    83 	// check we can't use the wrong plugin data
       
    84 	CTransientKey* transient2 = 0;
       
    85 	TRAP(err, transient2 = transInfo->CreateTransientKeyL(KBadPluginData)); 
       
    86 	TEST(tmp = (err != KErrNone));
       
    87 	res = tmp && res;
       
    88 
       
    89 	// avoid set but unused warning...
       
    90 	if (transient2 == 0) transient2 = 0;
       
    91 	
       
    92 	CProtectionKey* client = protection->ClientKeyL(0xabababab);
       
    93 	CleanupStack::PushL(client);
       
    94 
       
    95 	CProtectionKey* client2 = protection->ClientKeyL(0xcdcdcdcd);
       
    96 	CleanupStack::PushL(client2);
       
    97 
       
    98 	// check protection key is different from the client key 
       
    99 	TEST(tmp = (protection->KeyData() != client->KeyData()));
       
   100 	res = tmp && res;
       
   101 	TEST(tmp = (protection->KeyData().Length() == client->KeyData().Length()));
       
   102 	res = tmp && res;
       
   103 
       
   104 	// check two client keys are different 
       
   105 	TEST(tmp = (client->KeyData() != client2->KeyData()));
       
   106 	res = tmp && res;
       
   107 	TEST(tmp = (client->KeyData().Length() == client2->KeyData().Length()));
       
   108 	res = tmp && res;
       
   109 
       
   110 	// check key lengths larger than SHA1 output is handled correctly 
       
   111 	CProtectionKey* protection2 =
       
   112 		CProtectionKey::NewL(512);
       
   113 	CleanupStack::PushL(protection2);
       
   114 
       
   115 	CProtectionKey* client3 = protection2->ClientKeyL(0xcdcdcdcd);
       
   116 	CleanupStack::PushL(client3);
       
   117 	
       
   118 	// check protection key is different from the client key 
       
   119 	TEST(tmp = (protection2->KeyData() != client3->KeyData()));
       
   120 	res = tmp && res;
       
   121 	TEST(tmp = (protection2->KeyData().Length() ==
       
   122 				client3->KeyData().Length()));
       
   123 	res = tmp && res;
       
   124 
       
   125 	// cleanup
       
   126 	CleanupStack::PopAndDestroy(8, transInfo);
       
   127 	CleanupStack::PopAndDestroy(pluginName);
       
   128 
       
   129 	SetTestStepResult(res ? EPass : EFail);
       
   130 	
       
   131 __UHEAP_MARKEND;	
       
   132 	return TestStepResult();
       
   133 	}
       
   134