1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/plugins/version_1/virus/t_virus.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,492 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// f32test\server\t_virus.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <f32file.h>
1.22 +#include <e32test.h>
1.23 +#include <e32svr.h>
1.24 +#include <f32dbg.h>
1.25 +#include "t_server.h"
1.26 +
1.27 +GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]);
1.28 +
1.29 +#define TEST_FOR_ERROR( r ) TestIfEqual( r, KErrNone, __LINE__, __FILE__)
1.30 +#define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__)
1.31 +
1.32 +_LIT( KValueTestFailMsg, "ERROR Got %d expected %d" );
1.33 +GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[])
1.34 + {
1.35 + if( aExpected != aValue )
1.36 + {
1.37 + TText filenameU[512];
1.38 + TUint i = 0;
1.39 + for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++)
1.40 + {
1.41 + filenameU[i]=aFileName[i];
1.42 + }
1.43 + filenameU[i]=0;
1.44 + test.Printf( KValueTestFailMsg, aValue, aExpected );
1.45 + test.operator()( EFalse, aLine, &filenameU[0]);
1.46 + }
1.47 + }
1.48 +
1.49 +GLDEF_D RTest test(_L("T_VIRUS"));
1.50 +
1.51 +
1.52 +void CleanupFiles()
1.53 + {
1.54 + test.Next(_L("Delete any files leftover from a previous run"));
1.55 +
1.56 + TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly);
1.57 + TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
1.58 +
1.59 + TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
1.60 + TheFs.Delete(_L("C:\\virusdef.txt"));
1.61 +
1.62 + TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
1.63 + TheFs.Delete(_L("virus1.txt"));
1.64 +
1.65 + TheFs.SetAtt(_L("virus2.txt"),0, KEntryAttReadOnly);
1.66 + TheFs.Delete(_L("virus2.txt"));
1.67 +
1.68 + TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
1.69 + TheFs.Delete(_L("c:\\virus3.txt"));
1.70 +
1.71 + TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
1.72 + TheFs.Delete(_L("clean.txt"));
1.73 + }
1.74 +
1.75 +void CopyFilesL()
1.76 + {
1.77 + //So that we have the same file on WINS and a platform,
1.78 + //we expect to find all the files on Z:\test. We then
1.79 + //copy the virus scanner vsh and the definition file to
1.80 + //the C: drive and the files to be scanned to whatever
1.81 + //drive we're testing on.
1.82 + test.Next(_L("Copying files to the appropriate places"));
1.83 +
1.84 + TInt r = TheFs.MkDirAll(_L("C:\\sys\\bin\\"));
1.85 + if (r != KErrAlreadyExists)
1.86 + TEST_FOR_ERROR(r);
1.87 +
1.88 + //If we're not currently on drive C, make a sys\\bin directory
1.89 + //so we can test attempting to rename it
1.90 + r = TheFs.MkDirAll(_L("\\sys\\bin\\"));
1.91 + if (r != KErrAlreadyExists)
1.92 + TEST_FOR_ERROR(r);
1.93 +
1.94 + CFileMan* pFileMan = CFileMan::NewL(TheFs);
1.95 + r = pFileMan->Copy(_L("Z:\\test\\t_vshook.pxt"), _L("C:\\sys\\bin\\t_vshook.pxt"));
1.96 + TEST_FOR_ERROR(r);
1.97 +
1.98 + r = pFileMan->Copy(_L("Z:\\test\\virusdef.txt"), _L("C:\\virusdef.txt"));
1.99 + TEST_FOR_ERROR(r);
1.100 +
1.101 + r = pFileMan->Copy(_L("Z:\\test\\virus1.txt"), _L("virus1.txt"));
1.102 + TEST_FOR_ERROR(r);
1.103 +
1.104 + r = pFileMan->Copy(_L("Z:\\test\\virus2.txt"), _L("virus2.txt"));
1.105 + TEST_FOR_ERROR(r);
1.106 +
1.107 + r = pFileMan->Copy(_L("Z:\\test\\clean.txt"), _L("clean.txt"));
1.108 + TEST_FOR_ERROR(r);
1.109 +
1.110 + delete pFileMan;
1.111 + }
1.112 +
1.113 +void TestFileAccessBeforeVirusScanner()
1.114 + {
1.115 +
1.116 + //Check that we can read all the files we want before the
1.117 + //virus scanner is loaded.
1.118 + test.Next(_L("Test opening files before virus scanner is installed"));
1.119 +
1.120 + RFile file;
1.121 + TBuf8<512> buffer;
1.122 +
1.123 + TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
1.124 +
1.125 + TEST_FOR_ERROR(r);
1.126 + r = file.Read(buffer);
1.127 + TEST_FOR_ERROR(r);
1.128 + file.Close();
1.129 +
1.130 + r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
1.131 + TEST_FOR_ERROR(r);
1.132 + r = file.Read(buffer);
1.133 + TEST_FOR_ERROR(r);
1.134 + file.Close();
1.135 +
1.136 + r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
1.137 + TEST_FOR_ERROR(r);
1.138 + r = file.Read(buffer);
1.139 + TEST_FOR_ERROR(r);
1.140 + file.Close();
1.141 +
1.142 + r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
1.143 + TEST_FOR_ERROR(r);
1.144 + r = file.Read(buffer);
1.145 + TEST_FOR_ERROR(r);
1.146 + file.Close();
1.147 + }
1.148 +
1.149 +void TestLoadingOfVirusScanner()
1.150 + {
1.151 + test.Next(_L("Test the loading of the virus scanner"));
1.152 +
1.153 + // Try loading the virus scanner.
1.154 + TInt r = TheFs.AddPlugin(_L("t_vshook"));
1.155 + TEST_FOR_ERROR(r);
1.156 +
1.157 + // Try loading the virus scanner again.
1.158 + r = TheFs.AddPlugin(_L("t_vshook"));
1.159 + TEST_FOR_VALUE(r, KErrAlreadyExists);
1.160 +
1.161 +
1.162 + // Try mounting the virus scanner.
1.163 + r = TheFs.MountPlugin(_L("VsHook"));
1.164 + TEST_FOR_ERROR(r);
1.165 +
1.166 +
1.167 + //Test the name functions
1.168 + TFullName vsName;
1.169 + r = TheFs.PluginName(vsName,0,0);
1.170 + test.Printf(_L("Virus scanner name is: %S\n"), &vsName);
1.171 +
1.172 + }
1.173 +
1.174 +void TestUnloadingOfVirusScanner()
1.175 + {
1.176 + test.Next(_L("Test unloading the virus scanner"));
1.177 +
1.178 + //Unload the virus scanner
1.179 + //Wait for it to empty it's input queue.
1.180 + User::After(3000000);
1.181 +
1.182 + TInt r = TheFs.DismountPlugin(_L("VsHook"));
1.183 + TEST_FOR_ERROR(r);
1.184 +
1.185 + r = TheFs.DismountPlugin(_L("VsHook"));
1.186 + TEST_FOR_VALUE(r, KErrNotFound);
1.187 +
1.188 + r = TheFs.RemovePlugin(_L("VsHook"));
1.189 + TEST_FOR_ERROR(r);
1.190 +
1.191 + r = TheFs.RemovePlugin(_L("VsHook"));
1.192 + TEST_FOR_VALUE(r, KErrNotFound);
1.193 + }
1.194 +
1.195 +
1.196 +void TestFileAccessDuringVirusScannerL()
1.197 + {
1.198 +
1.199 + RFile file;
1.200 + TBuf8<512> buffer;
1.201 +
1.202 + test.Next(_L("Test opening files with the virus scanner installed"));
1.203 +
1.204 + TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
1.205 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.206 +
1.207 + r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
1.208 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.209 +
1.210 + r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
1.211 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.212 +
1.213 + r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny);
1.214 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.215 +
1.216 + // Set up a change notifier
1.217 + // - The virus scanner renames files before scanning, so check that the
1.218 + // internal rename operation hasn't caused a notification.
1.219 + TRequestStatus reqStat;
1.220 + TheFs.NotifyChange(ENotifyAll, reqStat, _L("C:\\F32-TST\\"));
1.221 + TEST_FOR_VALUE(reqStat.Int(), KRequestPending);
1.222 +
1.223 + r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
1.224 + TEST_FOR_ERROR(r);
1.225 + r = file.Read(buffer);
1.226 + TEST_FOR_ERROR(r);
1.227 + file.Close();
1.228 +
1.229 + // Verify that cleaning file didn't cause a notification
1.230 + TEST_FOR_VALUE(reqStat.Int(), KRequestPending);
1.231 + TheFs.NotifyChangeCancel(reqStat);
1.232 +
1.233 + //Files on Z: are not scanned, so we should be able to open
1.234 + //an infected file
1.235 + r = file.Open(TheFs, _L("Z:\\test\\virus1.txt"), EFileShareAny);
1.236 + TEST_FOR_ERROR(r);
1.237 + r = file.Read(buffer);
1.238 + TEST_FOR_ERROR(r);
1.239 + file.Close();
1.240 +
1.241 + test.Next(_L("Test opening of scanner files"));
1.242 + r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny);
1.243 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.244 +
1.245 + r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
1.246 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.247 +
1.248 +
1.249 +
1.250 + test.Next(_L("Test renaming of infected files"));
1.251 + //With RFs::Rename()
1.252 + r = TheFs.Rename(_L("virus1.txt"), _L("renamed1.txt"));
1.253 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.254 +
1.255 + //Test RFs::Replace()
1.256 + r = TheFs.Replace(_L("virus1.txt"), _L("renamed1.txt"));
1.257 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.258 +
1.259 + //CFileMan opens the file for exclusive access,
1.260 + //so wait for the virus scanner to finish.
1.261 + User::After(1000000);
1.262 +
1.263 + //With CFileMan
1.264 + CFileMan* pFileMan = CFileMan::NewL(TheFs);
1.265 + r = pFileMan->Rename(_L("virus1.txt"), _L("renamed1.txt"));
1.266 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.267 +
1.268 + test.Next(_L("Test renaming virus scanner files"));
1.269 + //Test renaming of virus definition file
1.270 + r = TheFs.Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt"));
1.271 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.272 +
1.273 + //With CFileMan
1.274 + r = pFileMan->Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt"));
1.275 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.276 +
1.277 + //Test renaming of virus scanner hook
1.278 + r = TheFs.Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt"));
1.279 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.280 +
1.281 + //With CFileMan
1.282 + r = pFileMan->Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt"));
1.283 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.284 +
1.285 +
1.286 + test.Next(_L("Test deleting an infected file"));
1.287 + r = TheFs.SetAtt(_L("virus2.txt"), 0,KEntryAttReadOnly);
1.288 + TEST_FOR_ERROR(r);
1.289 + r = TheFs.Delete(_L("virus2.txt"));
1.290 + TEST_FOR_ERROR(r);
1.291 +
1.292 + test.Next(_L("Test closing an infected file"));
1.293 + r = file.Create(TheFs, _L("c:\\virus3.txt"), EFileWrite | EFileShareAny);
1.294 + TEST_FOR_ERROR(r);
1.295 + r = file.Write(_L8("This a generated infected file."));
1.296 + TEST_FOR_ERROR(r);
1.297 + file.Close();
1.298 +
1.299 + //Test renaming a directory
1.300 + test.Next(_L("Test renaming a directory"));
1.301 + r = TheFs.Rename(_L("\\sys\\bin\\"), _L("\\virusdir1\\"));
1.302 + TEST_FOR_VALUE(r, KErrAccessDenied);
1.303 +
1.304 + delete pFileMan;
1.305 + }
1.306 +
1.307 +void TestReadFileSectionInterceptL()
1.308 + {
1.309 + TBuf8<16> testDes;
1.310 +
1.311 + test.Next(_L("Test ReadFileSection intercept GetName and GetFileAccessInfo support"));
1.312 + // trigger RFs::ReadFileSection intercept
1.313 + TInt r = TheFs.ReadFileSection(_L("clean.txt"),0,testDes,8);
1.314 + test(r==KErrNone);
1.315 + }
1.316 +
1.317 +
1.318 +void TestFileAccessAfterVirusScanner()
1.319 + {
1.320 +
1.321 + test.Next(_L("Test opening files after virus scanner is uninstalled"));
1.322 +
1.323 + RFile file;
1.324 + TBuf8<512> buffer;
1.325 +
1.326 + test.Next(_L("Test the virus definition file"));
1.327 + TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
1.328 + TEST_FOR_ERROR(r);
1.329 + r = file.Read(buffer);
1.330 + TEST_FOR_ERROR(r);
1.331 + file.Close();
1.332 +
1.333 + if (buffer.Length() == buffer.MaxLength())
1.334 + {
1.335 + //The definition file should be less than 512 bytes in length.
1.336 + r = KErrOverflow;
1.337 + TEST_FOR_ERROR(r);
1.338 + }
1.339 +
1.340 + //Make sure that the virus scanner has not "fixed" this file
1.341 + r = buffer.Find(_L8("infection"));
1.342 + TEST_FOR_VALUE(r, KErrNotFound);
1.343 +
1.344 + test.Next(_L("Test the clean file has not been corrected"));
1.345 + r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
1.346 + TEST_FOR_ERROR(r);
1.347 + r = file.Read(buffer);
1.348 + TEST_FOR_ERROR(r);
1.349 + file.Close();
1.350 +
1.351 + if (buffer.Length() == buffer.MaxLength())
1.352 + {
1.353 + //The clean file should be less than 512 bytes in length.
1.354 + r = KErrOverflow;
1.355 + TEST_FOR_ERROR(r);
1.356 + }
1.357 +
1.358 + //Make sure that the virus scanner has not "fixed" this file
1.359 + r = buffer.Find(_L8("infection"));
1.360 + TEST_FOR_VALUE(r, KErrNotFound);
1.361 +
1.362 +
1.363 + //Virus2.txt should have been successfully deleted.
1.364 + r = file.Open(TheFs, _L("virus2.txt"), EFileRead);
1.365 + TEST_FOR_VALUE(r, KErrNotFound);
1.366 +
1.367 + //Virus1.txt should have been corrected several times.
1.368 + //Here's the order in which we did things to it in
1.369 + //TestFileAccessDuringVirusScannerL():
1.370 + //1. Open for read.
1.371 + //2. Rename using RFS
1.372 + //3. Rename using CFileMan
1.373 + test.Next(_L("Test the infected file has been corrected"));
1.374 + r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
1.375 + TEST_FOR_ERROR(r);
1.376 + r = file.Read(buffer);
1.377 + TEST_FOR_ERROR(r);
1.378 + file.Close();
1.379 +
1.380 + if (buffer.Length() == buffer.MaxLength())
1.381 + {
1.382 + //The infected file should be less than 512 bytes in length.
1.383 + r = KErrOverflow;
1.384 + TEST_FOR_ERROR(r);
1.385 + }
1.386 +
1.387 + //Make sure that the virus scanner has "fixed" this file,
1.388 + //and that the fixing order is correct.
1.389 + TInt fixPos1 = buffer.Find(_L8("infection detected - file open"));
1.390 + if (fixPos1 < 0)
1.391 + {
1.392 + TEST_FOR_ERROR(fixPos1);
1.393 + }
1.394 + TInt fixPos2 = buffer.Find(_L8("infection detected - file rename"));
1.395 + if (fixPos2 < 0)
1.396 + {
1.397 + TEST_FOR_ERROR(fixPos2);
1.398 + }
1.399 + //The -32 is the length of the "infection detected - file rename"
1.400 + TPtrC8 rightPart = buffer.Right((buffer.Length() - fixPos2) - 32);
1.401 + TInt fixPos3 = rightPart.Find(_L8("infection detected - file rename"));
1.402 + fixPos3 += fixPos2 + 32;
1.403 + if (fixPos3 < 0)
1.404 + {
1.405 + TEST_FOR_ERROR(fixPos3);
1.406 + }
1.407 +
1.408 + if (!(fixPos3 > fixPos2 && fixPos3 > fixPos1 && fixPos2 > fixPos1))
1.409 + {
1.410 + //The fixes have not been written to the file in the correct
1.411 + //order
1.412 + r = KErrGeneral;
1.413 + TEST_FOR_ERROR(r);
1.414 + }
1.415 +
1.416 +
1.417 + test.Next(_L("Test a generated infection is detected"));
1.418 + r = file.Open(TheFs, _L("c:\\virus3.txt"), EFileShareAny);
1.419 + TEST_FOR_ERROR(r);
1.420 + r = file.Read(buffer);
1.421 + TEST_FOR_ERROR(r);
1.422 + file.Close();
1.423 +
1.424 + if (buffer.Length() == buffer.MaxLength())
1.425 + {
1.426 + //The clean file should be less than 512 bytes in length.
1.427 + r = KErrOverflow;
1.428 + TEST_FOR_ERROR(r);
1.429 + }
1.430 +
1.431 + //Make sure that the virus scanner has "fixed" this file
1.432 + r = buffer.Find(_L8("infection detected - file close"));
1.433 + if (r < 0)
1.434 + TEST_FOR_ERROR(r);
1.435 + }
1.436 +
1.437 +void DeleteFiles()
1.438 + {
1.439 +
1.440 + test.Next(_L("Cleanup files"));
1.441 + //Delete all of the files created using CopyFilesL() so
1.442 + //that the test can run multiple times.
1.443 + TInt r = TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly);
1.444 + TEST_FOR_ERROR(r);
1.445 + r = TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
1.446 + TEST_FOR_ERROR(r);
1.447 +
1.448 + r = TheFs.RmDir(_L("\\sys\\bin\\"));
1.449 + if (r != KErrInUse)
1.450 + TEST_FOR_ERROR(r);
1.451 +
1.452 + r = TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
1.453 + TEST_FOR_ERROR(r);
1.454 + r = TheFs.Delete(_L("C:\\virusdef.txt"));
1.455 + TEST_FOR_ERROR(r);
1.456 +
1.457 + r = TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
1.458 + TEST_FOR_ERROR(r);
1.459 + r = TheFs.Delete(_L("virus1.txt"));
1.460 + TEST_FOR_ERROR(r);
1.461 +
1.462 + r = TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
1.463 + TEST_FOR_ERROR(r);
1.464 + r = TheFs.Delete(_L("c:\\virus3.txt"));
1.465 + TEST_FOR_ERROR(r);
1.466 +
1.467 + r = TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
1.468 + TEST_FOR_ERROR(r);
1.469 + r = TheFs.Delete(_L("clean.txt"));
1.470 + TEST_FOR_ERROR(r);
1.471 + }
1.472 +
1.473 +
1.474 +GLDEF_C void CallTestsL()
1.475 + {
1.476 +
1.477 + CleanupFiles();
1.478 + CopyFilesL();
1.479 +
1.480 + TestFileAccessBeforeVirusScanner();
1.481 +
1.482 + TestLoadingOfVirusScanner();
1.483 +
1.484 + TestFileAccessDuringVirusScannerL();
1.485 +
1.486 + TestReadFileSectionInterceptL();
1.487 +
1.488 + TestUnloadingOfVirusScanner();
1.489 +
1.490 + TestFileAccessAfterVirusScanner();
1.491 +
1.492 + DeleteFiles();
1.493 +
1.494 + }
1.495 +