kerneltest/e32test/pccd/t_setkey.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This test tests reading and writing to the protected & unprotected area of an SD card
       
    15 // & verifies that mounting & dismounting the protected area does not disrupt reads and/or 
       
    16 // writes to the unprotected area.
       
    17 // NB For test to work, a valid key which matches the SD card under test needs to be put 
       
    18 // into the byte array testDeviceKeyRawData[].
       
    19 // ********* IMPORTANT NOTE TO SYMBIAN ENGINEERS WORKING WITH THIS TEST *********
       
    20 // The key MUST NOT BE CHECKED INTO PERFORCE as this would contravene the license agreement
       
    21 // we have with the SD Card Association.
       
    22 // ********* IMPORTANT NOTE TO SYMBIAN ENGINEERS WORKING WITH THIS TEST *********
       
    23 // 
       
    24 //
       
    25 
       
    26 #include <e32std.h>
       
    27 #include <e32std_private.h>
       
    28 #include <e32cons.h>
       
    29 #include <f32file.h>
       
    30 #include <e32test.h>
       
    31 
       
    32 const TUint32 KDriveNumProt = EDriveG;
       
    33 const TUint32 KDriveNumUnprot = EDriveF;
       
    34 TChar gDriveLetterProt;
       
    35 TChar gDriveLetterUnprot;
       
    36 TBool gMountProtectedArea = EFalse;
       
    37 
       
    38 GLDEF_D RTest test(_L("T_SETKEY"));
       
    39 
       
    40 // flags stolen from locmedia.h
       
    41 //const TInt KForceMediaChangeReOpenAllMediaDrivers	= 0;
       
    42 const TUint KForceMediaChangeReOpenMediaDriver		= 0x80000000UL;
       
    43 const TUint KMediaRemountForceMediaChange			= 0x00000001UL;
       
    44 
       
    45 class TSDCardSecureMountInfo
       
    46 	{
       
    47 public:
       
    48 	TUid iUid;
       
    49 	const TDesC8* iEncryptedDeviceKey;
       
    50 	const TDesC8* iRamMKBData;
       
    51 	TInt iMKBNumber;
       
    52 	};
       
    53 typedef TPckgBuf<TSDCardSecureMountInfo> TSDCardSecureMountInfoPckg;
       
    54 
       
    55 typedef enum 
       
    56 	{
       
    57 	EClearMountInfo=0,
       
    58 	ESetMountInfo=1
       
    59 	} TMountInfoAction;
       
    60 
       
    61 
       
    62 // Standard boilerplate for creating a console window ////////////////////////
       
    63 void StartAppL();
       
    64 void SetupConsoleL();
       
    65 
       
    66 GLDEF_C TInt E32Main()									// main function called by E32
       
    67     {
       
    68 	CTrapCleanup* cleanup=CTrapCleanup::New();			// get clean-up stack
       
    69 	TRAPD(error,SetupConsoleL());						// more initialization, then do example
       
    70 	__ASSERT_DEBUG(!error,User::Panic(_L("BossTextUi"),error));
       
    71 	delete cleanup;										// destroy clean-up stack
       
    72 	return 0;											// and return
       
    73     }
       
    74 
       
    75 // Determine whether we should run the full test or just mount the protected area
       
    76 void ParseCommandLine()
       
    77 	{
       
    78 	TBuf<32> args;
       
    79 	User::CommandLine(args);
       
    80 	
       
    81 	if (args == _L("-m"))
       
    82 		{
       
    83 		gMountProtectedArea = ETrue;
       
    84 		}
       
    85 	else if (args == _L("-?"))
       
    86 		{
       
    87 		test.Printf(_L("usage: t_setkey [-m]\n"));
       
    88 		test.Printf(_L("-m => mount proteced area and exit\n"));
       
    89 		}
       
    90 	}
       
    91 
       
    92 void SetupConsoleL()									// initialize and call example code under cleanup stack
       
    93     {
       
    94 	test.Title();
       
    95 	test.Start(_L("Starting tests..."));
       
    96 
       
    97 	ParseCommandLine();
       
    98 	TRAPD(error,StartAppL());					// perform example function
       
    99 	if (error) 
       
   100 		test.Printf(_L("failed: leave code=%d\n"), error);
       
   101 	else 
       
   102 		test.Printf(_L("ok\n"));
       
   103 
       
   104 	test.Printf(_L(" [press any key]\n"));
       
   105 	test.Getch();
       
   106 
       
   107 	test.End();
       
   108 	test.Close();
       
   109 	
       
   110     }
       
   111 
       
   112 // End of standard boilerplate ///////////////////////////////////////////////
       
   113 
       
   114 
       
   115 static const TUint8 testDeviceKeyRawData[160] =	
       
   116 	{
       
   117 
       
   118 // Symbian test key :
       
   119 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   120 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   121 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   122 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   123 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   124 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   125 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   126 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   127 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
       
   128 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
       
   129 
       
   130 
       
   131 	};
       
   132 
       
   133 TInt WriteThreadEntryPoint( TAny* aParam )
       
   134 	{
       
   135 	(void)aParam;
       
   136 	
       
   137 	RTest test(_L("WriteThread"));
       
   138 
       
   139 	test.Title();
       
   140 	
       
   141 	test.Start(_L("Starting WriteThread ..."));
       
   142 
       
   143 	TInt r;
       
   144 	RFs fs;
       
   145 
       
   146 	r = fs.Connect();
       
   147 	if(r != KErrNone)
       
   148 		{
       
   149 		test.Printf(_L("WT Connect err %d\n"), r);
       
   150 		}
       
   151 
       
   152 	RFile file;
       
   153 
       
   154 	TFileName fileNameUnprot = _L("?:\\TESTTHRD.TXT");
       
   155 	fileNameUnprot[0] = (TText) gDriveLetterUnprot;
       
   156 
       
   157 	//Open file on the user area drive
       
   158 	r = file.Replace(fs, fileNameUnprot, EFileWrite);
       
   159 	if(r != KErrNone)
       
   160 		{
       
   161 		test.Printf(_L("WT File Replace err %d\n\n"), r);
       
   162 		test(0);
       
   163 		}
       
   164 
       
   165 	TPtrC8 data(testDeviceKeyRawData, 8);
       
   166 
       
   167 	RThread().Rendezvous(KErrNone);
       
   168 
       
   169 	for(TInt iter=0; iter < KMaxTInt;iter++)	// keep sending write requests to the user area for ever
       
   170 		{
       
   171 		
       
   172 		r = file.Write(data);
       
   173 //test.Printf(_L("WT File Write err %d iter %d\r"), r, iter);
       
   174 
       
   175 		if(r != KErrNone)
       
   176 			{
       
   177 			test.Printf(_L("WT File Write err %d\n"), r);
       
   178 			test(0);
       
   179 			}
       
   180 		}
       
   181 
       
   182 	file.Close();
       
   183 	fs.Close();
       
   184 
       
   185 	test.End();
       
   186 	test.Close();
       
   187 
       
   188 	return 0;
       
   189 	}
       
   190     
       
   191 
       
   192 
       
   193 GLDEF_C void StartAppL()
       
   194     {
       
   195 
       
   196 	// Exit if no key defined
       
   197 	if (testDeviceKeyRawData[0] == 0 && testDeviceKeyRawData[1] == 0)
       
   198 		{
       
   199 		test.Printf(_L("This test needs to be recompiled with a valid key\n"));
       
   200 		test(0);
       
   201 		}
       
   202 
       
   203 	RFs fs;
       
   204 	TInt r;
       
   205 
       
   206 
       
   207 	test.Printf(_L("Starting Setkey\n"));
       
   208 	if(fs.Connect()!=KErrNone)
       
   209 		return;
       
   210 
       
   211 	test.Printf(_L("Connected to fileserver OK\n"));
       
   212 	TBuf8<160> mkBData;
       
   213 
       
   214 	mkBData.SetLength(160);
       
   215 	TInt i;
       
   216 	for (i = 0; i < 160; i++)
       
   217 		{
       
   218 		mkBData[i] = testDeviceKeyRawData[i];
       
   219 		}
       
   220 	
       
   221 	TSDCardSecureMountInfoPckg pckg;
       
   222 
       
   223 
       
   224 	pckg().iRamMKBData=(const TDesC8*)0x01;   //Unused for now.
       
   225 	pckg().iMKBNumber = 0;
       
   226 //	pckg().iMKBNumber = 11;		// for SD Binding ?
       
   227 	static TPtrC8 encryptedDeviceKey(testDeviceKeyRawData, sizeof(testDeviceKeyRawData));
       
   228 	pckg().iEncryptedDeviceKey = &encryptedDeviceKey;
       
   229 
       
   230 
       
   231 	r = fs.DriveToChar(KDriveNumProt, gDriveLetterProt);
       
   232 	r = fs.DriveToChar(KDriveNumUnprot, gDriveLetterUnprot);
       
   233 	TFileName fileNameUnprot = _L("?:\\TEST.TXT");
       
   234 	TFileName fileNameProt = _L("?:\\TEST.TXT");
       
   235 	fileNameUnprot[0] = (TText) gDriveLetterUnprot;
       
   236 	fileNameProt[0] = (TText) gDriveLetterProt;
       
   237 
       
   238 	TVolumeInfo v;
       
   239 
       
   240 	r = fs.Volume(v, KDriveNumProt); //Check
       
   241 test.Printf(_L("Volume() returned %d\n"), r);
       
   242 	test(r == KErrNotReady || r == KErrNone);
       
   243 
       
   244 
       
   245 	if (gMountProtectedArea)
       
   246 		{
       
   247 		test.Next(_L("test remount with KForceMediaChangeReOpenMediaDriver flag"));
       
   248 		// verify that the unprotected area does not get a change notification
       
   249 		TFileName filePathUnprot = _L("?:\\");
       
   250 		filePathUnprot[0] = (TText) gDriveLetterUnprot;
       
   251 		TRequestStatus changeStatus;
       
   252 		fs.NotifyChange(ENotifyAll, changeStatus, filePathUnprot);
       
   253 
       
   254 		r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   255 		test(r == KErrNone);
       
   256 		r = fs.Volume(v, KDriveNumProt); //Check
       
   257 		test.Printf(_L("Volume() returned %d\n"), r);
       
   258 		test(r == KErrNone || r == KErrCorrupt);
       
   259 
       
   260 		test.Next(_L("test this causes no change to unprotected drive"));
       
   261 		test.Printf(_L("changeStatus %d\n"), changeStatus.Int());
       
   262 		test (changeStatus.Int() == KRequestPending);
       
   263 		fs.NotifyChangeCancel(changeStatus);
       
   264 
       
   265 		test.Printf(_L("Remount Drive suceeded.  Secure area is now unlocked\n"));
       
   266 		fs.Close();
       
   267 		return;
       
   268 		}
       
   269 
       
   270 	// Set the mount info for the secure area using KMediaRemountForceMediaChange flag
       
   271 	// This is asynchronous in behaviour i.e. we need to wait for (two) media change notifications
       
   272 	// before the drive is ready for use
       
   273 	test.Next(_L("test remount with KMediaRemountForceMediaChange flag"));
       
   274 	TRequestStatus changeStatus;
       
   275 	fs.NotifyChange(ENotifyAll, changeStatus);
       
   276 	r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KMediaRemountForceMediaChange);
       
   277 test.Printf(_L("RemountDrive() returned %d\n"), r);
       
   278 	test(r == KErrNotReady || r == KErrNone);
       
   279 	
       
   280 	test.Printf(_L("Waiting for media change...\n"));
       
   281 	User::WaitForRequest(changeStatus);
       
   282 	
       
   283 	do
       
   284 		{
       
   285 
       
   286 
       
   287 		r = fs.Volume(v, KDriveNumProt); //Check
       
   288 		test.Printf(_L("Volume() returned %d\n"), r);
       
   289 
       
   290 	fs.NotifyChange(ENotifyAll, changeStatus);
       
   291 		}
       
   292 	while (r == KErrNotReady);
       
   293 	fs.NotifyChangeCancel(changeStatus);
       
   294 	
       
   295 
       
   296 
       
   297 	// Set the mount info for the secure area using KForceMediaChangeReOpenMediaDriver flag
       
   298 	// This should be synchronous in behaviour
       
   299 	test.Next(_L("test remount with KForceMediaChangeReOpenMediaDriver flag"));
       
   300 	r = fs.RemountDrive(KDriveNumProt, NULL, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   301 	test(r == KErrNone);
       
   302 	r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   303 	test(r == KErrNone);
       
   304 	r = fs.Volume(v, KDriveNumProt); //Check
       
   305 test.Printf(_L("Volume() returned %d\n"), r);
       
   306 	test(r == KErrNone);
       
   307 
       
   308 	test.Printf(_L("Remount Drive suceeded.  Secure area is now unlocked\n"));
       
   309 	test.Printf(_L("Press any key to continue...\n"));
       
   310 
       
   311 
       
   312 	test.Next(_L("test writing to protected & unprotected areas"));
       
   313 
       
   314 	RFile f1,f2;
       
   315 	TRequestStatus req1, req2, req3, req4;
       
   316 
       
   317 	const TInt KBufSize = 32 * 1024;
       
   318 	LOCAL_D TBuf8<KBufSize> gBuf;
       
   319 
       
   320 
       
   321 	test.Printf(_L("Opening files...\n"));
       
   322 test.Printf(_L("Opening %S...\n"), &fileNameUnprot);
       
   323 	r = f1.Replace(fs, fileNameUnprot,EFileStreamText|EFileWrite);
       
   324 	test (r == KErrNone);
       
   325 
       
   326 test.Printf(_L("Opening %S...\n"), &fileNameProt);
       
   327 	r = f2.Replace(fs, fileNameProt,EFileStreamText|EFileWrite);
       
   328 	test (r == KErrNone);
       
   329 	
       
   330 
       
   331 //	test.Printf(_L("Wait 10 secs for stack to power down...\n"));
       
   332 //	User::After(10 * 1000000);
       
   333 //	test.Printf(_L("done\n"));
       
   334 	
       
   335 	gBuf.SetLength(KBufSize);
       
   336 	
       
   337 test.Printf(_L("Writing files...\n"));
       
   338 
       
   339 	req1 = KRequestPending;
       
   340 	req2 = KRequestPending;
       
   341 	req3 = KRequestPending;
       
   342 	req4 = KRequestPending;
       
   343 
       
   344 	f1.Write(gBuf, req1);
       
   345 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   346 test (r == KErrNone);
       
   347 	f2.Write(gBuf, req2);
       
   348 
       
   349 test.Printf(_L("req1 %d, req2 %d\n"), req1.Int(), req2.Int());
       
   350 	test (req1 == KRequestPending || req1 == KErrNone);
       
   351 	test (req2 == KRequestPending || req2 == KErrNone);
       
   352 
       
   353 	// force a remount to test whether a write to the unprotected area
       
   354 	// is disrupted.... 
       
   355 	// NB Should set aFlags to KForceMediaChangeReOpenMediaDriver, otherwise a media change will occur which WILL
       
   356 	// prematurely complete any pending writes 
       
   357 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   358 test (r == KErrNone);
       
   359 
       
   360 //r = fs.RemountDrive(KDriveNumProt, &pckg, 1);
       
   361 test.Printf(_L("RemountDrive() returned %d\n"), r);
       
   362 	
       
   363 	f1.Write(gBuf, req3);
       
   364 	f2.Write(gBuf, req4);
       
   365 
       
   366 test.Printf(_L("WaitForRequests...\n"));
       
   367 	User::WaitForRequest(req1);
       
   368 	User::WaitForRequest(req2);
       
   369 	User::WaitForRequest(req3);
       
   370 	User::WaitForRequest(req4);
       
   371 
       
   372 test.Printf(_L("req1 %d, req2 %d req3 %d, req4 %d\n"), req1.Int(), req2.Int(), req3.Int(), req4.Int());
       
   373 	test (req1 == KErrNone);
       
   374 	test (req2 == KErrNone);
       
   375 	test (req3 == KErrNone);
       
   376 	test (req4 == KErrNone);
       
   377 	
       
   378 	f1.Close();
       
   379 	f2.Close();
       
   380 
       
   381 
       
   382 	// create a thread which continuously writes to the unprotected area
       
   383 	// whilst continually remounting protected area in this thread.
       
   384 	
       
   385 	test.Next(_L("test writing to protected & unprotected areas using different threads"));
       
   386 
       
   387 test.Printf(_L("Opening %S for read access...\n"), &fileNameUnprot);
       
   388 	r = f2.Open(fs, fileNameProt, EFileShareReadersOnly);
       
   389 	test (r == KErrNone);
       
   390 
       
   391 
       
   392 	test (r == KErrNone);
       
   393 	RThread thread;
       
   394 	r = thread.Create( _L("Write Thread"), WriteThreadEntryPoint, KDefaultStackSize, NULL, NULL);
       
   395 	if(r !=KErrNone)
       
   396 		{
       
   397 		test.Printf(_L("MT Thread Create 1 ret = %d\n"),r);
       
   398 		return;
       
   399 		}
       
   400 	TRequestStatus writeThreadStat;
       
   401 	
       
   402 	thread.Rendezvous(writeThreadStat);
       
   403 	thread.Resume();
       
   404 	User::WaitForRequest(writeThreadStat);
       
   405 //	thread.Close(); //close handle to thread
       
   406 	if(writeThreadStat.Int() != KErrNone)
       
   407 		{
       
   408 		test.Printf(_L("MT Thread Create 2 ret = %d\n"),writeThreadStat.Int());
       
   409 		test(0);
       
   410 		}
       
   411 
       
   412 	TBuf8<14> buf;
       
   413 
       
   414 
       
   415 	const TInt KMaxIterations = 1000;
       
   416 	for(TInt n=0; n<KMaxIterations; n++)
       
   417 		{
       
   418 		if (n % 10 == 0)
       
   419 			test.Printf(_L("iteration %d of %d\r"), n, KMaxIterations);
       
   420 
       
   421 		//Set the mount info for the secure area.
       
   422 		r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   423 		if (r == KErrNone)
       
   424 			{
       
   425 //			test.Printf(_L("Remount Drive KEY suceeded. Secure area is now unlocked\n"));
       
   426 			}
       
   427 		else
       
   428 			{
       
   429 			test.Printf(_L("Remount Drive KEY failed with %d\n"), r);
       
   430 			test(0);
       
   431 			}
       
   432 
       
   433 		// take it in turns to 
       
   434 		// (0) call Rfs:Volume()
       
   435 		// (1) call RFile::Read
       
   436 		// (2) do nothing
       
   437 		TInt testNum = n & 0x3;
       
   438 		if (testNum == 0)
       
   439 			{
       
   440 			TVolumeInfo volumeInfo;
       
   441 			r = fs.Volume( volumeInfo, KDriveNumProt );
       
   442 			if (r != KErrNone)
       
   443 				{
       
   444 				RDebug::Print( _L("Volume returned %d after RemountDrive"), r);
       
   445 				break;
       
   446 				}
       
   447 			}
       
   448 		else if (testNum == 1)
       
   449 			{
       
   450 			r = f2.Read(0,buf);
       
   451 			if(r != KErrNone)
       
   452 				{
       
   453 				test.Printf(_L("protected Drive Access failed %d\n"), r);
       
   454 				test(0);
       
   455 				}
       
   456 			}
       
   457 		else if (testNum == 2)
       
   458 			{
       
   459 			}
       
   460 		else if (testNum == 3)
       
   461 			{
       
   462 			}
       
   463 		
       
   464 
       
   465 //test.Printf(_L("protected Drive KEY Access succeeded\n"));
       
   466 		r = fs.RemountDrive(KDriveNumProt, NULL, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   467 		if (r == KErrNone)
       
   468 			{
       
   469 //			test.Printf(_L("Remount Drive NULL suceeded. Secure area is now locked\n"));
       
   470 			}
       
   471 		else
       
   472 			{
       
   473 			test.Printf(_L("Remount Drive NULL failed with %d\n"), r);
       
   474 			test(0);
       
   475 			}
       
   476 
       
   477 		if (testNum == 1)
       
   478 			{
       
   479 			r = f2.Read(0,buf);
       
   480 //test.Printf(_L("protected Drive NULL Access failed with %d\n"), r);
       
   481 			if (r != KErrNotReady)
       
   482 				{
       
   483 				test.Printf(_L("protected Drive NULL Access failed with unexpected error %d\n"), r);
       
   484 				test(0);
       
   485 				}
       
   486 			}
       
   487 	
       
   488 		}
       
   489 
       
   490 	f1.Close();
       
   491 
       
   492 
       
   493 	// comment in the next 2 lines to leave the protected area mounted...
       
   494 	r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
       
   495 	test(r == KErrNone);
       
   496 
       
   497 	thread.Kill(KErrNone);
       
   498 	thread.Close(); //close handle to thread
       
   499 
       
   500 	fs.Close();
       
   501 
       
   502 	}