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