sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // f32test\server\t_virus.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "t_server.h" sl@0: sl@0: GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]); sl@0: sl@0: #define TEST_FOR_ERROR( r ) TestIfEqual( r, KErrNone, __LINE__, __FILE__) sl@0: #define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__) sl@0: sl@0: _LIT( KValueTestFailMsg, "ERROR Got %d expected %d" ); sl@0: GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]) sl@0: { sl@0: if( aExpected != aValue ) sl@0: { sl@0: TText filenameU[512]; sl@0: TUint i = 0; sl@0: for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++) sl@0: { sl@0: filenameU[i]=aFileName[i]; sl@0: } sl@0: filenameU[i]=0; sl@0: test.Printf( KValueTestFailMsg, aValue, aExpected ); sl@0: test.operator()( EFalse, aLine, &filenameU[0]); sl@0: } sl@0: } sl@0: sl@0: GLDEF_D RTest test(_L("T_VIRUS")); sl@0: sl@0: sl@0: void CleanupFiles() sl@0: { sl@0: test.Next(_L("Delete any files leftover from a previous run")); sl@0: sl@0: TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt")); sl@0: sl@0: TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("C:\\virusdef.txt")); sl@0: sl@0: TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("virus1.txt")); sl@0: sl@0: TheFs.SetAtt(_L("virus2.txt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("virus2.txt")); sl@0: sl@0: TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("c:\\virus3.txt")); sl@0: sl@0: TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly); sl@0: TheFs.Delete(_L("clean.txt")); sl@0: } sl@0: sl@0: void CopyFilesL() sl@0: { sl@0: //So that we have the same file on WINS and a platform, sl@0: //we expect to find all the files on Z:\test. We then sl@0: //copy the virus scanner vsh and the definition file to sl@0: //the C: drive and the files to be scanned to whatever sl@0: //drive we're testing on. sl@0: test.Next(_L("Copying files to the appropriate places")); sl@0: sl@0: TInt r = TheFs.MkDirAll(_L("C:\\sys\\bin\\")); sl@0: if (r != KErrAlreadyExists) sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: //If we're not currently on drive C, make a sys\\bin directory sl@0: //so we can test attempting to rename it sl@0: r = TheFs.MkDirAll(_L("\\sys\\bin\\")); sl@0: if (r != KErrAlreadyExists) sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: CFileMan* pFileMan = CFileMan::NewL(TheFs); sl@0: r = pFileMan->Copy(_L("Z:\\test\\t_vshook.pxt"), _L("C:\\sys\\bin\\t_vshook.pxt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = pFileMan->Copy(_L("Z:\\test\\virusdef.txt"), _L("C:\\virusdef.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = pFileMan->Copy(_L("Z:\\test\\virus1.txt"), _L("virus1.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = pFileMan->Copy(_L("Z:\\test\\virus2.txt"), _L("virus2.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = pFileMan->Copy(_L("Z:\\test\\clean.txt"), _L("clean.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: delete pFileMan; sl@0: } sl@0: sl@0: void TestFileAccessBeforeVirusScanner() sl@0: { sl@0: sl@0: //Check that we can read all the files we want before the sl@0: //virus scanner is loaded. sl@0: test.Next(_L("Test opening files before virus scanner is installed")); sl@0: sl@0: RFile file; sl@0: TBuf8<512> buffer; sl@0: sl@0: TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny); sl@0: sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: r = file.Open(TheFs, _L("clean.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: } sl@0: sl@0: void TestLoadingOfVirusScanner() sl@0: { sl@0: test.Next(_L("Test the loading of the virus scanner")); sl@0: sl@0: // Try loading the virus scanner. sl@0: TInt r = TheFs.AddPlugin(_L("t_vshook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: // Try loading the virus scanner again. sl@0: r = TheFs.AddPlugin(_L("t_vshook")); sl@0: TEST_FOR_VALUE(r, KErrAlreadyExists); sl@0: sl@0: sl@0: // Try mounting the virus scanner. sl@0: r = TheFs.MountPlugin(_L("VsHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: sl@0: //Test the name functions sl@0: TFullName vsName; sl@0: r = TheFs.PluginName(vsName,0,0); sl@0: test.Printf(_L("Virus scanner name is: %S\n"), &vsName); sl@0: sl@0: } sl@0: sl@0: void TestUnloadingOfVirusScanner() sl@0: { sl@0: test.Next(_L("Test unloading the virus scanner")); sl@0: sl@0: //Unload the virus scanner sl@0: //Wait for it to empty it's input queue. sl@0: User::After(3000000); sl@0: sl@0: TInt r = TheFs.DismountPlugin(_L("VsHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.DismountPlugin(_L("VsHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: r = TheFs.RemovePlugin(_L("VsHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RemovePlugin(_L("VsHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: } sl@0: sl@0: sl@0: void TestFileAccessDuringVirusScannerL() sl@0: { sl@0: sl@0: RFile file; sl@0: TBuf8<512> buffer; sl@0: sl@0: test.Next(_L("Test opening files with the virus scanner installed")); sl@0: sl@0: TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: // Set up a change notifier sl@0: // - The virus scanner renames files before scanning, so check that the sl@0: // internal rename operation hasn't caused a notification. sl@0: TRequestStatus reqStat; sl@0: TheFs.NotifyChange(ENotifyAll, reqStat, _L("C:\\F32-TST\\")); sl@0: TEST_FOR_VALUE(reqStat.Int(), KRequestPending); sl@0: sl@0: r = file.Open(TheFs, _L("clean.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: // Verify that cleaning file didn't cause a notification sl@0: TEST_FOR_VALUE(reqStat.Int(), KRequestPending); sl@0: TheFs.NotifyChangeCancel(reqStat); sl@0: sl@0: //Files on Z: are not scanned, so we should be able to open sl@0: //an infected file sl@0: r = file.Open(TheFs, _L("Z:\\test\\virus1.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: test.Next(_L("Test opening of scanner files")); sl@0: r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: sl@0: sl@0: test.Next(_L("Test renaming of infected files")); sl@0: //With RFs::Rename() sl@0: r = TheFs.Rename(_L("virus1.txt"), _L("renamed1.txt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: //Test RFs::Replace() sl@0: r = TheFs.Replace(_L("virus1.txt"), _L("renamed1.txt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: //CFileMan opens the file for exclusive access, sl@0: //so wait for the virus scanner to finish. sl@0: User::After(1000000); sl@0: sl@0: //With CFileMan sl@0: CFileMan* pFileMan = CFileMan::NewL(TheFs); sl@0: r = pFileMan->Rename(_L("virus1.txt"), _L("renamed1.txt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: test.Next(_L("Test renaming virus scanner files")); sl@0: //Test renaming of virus definition file sl@0: r = TheFs.Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: //With CFileMan sl@0: r = pFileMan->Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: //Test renaming of virus scanner hook sl@0: r = TheFs.Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: //With CFileMan sl@0: r = pFileMan->Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: sl@0: test.Next(_L("Test deleting an infected file")); sl@0: r = TheFs.SetAtt(_L("virus2.txt"), 0,KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("virus2.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: test.Next(_L("Test closing an infected file")); sl@0: r = file.Create(TheFs, _L("c:\\virus3.txt"), EFileWrite | EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Write(_L8("This a generated infected file.")); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: //Test renaming a directory sl@0: test.Next(_L("Test renaming a directory")); sl@0: r = TheFs.Rename(_L("\\sys\\bin\\"), _L("\\virusdir1\\")); sl@0: TEST_FOR_VALUE(r, KErrAccessDenied); sl@0: sl@0: delete pFileMan; sl@0: } sl@0: sl@0: void TestReadFileSectionInterceptL() sl@0: { sl@0: TBuf8<16> testDes; sl@0: sl@0: test.Next(_L("Test ReadFileSection intercept GetName and GetFileAccessInfo support")); sl@0: // trigger RFs::ReadFileSection intercept sl@0: TInt r = TheFs.ReadFileSection(_L("clean.txt"),0,testDes,8); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: sl@0: void TestFileAccessAfterVirusScanner() sl@0: { sl@0: sl@0: test.Next(_L("Test opening files after virus scanner is uninstalled")); sl@0: sl@0: RFile file; sl@0: TBuf8<512> buffer; sl@0: sl@0: test.Next(_L("Test the virus definition file")); sl@0: TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: if (buffer.Length() == buffer.MaxLength()) sl@0: { sl@0: //The definition file should be less than 512 bytes in length. sl@0: r = KErrOverflow; sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: //Make sure that the virus scanner has not "fixed" this file sl@0: r = buffer.Find(_L8("infection")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: test.Next(_L("Test the clean file has not been corrected")); sl@0: r = file.Open(TheFs, _L("clean.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: if (buffer.Length() == buffer.MaxLength()) sl@0: { sl@0: //The clean file should be less than 512 bytes in length. sl@0: r = KErrOverflow; sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: //Make sure that the virus scanner has not "fixed" this file sl@0: r = buffer.Find(_L8("infection")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: sl@0: //Virus2.txt should have been successfully deleted. sl@0: r = file.Open(TheFs, _L("virus2.txt"), EFileRead); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: //Virus1.txt should have been corrected several times. sl@0: //Here's the order in which we did things to it in sl@0: //TestFileAccessDuringVirusScannerL(): sl@0: //1. Open for read. sl@0: //2. Rename using RFS sl@0: //3. Rename using CFileMan sl@0: test.Next(_L("Test the infected file has been corrected")); sl@0: r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: if (buffer.Length() == buffer.MaxLength()) sl@0: { sl@0: //The infected file should be less than 512 bytes in length. sl@0: r = KErrOverflow; sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: //Make sure that the virus scanner has "fixed" this file, sl@0: //and that the fixing order is correct. sl@0: TInt fixPos1 = buffer.Find(_L8("infection detected - file open")); sl@0: if (fixPos1 < 0) sl@0: { sl@0: TEST_FOR_ERROR(fixPos1); sl@0: } sl@0: TInt fixPos2 = buffer.Find(_L8("infection detected - file rename")); sl@0: if (fixPos2 < 0) sl@0: { sl@0: TEST_FOR_ERROR(fixPos2); sl@0: } sl@0: //The -32 is the length of the "infection detected - file rename" sl@0: TPtrC8 rightPart = buffer.Right((buffer.Length() - fixPos2) - 32); sl@0: TInt fixPos3 = rightPart.Find(_L8("infection detected - file rename")); sl@0: fixPos3 += fixPos2 + 32; sl@0: if (fixPos3 < 0) sl@0: { sl@0: TEST_FOR_ERROR(fixPos3); sl@0: } sl@0: sl@0: if (!(fixPos3 > fixPos2 && fixPos3 > fixPos1 && fixPos2 > fixPos1)) sl@0: { sl@0: //The fixes have not been written to the file in the correct sl@0: //order sl@0: r = KErrGeneral; sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: sl@0: test.Next(_L("Test a generated infection is detected")); sl@0: r = file.Open(TheFs, _L("c:\\virus3.txt"), EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: file.Close(); sl@0: sl@0: if (buffer.Length() == buffer.MaxLength()) sl@0: { sl@0: //The clean file should be less than 512 bytes in length. sl@0: r = KErrOverflow; sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: //Make sure that the virus scanner has "fixed" this file sl@0: r = buffer.Find(_L8("infection detected - file close")); sl@0: if (r < 0) sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: void DeleteFiles() sl@0: { sl@0: sl@0: test.Next(_L("Cleanup files")); sl@0: //Delete all of the files created using CopyFilesL() so sl@0: //that the test can run multiple times. sl@0: TInt r = TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RmDir(_L("\\sys\\bin\\")); sl@0: if (r != KErrInUse) sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("C:\\virusdef.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("virus1.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("c:\\virus3.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly); sl@0: TEST_FOR_ERROR(r); sl@0: r = TheFs.Delete(_L("clean.txt")); sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: sl@0: GLDEF_C void CallTestsL() sl@0: { sl@0: sl@0: CleanupFiles(); sl@0: CopyFilesL(); sl@0: sl@0: TestFileAccessBeforeVirusScanner(); sl@0: sl@0: TestLoadingOfVirusScanner(); sl@0: sl@0: TestFileAccessDuringVirusScannerL(); sl@0: sl@0: TestReadFileSectionInterceptL(); sl@0: sl@0: TestUnloadingOfVirusScanner(); sl@0: sl@0: TestFileAccessAfterVirusScanner(); sl@0: sl@0: DeleteFiles(); sl@0: sl@0: } sl@0: