os/kernelhwsrv/kerneltest/f32test/server/t_mmc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/server/t_mmc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,872 @@
     1.4 +// Copyright (c) 1998-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 +// f32test\server\t_mmc.cpp
    1.18 +// Tests locking, unlocking and clearing of a password on a suitable
    1.19 +// device.  Currently, this device is a MultiMediaCard.  The RFs API
    1.20 +// allows the user to store the password.
    1.21 +// This is a manual test, and the operator must observe that the notifier
    1.22 +// appears exactly when indicated by the program.
    1.23 +// 
    1.24 +//
    1.25 +
    1.26 +
    1.27 +#include <f32file.h>
    1.28 +#include <e32test.h>
    1.29 +#include <f32dbg.h>
    1.30 +#include <f32fsys.h>
    1.31 +
    1.32 +// definitions from p32mmc.h
    1.33 +
    1.34 +const TInt KMMCCIDLength = 16;
    1.35 +
    1.36 +class TMMC
    1.37 +	{
    1.38 +public:
    1.39 +	static inline TUint32 BigEndian32(const TUint8*);
    1.40 +	static inline void BigEndian4Bytes(TUint8* aPtr, TUint32 aVal);
    1.41 +	};
    1.42 +
    1.43 +
    1.44 +inline TUint32 TMMC::BigEndian32(const TUint8* aPtr)
    1.45 +	{return( (aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | (aPtr[3]) );}
    1.46 +
    1.47 +inline void TMMC::BigEndian4Bytes(TUint8* aPtr, TUint32 aVal)
    1.48 +	{
    1.49 +	aPtr[0] = (TUint8)((aVal >> 24) & 0xFF);
    1.50 +	aPtr[1] = (TUint8)((aVal >> 16) & 0xFF);
    1.51 +	aPtr[2] = (TUint8)((aVal >> 8) & 0xFF);
    1.52 +	aPtr[3] = (TUint8)(aVal & 0xFF);
    1.53 +	}
    1.54 +
    1.55 +
    1.56 +
    1.57 +// local test data
    1.58 +
    1.59 +LOCAL_D RTest test(_L("t_mmc"));
    1.60 +
    1.61 +LOCAL_D RFs TheFs;
    1.62 +LOCAL_D RFile TheFile;
    1.63 +
    1.64 +#if defined(__WINS__)
    1.65 +_LIT(KSessionPath, "X:\\");
    1.66 +const TInt KDrv = EDriveX;
    1.67 +const TText KDrvLtr = 'x';
    1.68 +#else
    1.69 +_LIT(KSessionPath, "D:\\");
    1.70 +const TInt KDrv = EDriveD;
    1.71 +const TText KDrvLtr = 'd';
    1.72 +#endif
    1.73 +
    1.74 +_LIT(KTestFileName, "testFile");
    1.75 +
    1.76 +enum TAccessType {EAccessUnlock, EAccessStore, EAccessCancel, EAccessWrongPw, EAccessNoNotifier};
    1.77 +
    1.78 +LOCAL_D TMediaPassword KNul, KPw1, KPw2, KPw3, KPw4, KPw5;
    1.79 +
    1.80 +#ifdef __WINS__
    1.81 +	const static TUint8 cid0[KMMCCIDLength] =	// "CID0ccccccccccc#";
    1.82 +		{
    1.83 +		'C',	'I',	'D',	'0',
    1.84 +		'c',	'c',	'c',	'c',
    1.85 +		'c',	'c',	'c',	'c',
    1.86 +		'c',	'c',	'c',	'#'
    1.87 +		};
    1.88 +#else
    1.89 +	const static TUint8 cid0[KMMCCIDLength] =	// big-endian, CID0
    1.90 +		{
    1.91 +		0x06,	0x00,	0x00,	0x31,
    1.92 +		0x36,	0x4d,	0x20,	0x20,
    1.93 +		0x20,	0x00,	0xb4,	0xff,
    1.94 +		0xff,	0xff,	0x63,	0xd9
    1.95 +		};
    1.96 +#endif
    1.97 +
    1.98 +// local test functions
    1.99 +
   1.100 +LOCAL_C void RemountMedia();
   1.101 +LOCAL_C void ClearControllerStore();
   1.102 +LOCAL_C void DeletePasswordFile();
   1.103 +LOCAL_C TInt CreateFile();
   1.104 +LOCAL_C TInt PWFileSize(TInt &aLength);
   1.105 +LOCAL_C TInt AccessDisk(TAccessType aAccess, const TDesC8 &aPWD);
   1.106 +LOCAL_C void TestHasPassword(TBool aIL, TBool aHP);
   1.107 +
   1.108 +LOCAL_C void TestLockUnlockR();
   1.109 +LOCAL_C void TestNullPasswords();
   1.110 +LOCAL_C void TestLockUnlock();
   1.111 +
   1.112 +LOCAL_C void TestPasswordStore();
   1.113 +LOCAL_C void TestWriteToDisk();
   1.114 +
   1.115 +LOCAL_C void TestFormat();
   1.116 +LOCAL_C void TestRemount();
   1.117 +
   1.118 +
   1.119 +/** force a remount on the removable media. */
   1.120 +
   1.121 +LOCAL_C void RemountMedia()
   1.122 +	{
   1.123 +#ifdef __WINS__
   1.124 +//	UserSvr::ForceRemountMedia(ERemovableMedia0);
   1.125 +	User::After(1 * 1500 * 1000);
   1.126 +#else
   1.127 +//	UserSvr::ForceRemountMedia(ERemovableMedia0);
   1.128 +//	User::After(1 * 1500 * 1000);
   1.129 +	test.Printf(_L("Remove and re-insert card.  Press \'z\' when finished.\n"));
   1.130 +	while (test.Getch() != 'z')
   1.131 +		{ /* empty. */ }
   1.132 +#endif
   1.133 +	}
   1.134 +
   1.135 +
   1.136 +/** ask the user to replace the current lockable media with another */
   1.137 +
   1.138 +LOCAL_C void ChangeMedia()
   1.139 +	{
   1.140 +#ifdef __WINS__
   1.141 +	test.Printf(_L("Press f4 whilst holding down f5\n"));
   1.142 +	test.Printf(_L("Press z when completed\n"));
   1.143 +	while(test.Getch() != 'z')
   1.144 +		{ /* empty */ }
   1.145 +#else
   1.146 +	test.Printf(_L("Remove and insert other card.  Press \'z\' when finished.\n"));
   1.147 +	while (test.Getch() != 'z')
   1.148 +		{ /* empty. */ }
   1.149 +#endif
   1.150 +	}
   1.151 +
   1.152 +
   1.153 +/** disable auto-unlock by clearing controller store */
   1.154 +
   1.155 +LOCAL_C void ClearControllerStore()
   1.156 +	{
   1.157 +	TBuf8<1> nulSt;
   1.158 +	test(TBusLocalDrive::WritePasswordData(nulSt) == KErrNone);// empty
   1.159 +	test(TBusLocalDrive::PasswordStoreLengthInBytes() == 0);
   1.160 +	}
   1.161 +
   1.162 +
   1.163 +/** delete the password store file.  Does not affect the auto-unlock mechanism. */
   1.164 +
   1.165 +LOCAL_C void DeletePasswordFile()
   1.166 +	{
   1.167 +	TInt r;
   1.168 +	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
   1.169 +	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
   1.170 +	do
   1.171 +		{
   1.172 +		User::After(100 * 1000);
   1.173 +		r = TheFs.Delete(mediaPWrdFile);
   1.174 +		} while (r == KErrInUse);
   1.175 +
   1.176 +	test(r == KErrNone || r == KErrNotFound);
   1.177 +	}
   1.178 +
   1.179 +
   1.180 +/** attempt to create a file on the removable media and return any error code */
   1.181 +
   1.182 +LOCAL_C TInt CreateFile()
   1.183 +	{
   1.184 +	RFile f;
   1.185 +	TInt r = f.Replace(TheFs, KTestFileName, EFileShareAny);
   1.186 +	if (r == KErrNone)
   1.187 +		f.Close();
   1.188 +	return r;
   1.189 +	}
   1.190 +
   1.191 +
   1.192 +/** get size of the media store file in bytes, and return any error code */
   1.193 +
   1.194 +LOCAL_C TInt PWFileSize(TInt &aLength)
   1.195 +	{
   1.196 +	TInt r;										// error code
   1.197 +
   1.198 +	// allow low priority background writer thread to start and finish
   1.199 +
   1.200 +	User::After(1 * 1000 * 1000);
   1.201 +
   1.202 +	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
   1.203 +	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
   1.204 +	RFile f;
   1.205 +	if ((r = f.Open(TheFs, mediaPWrdFile, EFileShareAny)) == KErrNone)
   1.206 +		{
   1.207 +		r = f.Size(aLength);
   1.208 +		f.Close();
   1.209 +		}
   1.210 +
   1.211 +	return r;
   1.212 +	}
   1.213 +
   1.214 +
   1.215 +/**
   1.216 + * write message to screen and attempt to access disk.  If card is locked then async
   1.217 + * notifier will be brought up from within file server.
   1.218 + */
   1.219 +
   1.220 +LOCAL_C TInt AccessDisk(TAccessType aAccess, const TDesC8 &aPWD)
   1.221 +	{
   1.222 +	TBuf16<16> pwd;
   1.223 +	pwd.Copy((const TUint16 *)aPWD.Ptr(), aPWD.Length() / 2);
   1.224 +
   1.225 +	if (aAccess == EAccessUnlock || aAccess == EAccessStore)
   1.226 +		test.Printf(_L("\"%S\": "), &pwd);
   1.227 +
   1.228 +	switch(aAccess)
   1.229 +		{
   1.230 +		case EAccessUnlock: 
   1.231 +			test.Printf(_L("unlock\n"));
   1.232 +			break;
   1.233 +
   1.234 +		case EAccessStore:
   1.235 +			test.Printf(_L("store\n"));
   1.236 +			break;
   1.237 +
   1.238 +		case EAccessCancel:
   1.239 +			test.Printf(_L("cancel\n"));
   1.240 +			break;
   1.241 +
   1.242 +		case EAccessWrongPw:
   1.243 +			test.Printf(_L("wrong\n"));
   1.244 +			break;
   1.245 +
   1.246 +		case EAccessNoNotifier:
   1.247 +			test.Printf(_L("** no notifier **\n"));
   1.248 +			break;
   1.249 +
   1.250 +		default:
   1.251 +			test(EFalse);
   1.252 +			break;
   1.253 +		}
   1.254 +
   1.255 +	TInt r = TheFile.Open(TheFs, KTestFileName, EFileShareAny);
   1.256 +	if(r == KErrNone)
   1.257 +		TheFile.Close();
   1.258 +
   1.259 +	return r;
   1.260 +	}
   1.261 +
   1.262 +
   1.263 +/**
   1.264 + * uses RFs::DriveInfo() to test if the card has a password.  Is checking
   1.265 + * that iHasPassword in TMMCCard is maintained properly.
   1.266 + */
   1.267 +
   1.268 +inline TBool bits_set(TUint aMsk, TUint aSel)
   1.269 +	{
   1.270 +	return (aMsk & aSel) == aSel;
   1.271 +	}
   1.272 +
   1.273 +LOCAL_C void TestHasPassword(TBool aIL, TBool aHP)
   1.274 +	{
   1.275 +	const TInt d = KDrv;
   1.276 +	RFs &fs = TheFs;
   1.277 +
   1.278 +	TDriveInfo di;
   1.279 +	test(fs.Drive(di, d) == KErrNone);
   1.280 +
   1.281 +#ifdef __WINS__
   1.282 +	test(aIL == bits_set(di.iMediaAtt, KMediaAttLocked | KMediaAttLockable | KMediaAttHasPassword));
   1.283 +	test(aHP == bits_set(di.iMediaAtt, KMediaAttLockable | KMediaAttHasPassword));
   1.284 +#else
   1.285 +	test(aIL == bits_set(di.iMediaAtt, KMediaAttLocked | KMediaAttHasPassword));
   1.286 +	test(aHP == bits_set(di.iMediaAtt, KMediaAttHasPassword));
   1.287 +#endif
   1.288 +	}
   1.289 +
   1.290 +
   1.291 +// -------- test functions --------
   1.292 +
   1.293 +
   1.294 +/**
   1.295 + * test return values of LockDrive(), UnlockDrive() and ClearPassword()
   1.296 + * without trying use the disk.  This tests that the functions return the
   1.297 + * same values as the TBusLocalDrive functions.
   1.298 + *
   1.299 + *			EPbPswdUnlock		EPbPswdLock			EPbPswdClear
   1.300 + *			right	wrong		right	wrong		right	wrong	
   1.301 + * locked	None	AccDen		AccDec	AccDen		AccDen	AccDen	
   1.302 + * unlocked	AldExst	AldExst		None	AccDec		None	AccDen	
   1.303 + *
   1.304 + * Locked means inaccessible, not just has password.
   1.305 + */
   1.306 +
   1.307 +LOCAL_C void TestLockUnlockR()
   1.308 +	{
   1.309 +	test.Start(_L("TestLockUnlockR"));
   1.310 +
   1.311 +	const TInt d = KDrv;
   1.312 +	RFs &fs = TheFs;
   1.313 +
   1.314 +	test.Next(_L("assign password"));
   1.315 +	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// assign test password
   1.316 +	TestHasPassword(EFalse, ETrue);
   1.317 +	RemountMedia();													// card is now locked
   1.318 +	TestHasPassword(ETrue, ETrue);
   1.319 +
   1.320 +	test.Next(_L("lock locked card"));
   1.321 +	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrAccessDenied);	// lock locked wrong
   1.322 +	TestHasPassword(ETrue, ETrue);
   1.323 +	test(fs.LockDrive(d, KPw1, KPw1, EFalse) == KErrAccessDenied);	// lock locked right
   1.324 +	TestHasPassword(ETrue, ETrue);
   1.325 +
   1.326 +	test.Next(_L("unlock locked card"));
   1.327 +	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrAccessDenied);		// unlock locked wrong
   1.328 +	TestHasPassword(ETrue, ETrue);
   1.329 +	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);				// unlock locked right
   1.330 +	TestHasPassword(EFalse, ETrue);
   1.331 +
   1.332 +	test.Next(_L("unlock unlocked card"));
   1.333 +	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrAlreadyExists);		// unlock unlocked right
   1.334 +	TestHasPassword(EFalse, ETrue);
   1.335 +	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrAlreadyExists);		// unlock unlocked wrong
   1.336 +	TestHasPassword(EFalse, ETrue);
   1.337 +
   1.338 +	test.Next(_L("lock unlocked card"));
   1.339 +	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrAccessDenied);	// lock unlocked wrong
   1.340 +	TestHasPassword(EFalse, ETrue);
   1.341 +	test(fs.LockDrive(d, KPw1, KPw1, EFalse) == KErrNone);			// lock unlocked right
   1.342 +	TestHasPassword(EFalse, ETrue);
   1.343 +
   1.344 +	test.Next(_L("clear unlocked card"));
   1.345 +	test(fs.ClearPassword(d, KPw2) == KErrAccessDenied);			// clear unlocked wrong
   1.346 +	TestHasPassword(EFalse, ETrue);
   1.347 +
   1.348 +	RemountMedia();
   1.349 +	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
   1.350 +	TestHasPassword(EFalse, ETrue);
   1.351 +	test(fs.ClearPassword(d, KPw1) == KErrNone);					// clear unlocked right
   1.352 +	TestHasPassword(EFalse, EFalse);
   1.353 +
   1.354 +	test.Next(_L("assign password"));
   1.355 +	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// assign test password
   1.356 +	TestHasPassword(EFalse, ETrue);
   1.357 +	RemountMedia();													// card is now locked
   1.358 +	TestHasPassword(ETrue, ETrue);
   1.359 +
   1.360 +	test.Next(_L("clear locked card"));
   1.361 +	test(fs.ClearPassword(d, KPw2) == KErrAccessDenied);			// clear locked wrong
   1.362 +	TestHasPassword(ETrue, ETrue);
   1.363 +	test(fs.ClearPassword(d, KPw1) == KErrAccessDenied);			// clear locked right
   1.364 +	TestHasPassword(ETrue, ETrue);
   1.365 +	
   1.366 +	test.Next(_L("clean up for following tests"));
   1.367 +	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
   1.368 +	TestHasPassword(EFalse, ETrue);
   1.369 +	test(fs.ClearPassword(d, KPw1) == KErrNone);
   1.370 +	TestHasPassword(EFalse, EFalse);
   1.371 +
   1.372 +	ClearControllerStore();
   1.373 +	DeletePasswordFile();
   1.374 +
   1.375 +	test.End();
   1.376 +	}
   1.377 +
   1.378 +
   1.379 +/** test the special cases where null passwords are used. */
   1.380 +
   1.381 +LOCAL_C void TestNullPasswords()
   1.382 +	{
   1.383 +	test.Start(_L("TestNullPasswords"));
   1.384 +
   1.385 +	test.Next(_L("card has no password"));
   1.386 +	test(TheFs.LockDrive(KDrv, KNul, KNul, ETrue) == KErrAccessDenied);
   1.387 +	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAlreadyExists);
   1.388 +	test(TheFs.ClearPassword(KDrv, KNul) == KErrAccessDenied);
   1.389 +
   1.390 +	test.Next(_L("card has password and is unlocked"));
   1.391 +	test(TheFs.LockDrive(KDrv, KNul, KPw1, ETrue) == KErrNone);
   1.392 +	RemountMedia();
   1.393 +	test(TheFs.LockDrive(KDrv, KNul, KNul, ETrue) == KErrAccessDenied);
   1.394 +	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAlreadyExists);
   1.395 +	test(TheFs.ClearPassword(KDrv, KNul) == KErrAccessDenied);
   1.396 +	test(TheFs.ClearPassword(KDrv, KPw1) == KErrNone);
   1.397 +
   1.398 +	test.Next(_L("unlock with null disallowed"));
   1.399 +	RemountMedia();
   1.400 +	test(TheFs.UnlockDrive(KDrv, KNul, ETrue) == KErrAccessDenied);
   1.401 +	test.Next(_L("check can still use card"));
   1.402 +	test(TheFs.LockDrive(KDrv, KNul, KPw1, ETrue) == KErrNone);	// check can still use
   1.403 +	test(TheFs.ClearPassword(KDrv, KPw1) == KErrNone);
   1.404 +
   1.405 +	test.Next(_L("clean up for following tests"));
   1.406 +	ClearControllerStore();
   1.407 +	DeletePasswordFile();
   1.408 +
   1.409 +	test.End();
   1.410 +	}
   1.411 +
   1.412 +
   1.413 +/** test LockDrive(), UnlockDrive() and ClearPassword() with locked media notifier */
   1.414 +
   1.415 +LOCAL_C void TestLockUnlock()
   1.416 +	{
   1.417 +	test.Start(_L("TestLockUnlock"));
   1.418 +
   1.419 +	const TInt d = KDrv;
   1.420 +	RFs &fs = TheFs;
   1.421 +
   1.422 +	test.Next(_L("create test file"));
   1.423 +	test(CreateFile() == KErrNone);
   1.424 +
   1.425 +	test.Next(_L("lock card and don't store"));
   1.426 +	test(fs.LockDrive(d, KNul, KPw1, EFalse) == KErrNone);			// unlocked KPw1
   1.427 +	TestHasPassword(EFalse, ETrue);
   1.428 +
   1.429 +	test.Next(_L("access with right pwd"));
   1.430 +	RemountMedia();													// locked KPw1
   1.431 +	TestHasPassword(ETrue, ETrue);
   1.432 +	test(AccessDisk(EAccessUnlock, KPw1) == KErrNone);
   1.433 +	TestHasPassword(EFalse, ETrue);
   1.434 +
   1.435 +	test.Next(_L("access with cancelled pwd"));
   1.436 +	RemountMedia();													// locked KPw1	
   1.437 +	TestHasPassword(ETrue, ETrue);
   1.438 +	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
   1.439 +	TestHasPassword(ETrue, ETrue);
   1.440 +
   1.441 +	test.Next(_L("access with new stored pwd"));
   1.442 +	RemountMedia();
   1.443 +	TestHasPassword(ETrue, ETrue);
   1.444 +	test(AccessDisk(EAccessStore, KPw1) == KErrNone);				// unlocked KPw1
   1.445 +	TestHasPassword(EFalse, ETrue);
   1.446 +
   1.447 +	test.Next(_L("access with stored pwd"));
   1.448 +	RemountMedia();													// unlocked KPw1 (use store)
   1.449 +	TestHasPassword(EFalse, ETrue);
   1.450 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.451 +	TestHasPassword(EFalse, ETrue);
   1.452 +
   1.453 +	test.Next(_L("lock wrong pwd"));
   1.454 +	test(fs.LockDrive(d, KPw3, KPw2, EFalse) == KErrAccessDenied);	// unlocked KPw1
   1.455 +	TestHasPassword(EFalse, ETrue);
   1.456 +
   1.457 +	test.Next(_L("change pwd"));
   1.458 +	test(fs.LockDrive(d, KPw1, KPw2, EFalse) == KErrNone);			// unlocked KPw2 (rem from store)
   1.459 +	TestHasPassword(EFalse, ETrue);
   1.460 +
   1.461 +	test.Next(_L("mapping removed from store"));
   1.462 +	RemountMedia();
   1.463 +	test(AccessDisk(EAccessUnlock, KPw2) == KErrNone);
   1.464 +	TestHasPassword(EFalse, ETrue);
   1.465 +
   1.466 +	test.Next(_L("wrong password"));
   1.467 +	RemountMedia();													// locked KPw2
   1.468 +	TestHasPassword(ETrue, ETrue);
   1.469 +	test(AccessDisk(EAccessWrongPw, KNul) == KErrLocked);
   1.470 +	TestHasPassword(ETrue, ETrue);
   1.471 +
   1.472 +	test.Next(_L("unlocked card"));
   1.473 +	test(fs.UnlockDrive(d, KPw2, ETrue) == KErrNone);				// unlocked KPw2 (add to store)
   1.474 +	TestHasPassword(EFalse, ETrue);
   1.475 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);			// before power down
   1.476 +	TestHasPassword(EFalse, ETrue);
   1.477 +
   1.478 +	test.Next(_L("clear wrong pwd"));
   1.479 +	test(fs.ClearPassword(d, KPw1) == KErrAccessDenied);			// KPw2 backup kept
   1.480 +	TestHasPassword(EFalse, ETrue);
   1.481 +	RemountMedia();													// unlocked KPw2 (use store)
   1.482 +	TestHasPassword(EFalse, ETrue);
   1.483 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.484 +	TestHasPassword(EFalse, ETrue);
   1.485 +
   1.486 +	test.Next(_L("change pwd"));
   1.487 +	test(fs.LockDrive(d, KPw2, KPw1, EFalse) == KErrNone);			// locked KPw1 (rem from store)
   1.488 +	TestHasPassword(EFalse, ETrue);
   1.489 +	RemountMedia();
   1.490 +	TestHasPassword(ETrue, ETrue);
   1.491 +	test(AccessDisk(EAccessUnlock, KPw1) == KErrNone);
   1.492 +	TestHasPassword(EFalse, ETrue);
   1.493 +
   1.494 +	test.Next(_L("cancelled pwd"));
   1.495 +	RemountMedia();
   1.496 +	TestHasPassword(ETrue, ETrue);
   1.497 +	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
   1.498 +	TestHasPassword(ETrue, ETrue);
   1.499 +
   1.500 +	test.Next(_L("clean up for following tests"));
   1.501 +	test(fs.UnlockDrive(d, KPw1, EFalse) == KErrNone);
   1.502 +	TestHasPassword(EFalse, ETrue);
   1.503 +	test(fs.ClearPassword(KDrv, KPw1) == KErrNone);
   1.504 +	TestHasPassword(EFalse, EFalse);
   1.505 +	RemountMedia();
   1.506 +	TestHasPassword(EFalse, EFalse);
   1.507 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.508 +	TestHasPassword(EFalse, EFalse);
   1.509 +	ClearControllerStore();
   1.510 +	DeletePasswordFile();
   1.511 +
   1.512 +	test.End();
   1.513 +	}
   1.514 +
   1.515 +
   1.516 +/**
   1.517 + * test password store by accessing disk in various states.  Ensures password
   1.518 + * store file is right size after each operation.  More detailed checking of
   1.519 + * the TBusLocalDrive::ReadPasswordData() ouput is done in t_pwstr.
   1.520 + */
   1.521 +
   1.522 +LOCAL_C void TestPasswordStore()
   1.523 +	{
   1.524 +	test.Start(_L("TestPasswordStore"));
   1.525 +
   1.526 +//	TInt r;										// general error code
   1.527 +	RFs &fs = TheFs;
   1.528 +	const TInt d = KDrv;
   1.529 +
   1.530 +	test.Next(_L("create test file on first media"));
   1.531 +	test(CreateFile() == KErrNone);
   1.532 +
   1.533 +	test.Next(_L("lock card and don't store"));
   1.534 +	test(fs.LockDrive(d, KNul, KPw5, EFalse) == KErrNone);	// {}
   1.535 +	TestHasPassword(EFalse, ETrue);
   1.536 +	TInt size1;
   1.537 +	test(PWFileSize(size1) == KErrNotFound);
   1.538 +
   1.539 +	test.Next(_L("lock card and store"));
   1.540 +	test(fs.LockDrive(d, KPw5, KPw3, ETrue) == KErrNone);	// {0 |-> 3}
   1.541 +	TestHasPassword(EFalse, ETrue);
   1.542 +	test.Next(_L("access media1 with stored password"));
   1.543 +
   1.544 +	RemountMedia();
   1.545 +	TestHasPassword(EFalse, ETrue);
   1.546 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.547 +	TestHasPassword(EFalse, ETrue);
   1.548 +
   1.549 +	test.Next(_L("test password file exists"));
   1.550 +	test(PWFileSize(size1) == KErrNone);
   1.551 +	test(size1 == 16 + 4 + KPw3.Length());
   1.552 +
   1.553 +	test.Next(_L("change cards and add second password to store"));
   1.554 +	ChangeMedia();
   1.555 +
   1.556 +	test.Next(_L("create test file on second media"));
   1.557 +	test(CreateFile() == KErrNone);
   1.558 +
   1.559 +	test.Next(_L("lock card and store"));
   1.560 +	test(fs.LockDrive(d, KNul, KPw4, ETrue) == KErrNone);	// {0 |-> 3, 1 |-> 4}
   1.561 +	TestHasPassword(EFalse, ETrue);
   1.562 +
   1.563 +	TInt size2;
   1.564 +	test(PWFileSize(size2) == KErrNone);
   1.565 +	test(size2 == 16 + 4 + KPw3.Length() + 16 + 4 + KPw4.Length());
   1.566 +
   1.567 +	test.Next(_L("access media2 with stored password"));
   1.568 +	RemountMedia();
   1.569 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.570 +	TestHasPassword(EFalse, ETrue);
   1.571 +
   1.572 +	test.Next(_L("change password"));
   1.573 +	test(fs.LockDrive(d, KPw4, KPw5, ETrue) == KErrNone);	// {0 |-> 3, 1 |-> 5}
   1.574 +	TestHasPassword(EFalse, ETrue);
   1.575 +
   1.576 +	TInt size3;
   1.577 +	test(PWFileSize(size3) == KErrNone);
   1.578 +	test(size3 == 16 + 4 + KPw3.Length() + 16 + 4 + KPw5.Length());
   1.579 +
   1.580 +	test.Next(_L("access media1 with stored password"));
   1.581 +	ChangeMedia();
   1.582 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.583 +
   1.584 +	test.Next(_L("remove password from media 1"));
   1.585 +	test(fs.ClearPassword(d, KPw3) == KErrNone);			// {1 |-> 5}	- 0 |-> 3
   1.586 +	TestHasPassword(EFalse, EFalse);
   1.587 +	test(PWFileSize(size3) == KErrNone);
   1.588 +	test(size3 == 16 + 4 + KPw5.Length());
   1.589 +
   1.590 +	test.Next(_L("clean up for following tests"));
   1.591 +	ChangeMedia();
   1.592 +	test(fs.ClearPassword(d, KPw5) == KErrNone);			// {}			- 1 |-> 5
   1.593 +	TestHasPassword(EFalse, EFalse);
   1.594 +	test(PWFileSize(size3) == KErrNone);
   1.595 +	test(size3 == 0);
   1.596 +	test.Printf(_L("replace original card\n"));
   1.597 +	ChangeMedia();											// use original card
   1.598 +	ClearControllerStore();
   1.599 +	DeletePasswordFile();
   1.600 +
   1.601 +	test.End();
   1.602 +	}
   1.603 +
   1.604 +
   1.605 +/**
   1.606 + * check the spin off delayed writer thread can work when many requests are queued.
   1.607 + * 
   1.608 + * The background writer runs at EPriorityMuchLess, so it will not be executed until
   1.609 + * this thread sleeps.
   1.610 + */
   1.611 +
   1.612 +LOCAL_C void TestWriteToDisk()
   1.613 +	{
   1.614 +	test.Start(_L("TestWriteToDisk"));
   1.615 +
   1.616 +	TInt r;										// error code
   1.617 +	TBuf8<1> noMappings;
   1.618 +
   1.619 +	test.Next(_L("Queuing threads"));
   1.620 +	test.Printf(_L("Queuing 100 writes\n"));
   1.621 +
   1.622 +	const TInt KQueueCnt = 100;
   1.623 +
   1.624 +	TInt i;
   1.625 +	TMediaPassword oldPswd;						// empty - no password at start
   1.626 +	TMediaPassword newPswd;
   1.627 +	for (i = 0; i < KQueueCnt; ++i)
   1.628 +		{
   1.629 +		newPswd.Fill(i, KMaxMediaPassword);
   1.630 +		test(TheFs.LockDrive(KDrv, oldPswd, newPswd, ETrue) == KErrNone);
   1.631 +		oldPswd.Copy(newPswd);
   1.632 +		}
   1.633 +
   1.634 +	test.Printf(_L("Waiting 20 seconds for threads to complete\n"));
   1.635 +	User::After(20 * 1000 * 1000);
   1.636 +
   1.637 +	// check password file contains the last writing.
   1.638 +
   1.639 +	test.Next(_L("check password file contains last writing"));
   1.640 +	const TInt KFileLen = 16 + sizeof(TUint32) + KMaxMediaPassword;
   1.641 +	RFile f;
   1.642 +	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
   1.643 +	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
   1.644 +	test(f.Open(TheFs, mediaPWrdFile, EFileShareExclusive | EFileStream | EFileRead) == KErrNone);
   1.645 +	TInt sz;
   1.646 +	test(f.Size(sz) == KErrNone);
   1.647 +	test(sz == KFileLen);
   1.648 +
   1.649 +	TBuf8<KFileLen> chkBuf;
   1.650 +	test(f.Read(chkBuf, KFileLen) == KErrNone);
   1.651 +	// defer checking buffer contents until after password cleared so not left
   1.652 +	// with locked card if test fails.
   1.653 +	f.Close();
   1.654 +
   1.655 +	test(TheFs.ClearPassword(KDrv, oldPswd) == KErrNone);
   1.656 +	User::After(1 * 1000 * 1000);				// wait to finish writing
   1.657 +	
   1.658 +	r = TheFs.Delete(mediaPWrdFile);
   1.659 +	test(r == KErrNone || r == KErrNotFound);
   1.660 +	test(TBusLocalDrive::WritePasswordData(noMappings) == KErrNone);
   1.661 +
   1.662 +	// check contents of password file correspond to last written buffer.
   1.663 +
   1.664 +	test(chkBuf.Length() == KFileLen);
   1.665 +	test(chkBuf.Mid(0, KMMCCIDLength) == TPtrC8(cid0, KMMCCIDLength));
   1.666 +	const TUint32 len = TMMC::BigEndian32(chkBuf.Mid(KMMCCIDLength, sizeof(TUint32)).Ptr());
   1.667 +	test(len == TInt(KMaxMediaPassword));
   1.668 +	test(chkBuf.Mid(KMMCCIDLength + sizeof(TUint32), KMMCCIDLength) == oldPswd);
   1.669 +
   1.670 +	test.Next(_L("clean up for following tests"));
   1.671 +	ClearControllerStore();
   1.672 +	DeletePasswordFile();
   1.673 +
   1.674 +	test.End();
   1.675 +	}
   1.676 +
   1.677 +
   1.678 +/** test unable to format locked card */
   1.679 +
   1.680 +LOCAL_C void TestFormat()
   1.681 +	{
   1.682 +	test.Start(_L("TestFormat"));
   1.683 +
   1.684 +//	TInt r;										// error code
   1.685 +	RFs &fs = TheFs;
   1.686 +	const TInt d = KDrv;
   1.687 +
   1.688 +	TBuf<3> bfDrv;
   1.689 +	bfDrv.Append(KDrvLtr);
   1.690 +	_LIT(KBP, ":\\");
   1.691 +	bfDrv.Append(KBP);
   1.692 +
   1.693 +	test.Next(_L("create test file"));
   1.694 +	test(CreateFile() == KErrNone);
   1.695 +
   1.696 +	test.Next(_L("lock drive"));
   1.697 +	test(TheFs.LockDrive(KDrv, KNul, KPw2, EFalse) == KErrNone);
   1.698 +	RemountMedia();
   1.699 +
   1.700 +	test.Next(_L("unlock card and don't store"));
   1.701 +	test(AccessDisk(EAccessUnlock, KPw2) == KErrNone);
   1.702 +
   1.703 +	// format unlocked drive
   1.704 +	test.Next(_L("format unlocked card"));
   1.705 +	RFormat fmt;
   1.706 +	TInt count;
   1.707 +	test(fmt.Open(fs, bfDrv, EHighDensity, count) == KErrNone);
   1.708 +	while (count > 0)
   1.709 +		{
   1.710 +		test.Printf(_L("\rfmt:%d  "), count);
   1.711 +		test(fmt.Next(count) == KErrNone);
   1.712 +		}
   1.713 +	test.Printf(_L("\n"));
   1.714 +	fmt.Close();
   1.715 +
   1.716 +	test.Next(_L("format locked media"));
   1.717 +	RemountMedia();								// locked KPw2
   1.718 +	test.Printf(_L("Notifier: No password required. Press cancel. \n"));
   1.719 +	test(fmt.Open(fs, bfDrv, EHighDensity, count) == KErrLocked);
   1.720 +
   1.721 +	test.Next(_L("unlock locked card"));
   1.722 +	test(fs.UnlockDrive(d, KPw2, EFalse) == KErrNone);
   1.723 +	test(fs.ClearPassword(d, KPw2) == KErrNone);
   1.724 +
   1.725 +	test.Next(_L("create test file"));
   1.726 +	test(CreateFile() == KErrNone);
   1.727 +
   1.728 +	test.Next(_L("remount media, check not locked"));
   1.729 +	RemountMedia();
   1.730 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.731 +
   1.732 +	test.Next(_L("clean up for following tests"));
   1.733 +	ClearControllerStore();
   1.734 +	DeletePasswordFile();
   1.735 +
   1.736 +	test.End();
   1.737 +	}
   1.738 +
   1.739 +
   1.740 +/** do media change with file open */
   1.741 +
   1.742 +LOCAL_C void TestRemount()
   1.743 +	{
   1.744 +	test.Start(_L("TestRemount"));
   1.745 +
   1.746 +//	TInt r;										// general error code
   1.747 +	RFs &fs = TheFs;
   1.748 +	const TInt d = KDrv;
   1.749 +
   1.750 +	test.Next(_L("create file"));
   1.751 +	TFileName fn;
   1.752 +	fn.Append(KDrvLtr);
   1.753 +	_LIT(KFN, ":\\openfile");
   1.754 +	fn.Append(KFN);
   1.755 +	test.Printf(_L("fn = \"%S\"\n"), &fn);
   1.756 +
   1.757 +	RFile f;
   1.758 +	test(f.Replace(fs, fn, EFileShareAny) == KErrNone);
   1.759 +
   1.760 +	test.Next(_L("lock card"));
   1.761 +	test(fs.LockDrive(d, KNul, KPw5, EFalse) == KErrNone);
   1.762 +
   1.763 +	test.Next(_L("access with right pwd"));
   1.764 +	RemountMedia();
   1.765 +	test(AccessDisk(EAccessUnlock, KPw5) == KErrNone);
   1.766 +
   1.767 +	test.Next(_L("access with wrong pwd"));
   1.768 +	RemountMedia();
   1.769 +	test(AccessDisk(EAccessWrongPw, KNul) == KErrLocked);
   1.770 +
   1.771 +	test.Next(_L("access with cancelled pwd"));
   1.772 +	RemountMedia();
   1.773 +	test(AccessDisk(EAccessCancel, KNul) == KErrLocked);
   1.774 +
   1.775 +	test.Next(_L("clear password"));
   1.776 +	test(fs.UnlockDrive(d, KPw5, EFalse) == KErrNone);
   1.777 +	test(fs.ClearPassword(d, KPw5) == KErrNone);
   1.778 +
   1.779 +	test(AccessDisk(EAccessNoNotifier, KNul) == KErrNone);
   1.780 +
   1.781 +	test.Next(_L("close and delete file"));
   1.782 +	f.Close();
   1.783 +	test(fs.Delete(fn) == KErrNone);
   1.784 +
   1.785 +	test.Next(_L("clean up for following tests"));
   1.786 +	ClearControllerStore();
   1.787 +	DeletePasswordFile();
   1.788 +
   1.789 +	test.End();
   1.790 +	}
   1.791 +
   1.792 +
   1.793 +/**
   1.794 + * entry point.  Displays instructions; sets up heap checking; sets up
   1.795 + * file server session and calls individual tests.  Mounts drive for WINS.
   1.796 + */
   1.797 +
   1.798 +_LIT(KSDProtDriverFileName,"MEDSDP");
   1.799 +_LIT(KSDProtDriverObjName,"Media.SdP");
   1.800 +GLDEF_C TInt E32Main()
   1.801 +    {
   1.802 + 	__UHEAP_MARK;
   1.803 +
   1.804 +	test.Title();
   1.805 +	test.Start(_L("Starting T_MMC"));
   1.806 +
   1.807 +	test.Printf(_L("The notifier should only apear along with user instructions.\n"));
   1.808 +	test.Printf(_L("The test has failed otherwise.\n"));
   1.809 +	test.Printf(_L("\n"));
   1.810 +	test.Printf(_L("Use return to simulate unlock button on notifier\n"));
   1.811 +	test.Printf(_L("Use space to simulate store button on notifier\n"));
   1.812 +	test.Printf(_L("Use escape to simulate cancel button on notifier\n"));
   1.813 +	test.Printf(_L("Press a key to continue ...\n\n"));
   1.814 +	test.Getch();
   1.815 +
   1.816 +	test(TheFs.Connect() == KErrNone);
   1.817 +
   1.818 +	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
   1.819 +	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
   1.820 +	TParsePtrC ppc(mediaPWrdFile);
   1.821 +	TInt r = TheFs.MkDir(ppc.DriveAndPath());
   1.822 +	test(r == KErrNone || r == KErrAlreadyExists);
   1.823 +
   1.824 +	// The media driver for the protected area of the SD card uses mount
   1.825 +	// info. This is also used for password unlocking by the MMC media driver.
   1.826 +	// Due to a temporary problem with the way mount info. is assigned to DMedia
   1.827 +	// objects which results in a conflict between the two drivers, unload
   1.828 +	// the protected area SD card driver for duration of test.
   1.829 +	TheFs.RemountDrive(KDrv,NULL,KLocDrvRemountPostponeRemount);
   1.830 +	User::FreePhysicalDevice(KSDProtDriverObjName);
   1.831 +
   1.832 +	test(TheFs.SetSessionPath(KSessionPath) == KErrNone);
   1.833 +	
   1.834 +	// initialize the TMediaPassword data
   1.835 +	KNul.Copy(reinterpret_cast<const TUint8 *>(L""), 0);
   1.836 +	KPw1.Copy(reinterpret_cast<const TUint8 *>(L"b"), 2);
   1.837 +	KPw2.Copy(reinterpret_cast<const TUint8 *>(L"cd"), 4);
   1.838 +	KPw3.Copy(reinterpret_cast<const TUint8 *>(L"def"), 6);
   1.839 +	KPw4.Copy(reinterpret_cast<const TUint8 *>(L"efgh"), 8);
   1.840 +	KPw5.Copy(reinterpret_cast<const TUint8 *>(L"fghij"), 10);
   1.841 +
   1.842 +	test.Next(_L("calling ClearControllerStore"));
   1.843 +	ClearControllerStore();
   1.844 +	test.Next(_L("calling DeletePasswordFile"));
   1.845 +	DeletePasswordFile();
   1.846 +
   1.847 +	test.Next(_L("calling TestLockUnlockR"));
   1.848 +	TestLockUnlockR();
   1.849 +	test.Next(_L("calling TestNullPasswords"));
   1.850 +	TestNullPasswords();
   1.851 +	test.Next(_L("calling TestLockUnlock"));
   1.852 +	TestLockUnlock();
   1.853 +
   1.854 +	test.Next(_L("calling TestPasswordStore"));
   1.855 +	TestPasswordStore();
   1.856 +	test.Next(_L("calling TestWriteToDisk"));
   1.857 +	TestWriteToDisk();
   1.858 +
   1.859 +	test.Next(_L("calling TestFormat"));
   1.860 +	TestFormat();
   1.861 +	test.Next(_L("calling TestRemount"));
   1.862 +	TestRemount();
   1.863 +
   1.864 +	// Restore SD Card protected area media driver
   1.865 +	User::LoadPhysicalDevice(KSDProtDriverFileName);
   1.866 +
   1.867 +	TheFs.Close();
   1.868 +	test.End();
   1.869 +	test.Close();
   1.870 +
   1.871 +	__UHEAP_MARKEND;
   1.872 +
   1.873 +	return KErrNone;
   1.874 +    }
   1.875 +