os/kernelhwsrv/kerneltest/f32test/plugins/version_2beta/t_plugin_v2beta.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/plugins/version_2beta/t_plugin_v2beta.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,479 @@
     1.4 +// Copyright (c) 2006-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\plugins\version_2beta\encrypt\t_plugin_v2beta.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 "f32_test_utils.h"
    1.26 +#include "t_server.h"
    1.27 +#include "encrypt.h"
    1.28 +#include "hex.h"
    1.29 +
    1.30 +GLREF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[]);
    1.31 +
    1.32 +#define TEST_FOR_ERROR( r )	{ TInt _r = (r); if (_r < 0) HandleError(_r, __LINE__); }
    1.33 +#define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__)
    1.34 +
    1.35 +LOCAL_C TBool gFileSystemExtensionLoaded = EFalse;
    1.36 +
    1.37 +LOCAL_C void HandleError(TInt aError, TInt aLine)
    1.38 +	{
    1.39 +	test.Printf(_L("Error %d\n"), aError);
    1.40 +	test.operator()(EFalse, aLine);
    1.41 +	}
    1.42 +
    1.43 +_LIT(KLitTestFileName,"encryptedfile.txt");
    1.44 +
    1.45 +_LIT8( KSampleText, "line 0 abcdefghijklmnopqrstuvwxyz\n"
    1.46 +					"line 1 abcdefghijklmnopqrstuvwxyz\n"
    1.47 +					"line 2 abcdefghijklmnopqrstuvwxyz\n"
    1.48 +					"line 3 abcdefghijklmnopqrstuvwxyz\n"
    1.49 +					"line 4 abcdefghijklmnopqrstuvwxyz\n"
    1.50 +					"line 5 abcdefghijklmnopqrstuvwxyz\n"
    1.51 +					"line 6 abcdefghijklmnopqrstuvwxyz\n"
    1.52 +					"line 7 abcdefghijklmnopqrstuvwxyz\n"
    1.53 +					"line 8 abcdefghijklmnopqrstuvwxyz\n"
    1.54 +					"line 9 abcdefghijklmnopqrstuvwxyz\n");
    1.55 +
    1.56 +_LIT( KValueTestFailMsg, "ERROR Got %d expected %d" );
    1.57 +GLDEF_C void TestIfEqual( TInt aValue, TInt aExpected, TInt aLine, char aFileName[])
    1.58 +	{
    1.59 +	if( aExpected != aValue )
    1.60 +		{
    1.61 +		TText filenameU[512];
    1.62 +		TUint i = 0;
    1.63 +		for (; (i < sizeof(filenameU)) && (aFileName[i] != (char)0); i++)
    1.64 +			{
    1.65 +			filenameU[i]=aFileName[i];
    1.66 +			}
    1.67 +		filenameU[i]=0;
    1.68 +		test.Printf( KValueTestFailMsg, aValue, aExpected );
    1.69 +		test.operator()( EFalse, aLine, &filenameU[0]);
    1.70 +		}
    1.71 +	}
    1.72 +
    1.73 +GLDEF_D RTest test(_L("t_plugin_v2beta"));
    1.74 +
    1.75 +
    1.76 +void TestFileAccessBeforeEncryptionPlugin()
    1.77 +	{
    1.78 +
    1.79 +	//Check that we can read all the files we want before the
    1.80 +	//encryption plugin is loaded.
    1.81 +	test.Next(_L("Test opening files before encryption plugin is installed"));
    1.82 +
    1.83 +	RFile file;
    1.84 +	TInt r = file.Replace(TheFs, KLitTestFileName, EFileShareAny);
    1.85 +	TEST_FOR_ERROR(r);
    1.86 +
    1.87 +	TBuf8<512> binBuffer;
    1.88 +	binBuffer.Copy(KSampleText);
    1.89 +	Encrypt(binBuffer);
    1.90 +
    1.91 +	TBuf8<512*2> hexBuffer;
    1.92 +	Hex(binBuffer, hexBuffer);
    1.93 +
    1.94 +	r = file.Write(hexBuffer);
    1.95 +	TEST_FOR_ERROR(r);
    1.96 +
    1.97 +	r = file.Flush();
    1.98 +	TEST_FOR_ERROR(r);
    1.99 +
   1.100 +	file.Close();
   1.101 +	}
   1.102 +
   1.103 +void TestLoadingOfEncryptionPlugin()
   1.104 +	{
   1.105 +	test.Next(_L("Test the loading of the encryption plugin"));
   1.106 +
   1.107 +	// Try loading the encryption plugin.
   1.108 +	TInt r = TheFs.AddPlugin(_L("t_enchook"));
   1.109 +	TEST_FOR_ERROR(r);
   1.110 +
   1.111 +	// Try loading the encryption plugin again.
   1.112 +	r = TheFs.AddPlugin(_L("t_enchook"));
   1.113 +	TEST_FOR_VALUE(r, KErrAlreadyExists);
   1.114 +
   1.115 +
   1.116 +	// Try mounting the plugin
   1.117 +	r = TheFs.MountPlugin(_L("EncHook"));
   1.118 +	TEST_FOR_ERROR(r);
   1.119 +
   1.120 +	//Test the name functions
   1.121 +	TFullName encName;
   1.122 +	r = TheFs.PluginName(encName,0,1);
   1.123 +	test.Printf(_L("Encryption plugin name is: %S\n"), &encName);
   1.124 +
   1.125 +	}
   1.126 +
   1.127 +void TestUnloadingOfEncryptionPlugin()
   1.128 +	{
   1.129 +	test.Next(_L("Test unloading the encryption plugin"));
   1.130 +
   1.131 +	//Unload the encryption plugin
   1.132 +	//Wait for it to empty it's input queue.
   1.133 +	User::After(3000000);
   1.134 +
   1.135 +	TInt r = TheFs.DismountPlugin(_L("EncHook"));
   1.136 +	TEST_FOR_ERROR(r);
   1.137 +
   1.138 +	r = TheFs.DismountPlugin(_L("EncHook"));
   1.139 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.140 +
   1.141 +	r = TheFs.RemovePlugin(_L("EncHook"));
   1.142 +	TEST_FOR_ERROR(r);
   1.143 +
   1.144 +	r = TheFs.RemovePlugin(_L("EncHook"));
   1.145 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.146 +	}
   1.147 +
   1.148 +
   1.149 +void TestFileAccessDuringEncryptionPluginL()
   1.150 +	{
   1.151 +
   1.152 +	RFile file;
   1.153 +	TBuf8<512> buf;
   1.154 +	TInt len = KSampleText().Length();
   1.155 +	TPtr8 buffer((TUint8*) buf.Ptr(), len, len);
   1.156 +
   1.157 +	test.Next(_L("Test opening & reading files with the encryption plugin installed"));
   1.158 +
   1.159 +	TInt r = file.Open(TheFs, KLitTestFileName, EFileShareAny);
   1.160 +	TEST_FOR_ERROR(r);
   1.161 +
   1.162 +
   1.163 +	buffer.SetLength(KSampleText().Length());
   1.164 +	r = file.Read(buffer);
   1.165 +
   1.166 +	TInt driveNum = CurrentDrive();
   1.167 +	TFSName name;
   1.168 +	TInt err = TheFs.FileSystemName(name, driveNum);
   1.169 +
   1.170 +	if (err != KErrNone)
   1.171 +		{
   1.172 +
   1.173 +		test.Printf(_L("Drive %C: is not ready!"), 'A'+driveNum);
   1.174 +		test(EFalse);
   1.175 +
   1.176 +		}
   1.177 +	else
   1.178 +		if ((gFileSystemExtensionLoaded || name.CompareF(_L("Win32"))==0)&& r == KErrNotSupported)
   1.179 +			{
   1.180 +			test.Printf(_L("File system extension does not support local buffers\n"));
   1.181 +			file.Close();
   1.182 +			return;
   1.183 +			}
   1.184 +
   1.185 +	TEST_FOR_ERROR(r);
   1.186 +
   1.187 +	r = buffer.Compare(KSampleText);
   1.188 +	TEST_FOR_VALUE(r, KErrNone);
   1.189 +
   1.190 +
   1.191 +	// read again - this should be read from cache
   1.192 +	TInt startPos = 0;
   1.193 +	file.Seek(ESeekStart, startPos);
   1.194 +	r = file.Read(buffer);
   1.195 +	TEST_FOR_ERROR(r);
   1.196 +	r = buffer.Compare(KSampleText);
   1.197 +	TEST_FOR_VALUE(r, KErrNone);
   1.198 +
   1.199 +
   1.200 +	file.Close();
   1.201 +	}
   1.202 +
   1.203 +
   1.204 +void TestFileAccessAfterEncryptionPlugin()
   1.205 +	{
   1.206 +
   1.207 +	test.Next(_L("Test opening files after encryption plugin is uninstalled"));
   1.208 +
   1.209 +	}
   1.210 +
   1.211 +void TestFormatDriveIntercept()
   1.212 +    {
   1.213 +    test.Next(_L("Test intercepting of formatting of the drive"));
   1.214 +    RFormat format;
   1.215 +
   1.216 +    TInt tracksRemaining;
   1.217 +    gSessionPath = _L("?:\\F32-TST\\");
   1.218 +    gSessionPath[0] = (TText) gDriveToTest;
   1.219 +    TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining);
   1.220 +    TEST_FOR_VALUE(r, KErrNone);
   1.221 +
   1.222 +    // Don't format the whole drive to save time since
   1.223 +    // it is plugin intercept which is being tested and not
   1.224 +    // the format operation itself
   1.225 +    r = format.Next(tracksRemaining);
   1.226 +    TEST_FOR_VALUE(r, KErrNone);
   1.227 +
   1.228 +    format.Close();
   1.229 +    }
   1.230 +
   1.231 +void TestFormatDriveAfterFormatPlugin()
   1.232 +    {
   1.233 +    test.Next(_L("Test formatting of the drive after plugin unloaded"));
   1.234 +    RFormat format;
   1.235 +
   1.236 +    TInt tracksRemaining;
   1.237 +    gSessionPath = _L("?:\\F32-TST\\");
   1.238 +    gSessionPath[0] = (TText) gDriveToTest;
   1.239 +
   1.240 +    TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining);
   1.241 +    TEST_FOR_VALUE(r, KErrNone);
   1.242 +
   1.243 +
   1.244 +    while(tracksRemaining)
   1.245 +        {
   1.246 +        r = format.Next(tracksRemaining);
   1.247 +        TEST_FOR_VALUE(r, KErrNone);
   1.248 +        }
   1.249 +
   1.250 +    format.Close();
   1.251 +    }
   1.252 +
   1.253 +void TestLoadingOfHexPlugin()
   1.254 +	{
   1.255 +	test.Next(_L("Test the loading of the hex plugin"));
   1.256 +
   1.257 +	// Try loading the hex plugin.
   1.258 +	TInt r = TheFs.AddPlugin(_L("t_hexhook"));
   1.259 +	TEST_FOR_ERROR(r);
   1.260 +
   1.261 +	// Try loading the hex plugin again.
   1.262 +	r = TheFs.AddPlugin(_L("t_hexhook"));
   1.263 +	TEST_FOR_VALUE(r, KErrAlreadyExists);
   1.264 +
   1.265 +
   1.266 +	// Try mounting the plugin
   1.267 +	r = TheFs.MountPlugin(_L("HexHook"));
   1.268 +	TEST_FOR_ERROR(r);
   1.269 +
   1.270 +	//Test the name functions
   1.271 +	TFullName hexName;
   1.272 +	r = TheFs.PluginName(hexName,0,2);
   1.273 +	test.Printf(_L("Hex plugin name is: %S\n"), &hexName);
   1.274 +
   1.275 +	}
   1.276 +
   1.277 +void TestUnloadingOfHexPlugin()
   1.278 +	{
   1.279 +	test.Next(_L("Test unloading the hex plugin"));
   1.280 +
   1.281 +	//Unload the hex plugin
   1.282 +	//Wait for it to empty it's input queue.
   1.283 +	User::After(3000000);
   1.284 +
   1.285 +	TInt r = TheFs.DismountPlugin(_L("HexHook"));
   1.286 +	TEST_FOR_ERROR(r);
   1.287 +
   1.288 +	r = TheFs.DismountPlugin(_L("HexHook"));
   1.289 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.290 +
   1.291 +	r = TheFs.RemovePlugin(_L("HexHook"));
   1.292 +	TEST_FOR_ERROR(r);
   1.293 +
   1.294 +	r = TheFs.RemovePlugin(_L("HexHook"));
   1.295 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.296 +	}
   1.297 +
   1.298 +
   1.299 +void TestLoadingOfTracePlugin()
   1.300 +	{
   1.301 +	test.Next(_L("Test the loading of the trace plugin"));
   1.302 +
   1.303 +	// Try loading the trace plugin.
   1.304 +	TInt r = TheFs.AddPlugin(_L("t_tracehook"));
   1.305 +	TEST_FOR_ERROR(r);
   1.306 +
   1.307 +	// Try loading the trace plugin again.
   1.308 +	r = TheFs.AddPlugin(_L("t_tracehook"));
   1.309 +	TEST_FOR_VALUE(r, KErrAlreadyExists);
   1.310 +
   1.311 +
   1.312 +	// Try mounting the plugin
   1.313 +	r = TheFs.MountPlugin(_L("TraceHook"));
   1.314 +	TEST_FOR_ERROR(r);
   1.315 +
   1.316 +	//Test the name functions
   1.317 +	TFullName traceName;
   1.318 +	r = TheFs.PluginName(traceName,0,0);
   1.319 +	test.Printf(_L("Trace plugin name is: %S\n"), &traceName);
   1.320 +
   1.321 +	}
   1.322 +
   1.323 +void TestUnloadingOfTracePlugin()
   1.324 +	{
   1.325 +	test.Next(_L("Test unloading the trace plugin"));
   1.326 +
   1.327 +	//Unload the trace plugin
   1.328 +	//Wait for it to empty it's input queue.
   1.329 +	User::After(3000000);
   1.330 +
   1.331 +	TInt r = TheFs.DismountPlugin(_L("TraceHook"));
   1.332 +	TEST_FOR_ERROR(r);
   1.333 +
   1.334 +	r = TheFs.DismountPlugin(_L("TraceHook"));
   1.335 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.336 +
   1.337 +	r = TheFs.RemovePlugin(_L("TraceHook"));
   1.338 +	TEST_FOR_ERROR(r);
   1.339 +
   1.340 +	r = TheFs.RemovePlugin(_L("TraceHook"));
   1.341 +	TEST_FOR_VALUE(r, KErrNotFound);
   1.342 +	}
   1.343 +
   1.344 +void TestLoadingOfFormatPlugin()
   1.345 +    {
   1.346 +    test.Next(_L("Test the loading of the format plugin"));
   1.347 +
   1.348 +    // Try loading the format plugin.
   1.349 +    TInt r = TheFs.AddPlugin(_L("t_formathook"));
   1.350 +    TEST_FOR_ERROR(r);
   1.351 +
   1.352 +    // Try loading the format plugin again.
   1.353 +    r = TheFs.AddPlugin(_L("t_formathook"));
   1.354 +    TEST_FOR_VALUE(r, KErrAlreadyExists);
   1.355 +
   1.356 +
   1.357 +    // Try mounting the plugin
   1.358 +    r = TheFs.MountPlugin(_L("FormatHook"));
   1.359 +    TEST_FOR_ERROR(r);
   1.360 +
   1.361 +    //Test the name functions
   1.362 +    TFullName formatName;
   1.363 +    r = TheFs.PluginName(formatName,0,0);
   1.364 +    test.Printf(_L("Format plugin name is: %S\n"), &formatName);
   1.365 +
   1.366 +    }
   1.367 +
   1.368 +void TestUnloadingOfFormatPlugin()
   1.369 +    {
   1.370 +    test.Next(_L("Test unloading the format plugin"));
   1.371 +
   1.372 +    //Unload the format plugin
   1.373 +    //Wait for it to empty it's input queue.
   1.374 +    User::After(3000000);
   1.375 +
   1.376 +    TInt r = TheFs.DismountPlugin(_L("FormatHook"));
   1.377 +    TEST_FOR_ERROR(r);
   1.378 +
   1.379 +    r = TheFs.DismountPlugin(_L("FormatHook"));
   1.380 +    TEST_FOR_VALUE(r, KErrNotFound);
   1.381 +
   1.382 +    r = TheFs.RemovePlugin(_L("FormatHook"));
   1.383 +    TEST_FOR_ERROR(r);
   1.384 +
   1.385 +    r = TheFs.RemovePlugin(_L("FormatHook"));
   1.386 +    TEST_FOR_VALUE(r, KErrNotFound);
   1.387 +    }
   1.388 +
   1.389 +void DeleteFiles()
   1.390 +	{
   1.391 +	test.Next(_L("Cleanup files"));
   1.392 +
   1.393 +	TInt r = TheFs.Delete(KLitTestFileName);
   1.394 +	TEST_FOR_ERROR(r);
   1.395 +	}
   1.396 +
   1.397 +
   1.398 +
   1.399 +
   1.400 +GLDEF_C void CallTestsL()
   1.401 +	{
   1.402 +	TInt theDrive;
   1.403 +	TInt r = TheFs.CharToDrive(gDriveToTest,theDrive);
   1.404 +	test(r == KErrNone);
   1.405 +	TVolumeInfo volInfo;
   1.406 +	r = TheFs.Volume(volInfo, theDrive);
   1.407 +	test (r == KErrNone);
   1.408 +	if (volInfo.iDrive.iType == EMediaRam)
   1.409 +		{
   1.410 +#if defined(__WINS__)
   1.411 +		if(gDriveToTest != 'C')
   1.412 +			{
   1.413 +#endif
   1.414 +			test.Printf(_L("Plugin not supported on RAM drive\n"));
   1.415 +			return;
   1.416 +#if defined(__WINS__)
   1.417 +			}
   1.418 +#endif
   1.419 +		}
   1.420 +
   1.421 +
   1.422 +	TFullName extName;
   1.423 +	r = TheFs.ExtensionName(extName,theDrive, 0);
   1.424 +	if (r == KErrNone)
   1.425 +		{
   1.426 +		test.Printf(_L("File system extension is present (%S)\n"), &extName);
   1.427 +		gFileSystemExtensionLoaded = ETrue;
   1.428 +		}
   1.429 +	else
   1.430 +		{
   1.431 +		test.Printf(_L("File system extension not present.\n"));
   1.432 +		}
   1.433 +
   1.434 +
   1.435 +	TestFileAccessBeforeEncryptionPlugin();
   1.436 +
   1.437 +	TestLoadingOfTracePlugin();
   1.438 +	TestLoadingOfEncryptionPlugin();
   1.439 +	TestLoadingOfHexPlugin();
   1.440 +
   1.441 +	TestFileAccessDuringEncryptionPluginL();
   1.442 +
   1.443 +	TestUnloadingOfHexPlugin();
   1.444 +	TestUnloadingOfEncryptionPlugin();
   1.445 +	TestUnloadingOfTracePlugin();
   1.446 +
   1.447 +	TestFileAccessAfterEncryptionPlugin();
   1.448 +
   1.449 +	DeleteFiles();
   1.450 +
   1.451 +
   1.452 +	// run T_FILE with trace plugin installed
   1.453 +
   1.454 +#if defined(__WINS__)	// only in WINS to save time
   1.455 +TestLoadingOfTracePlugin();
   1.456 +
   1.457 +	RProcess p;
   1.458 +
   1.459 +	TBuf<4> driveBuf=_L("?");
   1.460 +	driveBuf[0] = (TText) gDriveToTest;
   1.461 +
   1.462 +	test.Next(_L("Test running T_FILE with plugin installed"));
   1.463 +
   1.464 +	r = p.Create(_L("T_FILE.exe"), driveBuf);
   1.465 +	test(r==KErrNone);
   1.466 +	TRequestStatus status;
   1.467 +	p.Logon(status);
   1.468 +	p.Resume();
   1.469 +	User::WaitForRequest(status);
   1.470 +TestUnloadingOfTracePlugin();
   1.471 +#endif // __WINS__
   1.472 +
   1.473 +	// Cannot format drive C: so skip this test on that drive
   1.474 +	if (!F32_Test_Utils::Is_Win32(TheFs, EDriveC))
   1.475 +	    {
   1.476 +        TestLoadingOfFormatPlugin();
   1.477 +        TestFormatDriveIntercept();
   1.478 +        TestUnloadingOfFormatPlugin();
   1.479 +        TestFormatDriveAfterFormatPlugin();
   1.480 +        }
   1.481 +	}
   1.482 +