sl@0: // Copyright (c) 2006-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\plugins\version_2beta\encrypt\t_plugin_v2beta.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "f32_test_utils.h" sl@0: #include "t_server.h" sl@0: #include "encrypt.h" sl@0: #include "hex.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 ) { TInt _r = (r); if (_r < 0) HandleError(_r, __LINE__); } sl@0: #define TEST_FOR_VALUE( r, expected ) TestIfEqual( r, expected, __LINE__, __FILE__) sl@0: sl@0: LOCAL_C TBool gFileSystemExtensionLoaded = EFalse; sl@0: sl@0: LOCAL_C void HandleError(TInt aError, TInt aLine) sl@0: { sl@0: test.Printf(_L("Error %d\n"), aError); sl@0: test.operator()(EFalse, aLine); sl@0: } sl@0: sl@0: _LIT(KLitTestFileName,"encryptedfile.txt"); sl@0: sl@0: _LIT8( KSampleText, "line 0 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 1 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 2 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 3 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 4 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 5 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 6 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 7 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 8 abcdefghijklmnopqrstuvwxyz\n" sl@0: "line 9 abcdefghijklmnopqrstuvwxyz\n"); 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_plugin_v2beta")); sl@0: sl@0: sl@0: void TestFileAccessBeforeEncryptionPlugin() sl@0: { sl@0: sl@0: //Check that we can read all the files we want before the sl@0: //encryption plugin is loaded. sl@0: test.Next(_L("Test opening files before encryption plugin is installed")); sl@0: sl@0: RFile file; sl@0: TInt r = file.Replace(TheFs, KLitTestFileName, EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: TBuf8<512> binBuffer; sl@0: binBuffer.Copy(KSampleText); sl@0: Encrypt(binBuffer); sl@0: sl@0: TBuf8<512*2> hexBuffer; sl@0: Hex(binBuffer, hexBuffer); sl@0: sl@0: r = file.Write(hexBuffer); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = file.Flush(); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: file.Close(); sl@0: } sl@0: sl@0: void TestLoadingOfEncryptionPlugin() sl@0: { sl@0: test.Next(_L("Test the loading of the encryption plugin")); sl@0: sl@0: // Try loading the encryption plugin. sl@0: TInt r = TheFs.AddPlugin(_L("t_enchook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: // Try loading the encryption plugin again. sl@0: r = TheFs.AddPlugin(_L("t_enchook")); sl@0: TEST_FOR_VALUE(r, KErrAlreadyExists); sl@0: sl@0: sl@0: // Try mounting the plugin sl@0: r = TheFs.MountPlugin(_L("EncHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: //Test the name functions sl@0: TFullName encName; sl@0: r = TheFs.PluginName(encName,0,1); sl@0: test.Printf(_L("Encryption plugin name is: %S\n"), &encName); sl@0: sl@0: } sl@0: sl@0: void TestUnloadingOfEncryptionPlugin() sl@0: { sl@0: test.Next(_L("Test unloading the encryption plugin")); sl@0: sl@0: //Unload the encryption plugin 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("EncHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.DismountPlugin(_L("EncHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: r = TheFs.RemovePlugin(_L("EncHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RemovePlugin(_L("EncHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: } sl@0: sl@0: sl@0: void TestFileAccessDuringEncryptionPluginL() sl@0: { sl@0: sl@0: RFile file; sl@0: TBuf8<512> buf; sl@0: TInt len = KSampleText().Length(); sl@0: TPtr8 buffer((TUint8*) buf.Ptr(), len, len); sl@0: sl@0: test.Next(_L("Test opening & reading files with the encryption plugin installed")); sl@0: sl@0: TInt r = file.Open(TheFs, KLitTestFileName, EFileShareAny); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: sl@0: buffer.SetLength(KSampleText().Length()); sl@0: r = file.Read(buffer); sl@0: sl@0: TInt driveNum = CurrentDrive(); sl@0: TFSName name; sl@0: TInt err = TheFs.FileSystemName(name, driveNum); sl@0: sl@0: if (err != KErrNone) sl@0: { sl@0: sl@0: test.Printf(_L("Drive %C: is not ready!"), 'A'+driveNum); sl@0: test(EFalse); sl@0: sl@0: } sl@0: else sl@0: if ((gFileSystemExtensionLoaded || name.CompareF(_L("Win32"))==0)&& r == KErrNotSupported) sl@0: { sl@0: test.Printf(_L("File system extension does not support local buffers\n")); sl@0: file.Close(); sl@0: return; sl@0: } sl@0: sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = buffer.Compare(KSampleText); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: sl@0: sl@0: // read again - this should be read from cache sl@0: TInt startPos = 0; sl@0: file.Seek(ESeekStart, startPos); sl@0: r = file.Read(buffer); sl@0: TEST_FOR_ERROR(r); sl@0: r = buffer.Compare(KSampleText); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: sl@0: sl@0: file.Close(); sl@0: } sl@0: sl@0: sl@0: void TestFileAccessAfterEncryptionPlugin() sl@0: { sl@0: sl@0: test.Next(_L("Test opening files after encryption plugin is uninstalled")); sl@0: sl@0: } sl@0: sl@0: void TestFormatDriveIntercept() sl@0: { sl@0: test.Next(_L("Test intercepting of formatting of the drive")); sl@0: RFormat format; sl@0: sl@0: TInt tracksRemaining; sl@0: gSessionPath = _L("?:\\F32-TST\\"); sl@0: gSessionPath[0] = (TText) gDriveToTest; sl@0: TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: sl@0: // Don't format the whole drive to save time since sl@0: // it is plugin intercept which is being tested and not sl@0: // the format operation itself sl@0: r = format.Next(tracksRemaining); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: sl@0: format.Close(); sl@0: } sl@0: sl@0: void TestFormatDriveAfterFormatPlugin() sl@0: { sl@0: test.Next(_L("Test formatting of the drive after plugin unloaded")); sl@0: RFormat format; sl@0: sl@0: TInt tracksRemaining; sl@0: gSessionPath = _L("?:\\F32-TST\\"); sl@0: gSessionPath[0] = (TText) gDriveToTest; sl@0: sl@0: TInt r = format.Open(TheFs, gSessionPath, EQuickFormat, tracksRemaining); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: sl@0: sl@0: while(tracksRemaining) sl@0: { sl@0: r = format.Next(tracksRemaining); sl@0: TEST_FOR_VALUE(r, KErrNone); sl@0: } sl@0: sl@0: format.Close(); sl@0: } sl@0: sl@0: void TestLoadingOfHexPlugin() sl@0: { sl@0: test.Next(_L("Test the loading of the hex plugin")); sl@0: sl@0: // Try loading the hex plugin. sl@0: TInt r = TheFs.AddPlugin(_L("t_hexhook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: // Try loading the hex plugin again. sl@0: r = TheFs.AddPlugin(_L("t_hexhook")); sl@0: TEST_FOR_VALUE(r, KErrAlreadyExists); sl@0: sl@0: sl@0: // Try mounting the plugin sl@0: r = TheFs.MountPlugin(_L("HexHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: //Test the name functions sl@0: TFullName hexName; sl@0: r = TheFs.PluginName(hexName,0,2); sl@0: test.Printf(_L("Hex plugin name is: %S\n"), &hexName); sl@0: sl@0: } sl@0: sl@0: void TestUnloadingOfHexPlugin() sl@0: { sl@0: test.Next(_L("Test unloading the hex plugin")); sl@0: sl@0: //Unload the hex plugin 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("HexHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.DismountPlugin(_L("HexHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: r = TheFs.RemovePlugin(_L("HexHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RemovePlugin(_L("HexHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: } sl@0: sl@0: sl@0: void TestLoadingOfTracePlugin() sl@0: { sl@0: test.Next(_L("Test the loading of the trace plugin")); sl@0: sl@0: // Try loading the trace plugin. sl@0: TInt r = TheFs.AddPlugin(_L("t_tracehook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: // Try loading the trace plugin again. sl@0: r = TheFs.AddPlugin(_L("t_tracehook")); sl@0: TEST_FOR_VALUE(r, KErrAlreadyExists); sl@0: sl@0: sl@0: // Try mounting the plugin sl@0: r = TheFs.MountPlugin(_L("TraceHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: //Test the name functions sl@0: TFullName traceName; sl@0: r = TheFs.PluginName(traceName,0,0); sl@0: test.Printf(_L("Trace plugin name is: %S\n"), &traceName); sl@0: sl@0: } sl@0: sl@0: void TestUnloadingOfTracePlugin() sl@0: { sl@0: test.Next(_L("Test unloading the trace plugin")); sl@0: sl@0: //Unload the trace plugin 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("TraceHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.DismountPlugin(_L("TraceHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: r = TheFs.RemovePlugin(_L("TraceHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RemovePlugin(_L("TraceHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: } sl@0: sl@0: void TestLoadingOfFormatPlugin() sl@0: { sl@0: test.Next(_L("Test the loading of the format plugin")); sl@0: sl@0: // Try loading the format plugin. sl@0: TInt r = TheFs.AddPlugin(_L("t_formathook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: // Try loading the format plugin again. sl@0: r = TheFs.AddPlugin(_L("t_formathook")); sl@0: TEST_FOR_VALUE(r, KErrAlreadyExists); sl@0: sl@0: sl@0: // Try mounting the plugin sl@0: r = TheFs.MountPlugin(_L("FormatHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: //Test the name functions sl@0: TFullName formatName; sl@0: r = TheFs.PluginName(formatName,0,0); sl@0: test.Printf(_L("Format plugin name is: %S\n"), &formatName); sl@0: sl@0: } sl@0: sl@0: void TestUnloadingOfFormatPlugin() sl@0: { sl@0: test.Next(_L("Test unloading the format plugin")); sl@0: sl@0: //Unload the format plugin 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("FormatHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.DismountPlugin(_L("FormatHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: sl@0: r = TheFs.RemovePlugin(_L("FormatHook")); sl@0: TEST_FOR_ERROR(r); sl@0: sl@0: r = TheFs.RemovePlugin(_L("FormatHook")); sl@0: TEST_FOR_VALUE(r, KErrNotFound); sl@0: } sl@0: sl@0: void DeleteFiles() sl@0: { sl@0: test.Next(_L("Cleanup files")); sl@0: sl@0: TInt r = TheFs.Delete(KLitTestFileName); sl@0: TEST_FOR_ERROR(r); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: GLDEF_C void CallTestsL() sl@0: { sl@0: TInt theDrive; sl@0: TInt r = TheFs.CharToDrive(gDriveToTest,theDrive); sl@0: test(r == KErrNone); sl@0: TVolumeInfo volInfo; sl@0: r = TheFs.Volume(volInfo, theDrive); sl@0: test (r == KErrNone); sl@0: if (volInfo.iDrive.iType == EMediaRam) sl@0: { sl@0: #if defined(__WINS__) sl@0: if(gDriveToTest != 'C') sl@0: { sl@0: #endif sl@0: test.Printf(_L("Plugin not supported on RAM drive\n")); sl@0: return; sl@0: #if defined(__WINS__) sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: sl@0: TFullName extName; sl@0: r = TheFs.ExtensionName(extName,theDrive, 0); sl@0: if (r == KErrNone) sl@0: { sl@0: test.Printf(_L("File system extension is present (%S)\n"), &extName); sl@0: gFileSystemExtensionLoaded = ETrue; sl@0: } sl@0: else sl@0: { sl@0: test.Printf(_L("File system extension not present.\n")); sl@0: } sl@0: sl@0: sl@0: TestFileAccessBeforeEncryptionPlugin(); sl@0: sl@0: TestLoadingOfTracePlugin(); sl@0: TestLoadingOfEncryptionPlugin(); sl@0: TestLoadingOfHexPlugin(); sl@0: sl@0: TestFileAccessDuringEncryptionPluginL(); sl@0: sl@0: TestUnloadingOfHexPlugin(); sl@0: TestUnloadingOfEncryptionPlugin(); sl@0: TestUnloadingOfTracePlugin(); sl@0: sl@0: TestFileAccessAfterEncryptionPlugin(); sl@0: sl@0: DeleteFiles(); sl@0: sl@0: sl@0: // run T_FILE with trace plugin installed sl@0: sl@0: #if defined(__WINS__) // only in WINS to save time sl@0: TestLoadingOfTracePlugin(); sl@0: sl@0: RProcess p; sl@0: sl@0: TBuf<4> driveBuf=_L("?"); sl@0: driveBuf[0] = (TText) gDriveToTest; sl@0: sl@0: test.Next(_L("Test running T_FILE with plugin installed")); sl@0: sl@0: r = p.Create(_L("T_FILE.exe"), driveBuf); sl@0: test(r==KErrNone); sl@0: TRequestStatus status; sl@0: p.Logon(status); sl@0: p.Resume(); sl@0: User::WaitForRequest(status); sl@0: TestUnloadingOfTracePlugin(); sl@0: #endif // __WINS__ sl@0: sl@0: // Cannot format drive C: so skip this test on that drive sl@0: if (!F32_Test_Utils::Is_Win32(TheFs, EDriveC)) sl@0: { sl@0: TestLoadingOfFormatPlugin(); sl@0: TestFormatDriveIntercept(); sl@0: TestUnloadingOfFormatPlugin(); sl@0: TestFormatDriveAfterFormatPlugin(); sl@0: } sl@0: } sl@0: