os/kernelhwsrv/kerneltest/f32test/plugins/version_2beta/t_plugin_v2beta.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) 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\plugins\version_2beta\encrypt\t_plugin_v2beta.cpp
    15 //
    16 //
    17 
    18 #include <f32file.h>
    19 #include <e32test.h>
    20 #include <e32svr.h>
    21 #include <f32dbg.h>
    22 #include "f32_test_utils.h"
    23 #include "t_server.h"
    24 #include "encrypt.h"
    25 #include "hex.h"
    26 
    27 GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]);
    28 
    29 #define TEST_FOR_ERROR( r )	{ TInt _r = (r); if (_r < 0) HandleError(_r, __LINE__); }
    30 #define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__)
    31 
    32 LOCAL_C TBool gFileSystemExtensionLoaded = EFalse;
    33 
    34 LOCAL_C void HandleError(TInt aError, TInt aLine)
    35 	{
    36 	test.Printf(_L("Error %d\n"), aError);
    37 	test.operator()(EFalse, aLine);
    38 	}
    39 
    40 _LIT(KLitTestFileName,"encryptedfile.txt");
    41 
    42 _LIT8( KSampleText, "line 0 abcdefghijklmnopqrstuvwxyz\n"
    43 					"line 1 abcdefghijklmnopqrstuvwxyz\n"
    44 					"line 2 abcdefghijklmnopqrstuvwxyz\n"
    45 					"line 3 abcdefghijklmnopqrstuvwxyz\n"
    46 					"line 4 abcdefghijklmnopqrstuvwxyz\n"
    47 					"line 5 abcdefghijklmnopqrstuvwxyz\n"
    48 					"line 6 abcdefghijklmnopqrstuvwxyz\n"
    49 					"line 7 abcdefghijklmnopqrstuvwxyz\n"
    50 					"line 8 abcdefghijklmnopqrstuvwxyz\n"
    51 					"line 9 abcdefghijklmnopqrstuvwxyz\n");
    52 
    53 _LIT( KValueTestFailMsg, "ERROR Got %d expected %d" );
    54 GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[])
    55 	{
    56 	if( aExpected != aValue )
    57 		{
    58 		TText filenameU[512];
    59 		TUint i = 0;
    60 		for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++)
    61 			{
    62 			filenameU[i]=aFileName[i];
    63 			}
    64 		filenameU[i]=0;
    65 		test.Printf( KValueTestFailMsg, aValue, aExpected );
    66 		test.operator()( EFalse, aLine, &filenameU[0]);
    67 		}
    68 	}
    69 
    70 GLDEF_D RTest test(_L("t_plugin_v2beta"));
    71 
    72 
    73 void TestFileAccessBeforeEncryptionPlugin()
    74 	{
    75 
    76 	//Check that we can read all the files we want before the
    77 	//encryption plugin is loaded.
    78 	test.Next(_L("Test opening files before encryption plugin is installed"));
    79 
    80 	RFile file;
    81 	TInt r = file.Replace(TheFs, KLitTestFileName, EFileShareAny);
    82 	TEST_FOR_ERROR(r);
    83 
    84 	TBuf8<512> binBuffer;
    85 	binBuffer.Copy(KSampleText);
    86 	Encrypt(binBuffer);
    87 
    88 	TBuf8<512*2> hexBuffer;
    89 	Hex(binBuffer, hexBuffer);
    90 
    91 	r = file.Write(hexBuffer);
    92 	TEST_FOR_ERROR(r);
    93 
    94 	r = file.Flush();
    95 	TEST_FOR_ERROR(r);
    96 
    97 	file.Close();
    98 	}
    99 
   100 void TestLoadingOfEncryptionPlugin()
   101 	{
   102 	test.Next(_L("Test the loading of the encryption plugin"));
   103 
   104 	// Try loading the encryption plugin.
   105 	TInt r = TheFs.AddPlugin(_L("t_enchook"));
   106 	TEST_FOR_ERROR(r);
   107 
   108 	// Try loading the encryption plugin again.
   109 	r = TheFs.AddPlugin(_L("t_enchook"));
   110 	TEST_FOR_VALUE(r, KErrAlreadyExists);
   111 
   112 
   113 	// Try mounting the plugin
   114 	r = TheFs.MountPlugin(_L("EncHook"));
   115 	TEST_FOR_ERROR(r);
   116 
   117 	//Test the name functions
   118 	TFullName encName;
   119 	r = TheFs.PluginName(encName,0,1);
   120 	test.Printf(_L("Encryption plugin name is: %S\n"), &encName);
   121 
   122 	}
   123 
   124 void TestUnloadingOfEncryptionPlugin()
   125 	{
   126 	test.Next(_L("Test unloading the encryption plugin"));
   127 
   128 	//Unload the encryption plugin
   129 	//Wait for it to empty it's input queue.
   130 	User::After(3000000);
   131 
   132 	TInt r = TheFs.DismountPlugin(_L("EncHook"));
   133 	TEST_FOR_ERROR(r);
   134 
   135 	r = TheFs.DismountPlugin(_L("EncHook"));
   136 	TEST_FOR_VALUE(r, KErrNotFound);
   137 
   138 	r = TheFs.RemovePlugin(_L("EncHook"));
   139 	TEST_FOR_ERROR(r);
   140 
   141 	r = TheFs.RemovePlugin(_L("EncHook"));
   142 	TEST_FOR_VALUE(r, KErrNotFound);
   143 	}
   144 
   145 
   146 void TestFileAccessDuringEncryptionPluginL()
   147 	{
   148 
   149 	RFile file;
   150 	TBuf8<512> buf;
   151 	TInt len = KSampleText().Length();
   152 	TPtr8 buffer((TUint8*) buf.Ptr(), len, len);
   153 
   154 	test.Next(_L("Test opening & reading files with the encryption plugin installed"));
   155 
   156 	TInt r = file.Open(TheFs, KLitTestFileName, EFileShareAny);
   157 	TEST_FOR_ERROR(r);
   158 
   159 
   160 	buffer.SetLength(KSampleText().Length());
   161 	r = file.Read(buffer);
   162 
   163 	TInt driveNum = CurrentDrive();
   164 	TFSName name;
   165 	TInt err = TheFs.FileSystemName(name, driveNum);
   166 
   167 	if (err != KErrNone)
   168 		{
   169 
   170 		test.Printf(_L("Drive %C: is not ready!"), 'A'+driveNum);
   171 		test(EFalse);
   172 
   173 		}
   174 	else
   175 		if ((gFileSystemExtensionLoaded || name.CompareF(_L("Win32"))==0)&& r == KErrNotSupported)
   176 			{
   177 			test.Printf(_L("File system extension does not support local buffers\n"));
   178 			file.Close();
   179 			return;
   180 			}
   181 
   182 	TEST_FOR_ERROR(r);
   183 
   184 	r = buffer.Compare(KSampleText);
   185 	TEST_FOR_VALUE(r, KErrNone);
   186 
   187 
   188 	// read again - this should be read from cache
   189 	TInt startPos = 0;
   190 	file.Seek(ESeekStart, startPos);
   191 	r = file.Read(buffer);
   192 	TEST_FOR_ERROR(r);
   193 	r = buffer.Compare(KSampleText);
   194 	TEST_FOR_VALUE(r, KErrNone);
   195 
   196 
   197 	file.Close();
   198 	}
   199 
   200 
   201 void TestFileAccessAfterEncryptionPlugin()
   202 	{
   203 
   204 	test.Next(_L("Test opening files after encryption plugin is uninstalled"));
   205 
   206 	}
   207 
   208 void TestFormatDriveIntercept()
   209     {
   210     test.Next(_L("Test intercepting of formatting of the drive"));
   211     RFormat format;
   212 
   213     TInt tracksRemaining;
   214     gSessionPath = _L("?:\\F32-TST\\");
   215     gSessionPath[0] = (TText) gDriveToTest;
   216     TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining);
   217     TEST_FOR_VALUE(r, KErrNone);
   218 
   219     // Don't format the whole drive to save time since
   220     // it is plugin intercept which is being tested and not
   221     // the format operation itself
   222     r = format.Next(tracksRemaining);
   223     TEST_FOR_VALUE(r, KErrNone);
   224 
   225     format.Close();
   226     }
   227 
   228 void TestFormatDriveAfterFormatPlugin()
   229     {
   230     test.Next(_L("Test formatting of the drive after plugin unloaded"));
   231     RFormat format;
   232 
   233     TInt tracksRemaining;
   234     gSessionPath = _L("?:\\F32-TST\\");
   235     gSessionPath[0] = (TText) gDriveToTest;
   236 
   237     TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining);
   238     TEST_FOR_VALUE(r, KErrNone);
   239 
   240 
   241     while(tracksRemaining)
   242         {
   243         r = format.Next(tracksRemaining);
   244         TEST_FOR_VALUE(r, KErrNone);
   245         }
   246 
   247     format.Close();
   248     }
   249 
   250 void TestLoadingOfHexPlugin()
   251 	{
   252 	test.Next(_L("Test the loading of the hex plugin"));
   253 
   254 	// Try loading the hex plugin.
   255 	TInt r = TheFs.AddPlugin(_L("t_hexhook"));
   256 	TEST_FOR_ERROR(r);
   257 
   258 	// Try loading the hex plugin again.
   259 	r = TheFs.AddPlugin(_L("t_hexhook"));
   260 	TEST_FOR_VALUE(r, KErrAlreadyExists);
   261 
   262 
   263 	// Try mounting the plugin
   264 	r = TheFs.MountPlugin(_L("HexHook"));
   265 	TEST_FOR_ERROR(r);
   266 
   267 	//Test the name functions
   268 	TFullName hexName;
   269 	r = TheFs.PluginName(hexName,0,2);
   270 	test.Printf(_L("Hex plugin name is: %S\n"), &hexName);
   271 
   272 	}
   273 
   274 void TestUnloadingOfHexPlugin()
   275 	{
   276 	test.Next(_L("Test unloading the hex plugin"));
   277 
   278 	//Unload the hex plugin
   279 	//Wait for it to empty it's input queue.
   280 	User::After(3000000);
   281 
   282 	TInt r = TheFs.DismountPlugin(_L("HexHook"));
   283 	TEST_FOR_ERROR(r);
   284 
   285 	r = TheFs.DismountPlugin(_L("HexHook"));
   286 	TEST_FOR_VALUE(r, KErrNotFound);
   287 
   288 	r = TheFs.RemovePlugin(_L("HexHook"));
   289 	TEST_FOR_ERROR(r);
   290 
   291 	r = TheFs.RemovePlugin(_L("HexHook"));
   292 	TEST_FOR_VALUE(r, KErrNotFound);
   293 	}
   294 
   295 
   296 void TestLoadingOfTracePlugin()
   297 	{
   298 	test.Next(_L("Test the loading of the trace plugin"));
   299 
   300 	// Try loading the trace plugin.
   301 	TInt r = TheFs.AddPlugin(_L("t_tracehook"));
   302 	TEST_FOR_ERROR(r);
   303 
   304 	// Try loading the trace plugin again.
   305 	r = TheFs.AddPlugin(_L("t_tracehook"));
   306 	TEST_FOR_VALUE(r, KErrAlreadyExists);
   307 
   308 
   309 	// Try mounting the plugin
   310 	r = TheFs.MountPlugin(_L("TraceHook"));
   311 	TEST_FOR_ERROR(r);
   312 
   313 	//Test the name functions
   314 	TFullName traceName;
   315 	r = TheFs.PluginName(traceName,0,0);
   316 	test.Printf(_L("Trace plugin name is: %S\n"), &traceName);
   317 
   318 	}
   319 
   320 void TestUnloadingOfTracePlugin()
   321 	{
   322 	test.Next(_L("Test unloading the trace plugin"));
   323 
   324 	//Unload the trace plugin
   325 	//Wait for it to empty it's input queue.
   326 	User::After(3000000);
   327 
   328 	TInt r = TheFs.DismountPlugin(_L("TraceHook"));
   329 	TEST_FOR_ERROR(r);
   330 
   331 	r = TheFs.DismountPlugin(_L("TraceHook"));
   332 	TEST_FOR_VALUE(r, KErrNotFound);
   333 
   334 	r = TheFs.RemovePlugin(_L("TraceHook"));
   335 	TEST_FOR_ERROR(r);
   336 
   337 	r = TheFs.RemovePlugin(_L("TraceHook"));
   338 	TEST_FOR_VALUE(r, KErrNotFound);
   339 	}
   340 
   341 void TestLoadingOfFormatPlugin()
   342     {
   343     test.Next(_L("Test the loading of the format plugin"));
   344 
   345     // Try loading the format plugin.
   346     TInt r = TheFs.AddPlugin(_L("t_formathook"));
   347     TEST_FOR_ERROR(r);
   348 
   349     // Try loading the format plugin again.
   350     r = TheFs.AddPlugin(_L("t_formathook"));
   351     TEST_FOR_VALUE(r, KErrAlreadyExists);
   352 
   353 
   354     // Try mounting the plugin
   355     r = TheFs.MountPlugin(_L("FormatHook"));
   356     TEST_FOR_ERROR(r);
   357 
   358     //Test the name functions
   359     TFullName formatName;
   360     r = TheFs.PluginName(formatName,0,0);
   361     test.Printf(_L("Format plugin name is: %S\n"), &formatName);
   362 
   363     }
   364 
   365 void TestUnloadingOfFormatPlugin()
   366     {
   367     test.Next(_L("Test unloading the format plugin"));
   368 
   369     //Unload the format plugin
   370     //Wait for it to empty it's input queue.
   371     User::After(3000000);
   372 
   373     TInt r = TheFs.DismountPlugin(_L("FormatHook"));
   374     TEST_FOR_ERROR(r);
   375 
   376     r = TheFs.DismountPlugin(_L("FormatHook"));
   377     TEST_FOR_VALUE(r, KErrNotFound);
   378 
   379     r = TheFs.RemovePlugin(_L("FormatHook"));
   380     TEST_FOR_ERROR(r);
   381 
   382     r = TheFs.RemovePlugin(_L("FormatHook"));
   383     TEST_FOR_VALUE(r, KErrNotFound);
   384     }
   385 
   386 void DeleteFiles()
   387 	{
   388 	test.Next(_L("Cleanup files"));
   389 
   390 	TInt r = TheFs.Delete(KLitTestFileName);
   391 	TEST_FOR_ERROR(r);
   392 	}
   393 
   394 
   395 
   396 
   397 GLDEF_C void CallTestsL()
   398 	{
   399 	TInt theDrive;
   400 	TInt r = TheFs.CharToDrive(gDriveToTest,theDrive);
   401 	test(r == KErrNone);
   402 	TVolumeInfo volInfo;
   403 	r = TheFs.Volume(volInfo, theDrive);
   404 	test (r == KErrNone);
   405 	if (volInfo.iDrive.iType == EMediaRam)
   406 		{
   407 #if defined(__WINS__)
   408 		if(gDriveToTest != 'C')
   409 			{
   410 #endif
   411 			test.Printf(_L("Plugin not supported on RAM drive\n"));
   412 			return;
   413 #if defined(__WINS__)
   414 			}
   415 #endif
   416 		}
   417 
   418 
   419 	TFullName extName;
   420 	r = TheFs.ExtensionName(extName,theDrive, 0);
   421 	if (r == KErrNone)
   422 		{
   423 		test.Printf(_L("File system extension is present (%S)\n"), &extName);
   424 		gFileSystemExtensionLoaded = ETrue;
   425 		}
   426 	else
   427 		{
   428 		test.Printf(_L("File system extension not present.\n"));
   429 		}
   430 
   431 
   432 	TestFileAccessBeforeEncryptionPlugin();
   433 
   434 	TestLoadingOfTracePlugin();
   435 	TestLoadingOfEncryptionPlugin();
   436 	TestLoadingOfHexPlugin();
   437 
   438 	TestFileAccessDuringEncryptionPluginL();
   439 
   440 	TestUnloadingOfHexPlugin();
   441 	TestUnloadingOfEncryptionPlugin();
   442 	TestUnloadingOfTracePlugin();
   443 
   444 	TestFileAccessAfterEncryptionPlugin();
   445 
   446 	DeleteFiles();
   447 
   448 
   449 	// run T_FILE with trace plugin installed
   450 
   451 #if defined(__WINS__)	// only in WINS to save time
   452 TestLoadingOfTracePlugin();
   453 
   454 	RProcess p;
   455 
   456 	TBuf<4> driveBuf=_L("?");
   457 	driveBuf[0] = (TText) gDriveToTest;
   458 
   459 	test.Next(_L("Test running T_FILE with plugin installed"));
   460 
   461 	r = p.Create(_L("T_FILE.exe"), driveBuf);
   462 	test(r==KErrNone);
   463 	TRequestStatus status;
   464 	p.Logon(status);
   465 	p.Resume();
   466 	User::WaitForRequest(status);
   467 TestUnloadingOfTracePlugin();
   468 #endif // __WINS__
   469 
   470 	// Cannot format drive C: so skip this test on that drive
   471 	if (!F32_Test_Utils::Is_Win32(TheFs, EDriveC))
   472 	    {
   473         TestLoadingOfFormatPlugin();
   474         TestFormatDriveIntercept();
   475         TestUnloadingOfFormatPlugin();
   476         TestFormatDriveAfterFormatPlugin();
   477         }
   478 	}
   479