os/kernelhwsrv/kerneltest/f32test/demandpaging/t_denyclamp.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-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\demandpaging\t_denyclamp.cpp
    15 // This test suite has a reduced set of the tests in T_CLAMP, to verify
    16 // the behaviour when file clamping is attempted by an invalid user.
    17 // 002 GetDriveLetters() Assign the first drive that matches the required criteria
    18 // 003 Test1() Basic clamp operation
    19 // 004 Test2() Invalid clamp requests
    20 // 005 Test4() Clamp tests for non-writable file system
    21 // 006 Test5() Clamp requests on non-clamping file systems
    22 // This file has a reduced set of tests present in T_CLAMP, to verify
    23 // the behaviour when file clamping is attempted by an invalid user
    24 // 
    25 //
    26 
    27 //! @SYMTestCaseID			KBASE-T_DENYCLAMP-0329
    28 //! @SYMTestType			UT
    29 //! @SYMPREQ				PREQ1110
    30 //! @SYMTestCaseDesc		Demand Paging File Clamp tests (Deny)
    31 //! @SYMTestActions			001 Starting T_DENYCLAMP ...
    32 //! @SYMTestExpectedResults All tests should pass.
    33 //! @SYMTestPriority        High
    34 //! @SYMTestStatus          Implemented
    35 
    36 #include <e32test.h>
    37 RTest test(_L("T_DENYCLAMP"));
    38 
    39 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
    40 
    41 #include <f32file.h>
    42 #include <f32dbg.h>
    43 #include <e32ldr_private.h>
    44 RFs TheFs;
    45 
    46 _LIT(KFATName,"FAT");
    47 //_LIT(KFAT32Name,"FAT32");
    48 _LIT(KROFSName,"ROFS");
    49 _LIT(KLFFSName,"LFFS");
    50 _LIT(KCOMPName,"COMPOSITE"); // Z: name if Composite File System
    51 //#ifdef __WINS__
    52 //_LIT(KROMName,"WIN32");	// Clamping is not supported for non-composite filing system on Z:
    53 //#else
    54 _LIT(KROMName,"ROM");		 // Z: name if ROMFS (on hardware, not emulator)
    55 //#endif
    56 
    57 TChar NandFatDrv='?';
    58 TChar RofsDrv='?';
    59 TChar LffsDrv='?';
    60 TChar CompDrv='?';
    61 
    62 
    63 LOCAL_C void Test1()
    64 	{
    65 // Basic clamp operation
    66 	test.Next(_L("T_DENYCLAMP - Test1()"));
    67 
    68 	TBuf<256> fileName;	
    69 	TBuf<256> buf(_L("buffer for file used"));
    70 
    71 	fileName = _L("clampFile.tst");
    72 	RFile testFile;
    73 	TInt r=testFile.Replace(TheFs,fileName,EFileWrite);
    74 	test(r==KErrNone);
    75 	TPtrC8 pBuf((TUint8*)&buf);
    76 	testFile.Write(pBuf);
    77 	testFile.Flush();
    78 
    79 	// Attempt to clamp file should be rejected
    80 	RFileClamp handle;
    81 	r=handle.Clamp(testFile);
    82 	test(r==KErrPermissionDenied);
    83 
    84 	// Attempt to unclamp a file should be rejected
    85 	// Using an invalid-content cookie is OK - the request should
    86 	// be rejected before the content is examined
    87 	handle.iCookie[0]=MAKE_TINT64(-1,-1);
    88 	handle.iCookie[1]=0;
    89 	r=handle.Close(TheFs);
    90 	test (r==KErrPermissionDenied);
    91 
    92 	// Tidy up
    93 	testFile.Close();
    94 	r=TheFs.Delete(_L("clampFile.tst"));
    95 	test (r==KErrNone);
    96 	}
    97 
    98 
    99 LOCAL_C void Test2()
   100 	{
   101 // Invalid clamp requests
   102 	test.Next(_L("T_DENYCLAMP - Test2()"));
   103 	
   104 	// Test attempt to clamp empty file is rejected
   105 	RFileClamp handle2;
   106 	TBuf<256> file2Name;	
   107 	file2Name = _L("clampFile2.tst");
   108 	RFile testFile2;
   109 	TInt r=testFile2.Replace(TheFs,file2Name,EFileWrite);
   110 	test(r==KErrNone);
   111 	r=handle2.Clamp(testFile2);
   112 	test(r==KErrPermissionDenied);
   113 
   114 
   115 	// Try to unclamp non-existant file
   116 	// Using a invalid-content cookie is OK - the request should
   117 	// be rejected before the content is examined
   118 	handle2.iCookie[0] = MAKE_TINT64(-1,-1); // iCookie[0] holds the unique ID
   119 	handle2.iCookie[1] = MAKE_TINT64(-1,-1);
   120 	r=handle2.Close(TheFs);
   121 	test (r==KErrPermissionDenied);
   122 
   123 	// Tidy up
   124 	testFile2.Close();
   125 	r=TheFs.Delete(_L("clampFile2.tst"));
   126 	test (r==KErrNone);
   127 	}
   128 
   129 
   130 LOCAL_C void Test4(TDesC& aRoot)
   131 	{
   132 // Clamp tests for non-writable file system
   133 	test.Next(_L("T_DENYCLAMP - Test4()"));
   134 
   135 	TBuf<256> pathName;	
   136 #ifdef __WINS__
   137 	if((aRoot[0]=='Z')||(aRoot[0]=='z'))
   138 		pathName=_L("clean.txt");
   139 	else
   140 		pathName=_L("root.txt");
   141 #else
   142 	if((aRoot[0]=='Z')||(aRoot[0]=='z'))
   143 		pathName=_L("UnicodeData.txt");
   144 	else
   145 		pathName=_L("\\Test\\clamp.txt");	// For (non-composite) ROFS drive
   146 #endif
   147 	RFile testFile;
   148 	TInt r=testFile.Open(TheFs, pathName, EFileRead);
   149 	test(r==KErrNone);
   150 
   151 	// Attempt to clamp file
   152 	RFileClamp handle;
   153 	r=handle.Clamp(testFile);
   154 	test(r==KErrPermissionDenied);
   155 
   156 	// Unclamp file
   157 	// Using an invalid-content cookie is OK - the request should
   158 	// be rejected before the content is examined
   159 	handle.iCookie[0]=MAKE_TINT64(-1,-1);
   160 	handle.iCookie[1]=0;
   161 	r=handle.Close(TheFs);
   162 	test (r==KErrPermissionDenied);
   163 
   164 	testFile.Close();
   165 	}
   166 
   167 
   168 LOCAL_C void Test5()
   169 	{
   170 // Clamp requests on non-clamping file systems
   171 	test.Next(_L("T_DENYCLAMP - Test5()"));
   172 
   173 	TBuf<256> unsuppFile;	
   174 	unsuppFile = _L("unsuppFile.tst");
   175 	RFile testFile;
   176 	TInt r=testFile.Replace(TheFs,unsuppFile,EFileWrite);
   177 	test(r==KErrNone);
   178 
   179 	// Try to clamp a file on a file system that does
   180 	// not support clamping
   181 	RFileClamp handle;
   182 	r=handle.Clamp(testFile);
   183 	test(r==KErrPermissionDenied);
   184 
   185 	// Tidy up
   186 	testFile.Close();
   187 	r=TheFs.Delete(_L("unsuppFile.tst"));
   188 	test (r==KErrNone);
   189 	}	
   190 
   191 
   192 LOCAL_C void GetDriveLetters()
   193 	{
   194 // Assign the first drive that matches the required criteria
   195 	test.Next(_L("T_DENYCLAMP - GetDriveLetters()"));
   196 
   197 	TDriveList driveList;
   198 	TDriveInfo driveInfo;
   199 	TInt r=TheFs.DriveList(driveList);
   200 	test(r==KErrNone);
   201 	TInt drvNum;
   202 	TBool drivesFound = EFalse;
   203 	for(drvNum=0; (drvNum<KMaxDrives) && !drivesFound; drvNum++)
   204 		{
   205 		TChar drvLetter='?';
   206 		TFileName fileSystem;
   207 		if(!driveList[drvNum])
   208 			continue;
   209 		test(TheFs.Drive(driveInfo, drvNum) == KErrNone);
   210 		test(TheFs.DriveToChar(drvNum,drvLetter) == KErrNone);
   211 		r=TheFs.FileSystemName(fileSystem,drvNum);
   212 		fileSystem.UpperCase();
   213 		test((r==KErrNone)||(r==KErrNotFound));
   214 		// Check for FAT on NAND
   215 		if(NandFatDrv=='?')
   216 			{
   217 			if((driveInfo.iType==EMediaNANDFlash) && (fileSystem.Compare(KFATName)==0))
   218 				NandFatDrv=drvLetter;
   219 			}
   220 		// Check for ROFS
   221 		if(RofsDrv=='?')
   222 			{
   223 			if((driveInfo.iType==EMediaNANDFlash) && (fileSystem.Compare(KROFSName)==0))
   224 				RofsDrv=drvLetter;
   225 			}
   226 		// Check for LFFS
   227 		if(LffsDrv=='?')
   228 			{
   229 			if((driveInfo.iType==EMediaFlash) && (fileSystem.Compare(KLFFSName)==0))
   230 				LffsDrv=drvLetter;
   231 			}
   232 		// Check for CompFSys
   233 		if(CompDrv=='?')
   234 			{
   235 			if((driveInfo.iType==EMediaRom) && ((fileSystem.Compare(KROMName)==0)||(fileSystem.Compare(KCOMPName)==0)))
   236 				CompDrv=drvLetter;
   237 			}
   238 		drivesFound=((NandFatDrv!='?')&&(RofsDrv!='?')&&(LffsDrv!='?')&&(CompDrv!='?'));
   239 		}
   240 	if(NandFatDrv!='?')
   241 		test((NandFatDrv!=RofsDrv)&&(NandFatDrv!=LffsDrv)&&(NandFatDrv!=CompDrv));
   242 	if(RofsDrv!='?')
   243 		test((RofsDrv!=LffsDrv)&&(RofsDrv!=CompDrv));
   244 	if(LffsDrv!='?')
   245 		test(LffsDrv!=CompDrv);
   246 
   247 	RDebug::Printf("T_DENYCLAMP: FAT drive=%C, ROFS drive=%C, LFFS drive=%C, ROM-COMP drive=%C \n",(TText)NandFatDrv,(TText)RofsDrv,(TText)LffsDrv,(TText)CompDrv);
   248 	return;
   249 	}
   250 
   251 
   252 //
   253 // E32Main
   254 //
   255 
   256 TInt E32Main()
   257 	{
   258 	TInt r;
   259 	test.Title();
   260 	test.Start(_L("Starting T_DENYCLAMP ..."));
   261 	test(TheFs.Connect()==KErrNone);
   262 
   263 	GetDriveLetters();
   264 	TBuf<256> pathName;	
   265 
   266 	//************************************************************************
   267 	//
   268 	// Test on FAT (writable file system)
   269 	//
   270 	//************************************************************************
   271 	if(NandFatDrv!='?')
   272 		{
   273 		pathName=_L("?:\\CLAMP-TST\\");	// FAT on NAND
   274 		pathName[0]=(TText)NandFatDrv;
   275 		r=TheFs.MkDirAll(pathName);
   276 		test(r==KErrNone || r== KErrAlreadyExists);
   277 		TheFs.SetSessionPath(pathName);
   278 		test.Printf( _L("T_DENYCLAMP: testing FAT drive on %C\n"),(TText)NandFatDrv);
   279 
   280 		Test1();		// Basic clamp operation
   281 		Test2();		// Invalid clamp requests
   282 //		Test3(pathName);// Denied FS requests when files are clamped - invalid for T_DENYCLAMP
   283 
   284 		r=TheFs.RmDir(pathName);
   285 		test(r==KErrNone);
   286 		}
   287 	else
   288 		test.Printf( _L("T_DENYCLAMP: FAT drive not tested\n"));
   289 
   290 	//************************************************************************
   291 	//
   292 	// Test on ROFS (non-writable file system) 
   293 	//
   294 	//************************************************************************
   295 	if(RofsDrv!='?')
   296 		{
   297 		pathName=_L("?:\\");
   298 		pathName[0]=(TText)RofsDrv;
   299 		TheFs.SetSessionPath(pathName);
   300 		test.Printf( _L("T_DENYCLAMP: testing ROFS drive on %C\n"),(TText)RofsDrv);
   301 
   302 		Test4(pathName);	// Clamp tests for non-writable file system
   303 		}
   304 	else
   305 		test.Printf( _L("T_DENYCLAMP: ROFS drive not tested\n"));
   306 
   307 	//************************************************************************
   308 	//
   309 	// Test on Z: - Composite File System, or ROMFS (non-writable file system)
   310 	//
   311 	//************************************************************************
   312 	if(CompDrv!='?')
   313 		{
   314 		pathName=_L("?:\\TEST\\");
   315 		pathName[0]=(TText)CompDrv;
   316 		TheFs.SetSessionPath(pathName);
   317 		test.Printf( _L("T_DENYCLAMP: testing Z drive (on %C)\n"),(TText)CompDrv);
   318 
   319 		Test4(pathName);	// Clamp tests for non-writable file system
   320 		}
   321 	else
   322 		test.Printf( _L("T_DENYCLAMP: Z drive not tested\n"));
   323 
   324 	//************************************************************************
   325 	//
   326 	// Test on LFFS (non-clampable file system)
   327 	//
   328 	//************************************************************************
   329 	if(LffsDrv!='?')
   330 		{
   331 		TBuf<256> unsuppPath;	
   332 		unsuppPath=_L("?:\\CLAMP-TST\\");
   333 		unsuppPath[0]=(TText)LffsDrv;
   334 		r=TheFs.MkDirAll(unsuppPath);
   335 		test(r==KErrNone || r== KErrAlreadyExists);
   336 		TheFs.SetSessionPath(unsuppPath);
   337 		test.Printf( _L("T_DENYCLAMP: testing LFFS drive on %C\n"),(TText)LffsDrv);
   338 
   339 		Test5();		// Clamp requests on non-clamping file systems
   340 		}
   341 	else
   342 		test.Printf( _L("T_DENYCLAMP: LFFS drive not tested\n"));
   343 
   344 	test.End();
   345 	return 0;
   346 	}
   347 
   348 #else
   349 
   350 TInt E32Main()
   351 	{
   352 	test.Title();
   353 	test.Start(_L("Test does not run on UREL builds."));
   354 	test.End();
   355 	return 0;
   356 	}
   357 #endif