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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\server\t_pwstr.cpp
15 // Tests peripheral bus controller password store.
24 #include <e32def_private.h>
27 // define this macro to autodetect card re-insertion
28 #define __AUTO_DETECT_MEDIA_CHANGE__
30 const TUint KMMCCIDLength=16;
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
45 TUint8 iData[KMMCCIDLength]; // Big endian 128 bit bitfield representing CID
51 static inline TUint32 BigEndian32(const TUint8*);
52 static inline void BigEndian4Bytes(TUint8* aPtr, TUint32 aVal);
56 // -------- class TCID --------
58 inline TCID::TCID(const TUint8* aPtr)
59 {memcpy(&iData[0], aPtr, KMMCCIDLength);}
61 inline TCID& TCID::operator=(const TCID& aCID)
62 {memcpy(&iData[0], &aCID.iData[0], KMMCCIDLength); return(*this);}
64 inline TCID& TCID::operator=(const TUint8* aPtr)
65 {memcpy(&iData[0], aPtr, KMMCCIDLength); return(*this);}
67 inline TBool TCID::operator==(const TCID& aCID) const
68 {return(memcompare(&iData[0],KMMCCIDLength,&aCID.iData[0],KMMCCIDLength)==0);}
70 inline TBool TCID::operator==(const TUint8* aPtr) const
71 {return(memcompare(&iData[0],KMMCCIDLength,aPtr,KMMCCIDLength)==0);}
73 inline void TCID::Copy(TUint8* aPtr) const
74 {memcpy(aPtr, &iData[0], KMMCCIDLength);}
76 inline TUint8 TCID::At(TUint anIndex) const
77 {return(iData[KMMCCIDLength-1-anIndex]);}
80 inline TUint32 TMMC::BigEndian32(const TUint8* aPtr)
81 {return( (aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | (aPtr[3]) );}
83 inline void TMMC::BigEndian4Bytes(TUint8* aPtr, TUint32 aVal)
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);
93 LOCAL_D RTest test(_L("T_PWSTR"));
95 LOCAL_D TBusLocalDrive TBLD;
96 LOCAL_D TBool TBLDChangedFlag;
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.
102 LOCAL_D TInt RFsDNum = -1; // File Server Drive number
106 TInt iCIDIdx; // index in CID
107 TInt iPWDIdx; // index in PWD
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;
115 const TInt KPWDCnt(4);
116 LOCAL_C TMediaPassword *PWDs[KPWDCnt];
118 //Allocate enough unique CIDs to be able to overflow the store
119 const TInt KCIDCnt(KMaxNumOfStoreEntries+1);
120 LOCAL_C TCID *CIDs[KCIDCnt];
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];
128 // Static function prototypes.
130 LOCAL_C void AllocateTestData();
131 LOCAL_C void DeleteTestData();
133 LOCAL_C void AllocateCIDs();
134 LOCAL_C void DeleteCIDs();
136 LOCAL_C void AllocatePasswords();
137 LOCAL_C void DeletePasswords();
139 LOCAL_C void SetUpMapSizes();
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();
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();
156 LOCAL_C TInt AccessDisk();
157 LOCAL_C void TestAutoUnlock();
159 LOCAL_C void RunTests();
164 LOCAL_C void AllocateCIDs()
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.)
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.
177 // For actual card tests, CIDs[0] must correspond to the card's actual CID.
182 static TUint8 ht0[KMMCCIDLength] = // CID0
184 0x06, 0x00, 0x00, 0x31,
185 0x36, 0x4d, 0x20, 0x20,
186 0x20, 0x00, 0xb4, 0xff,
187 0xff, 0xff, 0x63, 0xd9
190 static TUint8 ht0[KMMCCIDLength] = // BPC2
192 0x06, 0x00, 0x00, 0x31,
193 0x36, 0x4d, 0x20, 0x20,
194 0x20, 0x00, 0x89, 0xff,
195 0xff, 0xff, 0x63, 0xa7
199 test.Start(_L("AllocateCIDs"));
202 for (i = 0; i < KCIDCnt; i++)
204 TUint8 bf[KMMCCIDLength];
209 bf[3] = TUint8('0' + i);
210 for (j = 4; j < KMMCCIDLength - 1; j++)
212 bf[KMMCCIDLength - 1] = '#';
217 TLocalDriveCapsV5 driveCaps;
218 TPckg<TLocalDriveCapsV5> driveCapsPkg(driveCaps);
219 if(TBLD.Caps(driveCapsPkg) == KErrNone)
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)
225 for(cidIdx=0; cidIdx<KMMCCIDLength; cidIdx++)
227 bf[cidIdx] = driveCaps.iSerialNum[KMMCCIDLength-cidIdx-1];
231 if(cidIdx == KMMCCIDLength)
233 test((CIDs[i] = new TCID(bf)) != NULL);
238 test((CIDs[i] = new TCID(bf)) != NULL);
240 test((CIDs[i] = new TCID(ht0)) != NULL);
246 test((CIDs[i] = new TCID(bf)) != NULL);
254 LOCAL_C void DeleteCIDs()
256 // Deletes static global media identifiers from the heap.
259 test.Start(_L("DeleteCIDs"));
262 for (i = 0; i < KCIDCnt; i++)
269 LOCAL_C void AllocatePasswords()
271 // Allocates a set of static global TMediaPassword objects on the heap.
272 // The passwords range from zero to 16 bytes in length.
275 test.Start(_L("AllocatePasswords"));
278 for (i = 0; i < KPWDCnt; i++)
280 test((PWDs[i] = new TMediaPassword) != NULL);
282 for (j = 0; j < i * 2; j++)
283 PWDs[i]->Append(TChar('a' + i + j));
290 LOCAL_C void DeletePasswords()
292 // Deletes static global TMediaPassword objects from the heap.
295 test.Start(_L("DeletePasswords"));
298 for (i = 0; i < KPWDCnt; i++)
305 LOCAL_C void SetUpMapSizes()
307 // Initializes static global mapSizes[,] with the persistent store mapping
308 // sizes of each CID and password.
311 test.Start(_L("SetUpMapSizes"));
314 for (i = 0; i < KCIDCnt; i++)
318 for (j = 0; j < KPWDCnt; j++)
319 mapSizes[i][j] = KMMCCIDLength + sizeof(TInt32) + PWDs[j]->Length();
326 LOCAL_C void AllocateTestData()
328 // Allocates all test data objects on the heap.
338 LOCAL_C void DeleteTestData()
340 // Frees all test data objects on the heap.
351 LOCAL_C void TestStaticStore()
353 // Tests the non card specific virtual functions in DPeriphBusController.
354 // TInt ReadPasswordData(TDes8 &aBuf);
355 // TInt WritePasswordData(const TDesC8 &aBuf);
356 // TInt PasswordStoreLengthInBytes();
358 // store is reset at start of DMMCController::WritePasswordData().
361 test.Start(_L("TestStore"));
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;
371 // WritePasswordData()
373 test.Next(_L("WritePasswordData()"));
375 test(TBLD.WritePasswordData(wStore) == KErrNone);// empty
376 test(TBLD.PasswordStoreLengthInBytes() == 0);
378 AddMapping(wStore, CIDs[1], PWDs[1]); // exactly one entry
379 test(TBLD.WritePasswordData(wStore) == KErrNone);
380 test(TBLD.PasswordStoreLengthInBytes() == mapSizes[1][1]);
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]);
387 for (i = 0; i < wStore.Length(); i++) // corrupt (partial)
390 TInt r(TBLD.WritePasswordData(wStore));
391 if (i == 0 || i == mapSizes[0][0] || i == mapSizes[0][0] + mapSizes[1][1])
394 test(r == KErrCorrupt && TBLD.PasswordStoreLengthInBytes() == 0);
397 test.Next(_L("Exceeding password store size"));
399 wStore.Zero(); // empty password store
400 test(TBLD.WritePasswordData(wStore) == KErrNone);
402 test.Printf(_L("Adding mappings...\n"));
404 const TMediaPassword password(_L8("abcdefghijklmnop")); //Need a max length password (KMaxMediaPassword)
405 for(TInt n=0; n<KCIDCnt; ++n)
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);
418 // ReadPasswordData().
420 test.Next(_L("ReadPasswordData()"));
422 wStore.Zero(); // empty
423 test(TBLD.WritePasswordData(wStore) == KErrNone);
424 test(TBLD.ReadPasswordData(rStore) == KErrNone);
425 test(rStore.Length() == 0);
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());
433 rStore.SetLength(TBLD.PasswordStoreLengthInBytes() + 4);
434 test(TBLD.ReadPasswordData(rStore) == 0);
435 test(rStore.Length() == TBLD.PasswordStoreLengthInBytes());
437 TBuf8<2> srStore; // max lt store len
438 test(TBLD.ReadPasswordData(srStore) == KErrOverflow);
440 // Stress test high turnover with memory failure.
442 test.Next(_L("Memory test"));
444 TInt r; // error code
447 for (m = 1; m < 100; m++)
449 __KHEAP_SETFAIL(RHeap::EDeterministic, m);
452 for (j = 1; j < KCIDCnt - 1; j++)
455 for (k = 1; k < KPWDCnt - 1; k++)
459 AddMapping(wStore, CIDs[j], PWDs[k]);
460 AddMapping(wStore, CIDs[j + 1], PWDs[k + 1]);
462 if ((r = TBLD.WritePasswordData(wStore)) != KErrNone)
464 test(r == KErrNoMemory);
465 test(TBLD.PasswordStoreLengthInBytes() == 0);
469 test(TBLD.ReadPasswordData(rStore) == KErrNone);
470 test(IsStoreValid(rStore) && StoresEqual(rStore, wStore));
475 } // for (m = 1; m < 16; m++)
477 // Clear the store for subsequent tests.
480 test(TBLD.WritePasswordData(wStore) == KErrNone);
481 test(TBLD.PasswordStoreLengthInBytes() == 0);
490 LOCAL_C void AddMapping(TDes8 &aSt, const TCID *aCID, const TMediaPassword *aPWD)
492 // Adds aCID |-> aPWD mapping to persistent file's store contents.
495 aSt.SetLength(aSt.Length() + KMMCCIDLength);
496 aCID->Copy(&aSt[aSt.Length() - KMMCCIDLength]);
498 TUint8 lenBuf[sizeof(TInt32)]; // TInt32, big endian
499 TMMC::BigEndian4Bytes(lenBuf, TInt32(aPWD->Length()));
500 aSt.Append(&lenBuf[0], sizeof(TInt32));
506 LOCAL_C TBool IsStoreValid(const TDesC8 &aSt)
508 // Checks the integrity of the supplied buffer.
511 TInt iBIdx; // buffer index
512 TBool corrupt(EFalse); // abort flag
513 for (iBIdx = 0; iBIdx < aSt.Length(); /* nop */)
515 // Enough raw data for CID, PWD_LEN and 1 byte of PWD.
516 corrupt = TUint(aSt.Length() - iBIdx) < KMMCCIDLength + sizeof(TInt32) + 1;
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()));
524 (pwd_len <= KMaxMediaPassword)
525 && aSt.Length() - iBIdx >= TInt(sizeof(TInt32)) + pwd_len );
529 // skip over PWD_LEN and PWD to next entry.
530 iBIdx += sizeof(TInt32) + pwd_len;
534 DumpStore(_L("invalid"), aSt);
540 LOCAL_C void PrintCID(const TCID &aCID)
542 // Prints the 128 bit CID in big endian format.
545 test.Printf(_L("CID: "));
547 for (i = 0; i < TInt(KMMCCIDLength); i += 4)
550 for (j = i; j < i + 4; ++j)
552 test.Printf(_L("%02x: %02x "), j, aCID.At(KMMCCIDLength - j - 1));
554 test.Printf(_L("\n"));
559 LOCAL_C void ParseStore(const TDesC8 &aSt, CArrayFixSeg<TTestMapping> *aMP)
561 // Fills aMP with the mappings in aSt.
564 TInt iBIdx; // buffer index
565 TInt r(KErrNone); // exit code
566 for (iBIdx = 0; r == KErrNone && iBIdx < aSt.Length(); /* nop */)
568 // Calculate index for CID.
569 TPtrC8 pCID(aSt.Mid(iBIdx, KMMCCIDLength)); // CID
570 const TCID cid(pCID.Ptr());
572 for (cidIdx = 0; cidIdx < KCIDCnt && !(*(CIDs[cidIdx]) == cid); cidIdx++)
574 // If invalid CID then print CID with valid CIDs.
575 if (!(cidIdx < KCIDCnt))
577 test.Printf(_L("ParseStore: invalid CID\n"));
580 for (i = 0; i < KCIDCnt; i++)
582 test.Printf(_L("ParseStore: valid CID %d\n"), i);
588 const TInt32 pwd_len(TMMC::BigEndian32(&aSt[iBIdx + KMMCCIDLength]));
590 // Calculate index for PWD.
592 pwd.Copy(&aSt[iBIdx + KMMCCIDLength + sizeof(TInt32)], pwd_len);
595 for (pwdIdx = 0; pwdIdx < KPWDCnt && *PWDs[pwdIdx] != pwd; pwdIdx++)
597 test(pwdIdx < KPWDCnt);
602 TRAP(r, aMP->InsertL(0, mp));
605 iBIdx += KMMCCIDLength + sizeof(TInt32) + pwd_len;
610 LOCAL_C void DumpStore(const TDesC &aName, const TDesC8 &aSt)
612 // Prints the contents of the supplied store.
615 test.Printf(_L("\nstore %S: len = %d\n"), &aName, aSt.Length());
618 for (i = 0; i < aSt.Length(); i += 8)
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"));
628 LOCAL_C TBool StoresEqual(const TDesC8 &aSt0, const TDesC8 &aSt1)
630 // Compares aSt1 with aSt2. Return value indicates whether or not the
631 // stores contain exactly the same mappings, but not necessarily in the
637 CArrayFixSeg<TTestMapping> *ramp0, *ramp1;
639 test((ramp0 = new(ELeave) CArrayFixSeg<TTestMapping>(2)) != NULL);
640 test((ramp1 = new(ELeave) CArrayFixSeg<TTestMapping>(2)) != NULL);
642 test(IsStoreValid(aSt0));
643 test(IsStoreValid(aSt1));
645 ParseStore(aSt0, ramp0);
646 ParseStore(aSt1, ramp1);
648 TArray<TTestMapping> a0(ramp0->Array());
649 TArray<TTestMapping> a1(ramp1->Array());
651 if (a0.Count() == a1.Count())
652 // if #a0 == #a1 and a0 <= a1 then a0 == a1.
654 TBool allInA1(ETrue);
656 for (i = 0; allInA1 && i < a0.Count(); i++)
660 for (j = 0; ! found && j < a0.Count(); j++)
663 a0[i].iCIDIdx == a1[j].iCIDIdx
664 && a0[i].iPWDIdx == a1[j].iPWDIdx );
677 DumpStore(_L("0"), aSt0);
678 DumpStore(_L("1"), aSt1);
685 LOCAL_C void RemountMedia()
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.
692 // TBLD.ForceMediaChange();
693 // UserSvr::ForceRemountMedia(ERemovableMedia0);
694 // User::After(1 * 1000 * 1000);
697 #ifdef __AUTO_DETECT_MEDIA_CHANGE__
699 test(fs.Connect() == KErrNone);
701 test.Printf(_L("Remove and re-insert card.."));
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"));
715 if (r == KErrNotReady)
716 test.Printf(_L("\rRemove and re-insert card.."));
718 if (r != KErrNone && r != KErrNotReady && r != KErrLocked)
719 test.Printf(_L("AccessDisk() returned %d"), r);
721 while (r == KErrNotReady);
723 test.Printf(_L("\n"));
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')
738 LOCAL_C void AttemptToUnlock(TMediaPassword &aPWD, TBool aStore)
740 // Tests that the card is locked and then tries to unlock it.
743 TInt r = AccessDisk();
745 test.Printf(_L("AccessDisk() returned %d\n"), r);
746 test(r == KErrLocked);
747 test(TBLD.Unlock(aPWD, aStore) == KErrNone);
751 LOCAL_C void TestLockUnlock()
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.
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
762 // Locked means inaccessible, not just has password.
765 test.Start(_L("TestLockUnlock"));
767 TMediaPassword nul(*PWDs[0]);
768 TMediaPassword arb1(*PWDs[1]);
769 TMediaPassword arb2(*PWDs[2]);
771 // Clear the password store for when function run on its own.
773 test(TBLD.WritePasswordData(nulSt) == KErrNone);// empty
774 test(TBLD.PasswordStoreLengthInBytes() == 0);
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
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
785 test.Next(_L("unlock locked card"));
786 test(TBLD.Unlock(arb2, EFalse) == KErrAccessDenied); // unlock locked wrong
787 AttemptToUnlock(arb1);
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
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
797 test.Next(_L("clear unlocked card"));
798 test(TBLD.Clear(arb2) == KErrAccessDenied); // clear unlocked wrong
800 //!!! If clear with wrong password, cannot clear with right password in same
801 // power session (H).
803 AttemptToUnlock(arb1);
804 test(TBLD.Clear(arb1) == KErrNone);
806 test.Next(_L("assign test password"));
807 test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone); // give test password
808 RemountMedia(); // make inaccessible
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
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);
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.
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.
834 LOCAL_C void TestElidePasswords()
836 test.Start(_L("TestElidePasswords"));
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");
842 TPersistentStore* pstoreAB;
843 test((pstoreAB = new TPersistentStore) != 0);
844 TPersistentStore& storeAB = *pstoreAB;
845 AddMapping(storeAB, CIDs[0], &ab);
847 TPersistentStore* pstoreCXYZ;
848 test((pstoreCXYZ = new TPersistentStore) != 0);
849 TPersistentStore& storeCXYZ = *pstoreCXYZ;
850 AddMapping(storeCXYZ, CIDs[0], &cxyz);
852 TPersistentStore *pstoreRd; // scratch for reading
853 test((pstoreRd = new TPersistentStore) != NULL);
854 TPersistentStore& storeRd = *pstoreRd;
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);
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);
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);
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);
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);
895 * test the special cases where null passwords are used. These are all failed with
896 * KErrAccessDenied by the controller.
899 LOCAL_C void TestNullPasswords()
901 test.Start(_L("TestNullPasswords"));
903 TMediaPassword nul(*PWDs[0]);
904 TMediaPassword arb1(*PWDs[1]);
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);
911 test.Next(_L("card has password and is unlocked"));
912 test(TBLD.SetPassword(nul, arb1, ETrue) == KErrNone);
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);
919 test.Next(_L("clean up for following tests"));
920 test(TBLD.Clear(arb1) == KErrNone);
922 test(TBLD.WritePasswordData(nulSt) == KErrNone);
923 test(TBLD.PasswordStoreLengthInBytes() == 0);
929 LOCAL_C void TestControllerStore()
931 // Performs standard password functions but stores the mappings in the controller store.
933 // + mapping added to store (if not exists)
934 // - mapping removed from store (if exists)
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-
941 // Locked means inaccessible, not just has password.
942 // When the user supplies a password, the mapping in the password store is not used.
944 // 1. A locked card with the right mapping in the store cannot happen because of the
945 // automatic unlocking mechanism.
947 // Tests start with an unlocked card that has no password.
950 test.Start(_L("TestControllerStore"));
952 test.Next(_L("allocate test data"));
954 TMediaPassword nul(*PWDs[0]);
955 TMediaPassword arb1(*PWDs[1]);
956 TMediaPassword arb2(*PWDs[2]);
958 TPersistentStore *pstoreDef; // { 3 |-> 3 }
959 test((pstoreDef = new TPersistentStore) != NULL);
960 TPersistentStore &storeDef = *pstoreDef;
961 AddMapping(storeDef, CIDs[3], PWDs[3]);
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]);
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]);
975 TPersistentStore *pstoreRd; // temp for reading
976 test((pstoreRd = new TPersistentStore) != NULL);
977 TPersistentStore &storeRd = *pstoreRd;
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);
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));
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));
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));
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));
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));
1025 // Unlock locked right in - see note 1.
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));
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));
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));
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));
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));
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));
1077 // Clear password for subsequent tests.
1079 test.Next(_L("clean up for following tests"));
1080 test(TBLD.WritePasswordData(storeDef) == KErrNone);
1082 AttemptToUnlock(arb1);
1083 test(TBLD.Clear(arb1) == KErrNone);
1085 test(TBLD.WritePasswordData(nulSt) == KErrNone);
1086 test(TBLD.PasswordStoreLengthInBytes() == 0);
1088 test.Next(_L("free test data"));
1099 LOCAL_C TInt AccessDisk()
1101 // Attempts to read the first sector of the removable media to determine whether
1105 const TInt KSectSize = 512;
1106 TBuf8<KSectSize> sect; // 8 + 512
1108 return TBLD.Read(0, KSectSize, sect);
1112 LOCAL_C void TestAutoUnlock()
1114 // Tests controller internal store unlocking mechanism.
1115 // A locked card should be transparently unlocked after the peripheral bus is
1119 test.Start(_L("TestAutoUnlock"));
1121 test.Next(_L("allocate test data"));
1123 TMediaPassword nul(*PWDs[0]);
1124 TMediaPassword arb1(*PWDs[1]);
1126 TPersistentStore *pstoreDef; // { 3 |-> 3 }
1127 test((pstoreDef = new TPersistentStore) != NULL);
1128 TPersistentStore &storeDef = *pstoreDef;
1129 AddMapping(storeDef, CIDs[3], PWDs[3]);
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]);
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]);
1143 TPersistentStore *pstoreRd; // temp for reading
1144 test((pstoreRd = new TPersistentStore) != NULL);
1145 TPersistentStore &storeRd = *pstoreRd;
1147 test.Next(_L("assign password"));
1148 test(TBLD.SetPassword(nul, arb1, EFalse) == KErrNone); // give password
1150 // No mapping in store.
1151 test.Next(_L("no mapping in store"));
1152 test(TBLD.WritePasswordData(storeDef) == KErrNone);
1154 test(AccessDisk() == KErrLocked);
1155 test(TBLD.ReadPasswordData(storeRd) == KErrNone);
1156 test(StoresEqual(storeRd, storeDef));
1158 // Right mapping in store.
1159 test.Next(_L("right mapping in store"));
1160 test(TBLD.WritePasswordData(store0_1) == KErrNone);
1162 test(AccessDisk() == KErrNone);
1163 test(TBLD.ReadPasswordData(storeRd) == KErrNone);
1164 test(StoresEqual(storeRd, store0_1));
1166 // Wrong mapping in store - mapping should be removed.
1167 test.Next(_L("wrong mapping in store"));
1168 test(TBLD.WritePasswordData(store0_2) == KErrNone);
1170 test(AccessDisk() == KErrLocked);
1171 test(TBLD.ReadPasswordData(storeRd) == KErrNone);
1172 test(StoresEqual(storeRd, storeDef));
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);
1180 test(AccessDisk() == KErrNone);
1181 test(TBLD.ReadPasswordData(storeRd) == KErrNone);
1182 test(StoresEqual(storeRd, storeDef));
1184 test.Next(_L("clean up for following tests"));
1186 test(TBLD.WritePasswordData(nulSt) == KErrNone);
1187 test(TBLD.PasswordStoreLengthInBytes() == 0);
1189 test.Next(_L("free test data"));
1199 LOCAL_C void TestPasswordFile()
1201 // Additional test added for INC066636
1203 // Tests that the MMC password file is created in the correct place on the disk
1204 // as defined by KMediaPWrdFile in f32fsys.h
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
1210 // o Password can be changed
1211 // o Password can be removed
1214 const TInt KDriveNum = RFsDNum;
1216 TInt error = KErrNone;
1219 test.Start(_L("Testing password file"));
1222 test.Next(_L("open connection"));
1224 test(theFs.Connect() == KErrNone);
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);
1236 // Verify that the password file does exist and is in the correct place
1237 test.Next(_L("check password file exists"));
1239 TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
1240 mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
1241 error = theFs.Entry(mediaPWrdFile, theEntry);
1242 test (KErrNone == error);
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);
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);
1258 // Clear the password
1259 test.Next(_L("clear the password"));
1260 error = theFs.ClearPassword(KDriveNum, newPWrd);
1261 test(KErrNone == error);
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);
1272 // Remove the password file
1273 test.Next(_L("tidy up"));
1274 error = theFs.Delete(mediaPWrdFile);
1275 test (KErrNone == error);
1284 LOCAL_C void TestFormatErase()
1286 // Additional test added for DEF067976 - MR1: Force Erase of MMC lock UI until complete
1288 // Tests that a card can be locked & then force-erased using the new format switch
1290 // Test modified for INC073653 - RFormat::Open returns KErrNone, even if card is locked
1292 // RFormat:Open now returns KErrLocked if media is locked (previously this wasn't returned
1293 // until calling RFormat::Next
1299 test.Start(_L("Testing force erase"));
1302 test.Next(_L("open connection"));
1304 test(fs.Connect() == KErrNone);
1306 // Clear the password store for when function run on its own.
1308 test(TBLD.WritePasswordData(nulSt) == KErrNone);// empty
1309 test(TBLD.PasswordStoreLengthInBytes() == 0);
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);
1318 test.Printf(_L("RFs::LockDrive() returned %d\n"), r);
1319 test(r == KErrNone);
1321 RemountMedia(); // card is now locked
1324 TPckgBuf<TInt> stepPkg;
1325 TDriveUnit driveUnit(RFsDNum);
1326 TDriveName driveName = driveUnit.Name();
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);
1334 test.Printf(_L("\n"));
1337 _LIT(KLitStars,"********************");
1338 test.Next(_L("force erase locked card"));
1339 r = fmt.Open(fs, driveName, EHighDensity | EForceErase, stepPkg());
1341 test.Printf(_L("RFormat::Open() returned %d\n"), r);
1342 test (r == KErrNone);
1344 while (stepPkg() > 0)
1346 TRequestStatus status;
1347 fmt.Next(stepPkg, status);
1348 test (status == KRequestPending || status == KErrNone);
1349 User::WaitForRequest(status);
1351 TInt length=(100-stepPkg())/5;
1352 length=Min(length,20);
1353 TPtrC stars=KLitStars().Left(length);
1354 test.Printf(_L("\r%S"),&stars);
1356 test.Printf(_L("\n"));
1364 LOCAL_C void TestWriteToPasswordStoreUnlocksCard()
1366 // Additional test added for INC096612 - Writing to password store should unlock the card
1368 // Tests that a card can be auto-unlocked just by writing to the password store (as this is what
1375 test.Start(_L("Testing writing to password store unlocks the card"));
1377 test.Next(_L("open connection"));
1379 test(fs.Connect() == KErrNone);
1381 // Clear the password store for when function run on its own.
1382 TMediaPassword& nulPWrd = *PWDs[0];
1383 TMediaPassword testPassword((const TUint8*) "xyz");
1385 test(TBLD.WritePasswordData(nulPWrd) == KErrNone);// empty
1386 test(TBLD.PasswordStoreLengthInBytes() == 0);
1388 test.Next(_L("lock card"));
1389 test.Next(_L("assign test password"));
1390 r = TBLD.SetPassword(nulPWrd, testPassword, EFalse);
1391 test(r == KErrNone);
1393 RemountMedia(); // card is now locked
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);
1402 test (r == KErrNone);
1403 test ((driveCaps.iMediaAtt & KMediaAttLocked) != 0);
1405 // Write correct password to store
1406 test.Next(_L("write correct password to store"));
1408 TPersistentStore *pstoreDef;
1409 test((pstoreDef = new TPersistentStore) != NULL);
1410 TPersistentStore &storeDef = *pstoreDef;
1411 AddMapping(storeDef, CIDs[0], &testPassword);
1412 r = TBLD.WritePasswordData(storeDef);
1414 test.Printf(_L("WritePasswordData() returned %d\n"), r);
1416 test(r == KErrNone);
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);
1423 test (r == KErrNone);
1424 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
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);
1431 test.Next(_L("test card is unlocked"));
1433 r = TBLD.Caps(driveCapsPkg);
1434 test.Printf(_L("Caps() returned %d , iMediaAtt %08x\n"), r, driveCaps.iMediaAtt);
1436 test (r == KErrNone);
1437 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1447 LOCAL_C TBool SetupDrivesForPlatform(TInt& aDrive, TInt &aRFsDriveNum)
1449 * Finds a suitable drive for the password store test
1451 * @param aDrive The number of the local drive to test
1452 * @return TBool ETrue if a suitable drive is found, EFalse otherwise.
1456 TDriveInfoV1Buf diBuf;
1457 UserHal::DriveInfo(diBuf);
1458 TDriveInfoV1 &di=diBuf();
1460 test.Printf(_L(" iRegisteredDriveBitmask 0x%08X"), di.iRegisteredDriveBitmask);
1464 TLocalDriveCapsV5Buf capsBuf;
1465 TBusLocalDrive TBLD;
1466 TLocalDriveCapsV5& caps = capsBuf();
1467 TPtrC8 localSerialNum;
1468 TInt registeredDriveNum = 0;
1469 for(aDrive=0; aDrive < KMaxLocalDrives; aDrive++)
1471 TInt driveNumberMask = 1 << aDrive;
1472 if ((di.iRegisteredDriveBitmask & driveNumberMask) == 0)
1475 test.Printf(_L(" Drive %d - %S\r\n"), aDrive, &di.iDriveName[registeredDriveNum]);
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))
1482 TBool TBLDChangedFlag;
1483 TInt r = TBLD.Connect(aDrive, TBLDChangedFlag);
1484 //test.Printf(_L(" Connect returned %d\n"), r);
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);
1499 registeredDriveNum++;
1502 if(aDrive == KMaxLocalDrives)
1504 test.Printf(_L(" MMC Drive Not Found\r\n"));
1508 // Work out the file server drive number (which isn't necessarily the same
1509 // as the TBusLocalDrive drive number)
1511 test(theFs.Connect() == KErrNone);
1514 for (i = EDriveA; i < EDriveZ; i++)
1516 TMediaSerialNumber serialNum;
1517 TInt r = theFs.GetMediaSerialNumber(serialNum, i);
1518 TInt len = serialNum.Length();
1520 for (n=0; n<len; n+=16)
1522 TBuf16<16*3 +1> buf;
1523 for (TInt m=n; m<n+16; m++)
1526 hexBuf.Format(_L("%02X "),serialNum[m]);
1529 buf.Append(_L("\n"));
1532 if (serialNum.Compare(localSerialNum) == 0)
1535 r = theFs.Volume(vi, i);
1536 TBool sizeMatch = (vi.iSize < caps.iSize);
1547 test.Printf(_L(" RFs MMC Drive Not Found\r\n"));
1557 TInt TestLockCard(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword &aOldPassword, TMediaPassword& aNewPassword, TBool aStore)
1559 TMediaPassword newPassWord;
1560 TMediaPassword oldPassWord;
1564 aFs.Drive(dInfo, RFsDNum);
1566 newPassWord.Append(aNewPassword);
1567 oldPassWord.Append(aOldPassword);
1569 test (dInfo.iMediaAtt & KMediaAttLockable);
1571 err=aFs.LockDrive(RFsDNum, oldPassWord, newPassWord, aStore );
1573 aFs.Drive(dInfo, aTheMemoryCardDrive);
1577 TInt TestUnlockCard(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword& aPassword, TBool aStore)
1579 TMediaPassword oldPw;
1581 oldPw.Append(aPassword);
1582 TInt err = aFs.UnlockDrive( aTheMemoryCardDrive, oldPw, aStore);
1586 TInt TestClearPassword(RFs& aFs, TInt aTheMemoryCardDrive, TMediaPassword& aPassword)
1588 TMediaPassword oldPwd = aPassword;
1590 TInt err = aFs.ClearPassword( aTheMemoryCardDrive, oldPwd );
1595 TInt ExecuteForcedEraseTestL(RFs& aFs, TInt aTheMemoryCardDrive)
1597 TInt err = aFs.ErasePassword( aTheMemoryCardDrive );
1602 TBool TestLocked(RFs& aFs, TInt aTheMemoryCardDrive)
1606 TInt r = aFs.Drive(info, aTheMemoryCardDrive);
1607 test (r == KErrNone);
1609 return (info.iMediaAtt & KMediaAttLocked)?(TBool)ETrue:(TBool)EFalse;
1612 void WaitForPowerDownLock(RFs& aFs, TInt aTheMemoryCardDrive)
1614 test.Printf(_L("Waiting for stack to power down...\n"));
1616 for (n=0; n<30 && !TestLocked(aFs, aTheMemoryCardDrive); n++)
1618 User::After(1000000);
1621 test(TestLocked(aFs, aTheMemoryCardDrive)); // should now be locked
1624 void WaitForPowerDownUnlock(RFs& /*aFs*/, TInt /*aTheMemoryCardDrive*/)
1626 test.Printf(_L("Allow some time for stack to power down"));
1627 for (TUint i=0; i < 80; ++i)
1629 User::After(100000);
1630 test.Printf(_L("."));
1632 test.Printf(_L("\n"));
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
1641 LOCAL_C void TestPowerDownStatus()
1644 TLocalDriveCapsV5 driveCaps;
1645 TPckg<TLocalDriveCapsV5> driveCapsPkg(driveCaps);
1646 TMediaPassword password = (TUint8*) "salasana";
1647 TMediaPassword oldpassword;
1649 test.Start(_L("Testing Power Down Status Reporting"));
1651 test.Next(_L("Open Connection"));
1653 test(fs.Connect() == KErrNone);
1655 // Lock card (with password stored)
1656 test.Next(_L("Locking Card - Password Stored"));
1658 test.Next(_L("Locking card (Successful)")) ;
1659 r = TestLockCard(fs, RFsDNum, oldpassword, password, ETrue);
1660 test(r == KErrNone);
1662 test(!TestLocked(fs, RFsDNum)); // not locked yet as stack hasn't powered down
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);
1668 test (r == KErrNone);
1669 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1671 WaitForPowerDownUnlock(fs, RFsDNum);
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);
1677 test (r == KErrNone);
1678 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1680 test.Next(_L("Clear password (Successful)"));
1681 r = TestClearPassword(fs, RFsDNum, password);
1682 test(r == KErrNone);
1684 // Lock card (without password in store)
1685 test.Next(_L("Locking card - Password NOT Stored"));
1687 test.Next(_L("Locking card (Successful)"));
1688 r = TestLockCard(fs, RFsDNum, oldpassword, password, EFalse);
1689 test(r == KErrNone);
1691 test(!TestLocked(fs, RFsDNum)); // not locked yet as stack hasn't powered down
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);
1697 test (r == KErrNone);
1698 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1700 WaitForPowerDownLock(fs, RFsDNum);
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);
1706 test (r == KErrNone);
1707 test ((driveCaps.iMediaAtt & KMediaAttLocked) != 0);
1710 test.Next(_L("Unlock 'locked' Card - Password Stored"));
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
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);
1721 test (r == KErrNone);
1722 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1724 WaitForPowerDownUnlock(fs, RFsDNum);
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);
1730 test (r == KErrNone);
1731 test ((driveCaps.iMediaAtt & KMediaAttLocked) == 0);
1733 test.Next(_L("Clearing Password (Successful)"));
1734 r = TestClearPassword(fs, RFsDNum, password);
1735 test(r == KErrNone);
1742 LOCAL_C void TestFsLockUnlock()
1746 test.Start(_L("Testing RFs APIs"));
1748 test.Next(_L("open connection"));
1750 test(fs.Connect() == KErrNone);
1753 test.Next(_L("test locking card"));
1755 TMediaPassword oldpassword;
1756 TMediaPassword newpassword = (TUint8*) "salasana";
1757 TMediaPassword wrongpwd = (TUint8*) "failtest";
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
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));
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));
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
1778 WaitForPowerDownLock(fs, RFsDNum);
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("?:\\");
1790 r = fs.DriveToChar(RFsDNum,driveLetter);
1792 sessionPath[0]=(TText)driveLetter;
1793 r = fs.CheckDisk(sessionPath);
1794 test(r == KErrNone || r == KErrLocked);
1795 WaitForPowerDownLock(fs, RFsDNum);
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);
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"));
1813 r = fs.CheckDisk(sessionPath);
1814 test(r == KErrLocked);
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
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
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
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
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
1840 WaitForPowerDownLock(fs, RFsDNum);
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));
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));
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
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
1861 WaitForPowerDownLock(fs, RFsDNum);
1864 test.Next(_L("test forced erase succeeds if powered down & locked"));
1865 r = ExecuteForcedEraseTestL(fs, RFsDNum);
1866 test(r == KErrNone);
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 (!)
1878 void TestUnlockDriveNotifyChange()
1881 test(fs.Connect() == KErrNone);
1883 TFileName sessionPath;
1884 sessionPath=_L("?:\\");
1886 TInt r=fs.DriveToChar(RFsDNum,driveLetter);
1888 sessionPath[0]=(TText)driveLetter;
1889 r=fs.SetSessionPath(sessionPath);
1895 nRes = fs.Drive(dInfo, RFsDNum);
1896 test(nRes == KErrNone);
1897 if (!(dInfo.iMediaAtt & KMediaAttLockable))
1899 test.Printf(_L("Drive %d is not lockable %d\n"), RFsDNum);
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);
1910 WaitForPowerDownLock(fs, RFsDNum);
1912 TRequestStatus reqStatNotify1(KRequestPending);
1914 //-- set up notifier
1915 fs.NotifyChange(ENotifyAll, reqStatNotify1, sessionPath);
1916 test(reqStatNotify1.Int() == KRequestPending);
1918 //-- unlock the drive
1919 nRes = fs.UnlockDrive(RFsDNum, newPassword, EFalse);
1920 test.Printf(_L("UnlockDrive() %d reqStatNotify1 %d\n"), nRes, reqStatNotify1.Int());
1922 //-- check that the notifier worked
1923 User::WaitForRequest(reqStatNotify1);
1924 test(reqStatNotify1.Int() == KErrNone);
1926 r = TestClearPassword(fs, RFsDNum, newPassword);
1927 test(r == KErrNone);
1928 test(!TestLocked(fs, RFsDNum));
1936 LOCAL_C void RunTests()
1938 // Main test routine. Calls other test functions.
1945 if(!SetupDrivesForPlatform(TBLDNum, RFsDNum))
1947 test.Printf(_L("MMC Drive Not Found - Skipping test\r\n"));
1952 test.Next(_L("Connecting TBLD"));
1953 test(TBLD.Connect(TBLDNum, TBLDChangedFlag) == KErrNone);
1955 test.Next(_L("Allocating test data"));
1958 test.Next(_L("Testing locking / unlocking using file server APIs"));
1961 test.Next(_L("Testing Power Down Status Reporting using file server APIs"));
1962 TestPowerDownStatus();
1964 test.Next(_L("Testing RFs::NotifyChange() with RFs::UnlockDrive()"));
1965 TestUnlockDriveNotifyChange();
1967 test.Next(_L("Forced Erase"));
1969 test.Next(_L("Testing store management"));
1971 test.Next(_L("Testing locking functions"));
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"));
1981 test.Next(_L("Testing password file"));
1983 test.Next(_L("Testing writing a valid password to store unlocks card"));
1984 TestWriteToPasswordStoreUnlocksCard();
1986 test.Next(_L("Disconnecting TBLD"));
1989 test.Next(_L("Deleting test data"));
2000 test.Start(_L("E32Main"));