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