Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // \f32test\loader\t_loader_delete.cpp
22 #include "t_loader_delete.h"
24 static RTest test(_L("t_loader_delete"));
27 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError);
28 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName);
29 static void SetHelperCaps(TUint32 aCaps);
30 static void CreateTestFile(RFs& aFs, const TDesC& aTestFile);
31 static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError);
33 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError)
35 Test calling RLoader::Delete from a process with the supplied capabilities.
37 @param aCapMask Capabilities of process which calls RLoader::Delete.
38 @param aExpectedError Expected error reason. The launched executable is expected
39 to panic with category KTldPanicCat and this reason, which
40 is the expected return code from RLoader::Delete.
43 TestWithCaps(aCaps, aExpectedError, KTldTcbFile);
44 TestWithCaps(aCaps, aExpectedError, KTldAllFilesFile);
46 // the following function function calls should fail with either
47 // KErrPermissionDenied if this process is not TCB+AllFiles, or with KErrBadName
48 // because the filename is not fully qualified.
49 TBool pdExp = (aExpectedError == KErrPermissionDenied);
51 // test filenames which are not fully qualified
52 TInt remapErr = pdExp ? KErrPermissionDenied : KErrBadName;
53 TestWithCaps(aCaps, remapErr, KTldFileNoPath);
54 TestWithCaps(aCaps, remapErr, KTldFileNoDrive);
56 // test cannot delete non-existent file
57 TInt rootNonExistErr = pdExp ? KErrPermissionDenied : KErrNotFound;
58 TestWithCaps(aCaps, rootNonExistErr, KTldFileNonExistRoot);
59 TInt dirNonExistErr = pdExp ? KErrPermissionDenied : KErrPathNotFound;
60 TestWithCaps(aCaps, dirNonExistErr, KTldFileNonExistDir);
63 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName)
65 Helper function for TestWithCaps(TUint32, TInt). This function invokes
66 a helper executable with the supplied capabilities and tells it to delete
67 the supplied filename.
69 @param aCapMask Capabilities of process which calls RLoader::Delete.
70 @param aExpectedError Expected error reason. The launched executable is expected
71 to panic with category KTldPanicCat and this reason, which
72 is the expected return code from RLoader::Delete.
73 @param aFileName The helper executable is told to delete this file.
77 _L("TestWithCaps,aCaps=0x%x,aExpectedError=%d,aFileName=\"%S\"\n"),
78 aCaps, aExpectedError, &aFileName);
82 // create the file to delete
87 // if this file is expected to exist then create it
89 TBool shouldExist = (aFileName == KTldTcbFile || aFileName == KTldAllFilesFile);
92 TParsePtrC pp(aFileName);
93 dirName.Set(pp.DriveAndPath());
94 r = fs.MkDirAll(dirName);
95 test(r == KErrNone || r == KErrAlreadyExists);
96 CreateTestFile(fs, aFileName);
100 RunHelper(aFileName, aExpectedError);
104 // if the file could not be deleted then delete it now
106 // a C++ bool for the following equality operator
107 bool exists = (fs.Entry(aFileName, e) == KErrNone);
108 test(exists == (aExpectedError != KErrNone));
112 r = fs.Delete(aFileName);
116 // delete the immediate containing directory. The error code is not
117 // used because the directory may be used for something else.
121 // delete the generated different-caps file
122 r = fs.Delete(_L("c:\\sys\\bin\\tld_helper_caps.exe"));
124 r = fs.Delete(_L("c:\\sys\\hash\\tld_helper_caps.exe"));
125 test(r == KErrNone || r == KErrNotFound || r == KErrPathNotFound);
130 static void SetHelperCaps(TUint32 aCaps)
132 Create an instance of the helper executable, tld_helper.exe,
133 with the supplied capabilities.
137 _LIT(KCommandLineArgsFormat, "tld_helper.exe %x c:\\sys\\bin\\tld_helper_caps.exe");
139 cmdLine.Format(KCommandLineArgsFormat, aCaps);
142 r = p.Create(_L("setcap.exe"), cmdLine);
147 test(rs == KRequestPending);
149 User::WaitForRequest(rs);
154 static void CreateTestFile(RFs& aFs, const TDesC& aTestFile)
156 Create an empty file with the supplied name. This function is used
157 to create file which can be deleted with RLoader::Delete.
159 @param aFs Open file server session.
160 @param aTestFile The test file's name.
166 r = f.Replace(aFs, aTestFile, EFileWrite | EFileStream | EFileShareExclusive);
171 static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError)
173 Invoke the helper executable, tell it to delete the supplied file.
175 @param aFileToDelete Name of file which helper executable should delete
176 with RLoader::Delete.
177 @param aExpectedError The expected return code from RLoader::Delete.
182 // run the helper exe, which will try to delete the file with RLoader::Delete
184 r = ph.Create(_L("tld_helper_caps.exe"), aFileToDelete);
189 test(rsh == KRequestPending);
191 User::WaitForRequest(rsh);
193 // process has died so check the panic category and reason match the expected values
194 test(ph.ExitType() == EExitPanic);
195 test(ph.ExitCategory() == KTldPanicCat);
196 test(ph.ExitReason() == aExpectedError);
203 Executable entrypoint calls test functions within heap check.
207 test.Start(_L("Testing RLoader::Delete"));
210 const TUint32 KTcbMask = 1UL << ECapabilityTCB;
211 const TUint32 KAllFilesMask = 1UL << ECapabilityAllFiles;
213 //Check whether RLoader::Delete handles the case of a bad descriptor being passed
214 //as the filename( KBadDescriptor is not itself the malformed desciptor but
215 //it trigers the check)
216 TestWithCaps(KTcbMask | KAllFilesMask , KErrBadDescriptor, KBadDescriptor);
218 // TCB | AllFiles sufficient without any other caps
219 TestWithCaps(KTcbMask | KAllFilesMask, KErrNone);
221 TestWithCaps(~KTcbMask, KErrPermissionDenied);
222 // AllFiles necessary
223 TestWithCaps(~KAllFilesMask, KErrPermissionDenied);
224 // neither TCB nor AllFiles
225 TestWithCaps(~(KTcbMask | KAllFilesMask), KErrPermissionDenied);
226 TestWithCaps(0, KErrPermissionDenied);