1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/pccd/t_setkey.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,502 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// This test tests reading and writing to the protected & unprotected area of an SD card
1.18 +// & verifies that mounting & dismounting the protected area does not disrupt reads and/or
1.19 +// writes to the unprotected area.
1.20 +// NB For test to work, a valid key which matches the SD card under test needs to be put
1.21 +// into the byte array testDeviceKeyRawData[].
1.22 +// ********* IMPORTANT NOTE TO SYMBIAN ENGINEERS WORKING WITH THIS TEST *********
1.23 +// The key MUST NOT BE CHECKED INTO PERFORCE as this would contravene the license agreement
1.24 +// we have with the SD Card Association.
1.25 +// ********* IMPORTANT NOTE TO SYMBIAN ENGINEERS WORKING WITH THIS TEST *********
1.26 +//
1.27 +//
1.28 +
1.29 +#include <e32std.h>
1.30 +#include <e32std_private.h>
1.31 +#include <e32cons.h>
1.32 +#include <f32file.h>
1.33 +#include <e32test.h>
1.34 +
1.35 +const TUint32 KDriveNumProt = EDriveG;
1.36 +const TUint32 KDriveNumUnprot = EDriveF;
1.37 +TChar gDriveLetterProt;
1.38 +TChar gDriveLetterUnprot;
1.39 +TBool gMountProtectedArea = EFalse;
1.40 +
1.41 +GLDEF_D RTest test(_L("T_SETKEY"));
1.42 +
1.43 +// flags stolen from locmedia.h
1.44 +//const TInt KForceMediaChangeReOpenAllMediaDrivers = 0;
1.45 +const TUint KForceMediaChangeReOpenMediaDriver = 0x80000000UL;
1.46 +const TUint KMediaRemountForceMediaChange = 0x00000001UL;
1.47 +
1.48 +class TSDCardSecureMountInfo
1.49 + {
1.50 +public:
1.51 + TUid iUid;
1.52 + const TDesC8* iEncryptedDeviceKey;
1.53 + const TDesC8* iRamMKBData;
1.54 + TInt iMKBNumber;
1.55 + };
1.56 +typedef TPckgBuf<TSDCardSecureMountInfo> TSDCardSecureMountInfoPckg;
1.57 +
1.58 +typedef enum
1.59 + {
1.60 + EClearMountInfo=0,
1.61 + ESetMountInfo=1
1.62 + } TMountInfoAction;
1.63 +
1.64 +
1.65 +// Standard boilerplate for creating a console window ////////////////////////
1.66 +void StartAppL();
1.67 +void SetupConsoleL();
1.68 +
1.69 +GLDEF_C TInt E32Main() // main function called by E32
1.70 + {
1.71 + CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
1.72 + TRAPD(error,SetupConsoleL()); // more initialization, then do example
1.73 + __ASSERT_DEBUG(!error,User::Panic(_L("BossTextUi"),error));
1.74 + delete cleanup; // destroy clean-up stack
1.75 + return 0; // and return
1.76 + }
1.77 +
1.78 +// Determine whether we should run the full test or just mount the protected area
1.79 +void ParseCommandLine()
1.80 + {
1.81 + TBuf<32> args;
1.82 + User::CommandLine(args);
1.83 +
1.84 + if (args == _L("-m"))
1.85 + {
1.86 + gMountProtectedArea = ETrue;
1.87 + }
1.88 + else if (args == _L("-?"))
1.89 + {
1.90 + test.Printf(_L("usage: t_setkey [-m]\n"));
1.91 + test.Printf(_L("-m => mount proteced area and exit\n"));
1.92 + }
1.93 + }
1.94 +
1.95 +void SetupConsoleL() // initialize and call example code under cleanup stack
1.96 + {
1.97 + test.Title();
1.98 + test.Start(_L("Starting tests..."));
1.99 +
1.100 + ParseCommandLine();
1.101 + TRAPD(error,StartAppL()); // perform example function
1.102 + if (error)
1.103 + test.Printf(_L("failed: leave code=%d\n"), error);
1.104 + else
1.105 + test.Printf(_L("ok\n"));
1.106 +
1.107 + test.Printf(_L(" [press any key]\n"));
1.108 + test.Getch();
1.109 +
1.110 + test.End();
1.111 + test.Close();
1.112 +
1.113 + }
1.114 +
1.115 +// End of standard boilerplate ///////////////////////////////////////////////
1.116 +
1.117 +
1.118 +static const TUint8 testDeviceKeyRawData[160] =
1.119 + {
1.120 +
1.121 +// Symbian test key :
1.122 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.123 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.124 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.125 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.126 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.127 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.128 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.129 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.130 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1.131 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
1.132 +
1.133 +
1.134 + };
1.135 +
1.136 +TInt WriteThreadEntryPoint( TAny* aParam )
1.137 + {
1.138 + (void)aParam;
1.139 +
1.140 + RTest test(_L("WriteThread"));
1.141 +
1.142 + test.Title();
1.143 +
1.144 + test.Start(_L("Starting WriteThread ..."));
1.145 +
1.146 + TInt r;
1.147 + RFs fs;
1.148 +
1.149 + r = fs.Connect();
1.150 + if(r != KErrNone)
1.151 + {
1.152 + test.Printf(_L("WT Connect err %d\n"), r);
1.153 + }
1.154 +
1.155 + RFile file;
1.156 +
1.157 + TFileName fileNameUnprot = _L("?:\\TESTTHRD.TXT");
1.158 + fileNameUnprot[0] = (TText) gDriveLetterUnprot;
1.159 +
1.160 + //Open file on the user area drive
1.161 + r = file.Replace(fs, fileNameUnprot, EFileWrite);
1.162 + if(r != KErrNone)
1.163 + {
1.164 + test.Printf(_L("WT File Replace err %d\n\n"), r);
1.165 + test(0);
1.166 + }
1.167 +
1.168 + TPtrC8 data(testDeviceKeyRawData, 8);
1.169 +
1.170 + RThread().Rendezvous(KErrNone);
1.171 +
1.172 + for(TInt iter=0; iter < KMaxTInt;iter++) // keep sending write requests to the user area for ever
1.173 + {
1.174 +
1.175 + r = file.Write(data);
1.176 +//test.Printf(_L("WT File Write err %d iter %d\r"), r, iter);
1.177 +
1.178 + if(r != KErrNone)
1.179 + {
1.180 + test.Printf(_L("WT File Write err %d\n"), r);
1.181 + test(0);
1.182 + }
1.183 + }
1.184 +
1.185 + file.Close();
1.186 + fs.Close();
1.187 +
1.188 + test.End();
1.189 + test.Close();
1.190 +
1.191 + return 0;
1.192 + }
1.193 +
1.194 +
1.195 +
1.196 +GLDEF_C void StartAppL()
1.197 + {
1.198 +
1.199 + // Exit if no key defined
1.200 + if (testDeviceKeyRawData[0] == 0 && testDeviceKeyRawData[1] == 0)
1.201 + {
1.202 + test.Printf(_L("This test needs to be recompiled with a valid key\n"));
1.203 + test(0);
1.204 + }
1.205 +
1.206 + RFs fs;
1.207 + TInt r;
1.208 +
1.209 +
1.210 + test.Printf(_L("Starting Setkey\n"));
1.211 + if(fs.Connect()!=KErrNone)
1.212 + return;
1.213 +
1.214 + test.Printf(_L("Connected to fileserver OK\n"));
1.215 + TBuf8<160> mkBData;
1.216 +
1.217 + mkBData.SetLength(160);
1.218 + TInt i;
1.219 + for (i = 0; i < 160; i++)
1.220 + {
1.221 + mkBData[i] = testDeviceKeyRawData[i];
1.222 + }
1.223 +
1.224 + TSDCardSecureMountInfoPckg pckg;
1.225 +
1.226 +
1.227 + pckg().iRamMKBData=(const TDesC8*)0x01; //Unused for now.
1.228 + pckg().iMKBNumber = 0;
1.229 +// pckg().iMKBNumber = 11; // for SD Binding ?
1.230 + static TPtrC8 encryptedDeviceKey(testDeviceKeyRawData, sizeof(testDeviceKeyRawData));
1.231 + pckg().iEncryptedDeviceKey = &encryptedDeviceKey;
1.232 +
1.233 +
1.234 + r = fs.DriveToChar(KDriveNumProt, gDriveLetterProt);
1.235 + r = fs.DriveToChar(KDriveNumUnprot, gDriveLetterUnprot);
1.236 + TFileName fileNameUnprot = _L("?:\\TEST.TXT");
1.237 + TFileName fileNameProt = _L("?:\\TEST.TXT");
1.238 + fileNameUnprot[0] = (TText) gDriveLetterUnprot;
1.239 + fileNameProt[0] = (TText) gDriveLetterProt;
1.240 +
1.241 + TVolumeInfo v;
1.242 +
1.243 + r = fs.Volume(v, KDriveNumProt); //Check
1.244 +test.Printf(_L("Volume() returned %d\n"), r);
1.245 + test(r == KErrNotReady || r == KErrNone);
1.246 +
1.247 +
1.248 + if (gMountProtectedArea)
1.249 + {
1.250 + test.Next(_L("test remount with KForceMediaChangeReOpenMediaDriver flag"));
1.251 + // verify that the unprotected area does not get a change notification
1.252 + TFileName filePathUnprot = _L("?:\\");
1.253 + filePathUnprot[0] = (TText) gDriveLetterUnprot;
1.254 + TRequestStatus changeStatus;
1.255 + fs.NotifyChange(ENotifyAll, changeStatus, filePathUnprot);
1.256 +
1.257 + r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.258 + test(r == KErrNone);
1.259 + r = fs.Volume(v, KDriveNumProt); //Check
1.260 + test.Printf(_L("Volume() returned %d\n"), r);
1.261 + test(r == KErrNone || r == KErrCorrupt);
1.262 +
1.263 + test.Next(_L("test this causes no change to unprotected drive"));
1.264 + test.Printf(_L("changeStatus %d\n"), changeStatus.Int());
1.265 + test (changeStatus.Int() == KRequestPending);
1.266 + fs.NotifyChangeCancel(changeStatus);
1.267 +
1.268 + test.Printf(_L("Remount Drive suceeded. Secure area is now unlocked\n"));
1.269 + fs.Close();
1.270 + return;
1.271 + }
1.272 +
1.273 + // Set the mount info for the secure area using KMediaRemountForceMediaChange flag
1.274 + // This is asynchronous in behaviour i.e. we need to wait for (two) media change notifications
1.275 + // before the drive is ready for use
1.276 + test.Next(_L("test remount with KMediaRemountForceMediaChange flag"));
1.277 + TRequestStatus changeStatus;
1.278 + fs.NotifyChange(ENotifyAll, changeStatus);
1.279 + r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KMediaRemountForceMediaChange);
1.280 +test.Printf(_L("RemountDrive() returned %d\n"), r);
1.281 + test(r == KErrNotReady || r == KErrNone);
1.282 +
1.283 + test.Printf(_L("Waiting for media change...\n"));
1.284 + User::WaitForRequest(changeStatus);
1.285 +
1.286 + do
1.287 + {
1.288 +
1.289 +
1.290 + r = fs.Volume(v, KDriveNumProt); //Check
1.291 + test.Printf(_L("Volume() returned %d\n"), r);
1.292 +
1.293 + fs.NotifyChange(ENotifyAll, changeStatus);
1.294 + }
1.295 + while (r == KErrNotReady);
1.296 + fs.NotifyChangeCancel(changeStatus);
1.297 +
1.298 +
1.299 +
1.300 + // Set the mount info for the secure area using KForceMediaChangeReOpenMediaDriver flag
1.301 + // This should be synchronous in behaviour
1.302 + test.Next(_L("test remount with KForceMediaChangeReOpenMediaDriver flag"));
1.303 + r = fs.RemountDrive(KDriveNumProt, NULL, (TUint) KForceMediaChangeReOpenMediaDriver);
1.304 + test(r == KErrNone);
1.305 + r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.306 + test(r == KErrNone);
1.307 + r = fs.Volume(v, KDriveNumProt); //Check
1.308 +test.Printf(_L("Volume() returned %d\n"), r);
1.309 + test(r == KErrNone);
1.310 +
1.311 + test.Printf(_L("Remount Drive suceeded. Secure area is now unlocked\n"));
1.312 + test.Printf(_L("Press any key to continue...\n"));
1.313 +
1.314 +
1.315 + test.Next(_L("test writing to protected & unprotected areas"));
1.316 +
1.317 + RFile f1,f2;
1.318 + TRequestStatus req1, req2, req3, req4;
1.319 +
1.320 + const TInt KBufSize = 32 * 1024;
1.321 + LOCAL_D TBuf8<KBufSize> gBuf;
1.322 +
1.323 +
1.324 + test.Printf(_L("Opening files...\n"));
1.325 +test.Printf(_L("Opening %S...\n"), &fileNameUnprot);
1.326 + r = f1.Replace(fs, fileNameUnprot,EFileStreamText|EFileWrite);
1.327 + test (r == KErrNone);
1.328 +
1.329 +test.Printf(_L("Opening %S...\n"), &fileNameProt);
1.330 + r = f2.Replace(fs, fileNameProt,EFileStreamText|EFileWrite);
1.331 + test (r == KErrNone);
1.332 +
1.333 +
1.334 +// test.Printf(_L("Wait 10 secs for stack to power down...\n"));
1.335 +// User::After(10 * 1000000);
1.336 +// test.Printf(_L("done\n"));
1.337 +
1.338 + gBuf.SetLength(KBufSize);
1.339 +
1.340 +test.Printf(_L("Writing files...\n"));
1.341 +
1.342 + req1 = KRequestPending;
1.343 + req2 = KRequestPending;
1.344 + req3 = KRequestPending;
1.345 + req4 = KRequestPending;
1.346 +
1.347 + f1.Write(gBuf, req1);
1.348 +r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.349 +test (r == KErrNone);
1.350 + f2.Write(gBuf, req2);
1.351 +
1.352 +test.Printf(_L("req1 %d, req2 %d\n"), req1.Int(), req2.Int());
1.353 + test (req1 == KRequestPending || req1 == KErrNone);
1.354 + test (req2 == KRequestPending || req2 == KErrNone);
1.355 +
1.356 + // force a remount to test whether a write to the unprotected area
1.357 + // is disrupted....
1.358 + // NB Should set aFlags to KForceMediaChangeReOpenMediaDriver, otherwise a media change will occur which WILL
1.359 + // prematurely complete any pending writes
1.360 +r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.361 +test (r == KErrNone);
1.362 +
1.363 +//r = fs.RemountDrive(KDriveNumProt, &pckg, 1);
1.364 +test.Printf(_L("RemountDrive() returned %d\n"), r);
1.365 +
1.366 + f1.Write(gBuf, req3);
1.367 + f2.Write(gBuf, req4);
1.368 +
1.369 +test.Printf(_L("WaitForRequests...\n"));
1.370 + User::WaitForRequest(req1);
1.371 + User::WaitForRequest(req2);
1.372 + User::WaitForRequest(req3);
1.373 + User::WaitForRequest(req4);
1.374 +
1.375 +test.Printf(_L("req1 %d, req2 %d req3 %d, req4 %d\n"), req1.Int(), req2.Int(), req3.Int(), req4.Int());
1.376 + test (req1 == KErrNone);
1.377 + test (req2 == KErrNone);
1.378 + test (req3 == KErrNone);
1.379 + test (req4 == KErrNone);
1.380 +
1.381 + f1.Close();
1.382 + f2.Close();
1.383 +
1.384 +
1.385 + // create a thread which continuously writes to the unprotected area
1.386 + // whilst continually remounting protected area in this thread.
1.387 +
1.388 + test.Next(_L("test writing to protected & unprotected areas using different threads"));
1.389 +
1.390 +test.Printf(_L("Opening %S for read access...\n"), &fileNameUnprot);
1.391 + r = f2.Open(fs, fileNameProt, EFileShareReadersOnly);
1.392 + test (r == KErrNone);
1.393 +
1.394 +
1.395 + test (r == KErrNone);
1.396 + RThread thread;
1.397 + r = thread.Create( _L("Write Thread"), WriteThreadEntryPoint, KDefaultStackSize, NULL, NULL);
1.398 + if(r !=KErrNone)
1.399 + {
1.400 + test.Printf(_L("MT Thread Create 1 ret = %d\n"),r);
1.401 + return;
1.402 + }
1.403 + TRequestStatus writeThreadStat;
1.404 +
1.405 + thread.Rendezvous(writeThreadStat);
1.406 + thread.Resume();
1.407 + User::WaitForRequest(writeThreadStat);
1.408 +// thread.Close(); //close handle to thread
1.409 + if(writeThreadStat.Int() != KErrNone)
1.410 + {
1.411 + test.Printf(_L("MT Thread Create 2 ret = %d\n"),writeThreadStat.Int());
1.412 + test(0);
1.413 + }
1.414 +
1.415 + TBuf8<14> buf;
1.416 +
1.417 +
1.418 + const TInt KMaxIterations = 1000;
1.419 + for(TInt n=0; n<KMaxIterations; n++)
1.420 + {
1.421 + if (n % 10 == 0)
1.422 + test.Printf(_L("iteration %d of %d\r"), n, KMaxIterations);
1.423 +
1.424 + //Set the mount info for the secure area.
1.425 + r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.426 + if (r == KErrNone)
1.427 + {
1.428 +// test.Printf(_L("Remount Drive KEY suceeded. Secure area is now unlocked\n"));
1.429 + }
1.430 + else
1.431 + {
1.432 + test.Printf(_L("Remount Drive KEY failed with %d\n"), r);
1.433 + test(0);
1.434 + }
1.435 +
1.436 + // take it in turns to
1.437 + // (0) call Rfs:Volume()
1.438 + // (1) call RFile::Read
1.439 + // (2) do nothing
1.440 + TInt testNum = n & 0x3;
1.441 + if (testNum == 0)
1.442 + {
1.443 + TVolumeInfo volumeInfo;
1.444 + r = fs.Volume( volumeInfo, KDriveNumProt );
1.445 + if (r != KErrNone)
1.446 + {
1.447 + RDebug::Print( _L("Volume returned %d after RemountDrive"), r);
1.448 + break;
1.449 + }
1.450 + }
1.451 + else if (testNum == 1)
1.452 + {
1.453 + r = f2.Read(0,buf);
1.454 + if(r != KErrNone)
1.455 + {
1.456 + test.Printf(_L("protected Drive Access failed %d\n"), r);
1.457 + test(0);
1.458 + }
1.459 + }
1.460 + else if (testNum == 2)
1.461 + {
1.462 + }
1.463 + else if (testNum == 3)
1.464 + {
1.465 + }
1.466 +
1.467 +
1.468 +//test.Printf(_L("protected Drive KEY Access succeeded\n"));
1.469 + r = fs.RemountDrive(KDriveNumProt, NULL, (TUint) KForceMediaChangeReOpenMediaDriver);
1.470 + if (r == KErrNone)
1.471 + {
1.472 +// test.Printf(_L("Remount Drive NULL suceeded. Secure area is now locked\n"));
1.473 + }
1.474 + else
1.475 + {
1.476 + test.Printf(_L("Remount Drive NULL failed with %d\n"), r);
1.477 + test(0);
1.478 + }
1.479 +
1.480 + if (testNum == 1)
1.481 + {
1.482 + r = f2.Read(0,buf);
1.483 +//test.Printf(_L("protected Drive NULL Access failed with %d\n"), r);
1.484 + if (r != KErrNotReady)
1.485 + {
1.486 + test.Printf(_L("protected Drive NULL Access failed with unexpected error %d\n"), r);
1.487 + test(0);
1.488 + }
1.489 + }
1.490 +
1.491 + }
1.492 +
1.493 + f1.Close();
1.494 +
1.495 +
1.496 + // comment in the next 2 lines to leave the protected area mounted...
1.497 + r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
1.498 + test(r == KErrNone);
1.499 +
1.500 + thread.Kill(KErrNone);
1.501 + thread.Close(); //close handle to thread
1.502 +
1.503 + fs.Close();
1.504 +
1.505 + }