os/kernelhwsrv/kerneltest/f32test/server/t_pwstr.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_pwstr.cpp
sl@0
    15
// Tests peripheral bus controller password store.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
//#include <p32mmc.h>
sl@0
    20
sl@0
    21
#include <e32test.h>
sl@0
    22
#include <f32fsys.h>
sl@0
    23
#include <e32def.h>
sl@0
    24
#include <e32def_private.h>
sl@0
    25
#include <e32hal.h>
sl@0
    26
sl@0
    27
// define this macro to autodetect card re-insertion
sl@0
    28
#define __AUTO_DETECT_MEDIA_CHANGE__
sl@0
    29
sl@0
    30
const TUint KMMCCIDLength=16;
sl@0
    31
sl@0
    32
class TCID
sl@0
    33
	{
sl@0
    34
public:
sl@0
    35
	inline TCID() {}					// Default constructor
sl@0
    36
	inline TCID(const TUint8*);
sl@0
    37
	inline TCID& operator=(const TCID&);
sl@0
    38
	inline TCID& operator=(const TUint8*);
sl@0
    39
	inline TBool operator==(const TCID&) const;
sl@0
    40
	inline TBool operator==(const TUint8*) const;
sl@0
    41
	inline void Copy(TUint8*) const;		// Copies big endian 16 bytes CID
sl@0
    42
	inline TUint8 At(TUint anIndex) const;	// Byte from CID at anIndex
sl@0
    43
//private:
sl@0
    44
public:
sl@0
    45
	TUint8 iData[KMMCCIDLength];		// Big endian 128 bit bitfield representing CID
sl@0
    46
	};
sl@0
    47
sl@0
    48
class TMMC
sl@0
    49
	{
sl@0
    50
public:
sl@0
    51
	static inline TUint32 BigEndian32(const TUint8*);
sl@0
    52
	static inline void BigEndian4Bytes(TUint8* aPtr, TUint32 aVal);
sl@0
    53
	};
sl@0
    54
sl@0
    55
sl@0
    56
//	--------  class TCID  --------
sl@0
    57
sl@0
    58
inline TCID::TCID(const TUint8* aPtr)
sl@0
    59
	{memcpy(&iData[0], aPtr, KMMCCIDLength);}
sl@0
    60
sl@0
    61
inline TCID& TCID::operator=(const TCID& aCID)
sl@0
    62
	{memcpy(&iData[0], &aCID.iData[0], KMMCCIDLength); return(*this);}
sl@0
    63
sl@0
    64
inline TCID& TCID::operator=(const TUint8* aPtr)
sl@0
    65
	{memcpy(&iData[0], aPtr, KMMCCIDLength); return(*this);}
sl@0
    66
sl@0
    67
inline TBool TCID::operator==(const TCID& aCID) const
sl@0
    68
	{return(memcompare(&iData[0],KMMCCIDLength,&aCID.iData[0],KMMCCIDLength)==0);}
sl@0
    69
sl@0
    70
inline TBool TCID::operator==(const TUint8* aPtr) const
sl@0
    71
	{return(memcompare(&iData[0],KMMCCIDLength,aPtr,KMMCCIDLength)==0);}
sl@0
    72
sl@0
    73
inline void TCID::Copy(TUint8* aPtr) const
sl@0
    74
	{memcpy(aPtr, &iData[0], KMMCCIDLength);}
sl@0
    75
sl@0
    76
inline TUint8 TCID::At(TUint anIndex) const
sl@0
    77
	{return(iData[KMMCCIDLength-1-anIndex]);}
sl@0
    78
sl@0
    79
sl@0
    80
inline TUint32 TMMC::BigEndian32(const TUint8* aPtr)
sl@0
    81
	{return( (aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | (aPtr[3]) );}
sl@0
    82
sl@0
    83
inline void TMMC::BigEndian4Bytes(TUint8* aPtr, TUint32 aVal)
sl@0
    84
	{
sl@0
    85
	aPtr[0] = (TUint8)((aVal >> 24) & 0xFF);
sl@0
    86
	aPtr[1] = (TUint8)((aVal >> 16) & 0xFF);
sl@0
    87
	aPtr[2] = (TUint8)((aVal >> 8) & 0xFF);
sl@0
    88
	aPtr[3] = (TUint8)(aVal & 0xFF);
sl@0
    89
	}
sl@0
    90
sl@0
    91
// Static data.
sl@0
    92
sl@0
    93
LOCAL_D RTest test(_L("T_PWSTR"));
sl@0
    94
sl@0
    95
LOCAL_D TBusLocalDrive TBLD;
sl@0
    96
LOCAL_D TBool TBLDChangedFlag;
sl@0
    97
sl@0
    98
LOCAL_D TInt TBLDNum = -1; 	// Change this to specify the drive under test
sl@0
    99
							// e.g. for the lm_pana board when fitted to the
sl@0
   100
							// integrator, TBLDNum should be set to 3.
sl@0
   101
sl@0
   102
LOCAL_D TInt RFsDNum = -1;	// File Server Drive number
sl@0
   103
sl@0
   104
struct TTestMapping
sl@0
   105
	{
sl@0
   106
	TInt iCIDIdx;							// index in CID
sl@0
   107
	TInt iPWDIdx;							// index in PWD
sl@0
   108
	};
sl@0
   109
sl@0
   110
const TInt KMaxLengthOfStoreMapping = KMMCCIDLength + sizeof(TInt32) + KMaxMediaPassword;
sl@0
   111
// EMaxPasswordLength is max size of the password store descriptor
sl@0
   112
// (which actually contains multiple mappings of CID and passwords)
sl@0
   113
const TInt KMaxNumOfStoreEntries= TPasswordStore::EMaxPasswordLength/KMaxLengthOfStoreMapping;
sl@0
   114
sl@0
   115
const TInt KPWDCnt(4);
sl@0
   116
LOCAL_C TMediaPassword *PWDs[KPWDCnt];
sl@0
   117
sl@0
   118
//Allocate enough unique CIDs to be able to overflow the store 
sl@0
   119
const TInt KCIDCnt(KMaxNumOfStoreEntries+1);
sl@0
   120
LOCAL_C TCID *CIDs[KCIDCnt];
sl@0
   121
sl@0
   122
//Let the descriptor be one mapping longer than allowed by the password
sl@0
   123
//store to test overflowing it.
sl@0
   124
const TInt KMaxPersistentStore(TPasswordStore::EMaxPasswordLength+KMaxLengthOfStoreMapping);
sl@0
   125
typedef TBuf8<KMaxPersistentStore> TPersistentStore;
sl@0
   126
LOCAL_C TInt mapSizes[KCIDCnt][KPWDCnt];
sl@0
   127
sl@0
   128
// Static function prototypes.
sl@0
   129
sl@0
   130
LOCAL_C void AllocateTestData();
sl@0
   131
LOCAL_C void DeleteTestData();
sl@0
   132
sl@0
   133
LOCAL_C void AllocateCIDs();
sl@0
   134
LOCAL_C void DeleteCIDs();
sl@0
   135
sl@0
   136
LOCAL_C void AllocatePasswords();
sl@0
   137
LOCAL_C void DeletePasswords();
sl@0
   138
sl@0
   139
LOCAL_C void SetUpMapSizes();
sl@0
   140
sl@0
   141
LOCAL_C void AddMapping(TDes8 &aSt, const TCID *aCID, const TMediaPassword *aPWD);
sl@0
   142
LOCAL_C void DumpStore(const TDesC &aName, const TDesC8 &aSt);
sl@0
   143
LOCAL_C TBool StoresEqual(const TDesC8 &aSt0, const TDesC8 &aSt1);
sl@0
   144
LOCAL_C TBool IsStoreValid(const TDesC8 &aSt);
sl@0
   145
LOCAL_C void PrintCID(const TCID &aCID);
sl@0
   146
LOCAL_C void ParseStore(const TDesC8 &aStore, CArrayFixSeg<TTestMapping> *aMP);
sl@0
   147
LOCAL_C void TestStaticStore();
sl@0
   148
sl@0
   149
LOCAL_C void RemountMedia();
sl@0
   150
LOCAL_C void AttemptToUnlock(TMediaPassword &aPWD, TBool aStore = EFalse);
sl@0
   151
LOCAL_C void TestLockUnlock();
sl@0
   152
LOCAL_C void TestElidePasswords();
sl@0
   153
LOCAL_C void TestNullPasswords();
sl@0
   154
LOCAL_C void TestControllerStore();
sl@0
   155
sl@0
   156
LOCAL_C TInt AccessDisk();
sl@0
   157
LOCAL_C void TestAutoUnlock();
sl@0
   158
sl@0
   159
LOCAL_C void RunTests();
sl@0
   160
sl@0
   161
// Test data
sl@0
   162
sl@0
   163
sl@0
   164
LOCAL_C void AllocateCIDs()
sl@0
   165
//
sl@0
   166
// Allocates a set of static global media identifiers on the heap.
sl@0
   167
// The identifiers are all exactly 128 bits.
sl@0
   168
// Because the test uses only one card, CIDs 1 through 3 can be arbitrary
sl@0
   169
// (they are just used to construct store data.)
sl@0
   170
// 
sl@0
   171
// Format is "CIDXccccccccccc#", where X is the ASCII digit for the index.
sl@0
   172
// The CID is stored internally in big endian format.
sl@0
   173
// TCID::At(TInt i) returns the i'th byte, i.e. cid >> (i * 8) & 0xff, which
sl@0
   174
// is the opposite order to the way they are stored in the array.
sl@0
   175
// CIDs are formed in the same way in pp_mmc.cpp, the WINS ASSP layer.
sl@0
   176
//
sl@0
   177
// For actual card tests, CIDs[0] must correspond to the card's actual CID.
sl@0
   178
//
sl@0
   179
	{
sl@0
   180
sl@0
   181
#if 1
sl@0
   182
	static TUint8 ht0[KMMCCIDLength] =			// CID0
sl@0
   183
		{
sl@0
   184
		0x06,	0x00,	0x00,	0x31,
sl@0
   185
		0x36,	0x4d,	0x20,	0x20,
sl@0
   186
		0x20,	0x00,	0xb4,	0xff,
sl@0
   187
		0xff,	0xff,	0x63,	0xd9
sl@0
   188
		};
sl@0
   189
#else
sl@0
   190
	static TUint8 ht0[KMMCCIDLength] =			// BPC2
sl@0
   191
		{
sl@0
   192
		0x06,	0x00,	0x00,	0x31,
sl@0
   193
		0x36,	0x4d,	0x20,	0x20,
sl@0
   194
		0x20,	0x00,	0x89,	0xff,
sl@0
   195
		0xff,	0xff,	0x63,	0xa7
sl@0
   196
		};
sl@0
   197
#endif
sl@0
   198
sl@0
   199
	test.Start(_L("AllocateCIDs"));
sl@0
   200
sl@0
   201
	TInt i;
sl@0
   202
	for (i = 0; i < KCIDCnt; i++)
sl@0
   203
		{
sl@0
   204
		TUint8 bf[KMMCCIDLength];
sl@0
   205
		TUint j;
sl@0
   206
		bf[0] = 'C';
sl@0
   207
		bf[1] = 'I';
sl@0
   208
		bf[2] = 'D';
sl@0
   209
		bf[3] = TUint8('0' + i);
sl@0
   210
		for (j = 4; j < KMMCCIDLength - 1; j++)
sl@0
   211
			bf[j] = 'c';
sl@0
   212
		bf[KMMCCIDLength - 1] = '#';
sl@0
   213
sl@0
   214
		if (i == 0)
sl@0
   215
			{
sl@0
   216
			TUint cidIdx = 0;
sl@0
   217
			TLocalDriveCapsV5 driveCaps;
sl@0
   218
			TPckg<TLocalDriveCapsV5> driveCapsPkg(driveCaps);	
sl@0
   219
			if(TBLD.Caps(driveCapsPkg) == KErrNone)
sl@0
   220
				{
sl@0
   221
				// V5 of TLocalDriveCapsV5 now contains a serial number
sl@0
   222
				// which for MMC cards is defined to be the unique CID
sl@0
   223
				if(driveCaps.iSerialNumLength == KMMCCIDLength)
sl@0
   224
					{
sl@0
   225
					for(cidIdx=0; cidIdx<KMMCCIDLength; cidIdx++)
sl@0
   226
						{
sl@0
   227
						bf[cidIdx] = driveCaps.iSerialNum[KMMCCIDLength-cidIdx-1];
sl@0
   228
						}
sl@0
   229
					}
sl@0
   230
				}
sl@0
   231
			if(cidIdx == KMMCCIDLength)
sl@0
   232
				{
sl@0
   233
				test((CIDs[i] = new TCID(bf)) != NULL);
sl@0
   234
				}
sl@0
   235
			else
sl@0
   236
				{
sl@0
   237
#ifdef __WINS__
sl@0
   238
				test((CIDs[i] = new TCID(bf)) != NULL);
sl@0
   239
#else
sl@0
   240
				test((CIDs[i] = new TCID(ht0)) != NULL);
sl@0
   241
#endif
sl@0
   242
				}
sl@0
   243
			}
sl@0
   244
		else
sl@0
   245
			{
sl@0
   246
			test((CIDs[i] = new TCID(bf)) != NULL);
sl@0
   247
			}
sl@0
   248
		}
sl@0
   249
sl@0
   250
	test.End();
sl@0
   251
	}
sl@0
   252
sl@0
   253
sl@0
   254
LOCAL_C void DeleteCIDs()
sl@0
   255
//
sl@0
   256
// Deletes static global media identifiers from the heap.
sl@0
   257
//
sl@0
   258
	{
sl@0
   259
	test.Start(_L("DeleteCIDs"));
sl@0
   260
sl@0
   261
	TInt i;
sl@0
   262
	for (i = 0; i < KCIDCnt; i++)
sl@0
   263
		delete CIDs[i];
sl@0
   264
sl@0
   265
	test.End();
sl@0
   266
	}
sl@0
   267
sl@0
   268
sl@0
   269
LOCAL_C void AllocatePasswords()
sl@0
   270
//
sl@0
   271
// Allocates a set of static global TMediaPassword objects on the heap.
sl@0
   272
// The passwords range from zero to 16 bytes in length.
sl@0
   273
//
sl@0
   274
	{
sl@0
   275
	test.Start(_L("AllocatePasswords"));
sl@0
   276
sl@0
   277
	TInt i;
sl@0
   278
	for (i = 0; i < KPWDCnt; i++)
sl@0
   279
		{
sl@0
   280
		test((PWDs[i] = new TMediaPassword) != NULL);
sl@0
   281
		TInt j;
sl@0
   282
		for (j = 0; j < i * 2; j++)
sl@0
   283
			PWDs[i]->Append(TChar('a' + i + j));
sl@0
   284
		}
sl@0
   285
sl@0
   286
	test.End();
sl@0
   287
	}
sl@0
   288
sl@0
   289
sl@0
   290
LOCAL_C void DeletePasswords()
sl@0
   291
//
sl@0
   292
// Deletes static global TMediaPassword objects from the heap.
sl@0
   293
//
sl@0
   294
	{
sl@0
   295
	test.Start(_L("DeletePasswords"));
sl@0
   296
sl@0
   297
	TInt i;
sl@0
   298
	for (i = 0; i < KPWDCnt; i++)
sl@0
   299
		delete PWDs[i];
sl@0
   300
sl@0
   301
	test.End();
sl@0
   302
	}
sl@0
   303
sl@0
   304
sl@0
   305
LOCAL_C void SetUpMapSizes()
sl@0
   306
//
sl@0
   307
// Initializes static global mapSizes[,] with the persistent store mapping
sl@0
   308
// sizes of each CID and password.
sl@0
   309
//
sl@0
   310
	{
sl@0
   311
	test.Start(_L("SetUpMapSizes"));
sl@0
   312
sl@0
   313
	TInt i;
sl@0
   314
	for (i = 0; i < KCIDCnt; i++)
sl@0
   315
		{
sl@0
   316
		TInt j;
sl@0
   317
sl@0
   318
		for (j = 0; j < KPWDCnt; j++)
sl@0
   319
			mapSizes[i][j] = KMMCCIDLength + sizeof(TInt32) + PWDs[j]->Length();
sl@0
   320
		}
sl@0
   321
sl@0
   322
	test.End();
sl@0
   323
	}
sl@0
   324
sl@0
   325
sl@0
   326
LOCAL_C void AllocateTestData()
sl@0
   327
//
sl@0
   328
// Allocates all test data objects on the heap.
sl@0
   329
//
sl@0
   330
	{
sl@0
   331
	AllocateCIDs();
sl@0
   332
	AllocatePasswords();
sl@0
   333
sl@0
   334
	SetUpMapSizes();
sl@0
   335
	}
sl@0
   336
sl@0
   337
sl@0
   338
LOCAL_C void DeleteTestData()
sl@0
   339
//
sl@0
   340
// Frees all test data objects on the heap.
sl@0
   341
//
sl@0
   342
	{
sl@0
   343
	DeletePasswords();
sl@0
   344
	DeleteCIDs();
sl@0
   345
	}
sl@0
   346
sl@0
   347
sl@0
   348
// Test functions.
sl@0
   349
sl@0
   350
sl@0
   351
LOCAL_C void TestStaticStore()
sl@0
   352
//
sl@0
   353
// Tests the non card specific virtual functions in DPeriphBusController.
sl@0
   354
//	TInt ReadPasswordData(TDes8 &aBuf);
sl@0
   355
//	TInt WritePasswordData(const TDesC8 &aBuf);
sl@0
   356
//	TInt PasswordStoreLengthInBytes();
sl@0
   357
//
sl@0
   358
// store is reset at start of DMMCController::WritePasswordData().
sl@0
   359
//
sl@0
   360
	{
sl@0
   361
	test.Start(_L("TestStore"));
sl@0
   362
sl@0
   363
	// TBuf8<KMaxPersistentStore> is 4 + 4 + 256 bytes, so allocate on heap.
sl@0
   364
	TPersistentStore *pwStore;
sl@0
   365
	test((pwStore = new TPersistentStore) != NULL);
sl@0
   366
	TPersistentStore &wStore = *pwStore;
sl@0
   367
	TPersistentStore *prStore;
sl@0
   368
	test((prStore = new TPersistentStore) != NULL);
sl@0
   369
	TPersistentStore &rStore = *prStore;
sl@0
   370
sl@0
   371
	// WritePasswordData()
sl@0
   372
sl@0
   373
	test.Next(_L("WritePasswordData()"));
sl@0
   374
sl@0
   375
	test(TBLD.WritePasswordData(wStore) == KErrNone);// empty
sl@0
   376
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   377
sl@0
   378
	AddMapping(wStore, CIDs[1], PWDs[1]);						// exactly one entry
sl@0
   379
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   380
	test(TBLD.PasswordStoreLengthInBytes() == mapSizes[1][1]);
sl@0
   381
sl@0
   382
	AddMapping(wStore, CIDs[2], PWDs[2]);						// exactly two entries
sl@0
   383
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   384
	test(TBLD.PasswordStoreLengthInBytes() == mapSizes[1][1] + mapSizes[2][2]);
sl@0
   385
sl@0
   386
	TInt i;
sl@0
   387
	for (i = 0; i < wStore.Length(); i++)						// corrupt (partial)
sl@0
   388
		{
sl@0
   389
		wStore.SetLength(i);
sl@0
   390
		TInt r(TBLD.WritePasswordData(wStore));
sl@0
   391
		if (i == 0 || i == mapSizes[0][0] || i == mapSizes[0][0] + mapSizes[1][1])
sl@0
   392
			test(r == KErrNone);
sl@0
   393
		else
sl@0
   394
			test(r == KErrCorrupt && TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   395
		}
sl@0
   396
sl@0
   397
	test.Next(_L("Exceeding password store size"));	
sl@0
   398
sl@0
   399
	wStore.Zero();	// empty password store
sl@0
   400
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   401
sl@0
   402
	test.Printf(_L("Adding mappings...\n"));
sl@0
   403
sl@0
   404
	const TMediaPassword password(_L8("abcdefghijklmnop")); //Need a max length password (KMaxMediaPassword)
sl@0
   405
	for(TInt n=0; n<KCIDCnt; ++n)
sl@0
   406
		{
sl@0
   407
		AddMapping(wStore, CIDs[n], &password);
sl@0
   408
		test.Printf(_L("Mapping:%d store size: %d bytes\n"),n , wStore.Length() );
sl@0
   409
		const TInt r = TBLD.WritePasswordData(wStore);
sl@0
   410
		test.Printf(_L("WritePasswordData() --> ret=%d\n"), r);
sl@0
   411
	 	if(n==KMaxNumOfStoreEntries)
sl@0
   412
	 		test(r == KErrOverflow);
sl@0
   413
	 	else
sl@0
   414
	 		test(r == KErrNone);	
sl@0
   415
		}
sl@0
   416
sl@0
   417
sl@0
   418
	// ReadPasswordData().
sl@0
   419
sl@0
   420
	test.Next(_L("ReadPasswordData()"));
sl@0
   421
sl@0
   422
	wStore.Zero();												// empty
sl@0
   423
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   424
	test(TBLD.ReadPasswordData(rStore) == KErrNone);
sl@0
   425
	test(rStore.Length() == 0);
sl@0
   426
sl@0
   427
	AddMapping(wStore, CIDs[1], PWDs[1]);						// exactly one entry
sl@0
   428
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   429
	rStore.SetLength(0);										// lt store len
sl@0
   430
	test(TBLD.ReadPasswordData(rStore) == KErrNone);
sl@0
   431
	test(rStore.Length() == TBLD.PasswordStoreLengthInBytes());
sl@0
   432
																// gt store len
sl@0
   433
	rStore.SetLength(TBLD.PasswordStoreLengthInBytes() + 4);
sl@0
   434
	test(TBLD.ReadPasswordData(rStore) == 0);
sl@0
   435
	test(rStore.Length() == TBLD.PasswordStoreLengthInBytes());
sl@0
   436
	
sl@0
   437
	TBuf8<2> srStore;											// max lt store len
sl@0
   438
	test(TBLD.ReadPasswordData(srStore) == KErrOverflow);
sl@0
   439
sl@0
   440
	// Stress test high turnover with memory failure.
sl@0
   441
sl@0
   442
	test.Next(_L("Memory test"));
sl@0
   443
sl@0
   444
	TInt r;										// error code
sl@0
   445
sl@0
   446
	TInt m;
sl@0
   447
	for (m = 1; m < 100; m++)
sl@0
   448
		{
sl@0
   449
		__KHEAP_SETFAIL(RHeap::EDeterministic, m);
sl@0
   450
sl@0
   451
		TInt j;
sl@0
   452
		for (j = 1; j < KCIDCnt - 1; j++)
sl@0
   453
			{
sl@0
   454
			TInt k;
sl@0
   455
			for (k = 1; k < KPWDCnt - 1; k++)
sl@0
   456
				{
sl@0
   457
				wStore.Zero();
sl@0
   458
sl@0
   459
				AddMapping(wStore, CIDs[j], PWDs[k]);
sl@0
   460
				AddMapping(wStore, CIDs[j + 1], PWDs[k + 1]);
sl@0
   461
sl@0
   462
				if ((r = TBLD.WritePasswordData(wStore)) != KErrNone)
sl@0
   463
					{
sl@0
   464
					test(r == KErrNoMemory);
sl@0
   465
					test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   466
					}
sl@0
   467
				else
sl@0
   468
					{
sl@0
   469
					test(TBLD.ReadPasswordData(rStore) == KErrNone);
sl@0
   470
					test(IsStoreValid(rStore) && StoresEqual(rStore, wStore));
sl@0
   471
					}
sl@0
   472
				}
sl@0
   473
			}
sl@0
   474
		__KHEAP_RESET;
sl@0
   475
		}	// for (m = 1; m < 16; m++)
sl@0
   476
sl@0
   477
	// Clear the store for subsequent tests.
sl@0
   478
sl@0
   479
	wStore.Zero();
sl@0
   480
	test(TBLD.WritePasswordData(wStore) == KErrNone);
sl@0
   481
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   482
sl@0
   483
	delete prStore;
sl@0
   484
	delete pwStore;
sl@0
   485
sl@0
   486
	test.End();
sl@0
   487
	}
sl@0
   488
sl@0
   489
sl@0
   490
LOCAL_C void AddMapping(TDes8 &aSt, const TCID *aCID, const TMediaPassword *aPWD)
sl@0
   491
//
sl@0
   492
// Adds aCID |-> aPWD mapping to persistent file's store contents.
sl@0
   493
//
sl@0
   494
	{
sl@0
   495
	aSt.SetLength(aSt.Length() + KMMCCIDLength);
sl@0
   496
	aCID->Copy(&aSt[aSt.Length() - KMMCCIDLength]);
sl@0
   497
sl@0
   498
	TUint8 lenBuf[sizeof(TInt32)];		// TInt32, big endian
sl@0
   499
	TMMC::BigEndian4Bytes(lenBuf, TInt32(aPWD->Length()));
sl@0
   500
	aSt.Append(&lenBuf[0], sizeof(TInt32));
sl@0
   501
sl@0
   502
	aSt.Append(*aPWD);
sl@0
   503
	}
sl@0
   504
sl@0
   505
sl@0
   506
LOCAL_C TBool IsStoreValid(const TDesC8 &aSt)
sl@0
   507
// 
sl@0
   508
// Checks the integrity of the supplied buffer.
sl@0
   509
// 
sl@0
   510
	{
sl@0
   511
	TInt iBIdx;									// buffer index
sl@0
   512
	TBool corrupt(EFalse);						// abort flag
sl@0
   513
	for (iBIdx = 0; iBIdx < aSt.Length(); /* nop */)
sl@0
   514
		{
sl@0
   515
		// Enough raw data for CID, PWD_LEN and 1 byte of PWD.
sl@0
   516
		corrupt = TUint(aSt.Length() - iBIdx) < KMMCCIDLength + sizeof(TInt32) + 1;
sl@0
   517
		if (corrupt)
sl@0
   518
			break;
sl@0
   519
		
sl@0
   520
		// PWD_LEN is valid and enough raw data left for PWD.
sl@0
   521
		iBIdx += KMMCCIDLength;
sl@0
   522
		const TInt32 pwd_len(TMMC::BigEndian32(aSt.Mid(iBIdx).TDesC8::Ptr()));
sl@0
   523
		corrupt = !(
sl@0
   524
				(pwd_len <= KMaxMediaPassword)
sl@0
   525
			&&	aSt.Length() - iBIdx >= TInt(sizeof(TInt32)) + pwd_len );
sl@0
   526
		if (corrupt)
sl@0
   527
			break;
sl@0
   528
		
sl@0
   529
		// skip over PWD_LEN and PWD to next entry.
sl@0
   530
		iBIdx += sizeof(TInt32) + pwd_len;
sl@0
   531
		}
sl@0
   532
sl@0
   533
	if (corrupt)
sl@0
   534
		DumpStore(_L("invalid"), aSt);
sl@0
   535
sl@0
   536
	return ! corrupt;
sl@0
   537
	}
sl@0
   538
sl@0
   539
sl@0
   540
LOCAL_C void PrintCID(const TCID &aCID)
sl@0
   541
//
sl@0
   542
// Prints the 128 bit CID in big endian format.
sl@0
   543
//
sl@0
   544
	{
sl@0
   545
	test.Printf(_L("CID: "));
sl@0
   546
	TInt i;
sl@0
   547
	for (i = 0; i < TInt(KMMCCIDLength); i += 4)
sl@0
   548
		{
sl@0
   549
		TInt j;
sl@0
   550
		for (j = i; j < i + 4; ++j)
sl@0
   551
			{
sl@0
   552
			test.Printf(_L("%02x: %02x "), j, aCID.At(KMMCCIDLength - j - 1));
sl@0
   553
			}
sl@0
   554
		test.Printf(_L("\n"));
sl@0
   555
		}
sl@0
   556
	}
sl@0
   557
sl@0
   558
sl@0
   559
LOCAL_C void ParseStore(const TDesC8 &aSt, CArrayFixSeg<TTestMapping> *aMP)
sl@0
   560
//
sl@0
   561
// Fills aMP with the mappings in aSt.
sl@0
   562
//
sl@0
   563
	{
sl@0
   564
	TInt iBIdx;									// buffer index
sl@0
   565
	TInt r(KErrNone);							// exit code
sl@0
   566
	for (iBIdx = 0; r == KErrNone && iBIdx < aSt.Length(); /* nop */)
sl@0
   567
		{
sl@0
   568
		// Calculate index for CID.
sl@0
   569
		TPtrC8 pCID(aSt.Mid(iBIdx, KMMCCIDLength));	// CID
sl@0
   570
		const TCID cid(pCID.Ptr());
sl@0
   571
		TInt cidIdx;
sl@0
   572
		for (cidIdx = 0; cidIdx < KCIDCnt && !(*(CIDs[cidIdx]) == cid); cidIdx++)
sl@0
   573
			{ /* empty. */ }
sl@0
   574
		// If invalid CID then print CID with valid CIDs.
sl@0
   575
		if (!(cidIdx < KCIDCnt))
sl@0
   576
			{
sl@0
   577
			test.Printf(_L("ParseStore: invalid CID\n"));
sl@0
   578
			PrintCID(cid);
sl@0
   579
			TInt i;
sl@0
   580
			for (i = 0; i < KCIDCnt; i++)
sl@0
   581
				{
sl@0
   582
				test.Printf(_L("ParseStore: valid CID %d\n"), i);
sl@0
   583
				PrintCID(*CIDs[i]);
sl@0
   584
				}
sl@0
   585
			test(EFalse);
sl@0
   586
			}
sl@0
   587
sl@0
   588
		const TInt32 pwd_len(TMMC::BigEndian32(&aSt[iBIdx + KMMCCIDLength]));
sl@0
   589
sl@0
   590
		// Calculate index for PWD.
sl@0
   591
		TMediaPassword pwd;
sl@0
   592
		pwd.Copy(&aSt[iBIdx + KMMCCIDLength + sizeof(TInt32)], pwd_len);
sl@0
   593
sl@0
   594
		TInt pwdIdx;
sl@0
   595
		for (pwdIdx = 0; pwdIdx < KPWDCnt && *PWDs[pwdIdx] != pwd; pwdIdx++)
sl@0
   596
			{ /* empty. */ }
sl@0
   597
		test(pwdIdx < KPWDCnt);
sl@0
   598
sl@0
   599
		TTestMapping mp;
sl@0
   600
		mp.iCIDIdx = cidIdx;
sl@0
   601
		mp.iPWDIdx = pwdIdx;
sl@0
   602
		TRAP(r, aMP->InsertL(0, mp));
sl@0
   603
		test(r == KErrNone);
sl@0
   604
sl@0
   605
		iBIdx += KMMCCIDLength + sizeof(TInt32) + pwd_len;
sl@0
   606
		}
sl@0
   607
	}
sl@0
   608
sl@0
   609
sl@0
   610
LOCAL_C void DumpStore(const TDesC &aName, const TDesC8 &aSt)
sl@0
   611
//
sl@0
   612
// Prints the contents of the supplied store.
sl@0
   613
//
sl@0
   614
	{
sl@0
   615
	test.Printf(_L("\nstore %S: len = %d\n"), &aName, aSt.Length());
sl@0
   616
sl@0
   617
	TInt i;
sl@0
   618
	for (i = 0; i < aSt.Length(); i += 8)
sl@0
   619
		{
sl@0
   620
		TInt j;
sl@0
   621
		for (j = i; j < Min(aSt.Length(), i + 8); j++)
sl@0
   622
			test.Printf(_L("%02d: %03d : %02x : %c \n "), j, aSt[j], aSt[j], aSt[j]);
sl@0
   623
		test.Printf(_L("\n"));
sl@0
   624
		}
sl@0
   625
	}
sl@0
   626
sl@0
   627
sl@0
   628
LOCAL_C TBool StoresEqual(const TDesC8 &aSt0, const TDesC8 &aSt1)
sl@0
   629
//
sl@0
   630
// Compares aSt1 with aSt2.  Return value indicates whether or not the
sl@0
   631
// stores contain exactly the same mappings, but not necessarily in the
sl@0
   632
// same order.
sl@0
   633
//
sl@0
   634
	{
sl@0
   635
	TBool same(EFalse);
sl@0
   636
sl@0
   637
	CArrayFixSeg<TTestMapping> *ramp0, *ramp1;
sl@0
   638
sl@0
   639
	test((ramp0 = new(ELeave) CArrayFixSeg<TTestMapping>(2)) != NULL);
sl@0
   640
	test((ramp1 = new(ELeave) CArrayFixSeg<TTestMapping>(2)) != NULL);
sl@0
   641
	
sl@0
   642
	test(IsStoreValid(aSt0));
sl@0
   643
	test(IsStoreValid(aSt1));
sl@0
   644
sl@0
   645
	ParseStore(aSt0, ramp0);
sl@0
   646
	ParseStore(aSt1, ramp1);
sl@0
   647
sl@0
   648
	TArray<TTestMapping> a0(ramp0->Array());
sl@0
   649
	TArray<TTestMapping> a1(ramp1->Array());
sl@0
   650
sl@0
   651
	if (a0.Count() == a1.Count())
sl@0
   652
	// if #a0 == #a1 and a0 <= a1 then a0 == a1.
sl@0
   653
		{
sl@0
   654
		TBool allInA1(ETrue);
sl@0
   655
		TInt i;
sl@0
   656
		for (i = 0; allInA1 && i < a0.Count(); i++)
sl@0
   657
			{
sl@0
   658
			TBool found(EFalse);
sl@0
   659
			TInt j;
sl@0
   660
			for (j = 0; ! found && j < a0.Count(); j++)
sl@0
   661
				{
sl@0
   662
				found = (
sl@0
   663
						a0[i].iCIDIdx == a1[j].iCIDIdx
sl@0
   664
					&&	a0[i].iPWDIdx == a1[j].iPWDIdx );
sl@0
   665
				}
sl@0
   666
			allInA1 = found;
sl@0
   667
			}
sl@0
   668
sl@0
   669
		same = allInA1;
sl@0
   670
		}
sl@0
   671
sl@0
   672
	delete ramp1;
sl@0
   673
	delete ramp0;
sl@0
   674
sl@0
   675
	if (! same)
sl@0
   676
		{
sl@0
   677
		DumpStore(_L("0"), aSt0);
sl@0
   678
		DumpStore(_L("1"), aSt1);
sl@0
   679
		}
sl@0
   680
sl@0
   681
	return same;
sl@0
   682
	}
sl@0
   683
sl@0
   684
sl@0
   685
LOCAL_C void RemountMedia()
sl@0
   686
//
sl@0
   687
// Forces a media remount and waits for it to take effect.  If the card has a
sl@0
   688
// password, it will become locked the next time that it is powered up.
sl@0
   689
//
sl@0
   690
	{
sl@0
   691
//#ifdef __WINS__
sl@0
   692
//	TBLD.ForceMediaChange();
sl@0
   693
//	UserSvr::ForceRemountMedia(ERemovableMedia0);
sl@0
   694
//	User::After(1 * 1000 * 1000);
sl@0
   695
//#else
sl@0
   696
sl@0
   697
#ifdef __AUTO_DETECT_MEDIA_CHANGE__
sl@0
   698
	RFs fs;
sl@0
   699
	test(fs.Connect() == KErrNone);
sl@0
   700
sl@0
   701
	test.Printf(_L("Remove and re-insert card.."));
sl@0
   702
sl@0
   703
	TInt r;
sl@0
   704
	do
sl@0
   705
		{
sl@0
   706
		TRequestStatus status;
sl@0
   707
		TDriveUnit driveUnit(RFsDNum);
sl@0
   708
		TDriveName driveName = driveUnit.Name();
sl@0
   709
		fs.NotifyChange(ENotifyAll, status, driveName);
sl@0
   710
		test(status == KRequestPending);
sl@0
   711
		User::WaitForRequest(status);
sl@0
   712
		test.Printf(_L("\rAccessing card...          \r"));
sl@0
   713
sl@0
   714
		r = AccessDisk();
sl@0
   715
		if (r == KErrNotReady)
sl@0
   716
			test.Printf(_L("\rRemove and re-insert card.."));
sl@0
   717
sl@0
   718
		if (r != KErrNone && r != KErrNotReady && r != KErrLocked)
sl@0
   719
			test.Printf(_L("AccessDisk() returned %d"), r);
sl@0
   720
		}
sl@0
   721
	while (r == KErrNotReady);
sl@0
   722
sl@0
   723
	test.Printf(_L("\n"));
sl@0
   724
sl@0
   725
	fs.Close();
sl@0
   726
sl@0
   727
#else
sl@0
   728
	// Power down the card so that it is locked the next time it is powered up.
sl@0
   729
	test.Printf(_L("Remove and re-insert card.  Press \'z\' when finished.\n"));
sl@0
   730
	while (test.Getch() != 'z')
sl@0
   731
		{ /* empty. */ }
sl@0
   732
#endif
sl@0
   733
sl@0
   734
//#endif
sl@0
   735
	}
sl@0
   736
sl@0
   737
sl@0
   738
LOCAL_C void AttemptToUnlock(TMediaPassword &aPWD, TBool aStore)
sl@0
   739
//
sl@0
   740
// Tests that the card is locked and then tries to unlock it.
sl@0
   741
//
sl@0
   742
	{
sl@0
   743
	TInt r = AccessDisk();
sl@0
   744
	if (r != KErrLocked)
sl@0
   745
		test.Printf(_L("AccessDisk() returned %d\n"), r);
sl@0
   746
	test(r == KErrLocked);
sl@0
   747
	test(TBLD.Unlock(aPWD, aStore) == KErrNone);
sl@0
   748
	}
sl@0
   749
sl@0
   750
sl@0
   751
LOCAL_C void TestLockUnlock()
sl@0
   752
//
sl@0
   753
// Tests TBusLocalDrive functions for locking / unlocking individual cards.
sl@0
   754
// Lock() currently means set password only.  The media must be remounted before it
sl@0
   755
// can really be locked.
sl@0
   756
//
sl@0
   757
//			EPbPswdUnlock		EPbPswdLock			EPbPswdClear
sl@0
   758
//			right	wrong		right	wrong		right	wrong	
sl@0
   759
// locked	None	AccDen		AccDec	AccDen		AccDen	AccDen	
sl@0
   760
// unlocked	AldExst	AldExst		None	AccDec		None	AccDen	
sl@0
   761
//
sl@0
   762
// Locked means inaccessible, not just has password.
sl@0
   763
// 
sl@0
   764
	{
sl@0
   765
	test.Start(_L("TestLockUnlock"));
sl@0
   766
sl@0
   767
	TMediaPassword nul(*PWDs[0]);
sl@0
   768
	TMediaPassword arb1(*PWDs[1]);
sl@0
   769
	TMediaPassword arb2(*PWDs[2]);
sl@0
   770
sl@0
   771
	// Clear the password store for when function run on its own.
sl@0
   772
	TBuf8<1> nulSt;
sl@0
   773
	test(TBLD.WritePasswordData(nulSt) == KErrNone);// empty
sl@0
   774
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   775
sl@0
   776
	// Give the card an arbitrary password
sl@0
   777
	test.Next(_L("assign test password"));
sl@0
   778
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);
sl@0
   779
	RemountMedia();												// card is now locked
sl@0
   780
sl@0
   781
	test.Next(_L("lock locked card"));
sl@0
   782
	test(TBLD.SetPassword(arb2, arb1, EFalse) == KErrAccessDenied);	// lock locked wrong
sl@0
   783
	test(TBLD.SetPassword(arb1, arb1, EFalse) == KErrAccessDenied);	// lock locked right
sl@0
   784
sl@0
   785
	test.Next(_L("unlock locked card"));
sl@0
   786
	test(TBLD.Unlock(arb2, EFalse) == KErrAccessDenied);		// unlock locked wrong
sl@0
   787
	AttemptToUnlock(arb1);
sl@0
   788
sl@0
   789
	test.Next(_L("unlock unlocked card"));
sl@0
   790
	test(TBLD.Unlock(arb1, EFalse) == KErrAlreadyExists);		// unlock unlocked right
sl@0
   791
	test(TBLD.Unlock(arb2, EFalse) == KErrAlreadyExists);		// unlock unlocked wrong
sl@0
   792
sl@0
   793
	test.Next(_L("lock unlocked card"));
sl@0
   794
	test(TBLD.SetPassword(arb2, arb1, EFalse) == KErrAccessDenied);	// lock unlocked wrong
sl@0
   795
	test(TBLD.SetPassword(arb1, arb1, EFalse) == KErrNone);			// lock unlocked right
sl@0
   796
sl@0
   797
	test.Next(_L("clear unlocked card"));
sl@0
   798
	test(TBLD.Clear(arb2) == KErrAccessDenied);					// clear unlocked wrong
sl@0
   799
sl@0
   800
	//!!! If clear with wrong password, cannot clear with right password in same
sl@0
   801
	// power session (H).
sl@0
   802
	RemountMedia();
sl@0
   803
	AttemptToUnlock(arb1);
sl@0
   804
	test(TBLD.Clear(arb1) == KErrNone);
sl@0
   805
sl@0
   806
	test.Next(_L("assign test password"));
sl@0
   807
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);				// give test password
sl@0
   808
	RemountMedia();												// make inaccessible
sl@0
   809
sl@0
   810
	test.Next(_L("clear locked card"));
sl@0
   811
	test(TBLD.Clear(arb2) == KErrAccessDenied);					// clear locked wrong
sl@0
   812
	test(TBLD.Clear(arb1) == KErrAccessDenied);					// clear locked right
sl@0
   813
sl@0
   814
	// Clear password for subsequent tests.
sl@0
   815
	test.Next(_L("clear password"));
sl@0
   816
	AttemptToUnlock(arb1);
sl@0
   817
	test(TBLD.Clear(arb1) == KErrNone);
sl@0
   818
	test(TBLD.WritePasswordData(nulSt) == KErrNone);
sl@0
   819
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   820
sl@0
   821
	test.End();
sl@0
   822
	}
sl@0
   823
sl@0
   824
sl@0
   825
/**
sl@0
   826
 * Because MultiMediaCards cannot distinguish where the current password ends
sl@0
   827
 * and the new password begins, test the media driver can abort those operations
sl@0
   828
 * that would end up giving the user unexpected passwords.
sl@0
   829
 * 
sl@0
   830
 * The stores are directly compared with buffers because they only use one password
sl@0
   831
 * and the passwords are not part of the standard test data.
sl@0
   832
 */
sl@0
   833
sl@0
   834
LOCAL_C void TestElidePasswords()
sl@0
   835
	{
sl@0
   836
	test.Start(_L("TestElidePasswords"));
sl@0
   837
sl@0
   838
	TMediaPassword a((const TUint8*) "a");		TMediaPassword bcxyz((const TUint8*) "bcxyz");
sl@0
   839
	TMediaPassword ab((const TUint8*) "ab");	TMediaPassword cxyz((const TUint8*) "cxyz");
sl@0
   840
	TMediaPassword abc((const TUint8*) "abc");	TMediaPassword xyz((const TUint8*) "xyz");
sl@0
   841
sl@0
   842
	TPersistentStore* pstoreAB;
sl@0
   843
	test((pstoreAB = new TPersistentStore) != 0);
sl@0
   844
	TPersistentStore& storeAB = *pstoreAB;
sl@0
   845
	AddMapping(storeAB, CIDs[0], &ab);
sl@0
   846
sl@0
   847
	TPersistentStore* pstoreCXYZ;
sl@0
   848
	test((pstoreCXYZ = new TPersistentStore) != 0);
sl@0
   849
	TPersistentStore& storeCXYZ = *pstoreCXYZ;
sl@0
   850
	AddMapping(storeCXYZ, CIDs[0], &cxyz);
sl@0
   851
sl@0
   852
	TPersistentStore *pstoreRd;									// scratch for reading
sl@0
   853
	test((pstoreRd = new TPersistentStore) != NULL);
sl@0
   854
	TPersistentStore& storeRd = *pstoreRd;
sl@0
   855
sl@0
   856
	TBuf8<1> nulSt;
sl@0
   857
	test(TBLD.SetPassword(nulSt, ab, ETrue) == KErrNone);
sl@0
   858
	RemountMedia();												// card is now locked
sl@0
   859
	test(AccessDisk() == KErrNone);
sl@0
   860
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   861
	test(storeRd == storeAB);
sl@0
   862
sl@0
   863
	test.Next(_L("current password too short"));
sl@0
   864
	test(TBLD.SetPassword(a, bcxyz, ETrue) == KErrAccessDenied);
sl@0
   865
	test(AccessDisk() == KErrNone);
sl@0
   866
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   867
	test(storeRd == storeAB);
sl@0
   868
sl@0
   869
	test.Next(_L("current password too long"));
sl@0
   870
	test(TBLD.SetPassword(abc, xyz, ETrue) == KErrAccessDenied);
sl@0
   871
	test(AccessDisk() == KErrNone);
sl@0
   872
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   873
	test(storeRd == storeAB);
sl@0
   874
sl@0
   875
	test.Next(_L("current password exactly right"));
sl@0
   876
	test(TBLD.SetPassword(ab, cxyz, ETrue) == KErrNone);
sl@0
   877
	test(AccessDisk() == KErrNone);
sl@0
   878
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   879
	test(storeRd == storeCXYZ);
sl@0
   880
sl@0
   881
	test.Next(_L("clean up for following tests"));
sl@0
   882
	test(TBLD.Clear(cxyz) == KErrNone);
sl@0
   883
	test(TBLD.WritePasswordData(nulSt) == KErrNone);
sl@0
   884
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   885
sl@0
   886
	delete pstoreRd;
sl@0
   887
	delete pstoreCXYZ;
sl@0
   888
	delete pstoreAB;
sl@0
   889
sl@0
   890
	test.End();
sl@0
   891
	}
sl@0
   892
sl@0
   893
sl@0
   894
/**
sl@0
   895
 * test the special cases where null passwords are used.  These are all failed with
sl@0
   896
 * KErrAccessDenied by the controller.
sl@0
   897
 */
sl@0
   898
sl@0
   899
LOCAL_C void TestNullPasswords()
sl@0
   900
	{
sl@0
   901
	test.Start(_L("TestNullPasswords"));
sl@0
   902
sl@0
   903
	TMediaPassword nul(*PWDs[0]);
sl@0
   904
	TMediaPassword arb1(*PWDs[1]);
sl@0
   905
sl@0
   906
	test.Next(_L("card has no password"));
sl@0
   907
	test(TBLD.SetPassword(nul, nul, ETrue) == KErrAccessDenied);
sl@0
   908
	test(TBLD.Unlock(nul, ETrue) == KErrAlreadyExists);
sl@0
   909
	test(TBLD.Clear(nul) == KErrAccessDenied);
sl@0
   910
sl@0
   911
	test.Next(_L("card has password and is unlocked"));
sl@0
   912
	test(TBLD.SetPassword(nul, arb1, ETrue) == KErrNone);
sl@0
   913
	RemountMedia();
sl@0
   914
	test(AccessDisk() == KErrNone);
sl@0
   915
	test(TBLD.SetPassword(nul, nul, ETrue) == KErrAccessDenied);
sl@0
   916
	test(TBLD.Unlock(nul, ETrue) == KErrAlreadyExists);
sl@0
   917
	test(TBLD.Clear(nul) == KErrAccessDenied);
sl@0
   918
sl@0
   919
	test.Next(_L("clean up for following tests"));
sl@0
   920
	test(TBLD.Clear(arb1) == KErrNone);
sl@0
   921
	TBuf8<1> nulSt;
sl@0
   922
	test(TBLD.WritePasswordData(nulSt) == KErrNone);
sl@0
   923
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
   924
sl@0
   925
	test.End();
sl@0
   926
	}
sl@0
   927
sl@0
   928
sl@0
   929
LOCAL_C void TestControllerStore()
sl@0
   930
// 
sl@0
   931
// Performs standard password functions but stores the mappings in the controller store.
sl@0
   932
//
sl@0
   933
// + mapping added to store (if not exists)
sl@0
   934
// - mapping removed from store (if exists)
sl@0
   935
// 
sl@0
   936
//			EPbPswdUnlock		EPbPswdLock			EPbPswdClear
sl@0
   937
//			right	wrong		right	wrong		right	wrong	
sl@0
   938
// locked	None1	AccDen-		AccDec	AccDen		AccDen	AccDen
sl@0
   939
// unlocked	AccDen	AccDen		None+	AccDec-		None-	AccDen-
sl@0
   940
//
sl@0
   941
// Locked means inaccessible, not just has password.
sl@0
   942
// When the user supplies a password, the mapping in the password store is not used.
sl@0
   943
//
sl@0
   944
// 1.	A locked card with the right mapping in the store cannot happen because of the
sl@0
   945
//		automatic unlocking mechanism.
sl@0
   946
//
sl@0
   947
// Tests start with an unlocked card that has no password.
sl@0
   948
//
sl@0
   949
	{
sl@0
   950
	test.Start(_L("TestControllerStore"));
sl@0
   951
sl@0
   952
	test.Next(_L("allocate test data"));
sl@0
   953
sl@0
   954
	TMediaPassword nul(*PWDs[0]);
sl@0
   955
	TMediaPassword arb1(*PWDs[1]);
sl@0
   956
	TMediaPassword arb2(*PWDs[2]);
sl@0
   957
sl@0
   958
	TPersistentStore *pstoreDef;								// { 3 |-> 3 }
sl@0
   959
	test((pstoreDef = new TPersistentStore) != NULL);
sl@0
   960
	TPersistentStore &storeDef = *pstoreDef;
sl@0
   961
	AddMapping(storeDef, CIDs[3], PWDs[3]);
sl@0
   962
sl@0
   963
	TPersistentStore *pstore0_1;								// { 3 |-> 3, 0 |-> 1 }
sl@0
   964
	test((pstore0_1 = new TPersistentStore) != NULL);
sl@0
   965
	TPersistentStore &store0_1 = *pstore0_1;
sl@0
   966
	AddMapping(store0_1, CIDs[3], PWDs[3]);
sl@0
   967
	AddMapping(store0_1, CIDs[0], PWDs[1]);
sl@0
   968
sl@0
   969
	TPersistentStore *pstore0_2;								// { 3 |-> 3, 0 |-> 2 }
sl@0
   970
	test((pstore0_2 = new TPersistentStore) != NULL);
sl@0
   971
	TPersistentStore &store0_2 = *pstore0_2;
sl@0
   972
	AddMapping(store0_2, CIDs[3], PWDs[3]);
sl@0
   973
	AddMapping(store0_2, CIDs[0], PWDs[2]);
sl@0
   974
sl@0
   975
	TPersistentStore *pstoreRd;									// temp for reading
sl@0
   976
	test((pstoreRd = new TPersistentStore) != NULL);
sl@0
   977
	TPersistentStore &storeRd = *pstoreRd;
sl@0
   978
sl@0
   979
	// Give card arbitrary password but do not lock or store.
sl@0
   980
	test.Next(_L("assign test password"));
sl@0
   981
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);
sl@0
   982
sl@0
   983
	// Lock
sl@0
   984
sl@0
   985
	// Lock unlocked right out.
sl@0
   986
	test.Next(_L("lock unlocked right out"));
sl@0
   987
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
   988
	test(TBLD.SetPassword(arb1, arb1, ETrue) == KErrNone);				// + (0 |-> 1)
sl@0
   989
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   990
	test(StoresEqual(storeRd, store0_1));
sl@0
   991
sl@0
   992
	// Lock unlocked right in (different to make sure store modified.)
sl@0
   993
	test.Next(_L("lock unlocked right in"));
sl@0
   994
	test(TBLD.WritePasswordData(store0_1) == KErrNone);
sl@0
   995
	test(TBLD.SetPassword(arb1, arb2, ETrue) == KErrNone);				// - (0 |-> 1) + (0 |-> 2)
sl@0
   996
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
   997
	test(StoresEqual(storeRd, store0_2));
sl@0
   998
sl@0
   999
	// Lock unlocked wrong out.
sl@0
  1000
	test.Next(_L("lock unlocked wrong out"));
sl@0
  1001
	test(TBLD.SetPassword(arb2, arb1, ETrue) == KErrNone);				// restore to arb1
sl@0
  1002
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1003
	test(TBLD.SetPassword(arb2, arb1, ETrue) == KErrAccessDenied);		// not add (0 |-> 1)
sl@0
  1004
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1005
	test(StoresEqual(storeRd, storeDef));
sl@0
  1006
sl@0
  1007
	// Lock unlocked wrong in.
sl@0
  1008
	test.Next(_L("lock unlocked wrong in"));
sl@0
  1009
	test(TBLD.WritePasswordData(store0_1) == KErrNone);
sl@0
  1010
	test(TBLD.SetPassword(arb2, arb1, ETrue) == KErrAccessDenied);		// - (0 |-> 1)
sl@0
  1011
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1012
	test(StoresEqual(storeRd, store0_1));
sl@0
  1013
sl@0
  1014
sl@0
  1015
	// Unlock
sl@0
  1016
sl@0
  1017
	// Unlock locked right out.
sl@0
  1018
	test.Next(_L("unlock locked right out"));
sl@0
  1019
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1020
	RemountMedia();												// make inaccessible
sl@0
  1021
	AttemptToUnlock(arb1, ETrue);								// + (0 |-> 1)
sl@0
  1022
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1023
	test(StoresEqual(storeRd, store0_1));
sl@0
  1024
	
sl@0
  1025
	// Unlock locked right in - see note 1.
sl@0
  1026
sl@0
  1027
	// Unlock locked wrong in.
sl@0
  1028
	test.Next(_L("unlock locked wrong in"));
sl@0
  1029
	test(TBLD.WritePasswordData(store0_2) == KErrNone);
sl@0
  1030
	RemountMedia();												// make inaccessible
sl@0
  1031
	test(TBLD.Unlock(arb2, ETrue) == KErrAccessDenied);			// - (0 |-> 2)
sl@0
  1032
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1033
	test(StoresEqual(storeRd, storeDef));
sl@0
  1034
sl@0
  1035
	// Unlock locked wrong out.
sl@0
  1036
	test.Next(_L("unlock locked wrong out"));
sl@0
  1037
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1038
	RemountMedia();												// make inaccessible
sl@0
  1039
	test(TBLD.Unlock(arb2, ETrue) == KErrAccessDenied);			// not add (0 |-> 2)
sl@0
  1040
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1041
	test(StoresEqual(storeRd, storeDef));
sl@0
  1042
sl@0
  1043
sl@0
  1044
	// Clear
sl@0
  1045
sl@0
  1046
	// Clear unlocked right out.
sl@0
  1047
	test.Next(_L("clear unlocked right out"));
sl@0
  1048
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1049
	AttemptToUnlock(arb1);										// make accessible
sl@0
  1050
	test(TBLD.Clear(arb1) == KErrNone);							// not add (0 |-> 1)
sl@0
  1051
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1052
	test(StoresEqual(storeRd, storeDef));
sl@0
  1053
sl@0
  1054
	// Clear unlocked right in.
sl@0
  1055
	test.Next(_L("clear unlocked right in"));
sl@0
  1056
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);				// give password
sl@0
  1057
	test(TBLD.WritePasswordData(store0_1) == KErrNone);
sl@0
  1058
	test(TBLD.Clear(arb1) == KErrNone);							// - (0 |-> 2)
sl@0
  1059
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1060
	test(StoresEqual(storeRd, storeDef));
sl@0
  1061
sl@0
  1062
	// Clear unlocked wrong out.
sl@0
  1063
	test.Next(_L("clear unlocked wrong out"));
sl@0
  1064
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);				// give password
sl@0
  1065
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1066
	test(TBLD.Clear(arb2) == KErrAccessDenied);					// not add (0 |-> 2)
sl@0
  1067
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1068
	test(StoresEqual(storeRd, storeDef));
sl@0
  1069
sl@0
  1070
	// Clear unlocked wrong in.
sl@0
  1071
	test.Next(_L("clear unlocked wrong in"));
sl@0
  1072
	test(TBLD.WritePasswordData(store0_1) == KErrNone);
sl@0
  1073
	test(TBLD.Clear(arb2) == KErrAccessDenied);					// - (0 |-> 2)
sl@0
  1074
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1075
	test(StoresEqual(storeRd, store0_1));
sl@0
  1076
sl@0
  1077
	// Clear password for subsequent tests.
sl@0
  1078
sl@0
  1079
	test.Next(_L("clean up for following tests"));
sl@0
  1080
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1081
	RemountMedia();
sl@0
  1082
	AttemptToUnlock(arb1);
sl@0
  1083
	test(TBLD.Clear(arb1) == KErrNone);
sl@0
  1084
	TBuf8<1> nulSt;
sl@0
  1085
	test(TBLD.WritePasswordData(nulSt) == KErrNone);
sl@0
  1086
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
  1087
sl@0
  1088
	test.Next(_L("free test data"));
sl@0
  1089
sl@0
  1090
	delete pstoreRd;
sl@0
  1091
	delete pstore0_2;
sl@0
  1092
	delete pstore0_1;
sl@0
  1093
	delete pstoreDef;
sl@0
  1094
sl@0
  1095
	test.End();
sl@0
  1096
	}
sl@0
  1097
sl@0
  1098
sl@0
  1099
LOCAL_C TInt AccessDisk()
sl@0
  1100
//
sl@0
  1101
// Attempts to read the first sector of the removable media to determine whether
sl@0
  1102
// it is locked.
sl@0
  1103
//
sl@0
  1104
	{
sl@0
  1105
	const TInt KSectSize = 512;
sl@0
  1106
	TBuf8<KSectSize> sect;						// 8 + 512
sl@0
  1107
sl@0
  1108
	return TBLD.Read(0, KSectSize, sect);
sl@0
  1109
	}
sl@0
  1110
sl@0
  1111
sl@0
  1112
LOCAL_C void TestAutoUnlock()
sl@0
  1113
//
sl@0
  1114
// Tests controller internal store unlocking mechanism.
sl@0
  1115
// A locked card should be transparently unlocked after the peripheral bus is
sl@0
  1116
// powered up.
sl@0
  1117
//
sl@0
  1118
	{
sl@0
  1119
	test.Start(_L("TestAutoUnlock"));
sl@0
  1120
sl@0
  1121
	test.Next(_L("allocate test data"));
sl@0
  1122
sl@0
  1123
	TMediaPassword nul(*PWDs[0]);
sl@0
  1124
	TMediaPassword arb1(*PWDs[1]);
sl@0
  1125
sl@0
  1126
	TPersistentStore *pstoreDef;								// { 3 |-> 3 }
sl@0
  1127
	test((pstoreDef = new TPersistentStore) != NULL);
sl@0
  1128
	TPersistentStore &storeDef = *pstoreDef;
sl@0
  1129
	AddMapping(storeDef, CIDs[3], PWDs[3]);
sl@0
  1130
sl@0
  1131
	TPersistentStore *pstore0_1;								// { 3 |-> 3, 0 |-> 1 }
sl@0
  1132
	test((pstore0_1 = new TPersistentStore) != NULL);
sl@0
  1133
	TPersistentStore &store0_1 = *pstore0_1;
sl@0
  1134
	AddMapping(store0_1, CIDs[3], PWDs[3]);
sl@0
  1135
	AddMapping(store0_1, CIDs[0], PWDs[1]);
sl@0
  1136
sl@0
  1137
	TPersistentStore *pstore0_2;								// { 3 |-> 3, 0 |-> 2 }
sl@0
  1138
	test((pstore0_2 = new TPersistentStore) != NULL);
sl@0
  1139
	TPersistentStore &store0_2 = *pstore0_2;
sl@0
  1140
	AddMapping(store0_2, CIDs[3], PWDs[3]);
sl@0
  1141
	AddMapping(store0_2, CIDs[0], PWDs[2]);
sl@0
  1142
sl@0
  1143
	TPersistentStore *pstoreRd;									// temp for reading
sl@0
  1144
	test((pstoreRd = new TPersistentStore) != NULL);
sl@0
  1145
	TPersistentStore &storeRd = *pstoreRd;
sl@0
  1146
sl@0
  1147
	test.Next(_L("assign password"));
sl@0
  1148
	test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone);				// give password
sl@0
  1149
sl@0
  1150
	// No mapping in store.
sl@0
  1151
	test.Next(_L("no mapping in store"));
sl@0
  1152
	test(TBLD.WritePasswordData(storeDef) == KErrNone);
sl@0
  1153
	RemountMedia();
sl@0
  1154
	test(AccessDisk() == KErrLocked);
sl@0
  1155
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1156
	test(StoresEqual(storeRd, storeDef));
sl@0
  1157
sl@0
  1158
	// Right mapping in store.
sl@0
  1159
	test.Next(_L("right mapping in store"));
sl@0
  1160
	test(TBLD.WritePasswordData(store0_1) == KErrNone);
sl@0
  1161
	RemountMedia();
sl@0
  1162
	test(AccessDisk() == KErrNone);
sl@0
  1163
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1164
	test(StoresEqual(storeRd, store0_1));
sl@0
  1165
sl@0
  1166
	// Wrong mapping in store - mapping should be removed.
sl@0
  1167
	test.Next(_L("wrong mapping in store"));
sl@0
  1168
	test(TBLD.WritePasswordData(store0_2) == KErrNone);
sl@0
  1169
	RemountMedia();
sl@0
  1170
	test(AccessDisk() == KErrLocked);
sl@0
  1171
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1172
	test(StoresEqual(storeRd, storeDef));
sl@0
  1173
sl@0
  1174
	// Redundant mapping in store.
sl@0
  1175
	test.Next(_L("redundant mapping in store"));
sl@0
  1176
	AttemptToUnlock(arb1);
sl@0
  1177
	test(TBLD.Clear(arb1) == KErrNone);
sl@0
  1178
	test(TBLD.WritePasswordData(store0_2) == KErrNone);
sl@0
  1179
	RemountMedia();
sl@0
  1180
	test(AccessDisk() == KErrNone);
sl@0
  1181
	test(TBLD.ReadPasswordData(storeRd) == KErrNone);
sl@0
  1182
	test(StoresEqual(storeRd, storeDef));
sl@0
  1183
sl@0
  1184
	test.Next(_L("clean up for following tests"));
sl@0
  1185
	TBuf8<1> nulSt;
sl@0
  1186
	test(TBLD.WritePasswordData(nulSt) == KErrNone);
sl@0
  1187
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
  1188
sl@0
  1189
	test.Next(_L("free test data"));
sl@0
  1190
	delete pstoreRd;
sl@0
  1191
	delete pstore0_2;
sl@0
  1192
	delete pstore0_1;
sl@0
  1193
	delete pstoreDef;
sl@0
  1194
sl@0
  1195
	test.End();
sl@0
  1196
	}
sl@0
  1197
sl@0
  1198
sl@0
  1199
LOCAL_C void TestPasswordFile()
sl@0
  1200
//
sl@0
  1201
// Additional test added for INC066636
sl@0
  1202
//
sl@0
  1203
// Tests that the MMC password file is created in the correct place on the disk
sl@0
  1204
// as defined by KMediaPWrdFile in f32fsys.h
sl@0
  1205
//
sl@0
  1206
// The following test cases are checked:
sl@0
  1207
//  o  Card can be locked
sl@0
  1208
//  o  Cannot lock the card or change its password if the wrong password is 
sl@0
  1209
//     specified
sl@0
  1210
//  o  Password can be changed
sl@0
  1211
//  o  Password can be removed
sl@0
  1212
//
sl@0
  1213
	{
sl@0
  1214
	const TInt KDriveNum = RFsDNum;
sl@0
  1215
sl@0
  1216
	TInt error = KErrNone;
sl@0
  1217
sl@0
  1218
sl@0
  1219
	test.Start(_L("Testing password file"));
sl@0
  1220
sl@0
  1221
	
sl@0
  1222
	test.Next(_L("open connection"));
sl@0
  1223
	RFs theFs;
sl@0
  1224
	test(theFs.Connect() == KErrNone);
sl@0
  1225
sl@0
  1226
	
sl@0
  1227
	
sl@0
  1228
	// Now set the first password that we will use
sl@0
  1229
	test.Next(_L("lock the media card"));	
sl@0
  1230
	TMediaPassword& nulPWrd = *PWDs[0];
sl@0
  1231
	TMediaPassword& oldPWrd = *PWDs[1];
sl@0
  1232
	error = theFs.LockDrive(KDriveNum, nulPWrd, oldPWrd, ETrue);
sl@0
  1233
	test(KErrNone == error);
sl@0
  1234
sl@0
  1235
sl@0
  1236
	// Verify that the password file does exist and is in the correct place
sl@0
  1237
	test.Next(_L("check password file exists"));
sl@0
  1238
	TEntry theEntry;
sl@0
  1239
	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
sl@0
  1240
	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
sl@0
  1241
	error = theFs.Entry(mediaPWrdFile, theEntry);
sl@0
  1242
	test (KErrNone == error);
sl@0
  1243
sl@0
  1244
	
sl@0
  1245
	// Attempt to set a new password without specifying the current one
sl@0
  1246
	test.Next(_L("change password failure"));	
sl@0
  1247
	TMediaPassword& newPWrd = *PWDs[2];
sl@0
  1248
	error = theFs.LockDrive(KDriveNum, nulPWrd, newPWrd, ETrue);
sl@0
  1249
	test(KErrAccessDenied == error);
sl@0
  1250
sl@0
  1251
sl@0
  1252
	// Change the password for a new one...
sl@0
  1253
	test.Next(_L("change password success"));	
sl@0
  1254
	error = theFs.LockDrive(KDriveNum, oldPWrd, newPWrd, ETrue);
sl@0
  1255
	test(KErrNone == error);
sl@0
  1256
sl@0
  1257
	
sl@0
  1258
	// Clear the password
sl@0
  1259
	test.Next(_L("clear the password"));	
sl@0
  1260
	error = theFs.ClearPassword(KDriveNum, newPWrd);
sl@0
  1261
	test(KErrNone == error);
sl@0
  1262
sl@0
  1263
sl@0
  1264
	// Check that the password has been removed from the file
sl@0
  1265
	// (KMediaPWrdFile should now be zero bytes in size)
sl@0
  1266
	test.Next(_L("check password removal"));
sl@0
  1267
	error = theFs.Entry(mediaPWrdFile, theEntry);
sl@0
  1268
	test (KErrNone == error);
sl@0
  1269
	test (0 == theEntry.iSize);
sl@0
  1270
sl@0
  1271
	
sl@0
  1272
	// Remove the password file
sl@0
  1273
	test.Next(_L("tidy up"));
sl@0
  1274
	error = theFs.Delete(mediaPWrdFile);
sl@0
  1275
	test (KErrNone == error);
sl@0
  1276
sl@0
  1277
sl@0
  1278
	theFs.Close();
sl@0
  1279
sl@0
  1280
	test.End();
sl@0
  1281
	}
sl@0
  1282
sl@0
  1283
sl@0
  1284
LOCAL_C void TestFormatErase()
sl@0
  1285
//
sl@0
  1286
// Additional test added for DEF067976 - MR1: Force Erase of MMC lock UI until complete 
sl@0
  1287
//
sl@0
  1288
// Tests that a card can be locked & then force-erased using the new format switch
sl@0
  1289
//
sl@0
  1290
// Test modified for INC073653 - RFormat::Open returns KErrNone, even if card is locked
sl@0
  1291
//
sl@0
  1292
// RFormat:Open now returns KErrLocked if media is locked (previously this wasn't returned
sl@0
  1293
// until calling RFormat::Next
sl@0
  1294
//
sl@0
  1295
//
sl@0
  1296
	{
sl@0
  1297
	TInt r = KErrNone;
sl@0
  1298
sl@0
  1299
	test.Start(_L("Testing force erase"));
sl@0
  1300
sl@0
  1301
	
sl@0
  1302
	test.Next(_L("open connection"));
sl@0
  1303
	RFs fs;
sl@0
  1304
	test(fs.Connect() == KErrNone);
sl@0
  1305
sl@0
  1306
	// Clear the password store for when function run on its own.
sl@0
  1307
	TBuf8<1> nulSt;
sl@0
  1308
	test(TBLD.WritePasswordData(nulSt) == KErrNone);// empty
sl@0
  1309
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
  1310
sl@0
  1311
	
sl@0
  1312
	test.Next(_L("lock card"));
sl@0
  1313
	// Now set the first password that we will use
sl@0
  1314
	TMediaPassword& nulPWrd = *PWDs[0];
sl@0
  1315
	TMediaPassword& oldPWrd = *PWDs[1];
sl@0
  1316
	r = fs.LockDrive(RFsDNum, nulPWrd, oldPWrd, EFalse);
sl@0
  1317
	if (r != KErrNone)
sl@0
  1318
		test.Printf(_L("RFs::LockDrive() returned %d\n"), r);
sl@0
  1319
	test(r == KErrNone);
sl@0
  1320
sl@0
  1321
	RemountMedia();		// card is now locked
sl@0
  1322
sl@0
  1323
	RFormat fmt;
sl@0
  1324
	TPckgBuf<TInt> stepPkg;
sl@0
  1325
	TDriveUnit driveUnit(RFsDNum);
sl@0
  1326
	TDriveName driveName = driveUnit.Name();
sl@0
  1327
sl@0
  1328
	test.Next(_L("format locked card"));
sl@0
  1329
	r = fmt.Open(fs, driveName, EHighDensity, stepPkg());
sl@0
  1330
	if (r != KErrLocked)
sl@0
  1331
		test.Printf(_L("RFormat::Next() returned %d\n"), r);
sl@0
  1332
	test(r == KErrLocked);
sl@0
  1333
sl@0
  1334
	test.Printf(_L("\n"));
sl@0
  1335
	fmt.Close();
sl@0
  1336
sl@0
  1337
	_LIT(KLitStars,"********************");
sl@0
  1338
	test.Next(_L("force erase locked card"));
sl@0
  1339
	r = fmt.Open(fs, driveName, EHighDensity | EForceErase, stepPkg());
sl@0
  1340
	if (r != KErrNone)
sl@0
  1341
		test.Printf(_L("RFormat::Open() returned %d\n"), r);
sl@0
  1342
	test (r == KErrNone);
sl@0
  1343
	
sl@0
  1344
	while (stepPkg() > 0)
sl@0
  1345
		{
sl@0
  1346
		TRequestStatus status;
sl@0
  1347
		fmt.Next(stepPkg, status);
sl@0
  1348
		test (status == KRequestPending || status == KErrNone);
sl@0
  1349
		User::WaitForRequest(status);
sl@0
  1350
sl@0
  1351
		TInt length=(100-stepPkg())/5;
sl@0
  1352
		length=Min(length,20);
sl@0
  1353
		TPtrC stars=KLitStars().Left(length);
sl@0
  1354
		test.Printf(_L("\r%S"),&stars);
sl@0
  1355
		}
sl@0
  1356
	test.Printf(_L("\n"));
sl@0
  1357
	fmt.Close();
sl@0
  1358
sl@0
  1359
	fs.Close();
sl@0
  1360
sl@0
  1361
	test.End();
sl@0
  1362
	}
sl@0
  1363
sl@0
  1364
LOCAL_C void TestWriteToPasswordStoreUnlocksCard()
sl@0
  1365
//
sl@0
  1366
// Additional test added for INC096612 - Writing to password store should unlock the card
sl@0
  1367
//
sl@0
  1368
// Tests that a card can be auto-unlocked just by writing to the password store (as this is what 
sl@0
  1369
// estart does)
sl@0
  1370
//
sl@0
  1371
//
sl@0
  1372
	{
sl@0
  1373
	TInt r = KErrNone;
sl@0
  1374
sl@0
  1375
	test.Start(_L("Testing writing to password store unlocks the card"));
sl@0
  1376
	
sl@0
  1377
	test.Next(_L("open connection"));
sl@0
  1378
	RFs fs;
sl@0
  1379
	test(fs.Connect() == KErrNone);
sl@0
  1380
sl@0
  1381
	// Clear the password store for when function run on its own.
sl@0
  1382
	TMediaPassword& nulPWrd = *PWDs[0];
sl@0
  1383
	TMediaPassword testPassword((const TUint8*) "xyz");
sl@0
  1384
sl@0
  1385
	test(TBLD.WritePasswordData(nulPWrd) == KErrNone);// empty
sl@0
  1386
	test(TBLD.PasswordStoreLengthInBytes() == 0);
sl@0
  1387
	
sl@0
  1388
	test.Next(_L("lock card"));
sl@0
  1389
	test.Next(_L("assign test password"));
sl@0
  1390
	r = TBLD.SetPassword(nulPWrd, testPassword, EFalse);
sl@0
  1391
	test(r == KErrNone);
sl@0
  1392
sl@0
  1393
	RemountMedia();		// card is now locked
sl@0
  1394
sl@0
  1395
	// test Caps() reports that card is locked
sl@0
  1396
	test.Next(_L("test card is locked"));
sl@0
  1397
	TLocalDriveCapsV5 driveCaps;
sl@0
  1398
	TPckg<TLocalDriveCapsV5> driveCapsPkg(driveCaps);	
sl@0
  1399
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1400
	test.Printf(_L("Caps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1401
sl@0
  1402
	test (r == KErrNone);
sl@0
  1403
	test ((driveCaps.iMediaAtt & KMediaAttLocked) != 0);
sl@0
  1404
sl@0
  1405
	// Write correct password to store
sl@0
  1406
	test.Next(_L("write correct password to store"));
sl@0
  1407
sl@0
  1408
	TPersistentStore *pstoreDef;
sl@0
  1409
	test((pstoreDef = new TPersistentStore) != NULL);
sl@0
  1410
	TPersistentStore &storeDef = *pstoreDef;
sl@0
  1411
	AddMapping(storeDef, CIDs[0], &testPassword);
sl@0
  1412
	r = TBLD.WritePasswordData(storeDef);
sl@0
  1413
sl@0
  1414
	test.Printf(_L("WritePasswordData() returned %d\n"), r);
sl@0
  1415
	
sl@0
  1416
	test(r == KErrNone);
sl@0
  1417
sl@0
  1418
	// test Caps() reports that card is unlocked
sl@0
  1419
	test.Next(_L("test card is unlocked"));
sl@0
  1420
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1421
	test.Printf(_L("Caps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1422
sl@0
  1423
	test (r == KErrNone);
sl@0
  1424
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1425
sl@0
  1426
	// Clear the password, remount and test card is unlocked
sl@0
  1427
	test.Next(_L("clear password, remount & test card is unlocked"));
sl@0
  1428
	test.Next(_L("clear the password"));	
sl@0
  1429
	test(TBLD.Clear(testPassword) == KErrNone);
sl@0
  1430
	RemountMedia();		
sl@0
  1431
	test.Next(_L("test card is unlocked"));
sl@0
  1432
sl@0
  1433
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1434
	test.Printf(_L("Caps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1435
sl@0
  1436
	test (r == KErrNone);
sl@0
  1437
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1438
sl@0
  1439
sl@0
  1440
	delete pstoreDef;
sl@0
  1441
	pstoreDef = NULL;
sl@0
  1442
	
sl@0
  1443
	test.End();
sl@0
  1444
	}
sl@0
  1445
sl@0
  1446
sl@0
  1447
LOCAL_C TBool SetupDrivesForPlatform(TInt& aDrive, TInt &aRFsDriveNum)
sl@0
  1448
/**
sl@0
  1449
 * Finds a suitable drive for the password store test
sl@0
  1450
 *
sl@0
  1451
 * @param aDrive  The number of the local drive to test
sl@0
  1452
 * @return TBool ETrue if a suitable drive is found, EFalse otherwise.
sl@0
  1453
 */
sl@0
  1454
	{
sl@0
  1455
	
sl@0
  1456
	TDriveInfoV1Buf diBuf;
sl@0
  1457
	UserHal::DriveInfo(diBuf);
sl@0
  1458
	TDriveInfoV1 &di=diBuf();
sl@0
  1459
sl@0
  1460
	test.Printf(_L(" iRegisteredDriveBitmask 0x%08X"), di.iRegisteredDriveBitmask);
sl@0
  1461
sl@0
  1462
	aDrive  = -1;
sl@0
  1463
	
sl@0
  1464
	TLocalDriveCapsV5Buf capsBuf;
sl@0
  1465
	TBusLocalDrive TBLD;
sl@0
  1466
	TLocalDriveCapsV5& caps = capsBuf();
sl@0
  1467
	TPtrC8 localSerialNum;
sl@0
  1468
	TInt registeredDriveNum = 0;
sl@0
  1469
	for(aDrive=0; aDrive < KMaxLocalDrives; aDrive++)
sl@0
  1470
		{
sl@0
  1471
		TInt driveNumberMask = 1 << aDrive;
sl@0
  1472
		if ((di.iRegisteredDriveBitmask & driveNumberMask) == 0)
sl@0
  1473
			continue;
sl@0
  1474
sl@0
  1475
		test.Printf(_L(" Drive %d -  %S\r\n"), aDrive, &di.iDriveName[registeredDriveNum]);
sl@0
  1476
sl@0
  1477
		// check that the card is readable (so we can ignore for empty card slots)
sl@0
  1478
		if ((di.iDriveName[registeredDriveNum].MatchF(_L("MultiMediaCard0")) == KErrNone) ||
sl@0
  1479
		    (di.iDriveName[registeredDriveNum].MatchF(_L("SDIOCard0")) == KErrNone))
sl@0
  1480
			{
sl@0
  1481
			
sl@0
  1482
			TBool TBLDChangedFlag;
sl@0
  1483
			TInt r = TBLD.Connect(aDrive, TBLDChangedFlag);
sl@0
  1484
//test.Printf(_L(" Connect returned %d\n"), r);
sl@0
  1485
			if (r == KErrNone)
sl@0
  1486
				{
sl@0
  1487
				r = TBLD.Caps(capsBuf);
sl@0
  1488
				localSerialNum.Set(caps.iSerialNum, caps.iSerialNumLength);
sl@0
  1489
				const TInt KSectSize = 512;
sl@0
  1490
				TBuf8<KSectSize> sect;
sl@0
  1491
				r = TBLD.Read(0, KSectSize, sect);
sl@0
  1492
//test.Printf(_L(" Read returned %d\n"), r);
sl@0
  1493
				
sl@0
  1494
				TBLD.Disconnect();
sl@0
  1495
				if (r == KErrNone)
sl@0
  1496
					break;
sl@0
  1497
				}
sl@0
  1498
			}
sl@0
  1499
		registeredDriveNum++;
sl@0
  1500
		}
sl@0
  1501
sl@0
  1502
	if(aDrive == KMaxLocalDrives)
sl@0
  1503
		{
sl@0
  1504
		test.Printf(_L(" MMC Drive Not Found\r\n"));
sl@0
  1505
		return EFalse;
sl@0
  1506
		}
sl@0
  1507
sl@0
  1508
	// Work out the file server drive number (which isn't necessarily the same 
sl@0
  1509
	// as the TBusLocalDrive drive number)
sl@0
  1510
	RFs theFs;
sl@0
  1511
	test(theFs.Connect() == KErrNone);
sl@0
  1512
sl@0
  1513
	TInt i;
sl@0
  1514
	for (i = EDriveA; i < EDriveZ; i++)
sl@0
  1515
		{
sl@0
  1516
		TMediaSerialNumber serialNum;
sl@0
  1517
	    TInt r = theFs.GetMediaSerialNumber(serialNum, i);
sl@0
  1518
		TInt len = serialNum.Length();
sl@0
  1519
		TInt n;
sl@0
  1520
		for (n=0; n<len; n+=16)
sl@0
  1521
		{
sl@0
  1522
		TBuf16<16*3 +1> buf;
sl@0
  1523
			for (TInt m=n; m<n+16; m++)
sl@0
  1524
				{
sl@0
  1525
				TBuf16<3> hexBuf;
sl@0
  1526
				hexBuf.Format(_L("%02X "),serialNum[m]);
sl@0
  1527
				buf.Append(hexBuf);
sl@0
  1528
				}
sl@0
  1529
		buf.Append(_L("\n"));
sl@0
  1530
		test.Printf(buf);
sl@0
  1531
		}
sl@0
  1532
		if (serialNum.Compare(localSerialNum) == 0)
sl@0
  1533
			{
sl@0
  1534
			TVolumeInfo vi;
sl@0
  1535
	        r = theFs.Volume(vi, i);
sl@0
  1536
			TBool sizeMatch = (vi.iSize < caps.iSize);
sl@0
  1537
			if (sizeMatch)
sl@0
  1538
				{
sl@0
  1539
				aRFsDriveNum = i;
sl@0
  1540
				break;
sl@0
  1541
				}
sl@0
  1542
			}
sl@0
  1543
		
sl@0
  1544
		}
sl@0
  1545
	if (i == EDriveZ)
sl@0
  1546
		{
sl@0
  1547
		test.Printf(_L(" RFs MMC Drive Not Found\r\n"));
sl@0
  1548
		return EFalse;
sl@0
  1549
		}
sl@0
  1550
sl@0
  1551
	theFs.Close();
sl@0
  1552
sl@0
  1553
	return ETrue;
sl@0
  1554
	}
sl@0
  1555
sl@0
  1556
sl@0
  1557
TInt TestLockCard(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword &aOldPassword, TMediaPassword& aNewPassword, TBool aStore)
sl@0
  1558
	{
sl@0
  1559
	TMediaPassword newPassWord;
sl@0
  1560
    TMediaPassword oldPassWord;
sl@0
  1561
    TInt err=0;
sl@0
  1562
    TDriveInfo dInfo;
sl@0
  1563
    
sl@0
  1564
    aFs.Drive(dInfo, RFsDNum);
sl@0
  1565
    
sl@0
  1566
    newPassWord.Append(aNewPassword);
sl@0
  1567
    oldPassWord.Append(aOldPassword);
sl@0
  1568
sl@0
  1569
    test (dInfo.iMediaAtt & KMediaAttLockable);
sl@0
  1570
      
sl@0
  1571
    err=aFs.LockDrive(RFsDNum, oldPassWord, newPassWord, aStore );
sl@0
  1572
sl@0
  1573
    aFs.Drive(dInfo, aTheMemoryCardDrive);
sl@0
  1574
    return err;   	
sl@0
  1575
	}
sl@0
  1576
sl@0
  1577
TInt TestUnlockCard(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword& aPassword, TBool aStore)
sl@0
  1578
	{
sl@0
  1579
	TMediaPassword oldPw;
sl@0
  1580
   
sl@0
  1581
	oldPw.Append(aPassword);
sl@0
  1582
   	TInt err = aFs.UnlockDrive( aTheMemoryCardDrive, oldPw, aStore);
sl@0
  1583
	return err;
sl@0
  1584
	}
sl@0
  1585
sl@0
  1586
TInt TestClearPassword(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword& aPassword)
sl@0
  1587
	{
sl@0
  1588
	TMediaPassword oldPwd = aPassword;
sl@0
  1589
sl@0
  1590
	TInt err = aFs.ClearPassword( aTheMemoryCardDrive, oldPwd );
sl@0
  1591
	return err;
sl@0
  1592
	}
sl@0
  1593
sl@0
  1594
sl@0
  1595
TInt ExecuteForcedEraseTestL(RFs& aFs, TInt aTheMemoryCardDrive)
sl@0
  1596
    {
sl@0
  1597
	TInt err = aFs.ErasePassword( aTheMemoryCardDrive );
sl@0
  1598
	return err;
sl@0
  1599
    }
sl@0
  1600
sl@0
  1601
sl@0
  1602
TBool TestLocked(RFs& aFs, TInt aTheMemoryCardDrive)
sl@0
  1603
	{
sl@0
  1604
    TDriveInfo info;
sl@0
  1605
sl@0
  1606
	TInt r = aFs.Drive(info, aTheMemoryCardDrive);
sl@0
  1607
	test (r == KErrNone);
sl@0
  1608
sl@0
  1609
	return (info.iMediaAtt & KMediaAttLocked)?(TBool)ETrue:(TBool)EFalse;
sl@0
  1610
	}
sl@0
  1611
sl@0
  1612
void WaitForPowerDownLock(RFs& aFs, TInt aTheMemoryCardDrive)
sl@0
  1613
	{
sl@0
  1614
	test.Printf(_L("Waiting for stack to power down...\n"));
sl@0
  1615
	TInt n;
sl@0
  1616
	for (n=0; n<30 && !TestLocked(aFs, aTheMemoryCardDrive); n++)
sl@0
  1617
		{
sl@0
  1618
		User::After(1000000);
sl@0
  1619
		}
sl@0
  1620
	test(n < 30);
sl@0
  1621
	test(TestLocked(aFs, aTheMemoryCardDrive));	// should now be locked
sl@0
  1622
	}
sl@0
  1623
	
sl@0
  1624
void WaitForPowerDownUnlock(RFs& /*aFs*/, TInt /*aTheMemoryCardDrive*/)
sl@0
  1625
	{
sl@0
  1626
	test.Printf(_L("Allow some time for stack to power down"));
sl@0
  1627
	for (TUint i=0; i < 80; ++i)
sl@0
  1628
    	{
sl@0
  1629
    	User::After(100000);
sl@0
  1630
    	test.Printf(_L("."));
sl@0
  1631
    	}
sl@0
  1632
    test.Printf(_L("\n"));
sl@0
  1633
	}
sl@0
  1634
	
sl@0
  1635
/*
sl@0
  1636
INC103721:
sl@0
  1637
The MMC Media drivers do not power up the MMC Stack to retrieve card status,
sl@0
  1638
the following tests ensure that the 'lock status' is correctly returned after a
sl@0
  1639
stack power down.
sl@0
  1640
*/	
sl@0
  1641
LOCAL_C void TestPowerDownStatus()
sl@0
  1642
	{
sl@0
  1643
	TInt r = KErrNone;
sl@0
  1644
	TLocalDriveCapsV5 driveCaps;
sl@0
  1645
	TPckg<TLocalDriveCapsV5> driveCapsPkg(driveCaps);	
sl@0
  1646
	TMediaPassword password = (TUint8*) "salasana";
sl@0
  1647
	TMediaPassword oldpassword;
sl@0
  1648
		
sl@0
  1649
	test.Start(_L("Testing Power Down Status Reporting"));
sl@0
  1650
sl@0
  1651
	test.Next(_L("Open Connection"));
sl@0
  1652
	RFs fs;
sl@0
  1653
	test(fs.Connect() == KErrNone);
sl@0
  1654
sl@0
  1655
// Lock card (with password stored) 
sl@0
  1656
	test.Next(_L("Locking Card - Password Stored"));
sl@0
  1657
sl@0
  1658
	test.Next(_L("Locking card (Successful)"))	;
sl@0
  1659
	r = TestLockCard(fs, RFsDNum, oldpassword, password, ETrue);
sl@0
  1660
	test(r == KErrNone); 
sl@0
  1661
		
sl@0
  1662
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1663
sl@0
  1664
	test.Next(_L("Card reports unlocked - before PowerDown"));
sl@0
  1665
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1666
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1667
sl@0
  1668
	test (r == KErrNone);
sl@0
  1669
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1670
sl@0
  1671
	WaitForPowerDownUnlock(fs, RFsDNum);
sl@0
  1672
	
sl@0
  1673
	test.Next(_L("Check card reports unlocked - after PowerDown"));
sl@0
  1674
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1675
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1676
sl@0
  1677
	test (r == KErrNone);
sl@0
  1678
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1679
	
sl@0
  1680
	test.Next(_L("Clear password (Successful)"));
sl@0
  1681
	r = TestClearPassword(fs, RFsDNum, password);
sl@0
  1682
	test(r == KErrNone);
sl@0
  1683
	
sl@0
  1684
// Lock card (without password in store)
sl@0
  1685
	test.Next(_L("Locking card - Password NOT Stored"));
sl@0
  1686
sl@0
  1687
	test.Next(_L("Locking card (Successful)"));
sl@0
  1688
	r = TestLockCard(fs, RFsDNum, oldpassword, password, EFalse);
sl@0
  1689
	test(r == KErrNone); 
sl@0
  1690
		
sl@0
  1691
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1692
	
sl@0
  1693
	test.Next(_L("Card is reports Unlocked - before PowerDown"));
sl@0
  1694
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1695
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1696
sl@0
  1697
	test (r == KErrNone);
sl@0
  1698
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1699
sl@0
  1700
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1701
	
sl@0
  1702
	test.Next(_L("Card reports Locked - after PowerDown"));
sl@0
  1703
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1704
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1705
sl@0
  1706
	test (r == KErrNone);
sl@0
  1707
	test ((driveCaps.iMediaAtt & KMediaAttLocked) != 0);
sl@0
  1708
	
sl@0
  1709
// Unlock card
sl@0
  1710
	test.Next(_L("Unlock 'locked' Card - Password Stored"));
sl@0
  1711
	
sl@0
  1712
	test.Next(_L("Unlocking card (Successful)"))	;
sl@0
  1713
	r = TestUnlockCard(fs, RFsDNum, password, ETrue);
sl@0
  1714
	test(r == KErrNone);
sl@0
  1715
	test (!TestLocked(fs, RFsDNum)); // not locked as stack hasn't powered down
sl@0
  1716
	
sl@0
  1717
	test.Next(_L("Card reports unlocked - before PowerDown"));
sl@0
  1718
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1719
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1720
sl@0
  1721
	test (r == KErrNone);
sl@0
  1722
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1723
sl@0
  1724
	WaitForPowerDownUnlock(fs, RFsDNum);
sl@0
  1725
	
sl@0
  1726
	test.Next(_L("Card reports unlocked - after PowerDown"));
sl@0
  1727
	r = TBLD.Caps(driveCapsPkg);
sl@0
  1728
	test.Printf(_L("\tCaps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
sl@0
  1729
sl@0
  1730
	test (r == KErrNone);
sl@0
  1731
	test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
sl@0
  1732
	
sl@0
  1733
	test.Next(_L("Clearing Password (Successful)"));
sl@0
  1734
	r = TestClearPassword(fs, RFsDNum, password);
sl@0
  1735
	test(r == KErrNone);
sl@0
  1736
	
sl@0
  1737
	fs.Close();
sl@0
  1738
	
sl@0
  1739
	test.End();
sl@0
  1740
	}
sl@0
  1741
sl@0
  1742
LOCAL_C void TestFsLockUnlock()
sl@0
  1743
	{
sl@0
  1744
	TInt r = KErrNone;
sl@0
  1745
sl@0
  1746
	test.Start(_L("Testing RFs APIs"));
sl@0
  1747
sl@0
  1748
	test.Next(_L("open connection"));
sl@0
  1749
	RFs fs;
sl@0
  1750
	test(fs.Connect() == KErrNone);
sl@0
  1751
sl@0
  1752
sl@0
  1753
	test.Next(_L("test locking card"));
sl@0
  1754
sl@0
  1755
	TMediaPassword oldpassword;
sl@0
  1756
	TMediaPassword newpassword = (TUint8*) "salasana";
sl@0
  1757
	TMediaPassword wrongpwd = (TUint8*) "failtest";
sl@0
  1758
sl@0
  1759
	r = TestLockCard(fs, RFsDNum, oldpassword, newpassword, EFalse);
sl@0
  1760
	test(r == KErrNone);
sl@0
  1761
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1762
sl@0
  1763
	test.Next(_L("test unlocking fails if still powered up"));
sl@0
  1764
	r = TestUnlockCard(fs, RFsDNum, newpassword, EFalse);
sl@0
  1765
	test(r == KErrAlreadyExists);		// already unlocked (as stack won't have powered down yet)
sl@0
  1766
	test (!TestLocked(fs, RFsDNum));
sl@0
  1767
sl@0
  1768
	test.Next(_L("test clearing succeeds if still powered up"));
sl@0
  1769
	r = TestClearPassword(fs, RFsDNum, newpassword);
sl@0
  1770
	test(r == KErrNone);
sl@0
  1771
	test(!TestLocked(fs, RFsDNum));
sl@0
  1772
	
sl@0
  1773
	test.Next(_L("test locking card again"));
sl@0
  1774
	r = TestLockCard(fs, RFsDNum, oldpassword, newpassword, EFalse);
sl@0
  1775
	test(r == KErrNone);
sl@0
  1776
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1777
sl@0
  1778
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1779
sl@0
  1780
	// DEF111681: CheckDisk is returning bad error code when run on locked SD card
sl@0
  1781
	// RFs::CheckDisk() should return KErrNone or KErrLocked (not KErrCorrupt) if the card is locked and the 
sl@0
  1782
	// stack powers down
sl@0
  1783
	// NB For FAT16 cards, the FAT will be entirely cached so CheckDisk will not actually access the media
sl@0
  1784
	// so KErrNone will be returned. For FAT32 cards, KErrLocked will be returned.
sl@0
  1785
	test.Next(_L("test CheckDisk() returns KErrLocked if the card is locked and the stack powered down"));
sl@0
  1786
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1787
	TFileName sessionPath;
sl@0
  1788
	sessionPath=_L("?:\\");
sl@0
  1789
	TChar driveLetter;
sl@0
  1790
	r = fs.DriveToChar(RFsDNum,driveLetter);
sl@0
  1791
	test(r==KErrNone);
sl@0
  1792
	sessionPath[0]=(TText)driveLetter;
sl@0
  1793
	r = fs.CheckDisk(sessionPath);
sl@0
  1794
	test(r == KErrNone || r == KErrLocked);
sl@0
  1795
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1796
sl@0
  1797
sl@0
  1798
	// DEF111700: Formatting a locked SD/MMC leaves it in a bad state (causes panics later)
sl@0
  1799
	// This was caused by format calling TDrive::MountMedia(ETrue) and then not dismounting
sl@0
  1800
	r = fs.RemountDrive(RFsDNum);
sl@0
  1801
	test (r == KErrNone);
sl@0
  1802
	RFormat fmt;
sl@0
  1803
	TPckgBuf<TInt> stepPkg;
sl@0
  1804
	TDriveUnit driveUnit(RFsDNum);
sl@0
  1805
	TDriveName driveName = driveUnit.Name();
sl@0
  1806
	test.Next(_L("format locked card"));
sl@0
  1807
	r = fmt.Open(fs, driveName, EHighDensity, stepPkg());
sl@0
  1808
	if (r != KErrLocked)
sl@0
  1809
		test.Printf(_L("RFormat::Next() returned %d\n"), r);
sl@0
  1810
	test(r == KErrLocked);
sl@0
  1811
	test.Printf(_L("\n"));
sl@0
  1812
	fmt.Close();
sl@0
  1813
	r = fs.CheckDisk(sessionPath);
sl@0
  1814
	test(r == KErrLocked);
sl@0
  1815
sl@0
  1816
sl@0
  1817
	test.Next(_L("test unlocking fails after powered down & unlocked with wrong password"));
sl@0
  1818
	r = TestUnlockCard(fs, RFsDNum, wrongpwd, EFalse);
sl@0
  1819
	test(r == KErrAccessDenied);		// unlocked should now fail
sl@0
  1820
sl@0
  1821
	test.Next(_L("test unlocking succeeds for correct password after powered down & locked"));
sl@0
  1822
	r = TestUnlockCard(fs, RFsDNum, newpassword, EFalse);
sl@0
  1823
	test(r == KErrNone);		// unlocked should now succeed
sl@0
  1824
sl@0
  1825
	test.Next(_L("test unlocking fails after successful unlock"));
sl@0
  1826
	r = TestUnlockCard(fs, RFsDNum, wrongpwd, EFalse);
sl@0
  1827
	test(r == KErrAlreadyExists);		// unlocked should now succeed
sl@0
  1828
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1829
sl@0
  1830
	test.Next(_L("test locking card with new password (with wrong password as old password)"));
sl@0
  1831
	r = TestLockCard(fs, RFsDNum, wrongpwd, newpassword, EFalse);
sl@0
  1832
	test(r == KErrAccessDenied);
sl@0
  1833
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1834
sl@0
  1835
	test.Next(_L("test locking card with new password (with right password as old password)"));
sl@0
  1836
	r = TestLockCard(fs, RFsDNum, newpassword, wrongpwd, EFalse);
sl@0
  1837
	test(r == KErrNone);
sl@0
  1838
	test(!TestLocked(fs, RFsDNum));	// not locked yet as stack hasn't powered down
sl@0
  1839
sl@0
  1840
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1841
	
sl@0
  1842
	test.Next(_L("test clearing fails with wrong password if powered down & locked"));
sl@0
  1843
	r = TestClearPassword(fs, RFsDNum, newpassword); // Note: we have set the wrong password as the new password
sl@0
  1844
	test(r == KErrAccessDenied);
sl@0
  1845
	test(TestLocked(fs, RFsDNum));
sl@0
  1846
sl@0
  1847
	test.Next(_L("test clearing succeeds with right password if powered down & locked"));
sl@0
  1848
	r = TestClearPassword(fs, RFsDNum, wrongpwd);
sl@0
  1849
	test(r == KErrNone);
sl@0
  1850
	test(!TestLocked(fs, RFsDNum));
sl@0
  1851
sl@0
  1852
	test.Next(_L("test locking card again"));
sl@0
  1853
	r = TestLockCard(fs, RFsDNum, oldpassword, newpassword, EFalse);
sl@0
  1854
	test(r == KErrNone);
sl@0
  1855
	test(!TestLocked(fs, RFsDNum));		// not locked yet as stack hasn't powered down
sl@0
  1856
sl@0
  1857
	test.Next(_L("test forced erase fails if still powered up"));
sl@0
  1858
	r = ExecuteForcedEraseTestL(fs, RFsDNum);
sl@0
  1859
	test(r == KErrAccessDenied);		// fails because card is not yet locked
sl@0
  1860
sl@0
  1861
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1862
sl@0
  1863
sl@0
  1864
	test.Next(_L("test forced erase succeeds if powered down & locked"));
sl@0
  1865
	r = ExecuteForcedEraseTestL(fs, RFsDNum);
sl@0
  1866
	test(r == KErrNone);
sl@0
  1867
sl@0
  1868
	fs.Close();
sl@0
  1869
	test.End();
sl@0
  1870
	}
sl@0
  1871
sl@0
  1872
sl@0
  1873
sl@0
  1874
/**
sl@0
  1875
PDEF104639: Phone automatically reboots when inserting memory card with password. 
sl@0
  1876
Testing that TheFs.UnlockDrive() results in a notification - and doesn't crash the file server (!)
sl@0
  1877
*/
sl@0
  1878
void TestUnlockDriveNotifyChange()
sl@0
  1879
	{
sl@0
  1880
	RFs fs;
sl@0
  1881
	test(fs.Connect() == KErrNone);
sl@0
  1882
sl@0
  1883
	TFileName sessionPath;
sl@0
  1884
	sessionPath=_L("?:\\");
sl@0
  1885
	TChar driveLetter;
sl@0
  1886
	TInt r=fs.DriveToChar(RFsDNum,driveLetter);
sl@0
  1887
	test(r==KErrNone);
sl@0
  1888
	sessionPath[0]=(TText)driveLetter;
sl@0
  1889
	r=fs.SetSessionPath(sessionPath);
sl@0
  1890
	test(r==KErrNone);
sl@0
  1891
    
sl@0
  1892
	TInt nRes;
sl@0
  1893
    TDriveInfo dInfo;
sl@0
  1894
sl@0
  1895
    nRes = fs.Drive(dInfo, RFsDNum);
sl@0
  1896
	test(nRes == KErrNone);
sl@0
  1897
	if (!(dInfo.iMediaAtt & KMediaAttLockable))
sl@0
  1898
		{
sl@0
  1899
		test.Printf(_L("Drive %d is not lockable %d\n"), RFsDNum);
sl@0
  1900
		fs.Close();
sl@0
  1901
		return;
sl@0
  1902
		}
sl@0
  1903
sl@0
  1904
	// attempt to lock the drive
sl@0
  1905
	TMediaPassword oldPassword;
sl@0
  1906
	TMediaPassword newPassword = (TUint8*) "salasana";
sl@0
  1907
    nRes = fs.LockDrive(RFsDNum, oldPassword, newPassword, EFalse );
sl@0
  1908
	test(nRes == KErrNone);
sl@0
  1909
sl@0
  1910
	WaitForPowerDownLock(fs, RFsDNum);
sl@0
  1911
sl@0
  1912
    TRequestStatus reqStatNotify1(KRequestPending);
sl@0
  1913
    
sl@0
  1914
    //-- set up notifier
sl@0
  1915
    fs.NotifyChange(ENotifyAll, reqStatNotify1, sessionPath);
sl@0
  1916
    test(reqStatNotify1.Int() == KRequestPending);
sl@0
  1917
sl@0
  1918
    //-- unlock the drive
sl@0
  1919
   	nRes = fs.UnlockDrive(RFsDNum, newPassword, EFalse);
sl@0
  1920
	test.Printf(_L("UnlockDrive() %d reqStatNotify1 %d\n"), nRes, reqStatNotify1.Int());
sl@0
  1921
    
sl@0
  1922
    //-- check that the notifier worked
sl@0
  1923
    User::WaitForRequest(reqStatNotify1);
sl@0
  1924
    test(reqStatNotify1.Int() == KErrNone);
sl@0
  1925
sl@0
  1926
	r = TestClearPassword(fs, RFsDNum, newPassword);
sl@0
  1927
	test(r == KErrNone);
sl@0
  1928
	test(!TestLocked(fs, RFsDNum));
sl@0
  1929
	
sl@0
  1930
	
sl@0
  1931
	
sl@0
  1932
	fs.Close();
sl@0
  1933
	}
sl@0
  1934
sl@0
  1935
sl@0
  1936
LOCAL_C void RunTests()
sl@0
  1937
//
sl@0
  1938
// Main test routine.  Calls other test functions.
sl@0
  1939
//
sl@0
  1940
	{
sl@0
  1941
	__UHEAP_MARK;
sl@0
  1942
sl@0
  1943
	if(TBLDNum == -1)
sl@0
  1944
		{
sl@0
  1945
		if(!SetupDrivesForPlatform(TBLDNum, RFsDNum))
sl@0
  1946
			{
sl@0
  1947
			test.Printf(_L("MMC Drive Not Found - Skipping test\r\n"));
sl@0
  1948
			return;
sl@0
  1949
			}
sl@0
  1950
		}
sl@0
  1951
sl@0
  1952
	test.Next(_L("Connecting TBLD"));
sl@0
  1953
	test(TBLD.Connect(TBLDNum, TBLDChangedFlag) == KErrNone);
sl@0
  1954
sl@0
  1955
	test.Next(_L("Allocating test data"));
sl@0
  1956
	AllocateTestData();
sl@0
  1957
sl@0
  1958
	test.Next(_L("Testing locking / unlocking using file server APIs"));
sl@0
  1959
	TestFsLockUnlock();
sl@0
  1960
	
sl@0
  1961
	test.Next(_L("Testing Power Down Status Reporting using file server APIs"));
sl@0
  1962
	TestPowerDownStatus();
sl@0
  1963
sl@0
  1964
    test.Next(_L("Testing RFs::NotifyChange() with RFs::UnlockDrive()"));
sl@0
  1965
	TestUnlockDriveNotifyChange();
sl@0
  1966
sl@0
  1967
	test.Next(_L("Forced Erase"));
sl@0
  1968
	TestFormatErase();
sl@0
  1969
	test.Next(_L("Testing store management"));
sl@0
  1970
	TestStaticStore();
sl@0
  1971
	test.Next(_L("Testing locking functions"));
sl@0
  1972
	TestLockUnlock();
sl@0
  1973
	test.Next(_L("Testing Elide Passwords"));
sl@0
  1974
	TestElidePasswords();
sl@0
  1975
	test.Next(_L("Testing Null Passwords"));
sl@0
  1976
	TestNullPasswords();
sl@0
  1977
	test.Next(_L("Testing controller store"));
sl@0
  1978
	TestControllerStore();
sl@0
  1979
	test.Next(_L("Testing auto unlock"));
sl@0
  1980
	TestAutoUnlock();
sl@0
  1981
	test.Next(_L("Testing password file"));
sl@0
  1982
	TestPasswordFile();
sl@0
  1983
	test.Next(_L("Testing writing a valid password to store unlocks card"));
sl@0
  1984
	TestWriteToPasswordStoreUnlocksCard();
sl@0
  1985
sl@0
  1986
	test.Next(_L("Disconnecting TBLD"));
sl@0
  1987
	TBLD.Disconnect();
sl@0
  1988
sl@0
  1989
	test.Next(_L("Deleting test data"));
sl@0
  1990
	DeleteTestData();
sl@0
  1991
sl@0
  1992
	__UHEAP_MARKEND;
sl@0
  1993
	}
sl@0
  1994
sl@0
  1995
sl@0
  1996
TInt E32Main()
sl@0
  1997
	{
sl@0
  1998
	
sl@0
  1999
	test.Title();
sl@0
  2000
	test.Start(_L("E32Main"));
sl@0
  2001
	
sl@0
  2002
	RunTests();
sl@0
  2003
sl@0
  2004
	test.End();
sl@0
  2005
	test.Close();
sl@0
  2006
sl@0
  2007
	return KErrNone;
sl@0
  2008
	}
sl@0
  2009