authenticationservices/authenticationserver/test/tauthkeys/step_persist.cpp
changeset 29 ece3df019add
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
       
     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 #include <f32file.h>
       
    22 
       
    23 using namespace AuthServer;
       
    24 
       
    25 CTStepPersist::CTStepPersist()
       
    26 	{
       
    27 	SetTestStepName(KTStepPersist);
       
    28 	}
       
    29 
       
    30 TVerdict CTStepPersist::doTestStepL()
       
    31 	{
       
    32 	if (TestStepResult() != EPass)
       
    33 		{
       
    34 		return TestStepResult();
       
    35 		}
       
    36 __UHEAP_MARK;		// Check for memory leaks
       
    37     
       
    38     TBool res = ETrue;
       
    39 	TBool tmp = ETrue;
       
    40 
       
    41 	iFs.CreatePrivatePath(RFs::GetSystemDrive());
       
    42     TBuf<50> path;
       
    43     iFs.PrivatePath(path);
       
    44     
       
    45     _LIT(kProtFile, "prot.xxx");
       
    46     _LIT(kTransFile, "trans.xxx");
       
    47 	_LIT8(KPluginData, "ABCDEFGHIJ");
       
    48 	_LIT(KPluginName, "PasswordPlugin");
       
    49 
       
    50     HBufC* pluginName = HBufC::NewLC(14);
       
    51 	*pluginName = KPluginName;
       
    52 
       
    53 	// create elements
       
    54 	CTransientKeyInfo* transInfo =
       
    55 	  CTransientKeyInfo::NewLC(9999);
       
    56 	  
       
    57     CTransientKey* transient =
       
    58 		transInfo->CreateTransientKeyL(KPluginData);
       
    59 	CleanupStack::PushL(transient);
       
    60 	
       
    61 	CProtectionKey* protection =
       
    62 		CProtectionKey::NewLC(8);
       
    63 	
       
    64     CEncryptedProtectionKey* encrypted =
       
    65 		transient->EncryptL(*protection);
       
    66 	CleanupStack::PushL(encrypted);
       
    67 
       
    68 	transInfo->SetEncryptedProtectionKeyL(encrypted);
       
    69 	CleanupStack::Pop(encrypted);
       
    70 	
       
    71 	// write the encrypted key
       
    72 	RFileWriteStream writeProt;
       
    73 	
       
    74     path.Insert(path.Length(), kProtFile);
       
    75 	User::LeaveIfError(writeProt.Replace(iFs, path,
       
    76 										 EFileShareExclusive | EFileStream));
       
    77     CleanupClosePushL(writeProt);
       
    78 	
       
    79 	encrypted->ExternalizeL(writeProt);
       
    80 		
       
    81 	CleanupStack::PopAndDestroy(&writeProt);
       
    82 	
       
    83 	// read the encrypted key	
       
    84 	RFileReadStream readStream;
       
    85 
       
    86 	User::LeaveIfError(readStream.Open(iFs, path,
       
    87 									   EFileShareExclusive | EFileStream));
       
    88 	CleanupClosePushL(readStream);
       
    89 	
       
    90 	CEncryptedProtectionKey* encrypted2 =
       
    91 		CEncryptedProtectionKey::NewL(readStream);
       
    92 	CleanupStack::PushL(encrypted2);
       
    93  
       
    94 	// test the restored key matches the saved
       
    95 	TEST(tmp = (encrypted->KeyData() == encrypted2->KeyData()));
       
    96 	res = tmp && res;
       
    97 
       
    98 	// write the transient key info
       
    99 	RFileWriteStream writeTrans;
       
   100 
       
   101 	iFs.PrivatePath(path);
       
   102     path.Insert(path.Length(), kTransFile);
       
   103 	User::LeaveIfError(writeTrans.Replace(iFs, path,
       
   104 										 EFileShareExclusive | EFileStream));
       
   105     CleanupClosePushL(writeTrans);
       
   106 	
       
   107 	transInfo->ExternalizeL(writeTrans);
       
   108 		
       
   109 	CleanupStack::PopAndDestroy(&writeTrans);
       
   110 
       
   111 	// read the transient key info 
       
   112 	RFileReadStream readStream2;
       
   113 
       
   114 	User::LeaveIfError(readStream2.Open(iFs, path,
       
   115 									   EFileShareExclusive | EFileStream));
       
   116 	CleanupClosePushL(readStream2);
       
   117 	
       
   118 	CTransientKeyInfo* transInfo2 =
       
   119 		CTransientKeyInfo::NewL(readStream2);
       
   120 	CleanupStack::PushL(transInfo2);
       
   121 
       
   122 	// recreate the transient key
       
   123 	CTransientKey* transient2 = 
       
   124 		transInfo2->CreateTransientKeyL(KPluginData);
       
   125 	CleanupStack::PushL(transient2);
       
   126 
       
   127 	// recreate the protection key.
       
   128 	CProtectionKey* decrypted =
       
   129 		transient2->DecryptL(transInfo2->EncryptedKey());
       
   130 	CleanupStack::PushL(decrypted);
       
   131     
       
   132     // test that the protection key and decrypted key are the same
       
   133 	TEST(tmp = (protection->KeyData() == decrypted->KeyData()));
       
   134 	res = tmp && res;
       
   135 		
       
   136 	SetTestStepResult(res ? EPass : EFail);
       
   137 
       
   138 	CleanupStack::PopAndDestroy(9, transInfo);
       
   139     CleanupStack::PopAndDestroy(pluginName);
       
   140 
       
   141     iFs.Delete(path);
       
   142 	
       
   143 __UHEAP_MARKEND;	
       
   144 	return TestStepResult();
       
   145 	}
       
   146