installationservices/swinstallationfw/test/plugins/nonnativeplugin/source/testnonnativeplugin.cpp
branchRCL_3
changeset 66 8b7f4e561641
parent 65 7333d7932ef7
child 70 e8965914fac7
equal deleted inserted replaced
65:7333d7932ef7 66:8b7f4e561641
     1 /*
       
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "testnonnativeplugin.h"
       
    20 #include "usiflog.h"
       
    21 #include <usif/sif/sif.h>
       
    22 #include <usif/sif/sifcommon.h>
       
    23 #include <usif/usiferror.h>
       
    24 #include <usif/sif/sifplugin.h>
       
    25 #include <usif/scr/screntries.h>
       
    26 #include <usif/scr/scr.h>
       
    27 #include <usif/sts/sts.h>
       
    28 #include <usif/sif/sifutils.h>
       
    29 
       
    30 using namespace Usif;
       
    31 
       
    32 namespace
       
    33 	{
       
    34 	const TInt KUserGrantableCapsBitMask = 49152; // ECapabilityReadUserData, ECapabilityWriteUserData
       
    35 	const TInt KMaxInstalledSize = 1234;
       
    36 	const TBool KHasExe = EFalse;
       
    37 	const TBool KDriveSelectionRequired = EFalse;	
       
    38 	const TAuthenticity KAuthenticity = EAuthenticated;
       
    39 	_LIT_SECURE_ID(KSifTestSuiteSecureId, 0x10285BCC);
       
    40 	_LIT_SECURE_ID(KSifRefInstallerServerSecureId, 0x1028634E);
       
    41 	_LIT_SECURE_ID(KSifLauncherSecureId, 0x10285BD0);
       
    42 	_LIT(KSifTestComponentVersion, "1.0.0");
       
    43 	_LIT(KSifTestGlobalComponentId, "test.nonnativepackage.txt");
       
    44 #ifndef TESTNONNATIVEPLUGIN2
       
    45 	const TInt KTestNonNativePluginImpId = 0x10285BD2;
       
    46 	const TComponentId KSifTestComponentId = 12345678;
       
    47 	_LIT(KSifTestSoftwareType, "plain");
       
    48 	_LIT(KSifTestComponentName, "Test Component");
       
    49 	_LIT(KSifTestComponentVendor, "Symbian");
       
    50 	_LIT(KSifTestInstallPath, "c:\\tusif\\tsif\\nonnativeplugin\\%S\\file%d");
       
    51 	_LIT(KSifTestOutIntParam, "NonNativePlugin");
       
    52 #else
       
    53 	const TInt KTestNonNativePluginImpId = 0x102866EF;
       
    54 	const TComponentId KSifTestComponentId = 87654321;
       
    55 	_LIT(KSifTestSoftwareType, "testSoftwareType");
       
    56 	_LIT(KSifTestComponentName, "Test Layered Execution Environment");
       
    57 	_LIT(KSifTestComponentVendor, "Test Vendor");
       
    58 	_LIT(KSifTestInstallPath, "c:\\tusif\\tsif\\nonnativeplugin2\\%S\\file%d");
       
    59 	_LIT(KSifTestOutIntParam, "NonNativePlugin2");
       
    60 #endif
       
    61 
       
    62 	const TImplementationProxy ImplementationTable[] = 
       
    63 		{
       
    64 		IMPLEMENTATION_PROXY_ENTRY(KTestNonNativePluginImpId, CTestNonNativePlugin::NewL)
       
    65 		};
       
    66 
       
    67 	void UnpackCapabilitySet(TInt aPackedCapSet, TCapabilitySet& aCapSet)
       
    68 		{
       
    69 		for (TInt c=0; c<ECapability_Limit; ++c)
       
    70 			{
       
    71 			const TInt cap = 1<<c;
       
    72 			if (aPackedCapSet&cap)
       
    73 				{
       
    74 				aCapSet.AddCapability(TCapability(c));
       
    75 				}
       
    76 			}
       
    77 		}
       
    78 
       
    79 	TComponentId AddComponentToScrL(RSoftwareComponentRegistry& aScr, const TDesC& aComponentName, const TDesC& aVendor)
       
    80 		{
       
    81 		// Check if the component is already installed
       
    82 		RSoftwareComponentRegistryView scrView;
       
    83 		CComponentFilter* filter = CComponentFilter::NewLC();
       
    84 		filter->SetNameL(aComponentName);
       
    85 		filter->SetVendorL(aVendor);
       
    86 
       
    87 		scrView.OpenViewL(aScr, filter);
       
    88 		CleanupClosePushL(scrView);
       
    89 
       
    90 		CComponentEntry* component = scrView.NextComponentL();
       
    91 		CleanupStack::PopAndDestroy(2, filter);
       
    92 		if (component != NULL)
       
    93 			{
       
    94 			delete component;
       
    95 			DEBUG_PRINTF2(_L("Non-native component: %S already installed"), &aComponentName);
       
    96 			User::Leave(KErrSifSameVersionAlreadyInstalled);
       
    97 			}
       
    98 
       
    99 		TComponentId componentId = aScr.AddComponentL(aComponentName, aVendor, KSifTestSoftwareType);
       
   100 		DEBUG_PRINTF2(_L("Added a non-native component with id %d"), componentId);
       
   101 
       
   102 		return componentId;
       
   103 		}
       
   104 
       
   105 	void CreateAndRegisterFilesL(RSoftwareComponentRegistry& aScr, TComponentId aComponentId, const TDesC& aComponentName)
       
   106 		{
       
   107 		RStsSession sts;
       
   108 		sts.CreateTransactionL();
       
   109 		CleanupClosePushL(sts);
       
   110 
       
   111 		const TInt numComponentFiles = 3;
       
   112 		const TInt maxIntDigits = 10;
       
   113 		
       
   114 		TInt componentSize = 0;
       
   115 		
       
   116 		for (TInt i=0; i<numComponentFiles; ++i)
       
   117 			{
       
   118 			// Generate the full path of the i-th file
       
   119 			const TInt maxPathLen = KSifTestInstallPath.iTypeLength+aComponentName.Length()+maxIntDigits;
       
   120 			HBufC* filePath = HBufC::NewLC(maxPathLen);
       
   121 			TPtr filePathPtr = filePath->Des();
       
   122 			filePathPtr.Format(KSifTestInstallPath, &aComponentName, i);
       
   123 
       
   124 			// Create the file
       
   125 			RFile file;
       
   126 			sts.CreateNewL(*filePath, file, TFileMode(EFileShareExclusive |EFileWrite));
       
   127 			_LIT8(KNonNativeFootprint, "This file belongs to the Non-Native Test component.");
       
   128 			User::LeaveIfError(file.Write(KNonNativeFootprint));
       
   129 			componentSize += KNonNativeFootprint.iTypeLength;
       
   130 			file.Close();
       
   131 
       
   132 			// Register the file in the SCR
       
   133 			aScr.RegisterComponentFileL(aComponentId, *filePath);
       
   134 
       
   135 			CleanupStack::PopAndDestroy(filePath);
       
   136 			}
       
   137 
       
   138 		aScr.SetComponentSizeL(aComponentId, componentSize);
       
   139 
       
   140 		sts.CommitL();
       
   141 		CleanupStack::PopAndDestroy(&sts);
       
   142 		}
       
   143 
       
   144 	// The literals below are hardcoded because they are used only here so there
       
   145 	// is no risk of duplication or a typo. Of course, they are also used in the
       
   146 	// sif.ini file but we cannot reuse them there in any case.
       
   147 
       
   148 	void ProcessOpaqueParamsL(const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams)
       
   149 		{
       
   150 		aOutputParams.AddIntL(KSifTestOutIntParam, ETrue);
       
   151 		TInt silent = ETrue;
       
   152 		TBool silentFound = aInputParams.GetIntByNameL(_L("DoSilent"), silent);
       
   153 		if (silentFound)
       
   154 			{
       
   155 			aOutputParams.AddIntL(_L("DoneSilently"), silent);
       
   156 			}
       
   157 		}
       
   158 
       
   159 	void InstallL(const TDesC& aComponentName, COpaqueNamedParams& aOutputParams)
       
   160 		{
       
   161 		RSoftwareComponentRegistry scr;
       
   162 		User::LeaveIfError(scr.Connect());
       
   163 		CleanupClosePushL(scr);
       
   164 		scr.CreateTransactionL();
       
   165 
       
   166 		TComponentId componentId = AddComponentToScrL(scr, aComponentName, KSifTestComponentVendor);
       
   167 		aOutputParams.AddIntL(KSifOutParam_ComponentId, componentId);
       
   168 
       
   169 		CreateAndRegisterFilesL(scr, componentId, aComponentName);
       
   170 
       
   171 		scr.CommitTransactionL();
       
   172 		CleanupStack::PopAndDestroy(&scr);
       
   173 		}
       
   174 
       
   175 	void InstallImplL(const TDesC& aFileName, const TSecurityContext& /*aSecurityContext*/,
       
   176 					const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams)
       
   177 		{
       
   178 		// The "DummyInstallation" param instructs the installer not to execute any real operation. This dummy installation
       
   179 		// is needed in order to test the ExcludeOperation mode supported by the SIF APIs. Running multiple concurrent
       
   180 		// SIF operations may lead to concurrent SCR transactions which are not supported. Hence, this dummy installation
       
   181 		// is used as a 'safe' installation that may be launched concurrently with a regular native installation.
       
   182 		TInt dummyInstallation = EFalse;
       
   183 		if (aInputParams.GetIntByNameL(_L("DummyInstallation"), dummyInstallation) && dummyInstallation)
       
   184 			{
       
   185 			return;
       
   186 			}
       
   187 
       
   188 		ProcessOpaqueParamsL(aInputParams, aOutputParams);
       
   189 
       
   190 		TPtrC name;
       
   191 		const TInt pos = aFileName.LocateReverse('\\');
       
   192 		if (pos != KErrNotFound)
       
   193 			{
       
   194 			name.Set(aFileName.Mid(pos+1));
       
   195 			}
       
   196 		else
       
   197 			{
       
   198 			name.Set(aFileName);
       
   199 			}
       
   200 		
       
   201 		InstallL(name, aOutputParams);
       
   202 		}
       
   203 
       
   204 	void InstallImplL(RFile& aFileHandle, const TSecurityContext& /*aSecurityContext*/,
       
   205 					const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams)
       
   206 		{
       
   207 		ProcessOpaqueParamsL(aInputParams, aOutputParams);
       
   208 		HBufC* name = HBufC::NewLC(KMaxFullName);
       
   209 		TPtr namePtr = name->Des();
       
   210 		User::LeaveIfError(aFileHandle.Name(namePtr));
       
   211 		InstallL(*name, aOutputParams);
       
   212 		CleanupStack::PopAndDestroy(name);
       
   213 		}
       
   214 
       
   215 	void UninstallImplL(TComponentId aComponentId, const TSecurityContext& /*aSecurityContext*/,
       
   216 						const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams)
       
   217 		{
       
   218 		ProcessOpaqueParamsL(aInputParams, aOutputParams);
       
   219 
       
   220 		// Uninstall the component using the SIF Utils Library
       
   221 		UninstallL(aComponentId);
       
   222 		}
       
   223 
       
   224 	void CheckSecurityContextL(const TSecurityContext& aSecurityContext)
       
   225 		{
       
   226 		// There are three executables that may issue software management request for TestNonNative packages.
       
   227 		// These are:
       
   228 		// (1) TEF test server (KSifTestSuiteSecureId) - all USIF tests
       
   229 		// (2) the Reference Installer (KSifRefInstallerServerSecureId) - cross installer tests (for embedded packages)
       
   230 		// (3) the SIF Launcher - for testing integration with AppArc
       
   231 		// For the first two we also check ReadUserData to ensure coverage in integration tests
       
   232 		if (aSecurityContext.SecureId() == KSifLauncherSecureId)
       
   233 			return;
       
   234 		aSecurityContext.HasCapabilityL(ECapabilityReadUserData);
       
   235 		if (aSecurityContext.SecureId() != KSifTestSuiteSecureId &&
       
   236 			aSecurityContext.SecureId() != KSifRefInstallerServerSecureId )
       
   237 			{
       
   238 			User::Leave(KErrPermissionDenied);
       
   239 			}
       
   240 		}
       
   241 
       
   242 	TBool CheckSecurityContext(const TSecurityContext& aSecurityContext)
       
   243 		{
       
   244 		if (aSecurityContext.SecureId() == KSifLauncherSecureId)
       
   245 			return ETrue;
       
   246 		return aSecurityContext.HasCapability(ECapabilityReadUserData) &&
       
   247 			   (aSecurityContext.SecureId() == KSifTestSuiteSecureId ||
       
   248 			    aSecurityContext.SecureId() == KSifRefInstallerServerSecureId);
       
   249 		}
       
   250 
       
   251 	void GetComponentInfoImplL(CComponentInfo& aComponentInfo, TBool aTestOverflow = EFalse)
       
   252 		{
       
   253 		// Prepare capabilities
       
   254 		TCapabilitySet capSet;
       
   255 		RPointerArray<Usif::CComponentInfo::CApplicationInfo>* applications = NULL;
       
   256 		capSet.SetEmpty();
       
   257 		UnpackCapabilitySet(KUserGrantableCapsBitMask, capSet);
       
   258 		
       
   259 		// Create the root node of the CComponentInfo object
       
   260 		CComponentInfo::CNode* rootNode = CComponentInfo::CNode::NewLC(KSifTestSoftwareType, KSifTestComponentName,
       
   261 						KSifTestComponentVersion, KSifTestComponentVendor, EDeactivated, ENewComponent,
       
   262 						KSifTestComponentId, KSifTestGlobalComponentId, KAuthenticity, capSet, KMaxInstalledSize, KHasExe, KDriveSelectionRequired, applications);
       
   263 
       
   264 		// Create a long string used for overflow tests
       
   265 		HBufC* longStr = NULL;
       
   266 
       
   267 		if (aTestOverflow)
       
   268 			{
       
   269 			// The CComponentInfo class defines KMaxComponentInfoStreamedSize as:
       
   270 			// KMaxComponentInfoStreamedSize = 2*KMaxHeapBufLength+6*sizeof(TInt)+sizeof(TInt8), where KMaxHeapBufLength = 2048
       
   271 			// So, we need a tree of CNode objects in order to cause a KErrOverflow error
       
   272 			const TInt longStrLen = 256;
       
   273 			longStr = HBufC::NewLC(longStrLen);
       
   274 			TPtr longStrPtr(longStr->Des());
       
   275 			longStrPtr.Fill(TChar(0xadcb), longStrLen);
       
   276 
       
   277 			const TInt numChildren = 3;
       
   278 			for (TInt i=0; i<numChildren; ++i)
       
   279 				{
       
   280 				CComponentInfo::CNode* childNode = CComponentInfo::CNode::NewLC(KSifTestSoftwareType, *longStr,
       
   281 						KSifTestComponentVersion, *longStr, EDeactivated, ENewComponent,
       
   282 						KSifTestComponentId, *longStr, KAuthenticity, capSet, KMaxInstalledSize, KHasExe, KDriveSelectionRequired, applications);
       
   283 				
       
   284 				rootNode->AddChildL(childNode);
       
   285 				CleanupStack::Pop(childNode);
       
   286 				}
       
   287 			
       
   288 			CleanupStack::PopAndDestroy(longStr);
       
   289 			}
       
   290 
       
   291 		aComponentInfo.SetRootNodeL(rootNode);
       
   292 		CleanupStack::Pop(rootNode);
       
   293 
       
   294 		}
       
   295 	}
       
   296 
       
   297 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   298 	{
       
   299 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   300 	return ImplementationTable;
       
   301 	}
       
   302 
       
   303 CTestNonNativePlugin* CTestNonNativePlugin::NewL()
       
   304 	{
       
   305 	DEBUG_PRINTF(_L8("Constructing CTestNonNativePlugin"));
       
   306 	CTestNonNativePlugin *self = new (ELeave) CTestNonNativePlugin();
       
   307 	CleanupStack::PushL(self);
       
   308 	self->ConstructL();
       
   309 	CleanupStack::Pop(self);
       
   310 	return self;
       
   311 	}
       
   312 
       
   313 void CTestNonNativePlugin::ConstructL()
       
   314 	{
       
   315 	}
       
   316 
       
   317 CTestNonNativePlugin::~CTestNonNativePlugin()
       
   318 	{
       
   319 	}
       
   320 
       
   321 void CTestNonNativePlugin::CancelOperation()
       
   322 	{
       
   323 	DEBUG_PRINTF(_L8("Test non-native plugin - Cancel"));
       
   324 	}
       
   325 
       
   326 void CTestNonNativePlugin::GetComponentInfo(const TDesC& aFileName, const TSecurityContext& aSecurityContext,
       
   327 										CComponentInfo& aComponentInfo, TRequestStatus& aStatus)
       
   328 	{
       
   329 	DEBUG_PRINTF(_L8("Test non-native plugin - GetComponentInfo by filename"));
       
   330 	aStatus = KRequestPending;
       
   331 
       
   332 	TInt err = KErrNone;
       
   333 	// We test a non-leaving version of TSecurityContext::HasCapability() here
       
   334 	if (CheckSecurityContext(aSecurityContext))
       
   335 		{
       
   336 		_LIT(KTxtTestOverflow, "z:\\tusif\\tsif\\data\\test.overflow.txt");
       
   337 		TRAP(err, GetComponentInfoImplL(aComponentInfo, aFileName == KTxtTestOverflow));
       
   338 		}
       
   339 	else
       
   340 		{
       
   341 		err = KErrPermissionDenied;
       
   342 		}
       
   343 
       
   344 	TRequestStatus *statusPtr(&aStatus);
       
   345 	User::RequestComplete(statusPtr, err);
       
   346 	}
       
   347 
       
   348 void CTestNonNativePlugin::GetComponentInfo(RFile& /*aFileHandle*/, const TSecurityContext& aSecurityContext,
       
   349 											CComponentInfo& aComponentInfo, TRequestStatus& aStatus)
       
   350 	{
       
   351 	DEBUG_PRINTF(_L8("Test non-native plugin - GetComponentInfo by file handle"));
       
   352 	aStatus = KRequestPending;
       
   353 
       
   354 	TRAPD(err, CheckSecurityContextL(aSecurityContext);
       
   355 			   GetComponentInfoImplL(aComponentInfo));
       
   356 
       
   357 	TRequestStatus *statusPtr(&aStatus);
       
   358 	User::RequestComplete(statusPtr, err);
       
   359 	}
       
   360 
       
   361 void CTestNonNativePlugin::Install(const TDesC& aFileName, const TSecurityContext& aSecurityContext,
       
   362 									const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams,
       
   363 									TRequestStatus& aStatus)
       
   364 	{
       
   365 	DEBUG_PRINTF2(_L("Test non-native plugin - install for %S"), &aFileName);
       
   366 	aStatus = KRequestPending;
       
   367 
       
   368 	TRAPD(err, CheckSecurityContextL(aSecurityContext);
       
   369 				InstallImplL(aFileName, aSecurityContext, aInputParams, aOutputParams));
       
   370 
       
   371 	TRequestStatus *statusPtr(&aStatus);
       
   372 	User::RequestComplete(statusPtr, err);
       
   373 	}
       
   374 
       
   375 void CTestNonNativePlugin::Install(RFile& aFileHandle, const TSecurityContext& aSecurityContext,
       
   376 									const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams,
       
   377 									TRequestStatus& aStatus)
       
   378 	{
       
   379 	DEBUG_PRINTF(_L("Test non-native plugin - install by file handle"));
       
   380 	aStatus = KRequestPending;
       
   381 	
       
   382 	TRAPD(err, CheckSecurityContextL(aSecurityContext);
       
   383 			   InstallImplL(aFileHandle, aSecurityContext, aInputParams, aOutputParams));
       
   384 
       
   385 	TRequestStatus *statusPtr(&aStatus);
       
   386 	User::RequestComplete(statusPtr, err);
       
   387 	}
       
   388 
       
   389 void CTestNonNativePlugin::Uninstall(TComponentId aComponentId, const TSecurityContext& aSecurityContext,
       
   390 								const COpaqueNamedParams& aInputParams, COpaqueNamedParams& aOutputParams, TRequestStatus& aStatus)
       
   391 	{
       
   392 	DEBUG_PRINTF2(_L8("Test non-native plugin - uninstall for ComponentId =  %d"), aComponentId);
       
   393 	aStatus = KRequestPending;
       
   394 
       
   395 	TRAPD(err, CheckSecurityContextL(aSecurityContext);
       
   396 	 		   UninstallImplL(aComponentId, aSecurityContext, aInputParams, aOutputParams));
       
   397 
       
   398 	TRequestStatus* statusPtr(&aStatus);
       
   399 	User::RequestComplete(statusPtr, err);
       
   400 	}
       
   401 
       
   402 void CTestNonNativePlugin::Activate(TComponentId /*aComponentId*/, const TSecurityContext& aSecurityContext, TRequestStatus& aStatus)
       
   403 	{
       
   404 	DEBUG_PRINTF(_L8("Test non-native plugin - activate"));
       
   405 	aStatus = KRequestPending;
       
   406 
       
   407 	TRAPD(err, CheckSecurityContextL(aSecurityContext));
       
   408 
       
   409 	TRequestStatus* statusPtr(&aStatus);
       
   410 	User::RequestComplete(statusPtr, err);
       
   411 	}
       
   412 
       
   413 void CTestNonNativePlugin::Deactivate(TComponentId /*aComponentId*/, const TSecurityContext& aSecurityContext, TRequestStatus& aStatus)
       
   414 	{
       
   415 	DEBUG_PRINTF(_L8("Test non-native plugin - deactivate"));
       
   416 	aStatus = KRequestPending;
       
   417 
       
   418 	TRAPD(err, CheckSecurityContextL(aSecurityContext));
       
   419 
       
   420 	TRequestStatus* statusPtr(&aStatus);
       
   421 	User::RequestComplete(statusPtr, err);
       
   422 	}