kerneltest/f32test/server/t_mmc.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-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 // f32test\server\t_mmc.cpp
       
    15 // Tests locking, unlocking and clearing of a password on a suitable
       
    16 // device.  Currently, this device is a MultiMediaCard.  The RFs API
       
    17 // allows the user to store the password.
       
    18 // This is a manual test, and the operator must observe that the notifier
       
    19 // appears exactly when indicated by the program.
       
    20 // 
       
    21 //
       
    22 
       
    23 
       
    24 #include <f32file.h>
       
    25 #include <e32test.h>
       
    26 #include <f32dbg.h>
       
    27 #include <f32fsys.h>
       
    28 
       
    29 // definitions from p32mmc.h
       
    30 
       
    31 const TInt KMMCCIDLength = 16;
       
    32 
       
    33 class TMMC
       
    34 	{
       
    35 public:
       
    36 	static inline TUint32 BigEndian32(const TUint8*);
       
    37 	static inline void BigEndian4Bytes(TUint8* aPtr, TUint32 aVal);
       
    38 	};
       
    39 
       
    40 
       
    41 inline TUint32 TMMC::BigEndian32(const TUint8* aPtr)
       
    42 	{return( (aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | (aPtr[3]) );}
       
    43 
       
    44 inline void TMMC::BigEndian4Bytes(TUint8* aPtr, TUint32 aVal)
       
    45 	{
       
    46 	aPtr[0] = (TUint8)((aVal >> 24) & 0xFF);
       
    47 	aPtr[1] = (TUint8)((aVal >> 16) & 0xFF);
       
    48 	aPtr[2] = (TUint8)((aVal >> 8) & 0xFF);
       
    49 	aPtr[3] = (TUint8)(aVal & 0xFF);
       
    50 	}
       
    51 
       
    52 
       
    53 
       
    54 // local test data
       
    55 
       
    56 LOCAL_D RTest test(_L("t_mmc"));
       
    57 
       
    58 LOCAL_D RFs TheFs;
       
    59 LOCAL_D RFile TheFile;
       
    60 
       
    61 #if defined(__WINS__)
       
    62 _LIT(KSessionPath, "X:\\");
       
    63 const TInt KDrv = EDriveX;
       
    64 const TText KDrvLtr = 'x';
       
    65 #else
       
    66 _LIT(KSessionPath, "D:\\");
       
    67 const TInt KDrv = EDriveD;
       
    68 const TText KDrvLtr = 'd';
       
    69 #endif
       
    70 
       
    71 _LIT(KTestFileName, "testFile");
       
    72 
       
    73 enum TAccessType {EAccessUnlock, EAccessStore, EAccessCancel, EAccessWrongPw, EAccessNoNotifier};
       
    74 
       
    75 LOCAL_D TMediaPassword KNul, KPw1, KPw2, KPw3, KPw4, KPw5;
       
    76 
       
    77 #ifdef __WINS__
       
    78 	const static TUint8 cid0[KMMCCIDLength] =	// "CID0ccccccccccc#";
       
    79 		{
       
    80 		'C',	'I',	'D',	'0',
       
    81 		'c',	'c',	'c',	'c',
       
    82 		'c',	'c',	'c',	'c',
       
    83 		'c',	'c',	'c',	'#'
       
    84 		};
       
    85 #else
       
    86 	const static TUint8 cid0[KMMCCIDLength] =	// big-endian, CID0
       
    87 		{
       
    88 		0x06,	0x00,	0x00,	0x31,
       
    89 		0x36,	0x4d,	0x20,	0x20,
       
    90 		0x20,	0x00,	0xb4,	0xff,
       
    91 		0xff,	0xff,	0x63,	0xd9
       
    92 		};
       
    93 #endif
       
    94 
       
    95 // local test functions
       
    96 
       
    97 LOCAL_C void RemountMedia();
       
    98 LOCAL_C void ClearControllerStore();
       
    99 LOCAL_C void DeletePasswordFile();
       
   100 LOCAL_C TInt CreateFile();
       
   101 LOCAL_C TInt PWFileSize(TInt &aLength);
       
   102 LOCAL_C TInt AccessDisk(TAccessType aAccess, const TDesC8 &aPWD);
       
   103 LOCAL_C void TestHasPassword(TBool aIL, TBool aHP);
       
   104 
       
   105 LOCAL_C void TestLockUnlockR();
       
   106 LOCAL_C void TestNullPasswords();
       
   107 LOCAL_C void TestLockUnlock();
       
   108 
       
   109 LOCAL_C void TestPasswordStore();
       
   110 LOCAL_C void TestWriteToDisk();
       
   111 
       
   112 LOCAL_C void TestFormat();
       
   113 LOCAL_C void TestRemount();
       
   114 
       
   115 
       
   116 /** force a remount on the removable media. */
       
   117 
       
   118 LOCAL_C void RemountMedia()
       
   119 	{
       
   120 #ifdef __WINS__
       
   121 //	UserSvr::ForceRemountMedia(ERemovableMedia0);
       
   122 	User::After(1 * 1500 * 1000);
       
   123 #else
       
   124 //	UserSvr::ForceRemountMedia(ERemovableMedia0);
       
   125 //	User::After(1 * 1500 * 1000);
       
   126 	test.Printf(_L("Remove and re-insert card.  Press \'z\' when finished.\n"));
       
   127 	while (test.Getch() != 'z')
       
   128 		{ /* empty. */ }
       
   129 #endif
       
   130 	}
       
   131 
       
   132 
       
   133 /** ask the user to replace the current lockable media with another */
       
   134 
       
   135 LOCAL_C void ChangeMedia()
       
   136 	{
       
   137 #ifdef __WINS__
       
   138 	test.Printf(_L("Press f4 whilst holding down f5\n"));
       
   139 	test.Printf(_L("Press z when completed\n"));
       
   140 	while(test.Getch() != 'z')
       
   141 		{ /* empty */ }
       
   142 #else
       
   143 	test.Printf(_L("Remove and insert other card.  Press \'z\' when finished.\n"));
       
   144 	while (test.Getch() != 'z')
       
   145 		{ /* empty. */ }
       
   146 #endif
       
   147 	}
       
   148 
       
   149 
       
   150 /** disable auto-unlock by clearing controller store */
       
   151 
       
   152 LOCAL_C void ClearControllerStore()
       
   153 	{
       
   154 	TBuf8<1> nulSt;
       
   155 	test(TBusLocalDrive::WritePasswordData(nulSt) == KErrNone);// empty
       
   156 	test(TBusLocalDrive::PasswordStoreLengthInBytes() == 0);
       
   157 	}
       
   158 
       
   159 
       
   160 /** delete the password store file.  Does not affect the auto-unlock mechanism. */
       
   161 
       
   162 LOCAL_C void DeletePasswordFile()
       
   163 	{
       
   164 	TInt r;
       
   165 	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
   166 	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
   167 	do
       
   168 		{
       
   169 		User::After(100 * 1000);
       
   170 		r = TheFs.Delete(mediaPWrdFile);
       
   171 		} while (r == KErrInUse);
       
   172 
       
   173 	test(r == KErrNone || r == KErrNotFound);
       
   174 	}
       
   175 
       
   176 
       
   177 /** attempt to create a file on the removable media and return any error code */
       
   178 
       
   179 LOCAL_C TInt CreateFile()
       
   180 	{
       
   181 	RFile f;
       
   182 	TInt r = f.Replace(TheFs, KTestFileName, EFileShareAny);
       
   183 	if (r == KErrNone)
       
   184 		f.Close();
       
   185 	return r;
       
   186 	}
       
   187 
       
   188 
       
   189 /** get size of the media store file in bytes, and return any error code */
       
   190 
       
   191 LOCAL_C TInt PWFileSize(TInt &aLength)
       
   192 	{
       
   193 	TInt r;										// error code
       
   194 
       
   195 	// allow low priority background writer thread to start and finish
       
   196 
       
   197 	User::After(1 * 1000 * 1000);
       
   198 
       
   199 	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
   200 	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
   201 	RFile f;
       
   202 	if ((r = f.Open(TheFs, mediaPWrdFile, EFileShareAny)) == KErrNone)
       
   203 		{
       
   204 		r = f.Size(aLength);
       
   205 		f.Close();
       
   206 		}
       
   207 
       
   208 	return r;
       
   209 	}
       
   210 
       
   211 
       
   212 /**
       
   213  * write message to screen and attempt to access disk.  If card is locked then async
       
   214  * notifier will be brought up from within file server.
       
   215  */
       
   216 
       
   217 LOCAL_C TInt AccessDisk(TAccessType aAccess, const TDesC8 &aPWD)
       
   218 	{
       
   219 	TBuf16<16> pwd;
       
   220 	pwd.Copy((const TUint16 *)aPWD.Ptr(), aPWD.Length() / 2);
       
   221 
       
   222 	if (aAccess == EAccessUnlock || aAccess == EAccessStore)
       
   223 		test.Printf(_L("\"%S\": "), &pwd);
       
   224 
       
   225 	switch(aAccess)
       
   226 		{
       
   227 		case EAccessUnlock: 
       
   228 			test.Printf(_L("unlock\n"));
       
   229 			break;
       
   230 
       
   231 		case EAccessStore:
       
   232 			test.Printf(_L("store\n"));
       
   233 			break;
       
   234 
       
   235 		case EAccessCancel:
       
   236 			test.Printf(_L("cancel\n"));
       
   237 			break;
       
   238 
       
   239 		case EAccessWrongPw:
       
   240 			test.Printf(_L("wrong\n"));
       
   241 			break;
       
   242 
       
   243 		case EAccessNoNotifier:
       
   244 			test.Printf(_L("** no notifier **\n"));
       
   245 			break;
       
   246 
       
   247 		default:
       
   248 			test(EFalse);
       
   249 			break;
       
   250 		}
       
   251 
       
   252 	TInt r = TheFile.Open(TheFs, KTestFileName, EFileShareAny);
       
   253 	if(r == KErrNone)
       
   254 		TheFile.Close();
       
   255 
       
   256 	return r;
       
   257 	}
       
   258 
       
   259 
       
   260 /**
       
   261  * uses RFs::DriveInfo() to test if the card has a password.  Is checking
       
   262  * that iHasPassword in TMMCCard is maintained properly.
       
   263  */
       
   264 
       
   265 inline TBool bits_set(TUint aMsk, TUint aSel)
       
   266 	{
       
   267 	return (aMsk & aSel) == aSel;
       
   268 	}
       
   269 
       
   270 LOCAL_C void TestHasPassword(TBool aIL, TBool aHP)
       
   271 	{
       
   272 	const TInt d = KDrv;
       
   273 	RFs &fs = TheFs;
       
   274 
       
   275 	TDriveInfo di;
       
   276 	test(fs.Drive(di, d) == KErrNone);
       
   277 
       
   278 #ifdef __WINS__
       
   279 	test(aIL == bits_set(di.iMediaAtt, KMediaAttLocked | KMediaAttLockable | KMediaAttHasPassword));
       
   280 	test(aHP == bits_set(di.iMediaAtt, KMediaAttLockable | KMediaAttHasPassword));
       
   281 #else
       
   282 	test(aIL == bits_set(di.iMediaAtt, KMediaAttLocked | KMediaAttHasPassword));
       
   283 	test(aHP == bits_set(di.iMediaAtt, KMediaAttHasPassword));
       
   284 #endif
       
   285 	}
       
   286 
       
   287 
       
   288 // -------- test functions --------
       
   289 
       
   290 
       
   291 /**
       
   292  * test return values of LockDrive(), UnlockDrive() and ClearPassword()
       
   293  * without trying use the disk.  This tests that the functions return the
       
   294  * same values as the TBusLocalDrive functions.
       
   295  *
       
   296  *			EPbPswdUnlock		EPbPswdLock			EPbPswdClear
       
   297  *			right	wrong		right	wrong		right	wrong	
       
   298  * locked	None	AccDen		AccDec	AccDen		AccDen	AccDen	
       
   299  * unlocked	AldExst	AldExst		None	AccDec		None	AccDen	
       
   300  *
       
   301  * Locked means inaccessible, not just has password.
       
   302  */
       
   303 
       
   304 LOCAL_C void TestLockUnlockR()
       
   305 	{
       
   306 	test.Start(_L("TestLockUnlockR"));
       
   307 
       
   308 	const TInt d = KDrv;
       
   309 	RFs &fs = TheFs;
       
   310 
       
   311 	test.Next(_L("assign password"));
       
   312 	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// assign test password
       
   313 	TestHasPassword(EFalse, ETrue);
       
   314 	RemountMedia();													// card is now locked
       
   315 	TestHasPassword(ETrue, ETrue);
       
   316 
       
   317 	test.Next(_L("lock locked card"));
       
   318 	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrAccessDenied);	// lock locked wrong
       
   319 	TestHasPassword(ETrue, ETrue);
       
   320 	test(fs.LockDrive(d, KPw1, KPw1, EFalse) == KErrAccessDenied);	// lock locked right
       
   321 	TestHasPassword(ETrue, ETrue);
       
   322 
       
   323 	test.Next(_L("unlock locked card"));
       
   324 	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrAccessDenied);		// unlock locked wrong
       
   325 	TestHasPassword(ETrue, ETrue);
       
   326 	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);				// unlock locked right
       
   327 	TestHasPassword(EFalse, ETrue);
       
   328 
       
   329 	test.Next(_L("unlock unlocked card"));
       
   330 	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrAlreadyExists);		// unlock unlocked right
       
   331 	TestHasPassword(EFalse, ETrue);
       
   332 	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrAlreadyExists);		// unlock unlocked wrong
       
   333 	TestHasPassword(EFalse, ETrue);
       
   334 
       
   335 	test.Next(_L("lock unlocked card"));
       
   336 	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrAccessDenied);	// lock unlocked wrong
       
   337 	TestHasPassword(EFalse, ETrue);
       
   338 	test(fs.LockDrive(d, KPw1, KPw1, EFalse) == KErrNone);			// lock unlocked right
       
   339 	TestHasPassword(EFalse, ETrue);
       
   340 
       
   341 	test.Next(_L("clear unlocked card"));
       
   342 	test(fs.ClearPassword(d, KPw2) == KErrAccessDenied);			// clear unlocked wrong
       
   343 	TestHasPassword(EFalse, ETrue);
       
   344 
       
   345 	RemountMedia();
       
   346 	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
       
   347 	TestHasPassword(EFalse, ETrue);
       
   348 	test(fs.ClearPassword(d, KPw1) == KErrNone);					// clear unlocked right
       
   349 	TestHasPassword(EFalse, EFalse);
       
   350 
       
   351 	test.Next(_L("assign password"));
       
   352 	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// assign test password
       
   353 	TestHasPassword(EFalse, ETrue);
       
   354 	RemountMedia();													// card is now locked
       
   355 	TestHasPassword(ETrue, ETrue);
       
   356 
       
   357 	test.Next(_L("clear locked card"));
       
   358 	test(fs.ClearPassword(d, KPw2) == KErrAccessDenied);			// clear locked wrong
       
   359 	TestHasPassword(ETrue, ETrue);
       
   360 	test(fs.ClearPassword(d, KPw1) == KErrAccessDenied);			// clear locked right
       
   361 	TestHasPassword(ETrue, ETrue);
       
   362 	
       
   363 	test.Next(_L("clean up for following tests"));
       
   364 	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
       
   365 	TestHasPassword(EFalse, ETrue);
       
   366 	test(fs.ClearPassword(d, KPw1) == KErrNone);
       
   367 	TestHasPassword(EFalse, EFalse);
       
   368 
       
   369 	ClearControllerStore();
       
   370 	DeletePasswordFile();
       
   371 
       
   372 	test.End();
       
   373 	}
       
   374 
       
   375 
       
   376 /** test the special cases where null passwords are used. */
       
   377 
       
   378 LOCAL_C void TestNullPasswords()
       
   379 	{
       
   380 	test.Start(_L("TestNullPasswords"));
       
   381 
       
   382 	test.Next(_L("card has no password"));
       
   383 	test(TheFs.LockDrive(KDrv, KNul, KNul, ETrue) == KErrAccessDenied);
       
   384 	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAlreadyExists);
       
   385 	test(TheFs.ClearPassword(KDrv, KNul) == KErrAccessDenied);
       
   386 
       
   387 	test.Next(_L("card has password and is unlocked"));
       
   388 	test(TheFs.LockDrive(KDrv, KNul, KPw1, ETrue) == KErrNone);
       
   389 	RemountMedia();
       
   390 	test(TheFs.LockDrive(KDrv, KNul, KNul, ETrue) == KErrAccessDenied);
       
   391 	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAlreadyExists);
       
   392 	test(TheFs.ClearPassword(KDrv, KNul) == KErrAccessDenied);
       
   393 	test(TheFs.ClearPassword(KDrv, KPw1) == KErrNone);
       
   394 
       
   395 	test.Next(_L("unlock with null disallowed"));
       
   396 	RemountMedia();
       
   397 	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAccessDenied);
       
   398 	test.Next(_L("check can still use card"));
       
   399 	test(TheFs.LockDrive(KDrv, KNul, KPw1, ETrue) == KErrNone);	// check can still use
       
   400 	test(TheFs.ClearPassword(KDrv, KPw1) == KErrNone);
       
   401 
       
   402 	test.Next(_L("clean up for following tests"));
       
   403 	ClearControllerStore();
       
   404 	DeletePasswordFile();
       
   405 
       
   406 	test.End();
       
   407 	}
       
   408 
       
   409 
       
   410 /** test LockDrive(), UnlockDrive() and ClearPassword() with locked media notifier */
       
   411 
       
   412 LOCAL_C void TestLockUnlock()
       
   413 	{
       
   414 	test.Start(_L("TestLockUnlock"));
       
   415 
       
   416 	const TInt d = KDrv;
       
   417 	RFs &fs = TheFs;
       
   418 
       
   419 	test.Next(_L("create test file"));
       
   420 	test(CreateFile() == KErrNone);
       
   421 
       
   422 	test.Next(_L("lock card and don't store"));
       
   423 	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// unlocked KPw1
       
   424 	TestHasPassword(EFalse, ETrue);
       
   425 
       
   426 	test.Next(_L("access with right pwd"));
       
   427 	RemountMedia();													// locked KPw1
       
   428 	TestHasPassword(ETrue, ETrue);
       
   429 	test(AccessDisk(EAccessUnlock, KPw1) == KErrNone);
       
   430 	TestHasPassword(EFalse, ETrue);
       
   431 
       
   432 	test.Next(_L("access with cancelled pwd"));
       
   433 	RemountMedia();													// locked KPw1	
       
   434 	TestHasPassword(ETrue, ETrue);
       
   435 	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
       
   436 	TestHasPassword(ETrue, ETrue);
       
   437 
       
   438 	test.Next(_L("access with new stored pwd"));
       
   439 	RemountMedia();
       
   440 	TestHasPassword(ETrue, ETrue);
       
   441 	test(AccessDisk(EAccessStore, KPw1) == KErrNone);				// unlocked KPw1
       
   442 	TestHasPassword(EFalse, ETrue);
       
   443 
       
   444 	test.Next(_L("access with stored pwd"));
       
   445 	RemountMedia();													// unlocked KPw1 (use store)
       
   446 	TestHasPassword(EFalse, ETrue);
       
   447 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   448 	TestHasPassword(EFalse, ETrue);
       
   449 
       
   450 	test.Next(_L("lock wrong pwd"));
       
   451 	test(fs.LockDrive(d, KPw3, KPw2, EFalse) == KErrAccessDenied);	// unlocked KPw1
       
   452 	TestHasPassword(EFalse, ETrue);
       
   453 
       
   454 	test.Next(_L("change pwd"));
       
   455 	test(fs.LockDrive(d, KPw1, KPw2, EFalse) == KErrNone);			// unlocked KPw2 (rem from store)
       
   456 	TestHasPassword(EFalse, ETrue);
       
   457 
       
   458 	test.Next(_L("mapping removed from store"));
       
   459 	RemountMedia();
       
   460 	test(AccessDisk(EAccessUnlock, KPw2) == KErrNone);
       
   461 	TestHasPassword(EFalse, ETrue);
       
   462 
       
   463 	test.Next(_L("wrong password"));
       
   464 	RemountMedia();													// locked KPw2
       
   465 	TestHasPassword(ETrue, ETrue);
       
   466 	test(AccessDisk(EAccessWrongPw, KNul) == KErrLocked);
       
   467 	TestHasPassword(ETrue, ETrue);
       
   468 
       
   469 	test.Next(_L("unlocked card"));
       
   470 	test(fs.UnlockDrive(d, KPw2, ETrue) == KErrNone);				// unlocked KPw2 (add to store)
       
   471 	TestHasPassword(EFalse, ETrue);
       
   472 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);			// before power down
       
   473 	TestHasPassword(EFalse, ETrue);
       
   474 
       
   475 	test.Next(_L("clear wrong pwd"));
       
   476 	test(fs.ClearPassword(d, KPw1) == KErrAccessDenied);			// KPw2 backup kept
       
   477 	TestHasPassword(EFalse, ETrue);
       
   478 	RemountMedia();													// unlocked KPw2 (use store)
       
   479 	TestHasPassword(EFalse, ETrue);
       
   480 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   481 	TestHasPassword(EFalse, ETrue);
       
   482 
       
   483 	test.Next(_L("change pwd"));
       
   484 	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrNone);			// locked KPw1 (rem from store)
       
   485 	TestHasPassword(EFalse, ETrue);
       
   486 	RemountMedia();
       
   487 	TestHasPassword(ETrue, ETrue);
       
   488 	test(AccessDisk(EAccessUnlock, KPw1) == KErrNone);
       
   489 	TestHasPassword(EFalse, ETrue);
       
   490 
       
   491 	test.Next(_L("cancelled pwd"));
       
   492 	RemountMedia();
       
   493 	TestHasPassword(ETrue, ETrue);
       
   494 	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
       
   495 	TestHasPassword(ETrue, ETrue);
       
   496 
       
   497 	test.Next(_L("clean up for following tests"));
       
   498 	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
       
   499 	TestHasPassword(EFalse, ETrue);
       
   500 	test(fs.ClearPassword(KDrv, KPw1) == KErrNone);
       
   501 	TestHasPassword(EFalse, EFalse);
       
   502 	RemountMedia();
       
   503 	TestHasPassword(EFalse, EFalse);
       
   504 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   505 	TestHasPassword(EFalse, EFalse);
       
   506 	ClearControllerStore();
       
   507 	DeletePasswordFile();
       
   508 
       
   509 	test.End();
       
   510 	}
       
   511 
       
   512 
       
   513 /**
       
   514  * test password store by accessing disk in various states.  Ensures password
       
   515  * store file is right size after each operation.  More detailed checking of
       
   516  * the TBusLocalDrive::ReadPasswordData() ouput is done in t_pwstr.
       
   517  */
       
   518 
       
   519 LOCAL_C void TestPasswordStore()
       
   520 	{
       
   521 	test.Start(_L("TestPasswordStore"));
       
   522 
       
   523 //	TInt r;										// general error code
       
   524 	RFs &fs = TheFs;
       
   525 	const TInt d = KDrv;
       
   526 
       
   527 	test.Next(_L("create test file on first media"));
       
   528 	test(CreateFile() == KErrNone);
       
   529 
       
   530 	test.Next(_L("lock card and don't store"));
       
   531 	test(fs.LockDrive(d, KNul, KPw5, EFalse) == KErrNone);	// {}
       
   532 	TestHasPassword(EFalse, ETrue);
       
   533 	TInt size1;
       
   534 	test(PWFileSize(size1) == KErrNotFound);
       
   535 
       
   536 	test.Next(_L("lock card and store"));
       
   537 	test(fs.LockDrive(d, KPw5, KPw3, ETrue) == KErrNone);	// {0 |-> 3}
       
   538 	TestHasPassword(EFalse, ETrue);
       
   539 	test.Next(_L("access media1 with stored password"));
       
   540 
       
   541 	RemountMedia();
       
   542 	TestHasPassword(EFalse, ETrue);
       
   543 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   544 	TestHasPassword(EFalse, ETrue);
       
   545 
       
   546 	test.Next(_L("test password file exists"));
       
   547 	test(PWFileSize(size1) == KErrNone);
       
   548 	test(size1 == 16 + 4 + KPw3.Length());
       
   549 
       
   550 	test.Next(_L("change cards and add second password to store"));
       
   551 	ChangeMedia();
       
   552 
       
   553 	test.Next(_L("create test file on second media"));
       
   554 	test(CreateFile() == KErrNone);
       
   555 
       
   556 	test.Next(_L("lock card and store"));
       
   557 	test(fs.LockDrive(d, KNul, KPw4, ETrue) == KErrNone);	// {0 |-> 3, 1 |-> 4}
       
   558 	TestHasPassword(EFalse, ETrue);
       
   559 
       
   560 	TInt size2;
       
   561 	test(PWFileSize(size2) == KErrNone);
       
   562 	test(size2 == 16 + 4 + KPw3.Length() + 16 + 4 + KPw4.Length());
       
   563 
       
   564 	test.Next(_L("access media2 with stored password"));
       
   565 	RemountMedia();
       
   566 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   567 	TestHasPassword(EFalse, ETrue);
       
   568 
       
   569 	test.Next(_L("change password"));
       
   570 	test(fs.LockDrive(d, KPw4, KPw5, ETrue) == KErrNone);	// {0 |-> 3, 1 |-> 5}
       
   571 	TestHasPassword(EFalse, ETrue);
       
   572 
       
   573 	TInt size3;
       
   574 	test(PWFileSize(size3) == KErrNone);
       
   575 	test(size3 == 16 + 4 + KPw3.Length() + 16 + 4 + KPw5.Length());
       
   576 
       
   577 	test.Next(_L("access media1 with stored password"));
       
   578 	ChangeMedia();
       
   579 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   580 
       
   581 	test.Next(_L("remove password from media 1"));
       
   582 	test(fs.ClearPassword(d, KPw3) == KErrNone);			// {1 |-> 5}	- 0 |-> 3
       
   583 	TestHasPassword(EFalse, EFalse);
       
   584 	test(PWFileSize(size3) == KErrNone);
       
   585 	test(size3 == 16 + 4 + KPw5.Length());
       
   586 
       
   587 	test.Next(_L("clean up for following tests"));
       
   588 	ChangeMedia();
       
   589 	test(fs.ClearPassword(d, KPw5) == KErrNone);			// {}			- 1 |-> 5
       
   590 	TestHasPassword(EFalse, EFalse);
       
   591 	test(PWFileSize(size3) == KErrNone);
       
   592 	test(size3 == 0);
       
   593 	test.Printf(_L("replace original card\n"));
       
   594 	ChangeMedia();											// use original card
       
   595 	ClearControllerStore();
       
   596 	DeletePasswordFile();
       
   597 
       
   598 	test.End();
       
   599 	}
       
   600 
       
   601 
       
   602 /**
       
   603  * check the spin off delayed writer thread can work when many requests are queued.
       
   604  * 
       
   605  * The background writer runs at EPriorityMuchLess, so it will not be executed until
       
   606  * this thread sleeps.
       
   607  */
       
   608 
       
   609 LOCAL_C void TestWriteToDisk()
       
   610 	{
       
   611 	test.Start(_L("TestWriteToDisk"));
       
   612 
       
   613 	TInt r;										// error code
       
   614 	TBuf8<1> noMappings;
       
   615 
       
   616 	test.Next(_L("Queuing threads"));
       
   617 	test.Printf(_L("Queuing 100 writes\n"));
       
   618 
       
   619 	const TInt KQueueCnt = 100;
       
   620 
       
   621 	TInt i;
       
   622 	TMediaPassword oldPswd;						// empty - no password at start
       
   623 	TMediaPassword newPswd;
       
   624 	for (i = 0; i < KQueueCnt; ++i)
       
   625 		{
       
   626 		newPswd.Fill(i, KMaxMediaPassword);
       
   627 		test(TheFs.LockDrive(KDrv, oldPswd, newPswd, ETrue) == KErrNone);
       
   628 		oldPswd.Copy(newPswd);
       
   629 		}
       
   630 
       
   631 	test.Printf(_L("Waiting 20 seconds for threads to complete\n"));
       
   632 	User::After(20 * 1000 * 1000);
       
   633 
       
   634 	// check password file contains the last writing.
       
   635 
       
   636 	test.Next(_L("check password file contains last writing"));
       
   637 	const TInt KFileLen = 16 + sizeof(TUint32) + KMaxMediaPassword;
       
   638 	RFile f;
       
   639 	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
   640 	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
   641 	test(f.Open(TheFs, mediaPWrdFile, EFileShareExclusive | EFileStream | EFileRead) == KErrNone);
       
   642 	TInt sz;
       
   643 	test(f.Size(sz) == KErrNone);
       
   644 	test(sz == KFileLen);
       
   645 
       
   646 	TBuf8<KFileLen> chkBuf;
       
   647 	test(f.Read(chkBuf, KFileLen) == KErrNone);
       
   648 	// defer checking buffer contents until after password cleared so not left
       
   649 	// with locked card if test fails.
       
   650 	f.Close();
       
   651 
       
   652 	test(TheFs.ClearPassword(KDrv, oldPswd) == KErrNone);
       
   653 	User::After(1 * 1000 * 1000);				// wait to finish writing
       
   654 	
       
   655 	r = TheFs.Delete(mediaPWrdFile);
       
   656 	test(r == KErrNone || r == KErrNotFound);
       
   657 	test(TBusLocalDrive::WritePasswordData(noMappings) == KErrNone);
       
   658 
       
   659 	// check contents of password file correspond to last written buffer.
       
   660 
       
   661 	test(chkBuf.Length() == KFileLen);
       
   662 	test(chkBuf.Mid(0, KMMCCIDLength) == TPtrC8(cid0, KMMCCIDLength));
       
   663 	const TUint32 len = TMMC::BigEndian32(chkBuf.Mid(KMMCCIDLength, sizeof(TUint32)).Ptr());
       
   664 	test(len == TInt(KMaxMediaPassword));
       
   665 	test(chkBuf.Mid(KMMCCIDLength + sizeof(TUint32), KMMCCIDLength) == oldPswd);
       
   666 
       
   667 	test.Next(_L("clean up for following tests"));
       
   668 	ClearControllerStore();
       
   669 	DeletePasswordFile();
       
   670 
       
   671 	test.End();
       
   672 	}
       
   673 
       
   674 
       
   675 /** test unable to format locked card */
       
   676 
       
   677 LOCAL_C void TestFormat()
       
   678 	{
       
   679 	test.Start(_L("TestFormat"));
       
   680 
       
   681 //	TInt r;										// error code
       
   682 	RFs &fs = TheFs;
       
   683 	const TInt d = KDrv;
       
   684 
       
   685 	TBuf<3> bfDrv;
       
   686 	bfDrv.Append(KDrvLtr);
       
   687 	_LIT(KBP, ":\\");
       
   688 	bfDrv.Append(KBP);
       
   689 
       
   690 	test.Next(_L("create test file"));
       
   691 	test(CreateFile() == KErrNone);
       
   692 
       
   693 	test.Next(_L("lock drive"));
       
   694 	test(TheFs.LockDrive(KDrv, KNul, KPw2, EFalse) == KErrNone);
       
   695 	RemountMedia();
       
   696 
       
   697 	test.Next(_L("unlock card and don't store"));
       
   698 	test(AccessDisk(EAccessUnlock, KPw2) == KErrNone);
       
   699 
       
   700 	// format unlocked drive
       
   701 	test.Next(_L("format unlocked card"));
       
   702 	RFormat fmt;
       
   703 	TInt count;
       
   704 	test(fmt.Open(fs, bfDrv, EHighDensity, count) == KErrNone);
       
   705 	while (count > 0)
       
   706 		{
       
   707 		test.Printf(_L("\rfmt:%d  "), count);
       
   708 		test(fmt.Next(count) == KErrNone);
       
   709 		}
       
   710 	test.Printf(_L("\n"));
       
   711 	fmt.Close();
       
   712 
       
   713 	test.Next(_L("format locked media"));
       
   714 	RemountMedia();								// locked KPw2
       
   715 	test.Printf(_L("Notifier: No password required. Press cancel. \n"));
       
   716 	test(fmt.Open(fs, bfDrv, EHighDensity, count) == KErrLocked);
       
   717 
       
   718 	test.Next(_L("unlock locked card"));
       
   719 	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrNone);
       
   720 	test(fs.ClearPassword(d, KPw2) == KErrNone);
       
   721 
       
   722 	test.Next(_L("create test file"));
       
   723 	test(CreateFile() == KErrNone);
       
   724 
       
   725 	test.Next(_L("remount media, check not locked"));
       
   726 	RemountMedia();
       
   727 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   728 
       
   729 	test.Next(_L("clean up for following tests"));
       
   730 	ClearControllerStore();
       
   731 	DeletePasswordFile();
       
   732 
       
   733 	test.End();
       
   734 	}
       
   735 
       
   736 
       
   737 /** do media change with file open */
       
   738 
       
   739 LOCAL_C void TestRemount()
       
   740 	{
       
   741 	test.Start(_L("TestRemount"));
       
   742 
       
   743 //	TInt r;										// general error code
       
   744 	RFs &fs = TheFs;
       
   745 	const TInt d = KDrv;
       
   746 
       
   747 	test.Next(_L("create file"));
       
   748 	TFileName fn;
       
   749 	fn.Append(KDrvLtr);
       
   750 	_LIT(KFN, ":\\openfile");
       
   751 	fn.Append(KFN);
       
   752 	test.Printf(_L("fn = \"%S\"\n"), &fn);
       
   753 
       
   754 	RFile f;
       
   755 	test(f.Replace(fs, fn, EFileShareAny) == KErrNone);
       
   756 
       
   757 	test.Next(_L("lock card"));
       
   758 	test(fs.LockDrive(d, KNul, KPw5, EFalse) == KErrNone);
       
   759 
       
   760 	test.Next(_L("access with right pwd"));
       
   761 	RemountMedia();
       
   762 	test(AccessDisk(EAccessUnlock, KPw5) == KErrNone);
       
   763 
       
   764 	test.Next(_L("access with wrong pwd"));
       
   765 	RemountMedia();
       
   766 	test(AccessDisk(EAccessWrongPw, KNul) == KErrLocked);
       
   767 
       
   768 	test.Next(_L("access with cancelled pwd"));
       
   769 	RemountMedia();
       
   770 	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
       
   771 
       
   772 	test.Next(_L("clear password"));
       
   773 	test(fs.UnlockDrive(d, KPw5, EFalse) == KErrNone);
       
   774 	test(fs.ClearPassword(d, KPw5) == KErrNone);
       
   775 
       
   776 	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
       
   777 
       
   778 	test.Next(_L("close and delete file"));
       
   779 	f.Close();
       
   780 	test(fs.Delete(fn) == KErrNone);
       
   781 
       
   782 	test.Next(_L("clean up for following tests"));
       
   783 	ClearControllerStore();
       
   784 	DeletePasswordFile();
       
   785 
       
   786 	test.End();
       
   787 	}
       
   788 
       
   789 
       
   790 /**
       
   791  * entry point.  Displays instructions; sets up heap checking; sets up
       
   792  * file server session and calls individual tests.  Mounts drive for WINS.
       
   793  */
       
   794 
       
   795 _LIT(KSDProtDriverFileName,"MEDSDP");
       
   796 _LIT(KSDProtDriverObjName,"Media.SdP");
       
   797 GLDEF_C TInt E32Main()
       
   798     {
       
   799  	__UHEAP_MARK;
       
   800 
       
   801 	test.Title();
       
   802 	test.Start(_L("Starting T_MMC"));
       
   803 
       
   804 	test.Printf(_L("The notifier should only apear along with user instructions.\n"));
       
   805 	test.Printf(_L("The test has failed otherwise.\n"));
       
   806 	test.Printf(_L("\n"));
       
   807 	test.Printf(_L("Use return to simulate unlock button on notifier\n"));
       
   808 	test.Printf(_L("Use space to simulate store button on notifier\n"));
       
   809 	test.Printf(_L("Use escape to simulate cancel button on notifier\n"));
       
   810 	test.Printf(_L("Press a key to continue ...\n\n"));
       
   811 	test.Getch();
       
   812 
       
   813 	test(TheFs.Connect() == KErrNone);
       
   814 
       
   815 	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
   816 	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
   817 	TParsePtrC ppc(mediaPWrdFile);
       
   818 	TInt r = TheFs.MkDir(ppc.DriveAndPath());
       
   819 	test(r == KErrNone || r == KErrAlreadyExists);
       
   820 
       
   821 	// The media driver for the protected area of the SD card uses mount
       
   822 	// info. This is also used for password unlocking by the MMC media driver.
       
   823 	// Due to a temporary problem with the way mount info. is assigned to DMedia
       
   824 	// objects which results in a conflict between the two drivers, unload
       
   825 	// the protected area SD card driver for duration of test.
       
   826 	TheFs.RemountDrive(KDrv,NULL,KLocDrvRemountPostponeRemount);
       
   827 	User::FreePhysicalDevice(KSDProtDriverObjName);
       
   828 
       
   829 	test(TheFs.SetSessionPath(KSessionPath) == KErrNone);
       
   830 	
       
   831 	// initialize the TMediaPassword data
       
   832 	KNul.Copy(reinterpret_cast<const TUint8 *>(L""), 0);
       
   833 	KPw1.Copy(reinterpret_cast<const TUint8 *>(L"b"), 2);
       
   834 	KPw2.Copy(reinterpret_cast<const TUint8 *>(L"cd"), 4);
       
   835 	KPw3.Copy(reinterpret_cast<const TUint8 *>(L"def"), 6);
       
   836 	KPw4.Copy(reinterpret_cast<const TUint8 *>(L"efgh"), 8);
       
   837 	KPw5.Copy(reinterpret_cast<const TUint8 *>(L"fghij"), 10);
       
   838 
       
   839 	test.Next(_L("calling ClearControllerStore"));
       
   840 	ClearControllerStore();
       
   841 	test.Next(_L("calling DeletePasswordFile"));
       
   842 	DeletePasswordFile();
       
   843 
       
   844 	test.Next(_L("calling TestLockUnlockR"));
       
   845 	TestLockUnlockR();
       
   846 	test.Next(_L("calling TestNullPasswords"));
       
   847 	TestNullPasswords();
       
   848 	test.Next(_L("calling TestLockUnlock"));
       
   849 	TestLockUnlock();
       
   850 
       
   851 	test.Next(_L("calling TestPasswordStore"));
       
   852 	TestPasswordStore();
       
   853 	test.Next(_L("calling TestWriteToDisk"));
       
   854 	TestWriteToDisk();
       
   855 
       
   856 	test.Next(_L("calling TestFormat"));
       
   857 	TestFormat();
       
   858 	test.Next(_L("calling TestRemount"));
       
   859 	TestRemount();
       
   860 
       
   861 	// Restore SD Card protected area media driver
       
   862 	User::LoadPhysicalDevice(KSDProtDriverFileName);
       
   863 
       
   864 	TheFs.Close();
       
   865 	test.End();
       
   866 	test.Close();
       
   867 
       
   868 	__UHEAP_MARKEND;
       
   869 
       
   870 	return KErrNone;
       
   871     }
       
   872