featuremgmt/featureregistry/test/helper/maketestconfig/maketestconfig.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2005-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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Puts some test feature registry configuration files - some corrupt, others legal -
       
    15 // in the C: private data cage of the setup executable. Sets up test RProperties.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalComponent
       
    22  @test
       
    23 */
       
    24 
       
    25 #include <e32cmn.h>
       
    26 #include <e32base.h>
       
    27 #include <f32file.h>
       
    28 #include <e32property.h>
       
    29 #include <featreg.h>
       
    30 #include "featregcmn.h"
       
    31 #include "maketestconfig.h"
       
    32 
       
    33 // names of test configurations that can be set up by this exe, specified as
       
    34 // command-line option:
       
    35 _LIT(KFeatRegTestConfig_force_setup,				"force_setup");
       
    36 _LIT(KFeatRegTestConfig_reset,						"reset");
       
    37 _LIT(KFeatRegTestConfig_corrupt_missing,			"corrupt_missing");
       
    38 _LIT(KFeatRegTestConfig_corrupt_noheader,			"corrupt_noheader");
       
    39 _LIT(KFeatRegTestConfig_corrupt_incompleteheader,	"corrupt_incompleteheader");
       
    40 _LIT(KFeatRegTestConfig_corrupt_invalidtypeprefix,	"corrupt_invalidtypeprefix");
       
    41 _LIT(KFeatRegTestConfig_corrupt_badversionnumber,	"corrupt_badversionnumber");
       
    42 _LIT(KFeatRegTestConfig_corrupt_toomuchdata,		"corrupt_toomuchdata");
       
    43 _LIT(KFeatRegTestConfig_corrupt_toolittledata,		"corrupt_toolittledata");
       
    44 _LIT(KFeatRegTestConfig_corrupt_entryoutoforder,	"corrupt_entryoutoforder");
       
    45 _LIT(KFeatRegTestConfig_corrupt_entryrepeated,		"corrupt_entryrepeated");
       
    46 _LIT(KFeatRegTestConfig_corrupt_badrange,			"corrupt_badrange");
       
    47 _LIT(KFeatRegTestConfig_valid_nofeatures,			"valid_nofeatures");
       
    48 _LIT(KFeatRegTestConfig_valid_small,				"valid_small");
       
    49 _LIT(KFeatRegTestConfig_valid_large,				"valid_large");
       
    50 _LIT(KFeatRegTestConfig_valid_perf,				    "valid_perf_");
       
    51 
       
    52 
       
    53 TConfigFileName ConfigFileName;
       
    54 
       
    55 
       
    56 static TInt DefineTestFlagProperty()
       
    57 	{
       
    58 	RProcess thisProcess;
       
    59 	// sanity check that feature property category in common header equals SID of this process
       
    60 	ASSERT(KFeaturePropCat == thisProcess.SecureId());
       
    61 	TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass);
       
    62 	TSecurityPolicy writePolicy(thisProcess.SecureId());
       
    63 	TInt result = RProperty::Define(KFeatRegConfigTestKey, RProperty::EInt, readPolicy, writePolicy);
       
    64 	if (result == KErrAlreadyExists)
       
    65 		{
       
    66 		result = KErrNone;
       
    67 		}
       
    68 	return result;
       
    69 	}
       
    70 
       
    71 static TInt WriteTestConfigFile(TUint32* aData, TInt aSize)
       
    72 	{
       
    73 	RFs fs;
       
    74 	TInt result = fs.Connect();
       
    75 	if (result == KErrNone)
       
    76 		{
       
    77 		result = fs.MkDirAll(ConfigFileName);
       
    78 		if ((result == KErrNone) || (result == KErrAlreadyExists))
       
    79 			{
       
    80 			RFile cfgFile;
       
    81 			result = cfgFile.Replace(fs, ConfigFileName, EFileWrite|EFileStream);
       
    82 			if (result == KErrNone)
       
    83 				{
       
    84 				result = cfgFile.Write(TPtrC8(reinterpret_cast<const TUint8 *>(aData), aSize));
       
    85 				cfgFile.Close();
       
    86 				}
       
    87 			}
       
    88 		fs.Close();
       
    89 		}
       
    90 	if (result == KErrNone)
       
    91 		{
       
    92 		result = DefineTestFlagProperty();
       
    93 		}
       
    94 	return result;
       
    95 	}
       
    96 
       
    97 TInt E32Main()
       
    98 	{
       
    99 	__UHEAP_MARK;
       
   100 
       
   101 	TFileName configName;
       
   102 	User::CommandLine(configName);
       
   103 
       
   104 	// construct config filename
       
   105 	GetSystemDrivePath(ConfigFileName);
       
   106 
       
   107 	// always delete feature property so featreg.dll re-loads it (note this
       
   108 	// executable has same UID3 as featreg setup:
       
   109 	TInt result = RProperty::Delete(KFeaturePropKey);
       
   110 	if (result == KErrNotFound)
       
   111 		{
       
   112 		result = KErrNone;
       
   113 		}
       
   114 	if (result != KErrNone)
       
   115 		{
       
   116 		goto cleanupReturn;
       
   117 		}
       
   118 
       
   119 	//--------------------
       
   120 	if (configName.Find(KFeatRegTestConfig_force_setup) >= 0)
       
   121 		{
       
   122 		// force setup
       
   123 		// nothing to do; just ensures feature property is deleted so setup is run
       
   124 		}
       
   125 	//--------------------
       
   126 	else if (configName.Find(KFeatRegTestConfig_reset) >= 0)
       
   127 		{
       
   128 		// reset
       
   129 		// delete "test-flag" property so setup looks at true config info
       
   130 		result = RProperty::Delete(KFeatRegConfigTestKey);
       
   131 		if (result == KErrNotFound)
       
   132 			{
       
   133 			result = KErrNone;
       
   134 			}
       
   135 		}
       
   136 	//--------------------
       
   137 	else if (configName.Find(KFeatRegTestConfig_corrupt_missing) >= 0)
       
   138 		{
       
   139 		// no header
       
   140 		RFs fs;
       
   141 		result = fs.Connect();
       
   142 		if (result == KErrNone)
       
   143 			{
       
   144 			result = fs.Delete(ConfigFileName);
       
   145 			if ((result == KErrNotFound) || (result == KErrPathNotFound))
       
   146 				{
       
   147 				result = KErrNone;
       
   148 				}
       
   149 			fs.Close();
       
   150 			}
       
   151 		if (result == KErrNone)
       
   152 			{
       
   153 			result = DefineTestFlagProperty();
       
   154 			}
       
   155 		}
       
   156 	//--------------------
       
   157 	else if (configName.Find(KFeatRegTestConfig_corrupt_noheader) >= 0)
       
   158 		{
       
   159 		// no header
       
   160 		result = WriteTestConfigFile(NULL, 0);
       
   161 		}
       
   162 	//--------------------
       
   163 	else if (configName.Find(KFeatRegTestConfig_corrupt_incompleteheader) >= 0)
       
   164 		{
       
   165 		// header is not complete
       
   166 		TUint32 fileData1[] =
       
   167 			{
       
   168 			validTypePrefix,
       
   169 			0,	// version number, must be zero
       
   170 			0	// entry count
       
   171 				// range count: missing
       
   172 			};
       
   173 		result = WriteTestConfigFile(fileData1, sizeof(fileData1));
       
   174 		}
       
   175 	//--------------------
       
   176 	else if (configName.Find(KFeatRegTestConfig_corrupt_invalidtypeprefix) >= 0)
       
   177 		{
       
   178 		// invalid type prefix
       
   179 		TUint32 fileData1[] =
       
   180 			{
       
   181 			invalidTypePrefix,	 // should be validTypePrefix
       
   182 			0,	// version number
       
   183 			0,	// entry count
       
   184 			0	// range count
       
   185 			};
       
   186 		result = WriteTestConfigFile(fileData1, sizeof(fileData1));
       
   187 		}
       
   188 
       
   189 	//--------------------
       
   190 	else if (configName.Find(KFeatRegTestConfig_corrupt_badversionnumber) >= 0)
       
   191 		{
       
   192 		// bad file version header
       
   193 		TUint32 fileData1[] =
       
   194 			{
       
   195 			validTypePrefix,
       
   196 			1,	// version number: not zero
       
   197 			0,	// entry count
       
   198 			0	// range count
       
   199 			};
       
   200 		result = WriteTestConfigFile(fileData1, sizeof(fileData1));
       
   201 		}
       
   202 	//--------------------
       
   203 	else if (configName.Find(KFeatRegTestConfig_corrupt_toomuchdata) >= 0)
       
   204 		{
       
   205 		// corrupt: config file contains more data than required by header
       
   206 		TUint32 fileData2[] =
       
   207 			{
       
   208 			validTypePrefix,
       
   209 			0,	// version number, must be zero
       
   210 			0,	// entry count
       
   211 			0,	// range count
       
   212 			0,	// unexpected extra data
       
   213 			};
       
   214 		result = WriteTestConfigFile(fileData2, sizeof(fileData2));
       
   215 		}
       
   216 	//--------------------
       
   217 	else if (configName.Find(KFeatRegTestConfig_corrupt_toolittledata) >= 0)
       
   218 		{
       
   219 		// corrupt: config file contains less data than required by header
       
   220 		TUint32 fileData3[] =
       
   221 			{
       
   222 			validTypePrefix,
       
   223 			0,	// version number, must be zero
       
   224 			2,	// entry count
       
   225 			1	// range count
       
   226 				// should be 2 entries (2 words each) and 1 default range (2 words each)
       
   227 			};
       
   228 		result = WriteTestConfigFile(fileData3, sizeof(fileData3));
       
   229 		}
       
   230 	//--------------------
       
   231 	else if (configName.Find(KFeatRegTestConfig_corrupt_entryoutoforder) >= 0)
       
   232 		{
       
   233 		// corrupt: feature entries not ordered from lowest to highest UID
       
   234 		TUint32 fileData4[] =
       
   235 			{
       
   236 			validTypePrefix,
       
   237 			0,	// version number, must be zero
       
   238 			3,	// entry count
       
   239 			0,	// range count
       
   240 			2,	// Feature UID = 2
       
   241 			1,	//   status low-bit set -> present
       
   242 			5,	// Feature UID = 5
       
   243 			1,	//   status low-bit set -> present
       
   244 			3,	// Feature UID = 3: Feature UIDs not in strictly increasing order
       
   245 			1	//   status low-bit set -> present
       
   246 			};
       
   247 		result = WriteTestConfigFile(fileData4, sizeof(fileData4));
       
   248 		}
       
   249 	//--------------------
       
   250 	else if (configName.Find(KFeatRegTestConfig_corrupt_entryrepeated) >= 0)
       
   251 		{
       
   252 		// corrupt: repeated feature UID
       
   253 		TUint32 fileData5[] =
       
   254 			{
       
   255 			validTypePrefix,
       
   256 			0,	// version number, must be zero
       
   257 			3,	// entry count
       
   258 			0,	// range count
       
   259 			2,	// Feature UID = 2
       
   260 			1,	//   status low-bit set -> present
       
   261 			5,	// Feature UID = 5
       
   262 			1,	//   status low-bit set -> present
       
   263 			5,	// Feature UID = 5: Feature UID repeated
       
   264 			1,	//   status low-bit set -> present
       
   265 			};
       
   266 		result = WriteTestConfigFile(fileData5, sizeof(fileData5));
       
   267 		}
       
   268 	//--------------------
       
   269 	else if (configName.Find(KFeatRegTestConfig_corrupt_badrange) >= 0)
       
   270 		{
       
   271 		// corrupt: default-supported range not listed in order lowUid-highUid
       
   272 		TUint32 fileData6[] =
       
   273 			{
       
   274 			validTypePrefix,
       
   275 			0,	// version number, must be zero
       
   276 			1,	// entry count
       
   277 			2,	// range count
       
   278 			2,	// Feature UID = 2
       
   279 			1,	//   status low-bit set -> present
       
   280 			5,	// Range 1 low UID
       
   281 			9,	//         high UID
       
   282 			88,	// Range 2 low UID
       
   283 			76	//         high UID: not >= lowUID
       
   284 			};
       
   285 		result = WriteTestConfigFile(fileData6, sizeof(fileData6));
       
   286 		}
       
   287 	//--------------------
       
   288 	else if (configName.Find(KFeatRegTestConfig_valid_nofeatures) >= 0)
       
   289 		{
       
   290 #ifndef SYMBIAN_FEATURE_MANAGER
       
   291 		// valid: no features
       
   292 				TUint32 fileData7[] =
       
   293 					{
       
   294 					validTypePrefix,
       
   295 					0,	// version number, must be zero
       
   296 					0,	// entry count
       
   297 					0	// range count
       
   298 					};
       
   299 #else	
       
   300 		// valid: no features
       
   301 				TUint32 fileData7[] =
       
   302 					{
       
   303 					validTypePrefix,
       
   304 					0,	// version number, must be zero
       
   305 					0,	// entry count
       
   306 					1,	// range count: we have to provide at least one range, otherwise the file will be corrupt from the FeatMgr perspective
       
   307 					KFeatRegTest_DummyRangeId,
       
   308 					KFeatRegTest_DummyRangeId
       
   309 					};		
       
   310 #endif	
       
   311 		result = WriteTestConfigFile(fileData7, sizeof(fileData7));
       
   312 		}
       
   313 	//--------------------
       
   314 	else if (configName.Find(KFeatRegTestConfig_valid_small) >= 0)
       
   315 		{
       
   316 		// valid: few features, one default=supported range
       
   317 		TUint32 fileData8[] =
       
   318 			{
       
   319 			validTypePrefix,
       
   320 			0,	// version number, must be zero
       
   321 			2,	// entry count
       
   322 			1,	// range count
       
   323 			KFeatRegTest1_02_SNFeatId,	// 1. feature UID
       
   324 			KFeatRegTest1_02_SNFeatSt,	//    status word; low bit set = supported
       
   325 			KFeatRegTest1_05_URFeatId,	// 2. feature UID
       
   326 			KFeatRegTest1_05_URFeatSt,	//    status word; low-bit not set = not supported
       
   327 			KFeatRegTest1_03_LoFeatId,	// low-feature-UID of default=supported range
       
   328 			KFeatRegTest1_04_HiFeatId	// high-feature-UID of default=supported range
       
   329 			};
       
   330 		result = WriteTestConfigFile(fileData8, sizeof(fileData8));
       
   331 		}
       
   332 	//--------------------
       
   333 	else if (configName.Find(KFeatRegTestConfig_valid_large) >= 0)
       
   334 		{
       
   335 		// valid: many features, several default=supported ranges
       
   336 		TUint32 fileData9[2 + 2*KFeatRegTest2_04_numTestFeatures + 2*KFeatRegTest2_07_rangeCount];
       
   337 		TInt j = sizeof(TFeatureHeader) / sizeof(TUint32);
       
   338 		TInt entryCount = 0;
       
   339 		TUint32 id = KFeatRegTest2_01_FirstId;
       
   340 		// feature entries
       
   341 		for (TInt e = KFeatRegTest2_04_numTestFeatures; e > 0; e--)
       
   342 			{
       
   343 			TBool featureSupported = !(id & KFeatRegTest2_05_NotSupportedBit);
       
   344 			TBool inDefaultSupportedRange = id & KFeatRegTest2_06_DefSupportedBit;
       
   345 			// only need to list feature if support differs from default ranges
       
   346 			if (featureSupported && (!inDefaultSupportedRange))
       
   347 				{
       
   348 				fileData9[j++] = id;
       
   349 				fileData9[j++] = RFeatureRegistry::EStatusSupportBit;
       
   350 				entryCount++;
       
   351 				}
       
   352 			else if ((!featureSupported) && inDefaultSupportedRange)
       
   353 				{
       
   354 				fileData9[j++] = id;
       
   355 				fileData9[j++] = 0;	// specifically listed as not supported
       
   356 				entryCount++;
       
   357 				}
       
   358 			id += KFeatRegTest2_03_IncrId;
       
   359 			}
       
   360 		// default=supported ranges
       
   361 		TUint32 lowId = KFeatRegTest2_06_DefSupportedBit;
       
   362 		for (TUint r = 0; r < KFeatRegTest2_07_rangeCount; r++)
       
   363 			{
       
   364 			fileData9[j++] = lowId;
       
   365 			fileData9[j++] = lowId + KFeatRegTest2_06_DefSupportedBit - 1;
       
   366 			lowId += 2*KFeatRegTest2_06_DefSupportedBit;
       
   367 			}
       
   368 		// header
       
   369 		fileData9[0] = validTypePrefix,
       
   370 		fileData9[1] = 0;	// version number, must be zero
       
   371 		fileData9[2] = entryCount;
       
   372 		fileData9[3] = KFeatRegTest2_07_rangeCount;
       
   373 		result = WriteTestConfigFile(fileData9, j*sizeof(TUint32));
       
   374 		}
       
   375 	else if (configName.Find(KFeatRegTestConfig_valid_perf) >= 0)
       
   376 		{
       
   377 		// extract the number of features to put into the file from the
       
   378 		// arguments supplied to the process from the script
       
   379 	    TInt digitchar = 0;
       
   380 	    TChar charItem(configName[digitchar]);
       
   381 	    while (charItem.IsDigit() == EFalse)
       
   382            charItem = configName[++digitchar]; 
       
   383 	    if (digitchar >= configName.Length())
       
   384 	    	{
       
   385 	    	result = KErrArgument;
       
   386 	    	goto cleanupReturn;
       
   387 	    	}
       
   388 	    
       
   389 		TPtrC16 counter = configName.Mid(digitchar);
       
   390 		TLex lex1(counter);
       
   391 		TInt count = 0;
       
   392         lex1.Val(count);
       
   393 		// valid: allocate space for maximum number of features we would expect
       
   394 		TInt j = sizeof(TFeatureHeader) / sizeof(TUint32);
       
   395 		TUint32 fileData9[4 + 2*8001]; // j must == 4
       
   396 		TInt entryCount = 0;
       
   397 		TUint32 id = KFeatRegTest2_01_FirstId;
       
   398 		// create 'count' number of feature entries
       
   399 		for (TInt e = count; e > 0; e--)
       
   400 			{
       
   401 			fileData9[j++] = id;
       
   402 			fileData9[j++] = (e&0x01) ? RFeatureRegistry::EStatusSupportBit : 0;
       
   403 			entryCount++;
       
   404 			id += KFeatRegTest2_03_IncrId;
       
   405 			}
       
   406 #ifndef SYMBIAN_FEATURE_MANAGER
       
   407 		// header
       
   408 		fileData9[0] = validTypePrefix,
       
   409 		fileData9[1] = 0;	// version number, must be zero
       
   410 		fileData9[2] = entryCount;
       
   411 		fileData9[3] = 0;
       
   412 #else
       
   413 		//we have to provide at least one range, otherwise the file will be corrupt from the FeatMgr perspective 
       
   414 		fileData9[j++] = KFeatRegTest_DummyRangeId;
       
   415 		fileData9[j++] = KFeatRegTest_DummyRangeId;
       
   416 		// header
       
   417 		fileData9[0] = validTypePrefix,
       
   418 		fileData9[1] = 0;	// version number, must be zero
       
   419 		fileData9[2] = entryCount;
       
   420 		fileData9[3] = 1;		
       
   421 #endif		
       
   422 		result = WriteTestConfigFile(fileData9, j*sizeof(TUint32));		
       
   423 		}
       
   424 	else
       
   425 		{
       
   426 		result = KErrArgument;
       
   427 		}
       
   428 
       
   429 cleanupReturn:
       
   430 	__UHEAP_MARKEND;
       
   431 
       
   432 	return result;
       
   433 	}
       
   434