Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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 *********
27 #include <e32std_private.h>
32 const TUint32 KDriveNumProt = EDriveG;
33 const TUint32 KDriveNumUnprot = EDriveF;
34 TChar gDriveLetterProt;
35 TChar gDriveLetterUnprot;
36 TBool gMountProtectedArea = EFalse;
38 GLDEF_D RTest test(_L("T_SETKEY"));
40 // flags stolen from locmedia.h
41 //const TInt KForceMediaChangeReOpenAllMediaDrivers = 0;
42 const TUint KForceMediaChangeReOpenMediaDriver = 0x80000000UL;
43 const TUint KMediaRemountForceMediaChange = 0x00000001UL;
45 class TSDCardSecureMountInfo
49 const TDesC8* iEncryptedDeviceKey;
50 const TDesC8* iRamMKBData;
53 typedef TPckgBuf<TSDCardSecureMountInfo> TSDCardSecureMountInfoPckg;
62 // Standard boilerplate for creating a console window ////////////////////////
66 GLDEF_C TInt E32Main() // main function called by E32
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
75 // Determine whether we should run the full test or just mount the protected area
76 void ParseCommandLine()
79 User::CommandLine(args);
83 gMountProtectedArea = ETrue;
85 else if (args == _L("-?"))
87 test.Printf(_L("usage: t_setkey [-m]\n"));
88 test.Printf(_L("-m => mount proteced area and exit\n"));
92 void SetupConsoleL() // initialize and call example code under cleanup stack
95 test.Start(_L("Starting tests..."));
98 TRAPD(error,StartAppL()); // perform example function
100 test.Printf(_L("failed: leave code=%d\n"), error);
102 test.Printf(_L("ok\n"));
104 test.Printf(_L(" [press any key]\n"));
112 // End of standard boilerplate ///////////////////////////////////////////////
115 static const TUint8 testDeviceKeyRawData[160] =
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
133 TInt WriteThreadEntryPoint( TAny* aParam )
137 RTest test(_L("WriteThread"));
141 test.Start(_L("Starting WriteThread ..."));
149 test.Printf(_L("WT Connect err %d\n"), r);
154 TFileName fileNameUnprot = _L("?:\\TESTTHRD.TXT");
155 fileNameUnprot[0] = (TText) gDriveLetterUnprot;
157 //Open file on the user area drive
158 r = file.Replace(fs, fileNameUnprot, EFileWrite);
161 test.Printf(_L("WT File Replace err %d\n\n"), r);
165 TPtrC8 data(testDeviceKeyRawData, 8);
167 RThread().Rendezvous(KErrNone);
169 for(TInt iter=0; iter < KMaxTInt;iter++) // keep sending write requests to the user area for ever
172 r = file.Write(data);
173 //test.Printf(_L("WT File Write err %d iter %d\r"), r, iter);
177 test.Printf(_L("WT File Write err %d\n"), r);
193 GLDEF_C void StartAppL()
196 // Exit if no key defined
197 if (testDeviceKeyRawData[0] == 0 && testDeviceKeyRawData[1] == 0)
199 test.Printf(_L("This test needs to be recompiled with a valid key\n"));
207 test.Printf(_L("Starting Setkey\n"));
208 if(fs.Connect()!=KErrNone)
211 test.Printf(_L("Connected to fileserver OK\n"));
214 mkBData.SetLength(160);
216 for (i = 0; i < 160; i++)
218 mkBData[i] = testDeviceKeyRawData[i];
221 TSDCardSecureMountInfoPckg pckg;
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;
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;
240 r = fs.Volume(v, KDriveNumProt); //Check
241 test.Printf(_L("Volume() returned %d\n"), r);
242 test(r == KErrNotReady || r == KErrNone);
245 if (gMountProtectedArea)
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);
254 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
256 r = fs.Volume(v, KDriveNumProt); //Check
257 test.Printf(_L("Volume() returned %d\n"), r);
258 test(r == KErrNone || r == KErrCorrupt);
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);
265 test.Printf(_L("Remount Drive suceeded. Secure area is now unlocked\n"));
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);
280 test.Printf(_L("Waiting for media change...\n"));
281 User::WaitForRequest(changeStatus);
287 r = fs.Volume(v, KDriveNumProt); //Check
288 test.Printf(_L("Volume() returned %d\n"), r);
290 fs.NotifyChange(ENotifyAll, changeStatus);
292 while (r == KErrNotReady);
293 fs.NotifyChangeCancel(changeStatus);
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);
302 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
304 r = fs.Volume(v, KDriveNumProt); //Check
305 test.Printf(_L("Volume() returned %d\n"), r);
308 test.Printf(_L("Remount Drive suceeded. Secure area is now unlocked\n"));
309 test.Printf(_L("Press any key to continue...\n"));
312 test.Next(_L("test writing to protected & unprotected areas"));
315 TRequestStatus req1, req2, req3, req4;
317 const TInt KBufSize = 32 * 1024;
318 LOCAL_D TBuf8<KBufSize> gBuf;
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);
326 test.Printf(_L("Opening %S...\n"), &fileNameProt);
327 r = f2.Replace(fs, fileNameProt,EFileStreamText|EFileWrite);
328 test (r == KErrNone);
331 // test.Printf(_L("Wait 10 secs for stack to power down...\n"));
332 // User::After(10 * 1000000);
333 // test.Printf(_L("done\n"));
335 gBuf.SetLength(KBufSize);
337 test.Printf(_L("Writing files...\n"));
339 req1 = KRequestPending;
340 req2 = KRequestPending;
341 req3 = KRequestPending;
342 req4 = KRequestPending;
344 f1.Write(gBuf, req1);
345 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
346 test (r == KErrNone);
347 f2.Write(gBuf, req2);
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);
353 // force a remount to test whether a write to the unprotected area
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);
360 //r = fs.RemountDrive(KDriveNumProt, &pckg, 1);
361 test.Printf(_L("RemountDrive() returned %d\n"), r);
363 f1.Write(gBuf, req3);
364 f2.Write(gBuf, req4);
366 test.Printf(_L("WaitForRequests...\n"));
367 User::WaitForRequest(req1);
368 User::WaitForRequest(req2);
369 User::WaitForRequest(req3);
370 User::WaitForRequest(req4);
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);
382 // create a thread which continuously writes to the unprotected area
383 // whilst continually remounting protected area in this thread.
385 test.Next(_L("test writing to protected & unprotected areas using different threads"));
387 test.Printf(_L("Opening %S for read access...\n"), &fileNameUnprot);
388 r = f2.Open(fs, fileNameProt, EFileShareReadersOnly);
389 test (r == KErrNone);
392 test (r == KErrNone);
394 r = thread.Create( _L("Write Thread"), WriteThreadEntryPoint, KDefaultStackSize, NULL, NULL);
397 test.Printf(_L("MT Thread Create 1 ret = %d\n"),r);
400 TRequestStatus writeThreadStat;
402 thread.Rendezvous(writeThreadStat);
404 User::WaitForRequest(writeThreadStat);
405 // thread.Close(); //close handle to thread
406 if(writeThreadStat.Int() != KErrNone)
408 test.Printf(_L("MT Thread Create 2 ret = %d\n"),writeThreadStat.Int());
415 const TInt KMaxIterations = 1000;
416 for(TInt n=0; n<KMaxIterations; n++)
419 test.Printf(_L("iteration %d of %d\r"), n, KMaxIterations);
421 //Set the mount info for the secure area.
422 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
425 // test.Printf(_L("Remount Drive KEY suceeded. Secure area is now unlocked\n"));
429 test.Printf(_L("Remount Drive KEY failed with %d\n"), r);
433 // take it in turns to
434 // (0) call Rfs:Volume()
435 // (1) call RFile::Read
437 TInt testNum = n & 0x3;
440 TVolumeInfo volumeInfo;
441 r = fs.Volume( volumeInfo, KDriveNumProt );
444 RDebug::Print( _L("Volume returned %d after RemountDrive"), r);
448 else if (testNum == 1)
453 test.Printf(_L("protected Drive Access failed %d\n"), r);
457 else if (testNum == 2)
460 else if (testNum == 3)
465 //test.Printf(_L("protected Drive KEY Access succeeded\n"));
466 r = fs.RemountDrive(KDriveNumProt, NULL, (TUint) KForceMediaChangeReOpenMediaDriver);
469 // test.Printf(_L("Remount Drive NULL suceeded. Secure area is now locked\n"));
473 test.Printf(_L("Remount Drive NULL failed with %d\n"), r);
480 //test.Printf(_L("protected Drive NULL Access failed with %d\n"), r);
481 if (r != KErrNotReady)
483 test.Printf(_L("protected Drive NULL Access failed with unexpected error %d\n"), r);
493 // comment in the next 2 lines to leave the protected area mounted...
494 r = fs.RemountDrive(KDriveNumProt, &pckg, (TUint) KForceMediaChangeReOpenMediaDriver);
497 thread.Kill(KErrNone);
498 thread.Close(); //close handle to thread