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