os/kernelhwsrv/kerneltest/f32test/plugins/version_1/virus/t_virus.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2005-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_virus.cpp
    15 // 
    16 //
    17 
    18 #include <f32file.h>
    19 #include <e32test.h>
    20 #include <e32svr.h>
    21 #include <f32dbg.h>
    22 #include "t_server.h"
    23 
    24 GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]);
    25 
    26 #define TEST_FOR_ERROR( r )	TestIfEqual( r, KErrNone, __LINE__, __FILE__)
    27 #define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__)
    28 
    29 _LIT( KValueTestFailMsg, "ERROR Got %d expected %d" );
    30 GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[])
    31 	{
    32 	if( aExpected != aValue )
    33 		{
    34 		TText filenameU[512];
    35 		TUint i = 0;
    36 		for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++)
    37 			{
    38 			filenameU[i]=aFileName[i];
    39 			}
    40 		filenameU[i]=0;
    41 		test.Printf( KValueTestFailMsg, aValue, aExpected );
    42 		test.operator()( EFalse, aLine, &filenameU[0]);
    43 		}
    44 	}
    45 
    46 GLDEF_D RTest test(_L("T_VIRUS"));
    47 
    48 
    49 void CleanupFiles()
    50 	{
    51 	test.Next(_L("Delete any files leftover from a previous run"));
    52 
    53 	TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly);
    54 	TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
    55 
    56 	TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
    57 	TheFs.Delete(_L("C:\\virusdef.txt"));
    58 	
    59 	TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
    60 	TheFs.Delete(_L("virus1.txt"));
    61 
    62 	TheFs.SetAtt(_L("virus2.txt"),0, KEntryAttReadOnly);
    63 	TheFs.Delete(_L("virus2.txt"));
    64 
    65 	TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
    66 	TheFs.Delete(_L("c:\\virus3.txt"));
    67 
    68 	TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
    69 	TheFs.Delete(_L("clean.txt"));
    70 	}
    71 
    72 void CopyFilesL()
    73 	{
    74 	//So that we have the same file on WINS and a platform,
    75 	//we expect to find all the files on Z:\test.  We then
    76 	//copy the virus scanner vsh and the definition file to
    77 	//the C: drive and the files to be scanned to whatever
    78 	//drive we're testing on.
    79 	test.Next(_L("Copying files to the appropriate places"));
    80 
    81 	TInt r = TheFs.MkDirAll(_L("C:\\sys\\bin\\"));
    82 	if (r != KErrAlreadyExists)
    83 		TEST_FOR_ERROR(r);
    84 
    85 	//If we're not currently on drive C, make a sys\\bin directory
    86 	//so we can test attempting to rename it
    87 	r = TheFs.MkDirAll(_L("\\sys\\bin\\"));
    88 	if (r != KErrAlreadyExists)
    89 		TEST_FOR_ERROR(r);
    90 
    91 	CFileMan* pFileMan = CFileMan::NewL(TheFs);
    92 	r = pFileMan->Copy(_L("Z:\\test\\t_vshook.pxt"), _L("C:\\sys\\bin\\t_vshook.pxt"));
    93 	TEST_FOR_ERROR(r);
    94 
    95 	r = pFileMan->Copy(_L("Z:\\test\\virusdef.txt"), _L("C:\\virusdef.txt"));
    96 	TEST_FOR_ERROR(r);
    97 
    98 	r = pFileMan->Copy(_L("Z:\\test\\virus1.txt"), _L("virus1.txt"));
    99 	TEST_FOR_ERROR(r);
   100 
   101 	r = pFileMan->Copy(_L("Z:\\test\\virus2.txt"), _L("virus2.txt"));
   102 	TEST_FOR_ERROR(r);
   103 
   104 	r = pFileMan->Copy(_L("Z:\\test\\clean.txt"), _L("clean.txt"));
   105 	TEST_FOR_ERROR(r);
   106 
   107 	delete pFileMan;
   108 	}
   109 
   110 void TestFileAccessBeforeVirusScanner()
   111 	{
   112 
   113 	//Check that we can read all the files we want before the
   114 	//virus scanner is loaded.
   115 	test.Next(_L("Test opening files before virus scanner is installed"));
   116 
   117 	RFile file;
   118 	TBuf8<512> buffer;
   119 
   120 	TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
   121 
   122 	TEST_FOR_ERROR(r);
   123 	r = file.Read(buffer);
   124 	TEST_FOR_ERROR(r);
   125 	file.Close();
   126 
   127 	r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
   128 	TEST_FOR_ERROR(r);
   129 	r = file.Read(buffer);
   130 	TEST_FOR_ERROR(r);
   131 	file.Close();
   132 
   133 	r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
   134 	TEST_FOR_ERROR(r);
   135 	r = file.Read(buffer);
   136 	TEST_FOR_ERROR(r);
   137 	file.Close();
   138 
   139 	r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
   140 	TEST_FOR_ERROR(r);
   141 	r = file.Read(buffer);
   142 	TEST_FOR_ERROR(r);
   143 	file.Close();
   144 	}
   145 
   146 void TestLoadingOfVirusScanner()
   147 	{
   148 	test.Next(_L("Test the loading of the virus scanner"));
   149 
   150 	// Try loading the virus scanner.
   151 	TInt r = TheFs.AddPlugin(_L("t_vshook"));
   152 	TEST_FOR_ERROR(r);
   153 
   154 	// Try loading the virus scanner again.
   155 	r = TheFs.AddPlugin(_L("t_vshook"));
   156 	TEST_FOR_VALUE(r, KErrAlreadyExists);
   157 
   158 
   159 	// Try mounting the virus scanner.
   160 	r = TheFs.MountPlugin(_L("VsHook"));
   161 	TEST_FOR_ERROR(r);
   162 
   163 
   164 	//Test the name functions
   165 	TFullName vsName;
   166 	r = TheFs.PluginName(vsName,0,0);
   167 	test.Printf(_L("Virus scanner name is: %S\n"), &vsName);
   168 
   169 	}
   170 
   171 void TestUnloadingOfVirusScanner()
   172 	{
   173 	test.Next(_L("Test unloading the virus scanner"));
   174 
   175 	//Unload the virus scanner
   176 	//Wait for it to empty it's input queue.
   177 	User::After(3000000);
   178 
   179 	TInt r = TheFs.DismountPlugin(_L("VsHook"));
   180 	TEST_FOR_ERROR(r);
   181 
   182 	r = TheFs.DismountPlugin(_L("VsHook"));
   183 	TEST_FOR_VALUE(r, KErrNotFound);
   184 
   185 	r = TheFs.RemovePlugin(_L("VsHook"));
   186 	TEST_FOR_ERROR(r);
   187 
   188 	r = TheFs.RemovePlugin(_L("VsHook"));
   189 	TEST_FOR_VALUE(r, KErrNotFound);
   190 	}
   191 
   192 
   193 void TestFileAccessDuringVirusScannerL()
   194 	{
   195 
   196 	RFile file;
   197 	TBuf8<512> buffer;
   198 
   199 	test.Next(_L("Test opening files with the virus scanner installed"));
   200 
   201 	TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
   202 	TEST_FOR_VALUE(r, KErrAccessDenied);
   203 
   204 	r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
   205 	TEST_FOR_VALUE(r, KErrAccessDenied);
   206 
   207 	r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
   208 	TEST_FOR_VALUE(r, KErrAccessDenied);
   209 
   210 	r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny);
   211 	TEST_FOR_VALUE(r, KErrAccessDenied);
   212 
   213 	// Set up a change notifier
   214 	// - The virus scanner renames files before scanning, so check that the
   215 	//	 internal rename operation hasn't caused a notification.
   216 	TRequestStatus reqStat;
   217 	TheFs.NotifyChange(ENotifyAll, reqStat, _L("C:\\F32-TST\\"));
   218 	TEST_FOR_VALUE(reqStat.Int(), KRequestPending);
   219 
   220 	r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
   221 	TEST_FOR_ERROR(r);
   222 	r = file.Read(buffer);
   223 	TEST_FOR_ERROR(r);
   224 	file.Close();
   225 
   226 	// Verify that cleaning file didn't cause a notification
   227 	TEST_FOR_VALUE(reqStat.Int(), KRequestPending);
   228 	TheFs.NotifyChangeCancel(reqStat);
   229 
   230 	//Files on Z: are not scanned, so we should be able to open
   231 	//an infected file
   232 	r = file.Open(TheFs, _L("Z:\\test\\virus1.txt"), EFileShareAny);
   233 	TEST_FOR_ERROR(r);
   234 	r = file.Read(buffer);
   235 	TEST_FOR_ERROR(r);
   236 	file.Close();
   237 
   238 	test.Next(_L("Test opening of scanner files"));
   239 	r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny);
   240 	TEST_FOR_VALUE(r, KErrAccessDenied);
   241 
   242 	r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
   243 	TEST_FOR_VALUE(r, KErrAccessDenied);
   244 
   245 
   246 
   247 	test.Next(_L("Test renaming of infected files"));
   248 	//With RFs::Rename()
   249 	r = TheFs.Rename(_L("virus1.txt"), _L("renamed1.txt"));
   250 	TEST_FOR_VALUE(r, KErrAccessDenied);
   251 
   252 	//Test RFs::Replace()
   253 	r = TheFs.Replace(_L("virus1.txt"), _L("renamed1.txt"));
   254 	TEST_FOR_VALUE(r, KErrAccessDenied);
   255 
   256 	//CFileMan opens the file for exclusive access,
   257 	//so wait for the virus scanner to finish.
   258 	User::After(1000000);
   259 
   260 	//With CFileMan
   261 	CFileMan* pFileMan = CFileMan::NewL(TheFs);
   262 	r = pFileMan->Rename(_L("virus1.txt"), _L("renamed1.txt"));
   263 	TEST_FOR_VALUE(r, KErrAccessDenied);
   264 
   265 	test.Next(_L("Test renaming virus scanner files"));
   266 	//Test renaming of virus definition file
   267 	r = TheFs.Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt"));
   268 	TEST_FOR_VALUE(r, KErrAccessDenied);
   269 
   270 	//With CFileMan
   271 	r = pFileMan->Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt"));
   272 	TEST_FOR_VALUE(r, KErrAccessDenied);
   273 
   274 	//Test renaming of virus scanner hook
   275 	r = TheFs.Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt"));
   276 	TEST_FOR_VALUE(r, KErrAccessDenied);
   277 
   278 	//With CFileMan
   279 	r = pFileMan->Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt"));
   280 	TEST_FOR_VALUE(r, KErrAccessDenied);
   281 
   282 
   283 	test.Next(_L("Test deleting an infected file"));
   284 	r = TheFs.SetAtt(_L("virus2.txt"), 0,KEntryAttReadOnly); 
   285 	TEST_FOR_ERROR(r);
   286 	r = TheFs.Delete(_L("virus2.txt"));
   287 	TEST_FOR_ERROR(r);
   288 
   289 	test.Next(_L("Test closing an infected file"));
   290 	r = file.Create(TheFs, _L("c:\\virus3.txt"), EFileWrite | EFileShareAny);
   291 	TEST_FOR_ERROR(r);
   292 	r = file.Write(_L8("This a generated infected file."));
   293 	TEST_FOR_ERROR(r);
   294 	file.Close();
   295 
   296 	//Test renaming a directory
   297 	test.Next(_L("Test renaming a directory"));
   298 	r = TheFs.Rename(_L("\\sys\\bin\\"), _L("\\virusdir1\\"));
   299 	TEST_FOR_VALUE(r, KErrAccessDenied);
   300 
   301 	delete pFileMan;
   302 	}
   303 
   304 void TestReadFileSectionInterceptL()
   305 	{
   306 	TBuf8<16> testDes;
   307 
   308 	test.Next(_L("Test ReadFileSection intercept GetName and GetFileAccessInfo support"));	
   309 	// trigger RFs::ReadFileSection intercept
   310 	TInt r = TheFs.ReadFileSection(_L("clean.txt"),0,testDes,8);
   311 	test(r==KErrNone);
   312 	}
   313 
   314 
   315 void TestFileAccessAfterVirusScanner()
   316 	{
   317 
   318 	test.Next(_L("Test opening files after virus scanner is uninstalled"));
   319 
   320 	RFile file;
   321 	TBuf8<512> buffer;
   322 
   323 	test.Next(_L("Test the virus definition file"));
   324 	TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
   325 	TEST_FOR_ERROR(r);
   326 	r = file.Read(buffer);
   327 	TEST_FOR_ERROR(r);
   328 	file.Close();
   329 
   330 	if (buffer.Length() == buffer.MaxLength())
   331 		{
   332 		//The definition file should be less than 512 bytes in length.
   333 		r = KErrOverflow;
   334 		TEST_FOR_ERROR(r);
   335 		}
   336 
   337 	//Make sure that the virus scanner has not "fixed" this file
   338 	r = buffer.Find(_L8("infection"));
   339 	TEST_FOR_VALUE(r, KErrNotFound);
   340 
   341 	test.Next(_L("Test the clean file has not been corrected"));
   342 	r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
   343 	TEST_FOR_ERROR(r);
   344 	r = file.Read(buffer);
   345 	TEST_FOR_ERROR(r);
   346 	file.Close();
   347 
   348 	if (buffer.Length() == buffer.MaxLength())
   349 		{
   350 		//The clean file should be less than 512 bytes in length.
   351 		r = KErrOverflow;
   352 		TEST_FOR_ERROR(r);
   353 		}
   354 
   355 	//Make sure that the virus scanner has not "fixed" this file
   356 	r = buffer.Find(_L8("infection"));
   357 	TEST_FOR_VALUE(r, KErrNotFound);
   358 	
   359 
   360 	//Virus2.txt should have been successfully deleted.
   361 	r = file.Open(TheFs, _L("virus2.txt"), EFileRead);
   362 	TEST_FOR_VALUE(r, KErrNotFound);
   363 
   364 	//Virus1.txt should have been corrected several times.
   365 	//Here's the order in which we did things to it in
   366 	//TestFileAccessDuringVirusScannerL():
   367 	//1. Open for read.
   368 	//2. Rename using RFS
   369 	//3. Rename using CFileMan
   370 	test.Next(_L("Test the infected file has been corrected"));
   371 	r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
   372 	TEST_FOR_ERROR(r);
   373 	r = file.Read(buffer);
   374 	TEST_FOR_ERROR(r);
   375 	file.Close();
   376 
   377 	if (buffer.Length() == buffer.MaxLength())
   378 		{
   379 		//The infected file should be less than 512 bytes in length.
   380 		r = KErrOverflow;
   381 		TEST_FOR_ERROR(r);
   382 		}
   383 
   384 	//Make sure that the virus scanner has "fixed" this file,
   385 	//and that the fixing order is correct.
   386 	TInt fixPos1 = buffer.Find(_L8("infection detected - file open"));
   387 	if (fixPos1 < 0)
   388 		{
   389 		TEST_FOR_ERROR(fixPos1);
   390 		}
   391 	TInt fixPos2 = buffer.Find(_L8("infection detected - file rename"));
   392 	if (fixPos2 < 0)
   393 		{
   394 		TEST_FOR_ERROR(fixPos2);
   395 		}
   396 	//The -32 is the length of the "infection detected - file rename"
   397 	TPtrC8 rightPart = buffer.Right((buffer.Length() - fixPos2) - 32);
   398 	TInt fixPos3 = rightPart.Find(_L8("infection detected - file rename"));
   399 	fixPos3 += fixPos2 + 32;
   400 	if (fixPos3 < 0)
   401 		{
   402 		TEST_FOR_ERROR(fixPos3);
   403 		}
   404 	
   405 	if (!(fixPos3 > fixPos2 && fixPos3 > fixPos1 && fixPos2 > fixPos1))
   406 		{
   407 		//The fixes have not been written to the file in the correct
   408 		//order
   409 		r = KErrGeneral;
   410 		TEST_FOR_ERROR(r);
   411 		}
   412 
   413 
   414 	test.Next(_L("Test a generated infection is detected"));
   415 	r = file.Open(TheFs, _L("c:\\virus3.txt"), EFileShareAny);
   416 	TEST_FOR_ERROR(r);
   417 	r = file.Read(buffer);
   418 	TEST_FOR_ERROR(r);
   419 	file.Close();
   420 
   421 	if (buffer.Length() == buffer.MaxLength())
   422 		{
   423 		//The clean file should be less than 512 bytes in length.
   424 		r = KErrOverflow;
   425 		TEST_FOR_ERROR(r);
   426 		}
   427 
   428 	//Make sure that the virus scanner has "fixed" this file
   429 	r = buffer.Find(_L8("infection detected - file close"));
   430 	if (r < 0)
   431 		TEST_FOR_ERROR(r);
   432 	}
   433 
   434 void DeleteFiles()
   435 	{
   436 
   437 	test.Next(_L("Cleanup files"));
   438 	//Delete all of the files created using CopyFilesL() so
   439 	//that the test can run multiple times.
   440 	TInt r = TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly);
   441 	TEST_FOR_ERROR(r);
   442 	r = TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
   443 	TEST_FOR_ERROR(r);
   444 
   445 	r = TheFs.RmDir(_L("\\sys\\bin\\"));
   446 	if (r != KErrInUse)
   447 		TEST_FOR_ERROR(r);
   448 
   449 	r = TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
   450 	TEST_FOR_ERROR(r);
   451 	r = TheFs.Delete(_L("C:\\virusdef.txt"));
   452 	TEST_FOR_ERROR(r);
   453 
   454 	r = TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
   455 	TEST_FOR_ERROR(r);
   456 	r = TheFs.Delete(_L("virus1.txt"));
   457 	TEST_FOR_ERROR(r);
   458 
   459 	r = TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
   460 	TEST_FOR_ERROR(r);
   461 	r = TheFs.Delete(_L("c:\\virus3.txt"));
   462 	TEST_FOR_ERROR(r);
   463 
   464 	r = TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
   465 	TEST_FOR_ERROR(r);
   466 	r = TheFs.Delete(_L("clean.txt"));
   467 	TEST_FOR_ERROR(r);
   468 	}	
   469 
   470 
   471 GLDEF_C void CallTestsL()
   472 	{
   473 
   474 	CleanupFiles();
   475 	CopyFilesL();
   476 
   477 	TestFileAccessBeforeVirusScanner();
   478 
   479 	TestLoadingOfVirusScanner();
   480 
   481 	TestFileAccessDuringVirusScannerL();
   482 
   483 	TestReadFileSectionInterceptL();
   484 
   485 	TestUnloadingOfVirusScanner();
   486 
   487 	TestFileAccessAfterVirusScanner();
   488 
   489 	DeleteFiles();
   490 
   491 	}
   492