kerneltest/f32test/server/t_pwstr.cpp
changeset 244 a77889bee936
parent 189 a5496987b1da
equal deleted inserted replaced
243:c7a0ce20c48c 244:a77889bee936
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    99 LOCAL_D TInt TBLDNum = -1; 	// Change this to specify the drive under test
    99 LOCAL_D TInt TBLDNum = -1; 	// Change this to specify the drive under test
   100 							// e.g. for the lm_pana board when fitted to the
   100 							// e.g. for the lm_pana board when fitted to the
   101 							// integrator, TBLDNum should be set to 3.
   101 							// integrator, TBLDNum should be set to 3.
   102 
   102 
   103 LOCAL_D TInt RFsDNum = -1;	// File Server Drive number
   103 LOCAL_D TInt RFsDNum = -1;	// File Server Drive number
       
   104 LOCAL_D TBool gManual = EFalse; // Manual Tests enabled
   104 
   105 
   105 struct TTestMapping
   106 struct TTestMapping
   106 	{
   107 	{
   107 	TInt iCIDIdx;							// index in CID
   108 	TInt iCIDIdx;							// index in CID
   108 	TInt iPWDIdx;							// index in PWD
   109 	TInt iPWDIdx;							// index in PWD
  1229 
  1230 
  1230 	
  1231 	
  1231 	test.Next(_L("open connection"));
  1232 	test.Next(_L("open connection"));
  1232 	RFs theFs;
  1233 	RFs theFs;
  1233 	test(theFs.Connect() == KErrNone);
  1234 	test(theFs.Connect() == KErrNone);
  1234 
  1235 	
  1235 	
  1236 	// So that we are in a consistant state lets
       
  1237 	// Remove the password file first. i.e. could be passwords left over from previous test failures
       
  1238     test.Next(_L("tidy up"));
       
  1239     TEntry theEntry;
       
  1240     TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
  1241     mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
  1242     test.Printf(_L("password file : %S\n"),&mediaPWrdFile);
       
  1243     error = theFs.Delete(mediaPWrdFile);
       
  1244     // Should be either KErrNone, KErrPathNotFound or KErrNotFound
       
  1245     test.Printf(_L("password file deleted: %d\n"),error);
  1236 	
  1246 	
  1237 	// Now set the first password that we will use
  1247 	// Now set the first password that we will use
  1238 	test.Next(_L("lock the media card"));	
  1248 	test.Next(_L("lock the media card"));	
  1239 	TMediaPassword& nulPWrd = *PWDs[0];
  1249 	TMediaPassword& nulPWrd = *PWDs[0];
  1240 	TMediaPassword& oldPWrd = *PWDs[1];
  1250 	TMediaPassword& oldPWrd = *PWDs[1];
  1241 	error = theFs.LockDrive(KDriveNum, nulPWrd, oldPWrd, ETrue);
  1251 	error = theFs.LockDrive(KDriveNum, nulPWrd, oldPWrd, ETrue);
  1242 	test(KErrNone == error);
  1252 
  1243 
  1253 	if (KErrNotSupported == error)
       
  1254 	    {
       
  1255         // Appears that passwords aren't supported on this drive config (i.e. NFE)
       
  1256         theFs.Close();
       
  1257         test.End();
       
  1258         return;
       
  1259 	    }
       
  1260 	test_KErrNone(error);
  1244 
  1261 
  1245 	// Verify that the password file does exist and is in the correct place
  1262 	// Verify that the password file does exist and is in the correct place
  1246 	test.Next(_L("check password file exists"));
  1263 	test.Next(_L("check password file exists"));
  1247 	TEntry theEntry;
       
  1248 	TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
       
  1249 	mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
       
  1250 	error = theFs.Entry(mediaPWrdFile, theEntry);
  1264 	error = theFs.Entry(mediaPWrdFile, theEntry);
  1251 	test (KErrNone == error);
  1265 	test.Printf(_L("password file exists: %d\n"),error);
  1252 
  1266 	if (error != KErrNone && error != KErrNotFound)
       
  1267 	    {
       
  1268         test(0);
       
  1269 	    }	
  1253 	
  1270 	
  1254 	// Attempt to set a new password without specifying the current one
  1271 	// Attempt to set a new password without specifying the current one
  1255 	test.Next(_L("change password failure"));	
  1272 	test.Next(_L("change password failure"));	
  1256 	TMediaPassword& newPWrd = *PWDs[2];
  1273 	TMediaPassword& newPWrd = *PWDs[2];
  1257 	error = theFs.LockDrive(KDriveNum, nulPWrd, newPWrd, ETrue);
  1274 	error = theFs.LockDrive(KDriveNum, nulPWrd, newPWrd, ETrue);
  1259 
  1276 
  1260 
  1277 
  1261 	// Change the password for a new one...
  1278 	// Change the password for a new one...
  1262 	test.Next(_L("change password success"));	
  1279 	test.Next(_L("change password success"));	
  1263 	error = theFs.LockDrive(KDriveNum, oldPWrd, newPWrd, ETrue);
  1280 	error = theFs.LockDrive(KDriveNum, oldPWrd, newPWrd, ETrue);
  1264 	test(KErrNone == error);
  1281 	test_KErrNone(error);
  1265 
  1282 
  1266 	
  1283 	
  1267 	// Clear the password
  1284 	// Clear the password
       
  1285 	// NB The file server uses a separate thread to write to the password file,
       
  1286 	// so we may need to wait a short while to see any change...
  1268 	test.Next(_L("clear the password"));	
  1287 	test.Next(_L("clear the password"));	
  1269 	error = theFs.ClearPassword(KDriveNum, newPWrd);
  1288 	error = theFs.ClearPassword(KDriveNum, newPWrd);
  1270 	test(KErrNone == error);
  1289 	test_KErrNone(error);
  1271 
  1290 
  1272 
  1291 
  1273 	// Check that the password has been removed from the file
  1292 	// Check that the password has been removed from the file
  1274 	// (KMediaPWrdFile should now be zero bytes in size)
  1293 	// (KMediaPWrdFile should now be zero bytes in size)
  1275 	test.Next(_L("check password removal"));
  1294 	test.Next(_L("check password removal"));
  1276 	error = theFs.Entry(mediaPWrdFile, theEntry);
  1295 	theEntry.iSize = KMaxTInt;
  1277 	test (KErrNone == error);
  1296 	TInt n;
  1278 	test (0 == theEntry.iSize);
  1297 	for (n=0; theEntry.iSize > 0 && n<10; n++)
       
  1298 		{
       
  1299 		error = theFs.Entry(mediaPWrdFile, theEntry);
       
  1300 	test_KErrNone(error);
       
  1301 		test.Printf(_L("Password file size is %d\n"), theEntry.iSize);
       
  1302 		if (theEntry.iSize > 0)
       
  1303 			User::After(1000000);
       
  1304 		}
       
  1305 	test (theEntry.iSize == 0);
  1279 
  1306 
  1280 	
  1307 	
  1281 	// Remove the password file
  1308 	// Remove the password file
  1282 	test.Next(_L("tidy up"));
  1309 	test.Next(_L("tidy up"));
  1283 	error = theFs.Delete(mediaPWrdFile);
  1310 	error = KErrInUse;
  1284 	test (KErrNone == error);
  1311 	for (n=0; error == KErrInUse && n<10; n++)
       
  1312 		{
       
  1313 		error = theFs.Delete(mediaPWrdFile);
       
  1314 		test.Printf(_L("Deleting %S, Iter %d, r %d\n"), &mediaPWrdFile, n, error);
       
  1315 		if (error == KErrInUse)
       
  1316 			User::After(1000000);
       
  1317 		}
       
  1318 	test_KErrNone(error);
  1285 
  1319 
  1286 
  1320 
  1287 	theFs.Close();
  1321 	theFs.Close();
  1288 
  1322 
  1289 	test.End();
  1323 	test.End();
  1464 	
  1498 	
  1465 	TDriveInfoV1Buf diBuf;
  1499 	TDriveInfoV1Buf diBuf;
  1466 	UserHal::DriveInfo(diBuf);
  1500 	UserHal::DriveInfo(diBuf);
  1467 	TDriveInfoV1 &di=diBuf();
  1501 	TDriveInfoV1 &di=diBuf();
  1468 
  1502 
  1469 	test.Printf(_L(" iRegisteredDriveBitmask 0x%08X"), di.iRegisteredDriveBitmask);
  1503 	test.Printf(_L(" iRegisteredDriveBitmask 0x%08X\n"), di.iRegisteredDriveBitmask);
  1470 
  1504 
  1471 	aDrive  = -1;
  1505 	aDrive  = -1;
  1472 	
  1506 	
  1473 	TLocalDriveCapsV5Buf capsBuf;
  1507 	TLocalDriveCapsV5Buf capsBuf;
  1474 	TBusLocalDrive TBLD;
  1508 	TBusLocalDrive TBLD;
  1475 	TLocalDriveCapsV5& caps = capsBuf();
  1509 	TLocalDriveCapsV5& caps = capsBuf();
       
  1510 	
  1476 	TPtrC8 localSerialNum;
  1511 	TPtrC8 localSerialNum;
  1477 	TInt registeredDriveNum = 0;
  1512 	TInt registeredDriveNum = 0;
       
  1513 		
       
  1514 	// Find a Drive that has Password support.
  1478 	for(aDrive=0; aDrive < KMaxLocalDrives; aDrive++)
  1515 	for(aDrive=0; aDrive < KMaxLocalDrives; aDrive++)
  1479 		{
  1516 		{
  1480 		TInt driveNumberMask = 1 << aDrive;
  1517 		TInt driveNumberMask = 1 << aDrive;
  1481 		if ((di.iRegisteredDriveBitmask & driveNumberMask) == 0)
  1518 		if ((di.iRegisteredDriveBitmask & driveNumberMask) == 0)
  1482 			continue;
  1519 			continue;
  1483 
  1520 
  1484 		test.Printf(_L(" Drive %d -  %S\r\n"), aDrive, &di.iDriveName[registeredDriveNum]);
  1521 		test.Printf(_L(" Drive %d -  %S\r\n"), aDrive, &di.iDriveName[registeredDriveNum]);
  1485 
  1522 
  1486 		// check that the card is readable (so we can ignore for empty card slots)
  1523         TBool TBLDChangedFlag;
  1487 		if ((di.iDriveName[registeredDriveNum].MatchF(_L("MultiMediaCard0")) == KErrNone) ||
  1524         TInt r = TBLD.Connect(aDrive, TBLDChangedFlag);
  1488 		    (di.iDriveName[registeredDriveNum].MatchF(_L("SDIOCard0")) == KErrNone))
       
  1489 			{
       
  1490 			
       
  1491 			TBool TBLDChangedFlag;
       
  1492 			TInt r = TBLD.Connect(aDrive, TBLDChangedFlag);
       
  1493 //test.Printf(_L(" Connect returned %d\n"), r);
  1525 //test.Printf(_L(" Connect returned %d\n"), r);
  1494 			if (r == KErrNone)
  1526         if (r == KErrNone)
  1495 				{
  1527             {
  1496 				r = TBLD.Caps(capsBuf);
  1528             r = TBLD.Caps(capsBuf);
  1497 				localSerialNum.Set(caps.iSerialNum, caps.iSerialNumLength);
  1529             
  1498 				const TInt KSectSize = 512;
  1530             //Check media is lockable if not carry on			
  1499 				TBuf8<KSectSize> sect;
  1531             if (caps.iMediaAtt & KMediaAttLockable)
  1500 				r = TBLD.Read(0, KSectSize, sect);
  1532                 {
       
  1533                 test.Printf(_L("Drive %d is Lockable\n"),aDrive);
       
  1534                 localSerialNum.Set(caps.iSerialNum, caps.iSerialNumLength);
       
  1535                 const TInt KSectSize = 512;
       
  1536                 TBuf8<KSectSize> sect;
       
  1537                 r = TBLD.Read(0, KSectSize, sect);
  1501 //test.Printf(_L(" Read returned %d\n"), r);
  1538 //test.Printf(_L(" Read returned %d\n"), r);
  1502 				
  1539 				
  1503 				TBLD.Disconnect();
  1540 				TBLD.Disconnect();
  1504 				if (r == KErrNone)
  1541 				if (r == KErrNone)
  1505 					break;
  1542 					break;
  1506 				}
  1543 				}
       
  1544 
       
  1545 			TBLD.Disconnect();				
  1507 			}
  1546 			}
  1508 		registeredDriveNum++;
  1547 		registeredDriveNum++;
  1509 		}
  1548 		}
  1510 
  1549 
  1511 	if(aDrive == KMaxLocalDrives)
  1550 	if(aDrive == KMaxLocalDrives)
  1623 	test.Printf(_L("Waiting for stack to power down...\n"));
  1662 	test.Printf(_L("Waiting for stack to power down...\n"));
  1624 	TInt n;
  1663 	TInt n;
  1625 	for (n=0; n<30 && !TestLocked(aFs, aTheMemoryCardDrive); n++)
  1664 	for (n=0; n<30 && !TestLocked(aFs, aTheMemoryCardDrive); n++)
  1626 		{
  1665 		{
  1627 		User::After(1000000);
  1666 		User::After(1000000);
  1628 		}
  1667 		test.Printf(_L("."));
       
  1668 		}
       
  1669 	test.Printf(_L("\n"));
  1629 	test(n < 30);
  1670 	test(n < 30);
  1630 	test(TestLocked(aFs, aTheMemoryCardDrive));	// should now be locked
  1671 	test(TestLocked(aFs, aTheMemoryCardDrive));	// should now be locked
  1631 	}
  1672 	}
  1632 	
  1673 	
  1633 void WaitForPowerDownUnlock(RFs& /*aFs*/, TInt /*aTheMemoryCardDrive*/)
  1674 void WaitForPowerDownUnlock(RFs& /*aFs*/, TInt /*aTheMemoryCardDrive*/)
  1939 	
  1980 	
  1940 	
  1981 	
  1941 	fs.Close();
  1982 	fs.Close();
  1942 	}
  1983 	}
  1943 
  1984 
  1944 
       
  1945 LOCAL_C void RunTests()
  1985 LOCAL_C void RunTests()
  1946 //
  1986 //
  1947 // Main test routine.  Calls other test functions.
  1987 // Main test routine.  Calls other test functions.
  1948 //
  1988 //
  1949 	{
  1989 	{
  1962 	test(TBLD.Connect(TBLDNum, TBLDChangedFlag) == KErrNone);
  2002 	test(TBLD.Connect(TBLDNum, TBLDChangedFlag) == KErrNone);
  1963 
  2003 
  1964 	test.Next(_L("Allocating test data"));
  2004 	test.Next(_L("Allocating test data"));
  1965 	AllocateTestData();
  2005 	AllocateTestData();
  1966 
  2006 
  1967 	test.Next(_L("Testing locking / unlocking using file server APIs"));
  2007     if (gManual)
  1968 	TestFsLockUnlock();
  2008         {
  1969 	
  2009 		test.Next(_L("Testing locking / unlocking using file server APIs"));
  1970 	test.Next(_L("Testing Power Down Status Reporting using file server APIs"));
  2010 		TestFsLockUnlock();
  1971 	TestPowerDownStatus();
  2011 	
  1972 
  2012 		test.Next(_L("Testing Power Down Status Reporting using file server APIs"));
  1973     test.Next(_L("Testing RFs::NotifyChange() with RFs::UnlockDrive()"));
  2013 		TestPowerDownStatus();
  1974 	TestUnlockDriveNotifyChange();
  2014 
  1975 
  2015 	    test.Next(_L("Testing RFs::NotifyChange() with RFs::UnlockDrive()"));
  1976 	test.Next(_L("Forced Erase"));
  2016 		TestUnlockDriveNotifyChange();
  1977 	TestFormatErase();
  2017 
       
  2018 		test.Next(_L("Forced Erase"));
       
  2019 		TestFormatErase();
       
  2020 		}
       
  2021 		
  1978 	test.Next(_L("Testing store management"));
  2022 	test.Next(_L("Testing store management"));
  1979 	TestStaticStore();
  2023 	TestStaticStore();
  1980 	test.Next(_L("Testing locking functions"));
  2024 
  1981 	TestLockUnlock();
  2025     if (gManual)
  1982 	test.Next(_L("Testing Elide Passwords"));
  2026         {		
  1983 	TestElidePasswords();
  2027 		test.Next(_L("Testing locking functions"));
  1984 	test.Next(_L("Testing Null Passwords"));
  2028 		TestLockUnlock();
  1985 	TestNullPasswords();
  2029 		test.Next(_L("Testing Elide Passwords"));
  1986 	test.Next(_L("Testing controller store"));
  2030 		TestElidePasswords();		
  1987 	TestControllerStore();
  2031 		test.Next(_L("Testing Null Passwords"));
  1988 	test.Next(_L("Testing auto unlock"));
  2032 		TestNullPasswords();
  1989 	TestAutoUnlock();
  2033 		test.Next(_L("Testing controller store"));
       
  2034 		TestControllerStore();
       
  2035 		test.Next(_L("Testing auto unlock"));
       
  2036 		TestAutoUnlock();
       
  2037 		}
       
  2038 		
  1990 	test.Next(_L("Testing password file"));
  2039 	test.Next(_L("Testing password file"));
  1991 	TestPasswordFile();
  2040 	TestPasswordFile();
  1992 	test.Next(_L("Testing writing a valid password to store unlocks card"));
  2041 	
  1993 	TestWriteToPasswordStoreUnlocksCard();
  2042     if (gManual)
  1994 
  2043         {		
       
  2044 		test.Next(_L("Testing writing a valid password to store unlocks card"));
       
  2045 		TestWriteToPasswordStoreUnlocksCard();
       
  2046 		}
       
  2047     
  1995 	test.Next(_L("Disconnecting TBLD"));
  2048 	test.Next(_L("Disconnecting TBLD"));
  1996 	TBLD.Disconnect();
  2049 	TBLD.Disconnect();
  1997 
  2050 
  1998 	test.Next(_L("Deleting test data"));
  2051 	test.Next(_L("Deleting test data"));
  1999 	DeleteTestData();
  2052 	DeleteTestData();
  2000 
  2053 
  2001 	__UHEAP_MARKEND;
  2054 	__UHEAP_MARKEND;
  2002 	}
  2055 	}
  2003 
  2056 
       
  2057 LOCAL_D void ParseCommandLineArgs()
       
  2058     {
       
  2059     
       
  2060     TBuf<0x100> cmd;
       
  2061     User::CommandLine(cmd);
       
  2062     TLex lex(cmd);
       
  2063 
       
  2064     for (TPtrC token=lex.NextToken(); token.Length() != 0;token.Set(lex.NextToken()))
       
  2065         {
       
  2066         if (token.CompareF(_L("-m"))== 0)
       
  2067             {
       
  2068             gManual = ETrue;
       
  2069             continue;
       
  2070             }
       
  2071         }
       
  2072     }
  2004 
  2073 
  2005 TInt E32Main()
  2074 TInt E32Main()
  2006 	{
  2075 	{
  2007 	
  2076 	
  2008 	test.Title();
  2077 	test.Title();
  2009 	test.Start(_L("E32Main"));
  2078 	test.Start(_L("T_PWSTR"));
  2010 	
  2079 
  2011 	RunTests();
  2080 #if defined(__WINS__)
  2012 
  2081 	if (!gManual)
       
  2082 	    {
       
  2083         test.Printf(_L("Automated T_PWSTR not supported on emulated devices\n"));
       
  2084 	    }
       
  2085 	else
       
  2086 #endif
       
  2087 	    {
       
  2088         ParseCommandLineArgs();	
       
  2089         
       
  2090         RunTests();
       
  2091 	    }
       
  2092 
       
  2093 	
  2013 	test.End();
  2094 	test.End();
  2014 	test.Close();
  2095 	test.Close();
  2015 
  2096 
  2016 	return KErrNone;
  2097 	return KErrNone;
  2017 	}
  2098 	}