Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "t_testhandler.h"
27 // run failure tests first
29 _LIT8(KTestExeStart, "<testexe>");
30 _LIT8(KExcludedCapsStart, "<excludedcapabilities>");
31 _LIT8(KPolicyStart, "<policy>");
32 _LIT8(KPreActionsStart, "<preactions>");
33 _LIT8(KPassAcionStart, "<passactions>");
34 _LIT8(KFailAcionStart, "<failactions>");
35 _LIT8(KPostActionsStart, "<postactions>");
36 _LIT8(KSecureIdStart, "<secureid>");
37 _LIT8(KSecureIdEnd, "</secureid>");
38 _LIT8(KVendorIdStart, "<vendorid>");
39 _LIT8(KVendorIdEnd, "</vendorid>");
41 _LIT(KFormat,"Action Name : %S \n");
42 _LIT(KSetCapExe, "setcap");
43 _LIT(KSetCapExe2, "setcap : ");
44 //NOTE :If the below literal is uncommented , remove the c:\hardcoded reference
45 // and replace with RFs::GetSystemDrive().
46 //_LIT(KTestExeTmpPath, "c:\\sys\\bin\\policytest_exe.exe");
47 _LIT(KPassScriptPath, "\\policytest_script_pass.txt");
48 _LIT(KFailScriptPath, "\\policytest_script_fail.txt");
49 _LIT(KLogTmpPath, "\\policytest_log.txt");
50 _LIT(KTestPath, "policytest_");
51 _LIT(KFailTestRunning, "Fail Test Script Running");
52 _LIT(KPassTestRunning, "Pass Test Script Running");
55 const TUint KCapabilityAll = 0xffffffff;
57 CPolicyTest* CPolicyTest::NewL(CConsoleBase& aConsole,
59 const TTestActionSpec& aTestActionSpec)
61 CPolicyTest* self = CPolicyTest::NewLC(aConsole, aOut, aTestActionSpec);
62 CleanupStack::Pop(self);
66 CPolicyTest* CPolicyTest::NewLC(CConsoleBase& aConsole,
68 const TTestActionSpec& aTestActionSpec)
70 CPolicyTest* self = new(ELeave) CPolicyTest(aConsole, aOut);
71 CleanupStack::PushL(self);
72 self->ConstructL(aTestActionSpec);
76 CPolicyTest::CPolicyTest(CConsoleBase& aConsole,
78 : CTestAction(aConsole, aOut)
82 void CPolicyTest::ConstructL(const TTestActionSpec& aTestActionSpec)
84 CTestAction::ConstructL(aTestActionSpec);
85 iExpectedResult = KErrNone;
87 User::LeaveIfError(iFs.Connect());
90 User::LeaveIfError(thisProcess.Open(thisProcess.Id()));
91 iProcessSecureId = thisProcess.SecureId();
92 iProcessVendorId = thisProcess.VendorId();
95 SetTestExeL(Input::ParseElement(aTestActionSpec.iActionBody, KTestExeStart));
96 Input::ParseCapabilitySetL(Input::ParseElement(aTestActionSpec.iActionBody, KExcludedCapsStart), iExcludedCaps);
97 SetPolicyL(Input::ParseElement(aTestActionSpec.iActionBody, KPolicyStart));
98 iPreActions = Input::ParseElement(aTestActionSpec.iActionBody, KPreActionsStart).AllocL();
99 SetTestActionL(Input::ParseElement(aTestActionSpec.iActionBody, KPassAcionStart),
100 Input::ParseElement(aTestActionSpec.iActionBody, KFailAcionStart));
101 iPostActions = Input::ParseElement(aTestActionSpec.iActionBody, KPostActionsStart).AllocL();
104 CPolicyTest::~CPolicyTest()
108 iCapabilities.Close();
116 void CPolicyTest::BadUsageL(const TDesC& aMessage)
118 iOut.writeString(_L("Error in script action testpolicy"));
120 iOut.writeString(aMessage);
122 User::Leave(KErrArgument);
125 void CPolicyTest::SetTestExeL(const TDesC8& aPath)
127 if (aPath == KNullDesC8)
129 BadUsageL(_L("testexe not specified"));
132 iTestExe = HBufC::NewMaxL(aPath.Length());
133 TPtr ptr = iTestExe->Des();
137 void CPolicyTest::SetPolicyL(const TDesC8& aSpec)
139 iSecureId = Input::ParseIntElement(aSpec, KSecureIdStart, KSecureIdEnd);
140 iVendorId = Input::ParseIntElement(aSpec, KVendorIdStart, KVendorIdEnd);
142 TCapabilitySet capSet;
143 Input::ParseCapabilitySetL(aSpec, capSet);
145 // Extract capabilities into array
146 for (TInt i = 0 ; i < ECapability_Limit ; ++i)
148 TCapability c = static_cast<TCapability>(i);
149 if (capSet.HasCapability(c))
151 User::LeaveIfError(iCapabilities.Append(c));
156 void CPolicyTest::SetTestActionL(const TDesC8& aPassAction, const TDesC8& aFailAction)
158 if (aPassAction == KNullDesC8)
160 BadUsageL(_L("passactions not specified"));
163 iPassAction = aPassAction.AllocL();
165 if (aFailAction == KNullDesC8)
167 BadUsageL(_L("failactions not specified"));
170 iFailAction = aFailAction.AllocL();
173 void CPolicyTest::PerformAction(TRequestStatus& aStatus)
184 TDriveUnit sysDrive (RFs::GetSystemDrive());
185 TDriveName sysDriveName (sysDrive.Name());
187 TBuf<128> scriptFile (sysDriveName);
188 scriptFile.Append(KPassScriptPath);
189 WriteScriptFileL(scriptFile, *iPassAction);
191 scriptFile.Copy(sysDriveName);
192 scriptFile.Append(KFailScriptPath);
193 WriteScriptFileL(scriptFile, *iFailAction);
199 if (iTestState == ETestFinished)
202 TRequestStatus* status = &aStatus;
203 User::RequestComplete(status, KErrNone);
213 CheckProcessTermintationL();
215 iState = EProcessResults;
218 case EProcessResults:
219 CheckProcessTermintationL();
220 ProcessResultsL(aStatus);
225 iActionState = EPostrequisite;
226 TRequestStatus* status = &aStatus;
227 User::RequestComplete(status, aStatus.Int());
232 void CPolicyTest::StartProcessL(const TDesC& aExe, const TDesC& aCommandLine, TRequestStatus& aStatus)
234 iOut.writeString(_L("Starting child process: "));
235 iOut.writeString(aExe);
236 iOut.writeString(_L(" "));
237 iOut.writeString(aCommandLine);
240 User::LeaveIfError(iProcess.Create(aExe, aCommandLine));
241 iProcess.Logon(aStatus);
245 void CPolicyTest::CheckProcessTermintationL()
247 if (iProcess.ExitType() == EExitPanic)
249 iOut.writeString(_L("Child process panicked: "));
250 iOut.writeString(iProcess.ExitCategory());
251 iOut.writeString(_L(" "));
252 iOut.writeNum(iProcess.ExitReason());
254 User::Leave(KErrGeneral);
256 ASSERT(iProcess.ExitType() == EExitKill);
260 void CPolicyTest::GetNextTest()
262 // Step through capabilities to be tested
263 if (iTestState == ETestFailCap)
266 if (iCapIndex < iCapabilities.Count())
270 // Step through possible tests until we hit a vaild test
273 iTestState = static_cast<TTestState>(iTestState + 1);
275 while (!((iTestState == ETestFailSID && iSecureId) ||
276 (iTestState == ETestFailVID && iVendorId) ||
277 (iTestState == ETestFailCap && iCapabilities.Count()) ||
278 (iTestState == ETestPass) ||
279 (iTestState == ETestFinished)));
282 void CPolicyTest::SetupTestL(TRequestStatus& aStatus)
288 TInt wrongSecureId = iSecureId + 1;
289 iOut.write(_L("Failure test: Wrong SID (%08x):\n\n"), wrongSecureId);
290 SetTestSecurityInfoL(wrongSecureId, iVendorId, KCapabilityAll, aStatus);
296 TInt wrongVendorId = iVendorId + 1;
297 iOut.write(_L("Failure test: Wrong VID (%08x):\n\n"), wrongVendorId);
298 SetTestSecurityInfoL(iSecureId, wrongVendorId, KCapabilityAll, aStatus);
304 TCapability missingCap = iCapabilities[iCapIndex];
306 iOut.writeString(_L("Failure test: Missing capability ("));
307 iOut.writeCapabilityL(missingCap);
308 iOut.writeString(_L("):\n\n"));
310 TUint capSet = ~ (1 << missingCap);
311 SetTestSecurityInfoL(iSecureId, iVendorId, capSet, aStatus);
317 iOut.write(_L("Pass test:\n\n"));
320 for (TInt i = 0 ; i < iCapabilities.Count() ; ++i)
322 capSet |= 1 << iCapabilities[i];
325 SetTestSecurityInfoL(iSecureId, iVendorId, capSet, aStatus);
334 void CPolicyTest::SetTestSecurityInfoL(TInt aSecureId, TInt aVendorId, TUint aCapSet, TRequestStatus& aStatus)
336 // Remove excluded capabilities
337 for (TInt i = 0 ; i < ECapability_Limit ; ++i)
339 if (iExcludedCaps.HasCapability(static_cast<TCapability>(i)))
341 aCapSet &= ~ (1 << i);
345 TBuf<128> commandLine;
346 commandLine.AppendFormat(_L("%S %08x "), iTestExe, aCapSet);
349 commandLine.AppendFormat(_L("-SID %08x "), aSecureId);
353 commandLine.AppendFormat(_L("-VID %08x "), aVendorId);
355 // commandLine.Append(KTestExeTmpPath);
356 iTestExeTmpNewPath = KTestPath;
357 iTestExeTmpNewPath.Append(*iTestExe);
358 commandLine.Append(iTestExeTmpNewPath);
360 TBuf<128> isetcapTmpNewPath1; //stores the value of commandline
361 TBuf<128> isetcapTmpNewPath; //stores the value of KsetCapexe2
362 isetcapTmpNewPath = KSetCapExe2;
363 isetcapTmpNewPath1= commandLine;
364 isetcapTmpNewPath.Append(isetcapTmpNewPath1);
365 RDebug::RawPrint(isetcapTmpNewPath);
366 StartProcessL(KSetCapExe, commandLine, aStatus);
370 void CPolicyTest::WriteScriptFileL(const TDesC& aPath, const TDesC8& aAction)
372 iFs.Delete(aPath); // ignore errors
375 User::LeaveIfError(file.Create(iFs, aPath, EFileShareExclusive | EFileWrite));
376 CleanupClosePushL(file);
378 User::LeaveIfError(file.Write(*iPreActions));
379 User::LeaveIfError(file.Write(aAction));
380 User::LeaveIfError(file.Write(*iPostActions));
382 CleanupStack::PopAndDestroy(&file);
385 void CPolicyTest::RunTestL(TRequestStatus& aStatus)
389 hptr16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*iNameInfo);
390 RDebug::Print(KFormat,hptr16);
393 TDriveUnit sysDrive (RFs::GetSystemDrive());
394 TDriveName sysDriveName (sysDrive.Name());
396 TBuf<128> passScriptFile (sysDriveName);
397 passScriptFile.Append(KPassScriptPath);
399 TBuf<128> failScriptFile (sysDriveName);
400 failScriptFile.Append(KFailScriptPath);
402 TPtrC script = (iTestState == ETestPass) ? passScriptFile : failScriptFile;
403 (iTestState == ETestPass) ? RDebug::RawPrint(KPassTestRunning) : RDebug::RawPrint(KFailTestRunning);
406 TBuf<128> logTmpFile (sysDriveName);
407 logTmpFile.Append(KLogTmpPath);
408 iFs.Delete(logTmpFile); // ignore errors
410 TBuf<128> commandLine;
411 commandLine.AppendFormat(_L("%S %S"), &script, &logTmpFile);
413 StartProcessL(iTestExeTmpNewPath, commandLine, aStatus);
416 void CPolicyTest::ProcessResultsL(TRequestStatus& aStatus)
418 _LIT8(KSummaryLine, " tests failed out of ");
419 _LIT8(KNewLine, "\r\n");
421 TInt failCount = KErrNotFound, runCount;
423 // Read entire log file into memory to process
425 TDriveUnit sysDrive (RFs::GetSystemDrive());
426 TBuf<128> logTmpFile (sysDrive.Name());
427 logTmpFile.Append(KLogTmpPath);
428 User::LeaveIfError(file.Open(iFs, logTmpFile, EFileShareReadersOnly | EFileRead));
429 CleanupClosePushL(file);
432 User::LeaveIfError(file.Size(size));
433 HBufC8* buffer = HBufC8::NewLC(size);
434 TPtr8 ptr = buffer->Des();
436 User::LeaveIfError(file.Read(ptr));
438 iOut.writeString(_L("Child test output:\n"));
443 TInt nextNewline = buffer->Mid(pos).Find(KNewLine);
445 // Split buffer into lines
447 if (nextNewline == KErrNotFound)
449 line.Set(buffer->Mid(pos));
453 line.Set(buffer->Mid(pos, nextNewline + KNewLine().Length()));
455 pos += line.Length();
457 // Search for summary line
458 TInt pos2 = line.Find(KSummaryLine);
459 if (pos2 != KErrNotFound)
461 // Parse the summary line to work out if the test passed
462 TLex8 lex1(line.Left(pos2));
463 TInt err1 = lex1.Val(failCount);
464 TLex8 lex2(line.Mid(pos2 + KSummaryLine().Length()));
465 TInt err2 = lex2.Val(runCount);
467 if (err1 != KErrNone || err2 != KErrNone)
469 iOut.writeString(_L("Failed to parse summary line\n"));
470 User::LeaveIfError(err1);
471 User::LeaveIfError(err2);
476 // Don't print the summary line as this will confuse whatever parsed
478 iOut.writeString(_L("> "));
479 iOut.writeString(line);
483 if (failCount == KErrNotFound)
485 iOut.writeString(_L("Couldn't find summary line in test output\n"));
486 User::Leave(KErrNotFound);
488 iFailCount += failCount;
490 // Print results in different format
491 iOut.write(_L("Tests run: %d\n"), runCount);
492 iOut.write(_L("Tests failed: %d\n"), failCount);
495 CleanupStack::PopAndDestroy(2, &file);
497 TRequestStatus* status = &aStatus;
498 User::RequestComplete(status, KErrNone);
501 void CPolicyTest::PerformCancel()
503 // not implemented - need to pass original status object to LogonCancel
510 iProcess.LogonCancel();
511 iProcess.Kill(KErrCancel);
518 void CPolicyTest::Reset()
522 iTestState = ETestNone;
527 void CPolicyTest::DoReportAction()
529 iOut.writeString(_L("Running policy tests...\n\n"));
532 void CPolicyTest::DoCheckResult(TInt aError)
534 if (aError == KErrNone && iFailCount > 0)
536 iOut.write(_L("%d tests failed\n"), iFailCount);
537 aError = KErrGeneral;
540 iResult = (aError == iExpectedResult);