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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\server\t_virus.cpp
24 GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]);
26 #define TEST_FOR_ERROR( r ) TestIfEqual( r, KErrNone, __LINE__, __FILE__)
27 #define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__)
29 _LIT( KValueTestFailMsg, "ERROR Got %d expected %d" );
30 GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[])
32 if( aExpected != aValue )
36 for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++)
38 filenameU[i]=aFileName[i];
41 test.Printf( KValueTestFailMsg, aValue, aExpected );
42 test.operator()( EFalse, aLine, &filenameU[0]);
46 GLDEF_D RTest test(_L("T_VIRUS"));
51 test.Next(_L("Delete any files leftover from a previous run"));
53 TheFs.SetAtt(_L("C:\\sys\\bin\\t_vshook.pxt"),0, KEntryAttReadOnly);
54 TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
56 TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
57 TheFs.Delete(_L("C:\\virusdef.txt"));
59 TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
60 TheFs.Delete(_L("virus1.txt"));
62 TheFs.SetAtt(_L("virus2.txt"),0, KEntryAttReadOnly);
63 TheFs.Delete(_L("virus2.txt"));
65 TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
66 TheFs.Delete(_L("c:\\virus3.txt"));
68 TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
69 TheFs.Delete(_L("clean.txt"));
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"));
81 TInt r = TheFs.MkDirAll(_L("C:\\sys\\bin\\"));
82 if (r != KErrAlreadyExists)
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)
91 CFileMan* pFileMan = CFileMan::NewL(TheFs);
92 r = pFileMan->Copy(_L("Z:\\test\\t_vshook.pxt"), _L("C:\\sys\\bin\\t_vshook.pxt"));
95 r = pFileMan->Copy(_L("Z:\\test\\virusdef.txt"), _L("C:\\virusdef.txt"));
98 r = pFileMan->Copy(_L("Z:\\test\\virus1.txt"), _L("virus1.txt"));
101 r = pFileMan->Copy(_L("Z:\\test\\virus2.txt"), _L("virus2.txt"));
104 r = pFileMan->Copy(_L("Z:\\test\\clean.txt"), _L("clean.txt"));
110 void TestFileAccessBeforeVirusScanner()
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"));
120 TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
123 r = file.Read(buffer);
127 r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
129 r = file.Read(buffer);
133 r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
135 r = file.Read(buffer);
139 r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
141 r = file.Read(buffer);
146 void TestLoadingOfVirusScanner()
148 test.Next(_L("Test the loading of the virus scanner"));
150 // Try loading the virus scanner.
151 TInt r = TheFs.AddPlugin(_L("t_vshook"));
154 // Try loading the virus scanner again.
155 r = TheFs.AddPlugin(_L("t_vshook"));
156 TEST_FOR_VALUE(r, KErrAlreadyExists);
159 // Try mounting the virus scanner.
160 r = TheFs.MountPlugin(_L("VsHook"));
164 //Test the name functions
166 r = TheFs.PluginName(vsName,0,0);
167 test.Printf(_L("Virus scanner name is: %S\n"), &vsName);
171 void TestUnloadingOfVirusScanner()
173 test.Next(_L("Test unloading the virus scanner"));
175 //Unload the virus scanner
176 //Wait for it to empty it's input queue.
177 User::After(3000000);
179 TInt r = TheFs.DismountPlugin(_L("VsHook"));
182 r = TheFs.DismountPlugin(_L("VsHook"));
183 TEST_FOR_VALUE(r, KErrNotFound);
185 r = TheFs.RemovePlugin(_L("VsHook"));
188 r = TheFs.RemovePlugin(_L("VsHook"));
189 TEST_FOR_VALUE(r, KErrNotFound);
193 void TestFileAccessDuringVirusScannerL()
199 test.Next(_L("Test opening files with the virus scanner installed"));
201 TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
202 TEST_FOR_VALUE(r, KErrAccessDenied);
204 r = file.Open(TheFs, _L("virus1.txt"), EFileShareAny);
205 TEST_FOR_VALUE(r, KErrAccessDenied);
207 r = file.Open(TheFs, _L("virus2.txt"), EFileShareAny);
208 TEST_FOR_VALUE(r, KErrAccessDenied);
210 r = file.Open(TheFs, _L("C:\\sys\\bin\\t_vshook.pxt"), EFileShareAny);
211 TEST_FOR_VALUE(r, KErrAccessDenied);
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);
220 r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
222 r = file.Read(buffer);
226 // Verify that cleaning file didn't cause a notification
227 TEST_FOR_VALUE(reqStat.Int(), KRequestPending);
228 TheFs.NotifyChangeCancel(reqStat);
230 //Files on Z: are not scanned, so we should be able to open
232 r = file.Open(TheFs, _L("Z:\\test\\virus1.txt"), EFileShareAny);
234 r = file.Read(buffer);
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);
242 r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
243 TEST_FOR_VALUE(r, KErrAccessDenied);
247 test.Next(_L("Test renaming of infected files"));
249 r = TheFs.Rename(_L("virus1.txt"), _L("renamed1.txt"));
250 TEST_FOR_VALUE(r, KErrAccessDenied);
252 //Test RFs::Replace()
253 r = TheFs.Replace(_L("virus1.txt"), _L("renamed1.txt"));
254 TEST_FOR_VALUE(r, KErrAccessDenied);
256 //CFileMan opens the file for exclusive access,
257 //so wait for the virus scanner to finish.
258 User::After(1000000);
261 CFileMan* pFileMan = CFileMan::NewL(TheFs);
262 r = pFileMan->Rename(_L("virus1.txt"), _L("renamed1.txt"));
263 TEST_FOR_VALUE(r, KErrAccessDenied);
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);
271 r = pFileMan->Rename(_L("C:\\virusdef.txt"), _L("C:\\renameddef.txt"));
272 TEST_FOR_VALUE(r, KErrAccessDenied);
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);
279 r = pFileMan->Rename(_L("C:\\sys\\bin\\t_vshook.pxt"), _L("C:\\sys\\bin\\renames.pxt"));
280 TEST_FOR_VALUE(r, KErrAccessDenied);
283 test.Next(_L("Test deleting an infected file"));
284 r = TheFs.SetAtt(_L("virus2.txt"), 0,KEntryAttReadOnly);
286 r = TheFs.Delete(_L("virus2.txt"));
289 test.Next(_L("Test closing an infected file"));
290 r = file.Create(TheFs, _L("c:\\virus3.txt"), EFileWrite | EFileShareAny);
292 r = file.Write(_L8("This a generated infected file."));
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);
304 void TestReadFileSectionInterceptL()
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);
315 void TestFileAccessAfterVirusScanner()
318 test.Next(_L("Test opening files after virus scanner is uninstalled"));
323 test.Next(_L("Test the virus definition file"));
324 TInt r = file.Open(TheFs, _L("C:\\virusdef.txt"), EFileShareAny);
326 r = file.Read(buffer);
330 if (buffer.Length() == buffer.MaxLength())
332 //The definition file should be less than 512 bytes in length.
337 //Make sure that the virus scanner has not "fixed" this file
338 r = buffer.Find(_L8("infection"));
339 TEST_FOR_VALUE(r, KErrNotFound);
341 test.Next(_L("Test the clean file has not been corrected"));
342 r = file.Open(TheFs, _L("clean.txt"), EFileShareAny);
344 r = file.Read(buffer);
348 if (buffer.Length() == buffer.MaxLength())
350 //The clean file should be less than 512 bytes in length.
355 //Make sure that the virus scanner has not "fixed" this file
356 r = buffer.Find(_L8("infection"));
357 TEST_FOR_VALUE(r, KErrNotFound);
360 //Virus2.txt should have been successfully deleted.
361 r = file.Open(TheFs, _L("virus2.txt"), EFileRead);
362 TEST_FOR_VALUE(r, KErrNotFound);
364 //Virus1.txt should have been corrected several times.
365 //Here's the order in which we did things to it in
366 //TestFileAccessDuringVirusScannerL():
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);
373 r = file.Read(buffer);
377 if (buffer.Length() == buffer.MaxLength())
379 //The infected file should be less than 512 bytes in length.
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"));
389 TEST_FOR_ERROR(fixPos1);
391 TInt fixPos2 = buffer.Find(_L8("infection detected - file rename"));
394 TEST_FOR_ERROR(fixPos2);
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;
402 TEST_FOR_ERROR(fixPos3);
405 if (!(fixPos3 > fixPos2 && fixPos3 > fixPos1 && fixPos2 > fixPos1))
407 //The fixes have not been written to the file in the correct
414 test.Next(_L("Test a generated infection is detected"));
415 r = file.Open(TheFs, _L("c:\\virus3.txt"), EFileShareAny);
417 r = file.Read(buffer);
421 if (buffer.Length() == buffer.MaxLength())
423 //The clean file should be less than 512 bytes in length.
428 //Make sure that the virus scanner has "fixed" this file
429 r = buffer.Find(_L8("infection detected - file close"));
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);
442 r = TheFs.Delete(_L("C:\\sys\\bin\\t_vshook.pxt"));
445 r = TheFs.RmDir(_L("\\sys\\bin\\"));
449 r = TheFs.SetAtt(_L("C:\\virusdef.txt"),0, KEntryAttReadOnly);
451 r = TheFs.Delete(_L("C:\\virusdef.txt"));
454 r = TheFs.SetAtt(_L("virus1.txt"),0, KEntryAttReadOnly);
456 r = TheFs.Delete(_L("virus1.txt"));
459 r = TheFs.SetAtt(_L("c:\\virus3.txt"),0, KEntryAttReadOnly);
461 r = TheFs.Delete(_L("c:\\virus3.txt"));
464 r = TheFs.SetAtt(_L("clean.txt"),0, KEntryAttReadOnly);
466 r = TheFs.Delete(_L("clean.txt"));
471 GLDEF_C void CallTestsL()
477 TestFileAccessBeforeVirusScanner();
479 TestLoadingOfVirusScanner();
481 TestFileAccessDuringVirusScannerL();
483 TestReadFileSectionInterceptL();
485 TestUnloadingOfVirusScanner();
487 TestFileAccessAfterVirusScanner();