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