os/security/contentmgmt/referencedrmagent/tcaf/source/bitsetstep.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003-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 <caf/caf.h>
    20 #include <caf/bitset.h>
    21 #include <caf/attribute.h>
    22 #include <s32mem.h>
    23 #include "bitsetstep.h"
    24 
    25 const TInt KAttrTop = 10;
    26 
    27 using namespace ContentAccess;
    28 
    29 CBitsetBaseStep::CBitsetBaseStep(const TDesC& aStepName)
    30 	{
    31 	SetTestStepName(aStepName);
    32 	}
    33 
    34 TVerdict CBitsetBaseStep::doTestStepPreambleL()
    35 	{
    36 	return TestStepResult();
    37 	}
    38 
    39 TVerdict CBitsetBaseStep::doTestStepPostambleL()
    40 	{
    41 	return TestStepResult();
    42 	}
    43 
    44 CBitset* CBitsetBaseStep::GetBitsetLC(const TDesC& aHeader)
    45 	{
    46 	_LIT(KBitCount, "bitcount");
    47 	_LIT(KBit, "bit%02d");
    48 
    49 	// Firstly, from the script, get the number of bits to set. If aHeader
    50 	// is "left-", then the bitcount key is "left-bitcount"
    51 	HBufC* buf = HBufC::NewLC(aHeader.Length() + KBitCount().Length());
    52 	TPtr ptr(buf->Des());
    53 	ptr = aHeader;
    54 	ptr.Append(KBitCount());
    55 	TInt bitcount = 0;
    56 	GetIntFromConfig(ConfigSection(), ptr, bitcount);
    57 
    58 	INFO_PRINTF3(_L("%S = %d"), &ptr, bitcount);
    59 
    60 	// Now, create the bitset
    61 	CBitset* bitset = CBitset::NewLC(bitcount);
    62 	
    63 	TInt i = 0;
    64 	for (; i < bitcount; ++i)
    65 		{
    66 		TInt bit = 0;
    67 		ptr = aHeader;
    68 		ptr.AppendFormat(KBit, i);
    69 		GetIntFromConfig(ConfigSection(), ptr, bit);
    70 		if (bit)
    71 			{
    72 			bitset->Set(i);
    73 			INFO_PRINTF2(_L("%S is set"), &ptr);
    74 			}
    75 		}
    76 
    77 	CleanupStack::Pop(bitset);
    78 	CleanupStack::PopAndDestroy(buf);
    79 	CleanupStack::PushL(bitset);
    80 	return bitset;
    81 	}
    82 
    83 /*
    84  * Step2 performs some basic internal Bitset sanity tests
    85  *
    86  */
    87 
    88 CBasicBitsetStep::CBasicBitsetStep() 
    89 	: CBitsetBaseStep(KBasicBitsetStep)
    90 	{
    91 	}
    92 
    93 //	EIsProtected EIsForwardable EIsModifyable EIsCopyable
    94 TVerdict CBasicBitsetStep::doTestStepL()
    95 	{
    96 	TInt i;
    97 	SetTestStepResult(EPass); // Default result to PASS
    98 
    99 	__UHEAP_MARK;
   100 	INFO_PRINTF1(_L("Basic Bitset Test"));
   101 
   102 	// Note we must size according to current EAttrTop (attribute.h)
   103 	INFO_PRINTF1(_L("Creating set1..."));
   104 	CBitset *set1 = CBitset::NewLC((TAttribute) KAttrTop); // on cleanup
   105 
   106 	// check that all the bits are initially not set
   107 	for(i = 0; i < KAttrTop; i++)
   108 		{
   109 		if (set1->IsSet(i))
   110 			{
   111 			INFO_PRINTF1(_L("Bitset::NewLC() test failed."));
   112 			SetTestStepResult(EFail);
   113 			}
   114 		}
   115 	
   116 	INFO_PRINTF1(_L("Performing single bit set/test..."));
   117 	set1->Set(EIsForwardable);
   118 
   119 	// check that only EIsForwardable is set
   120 	for(i = 0; i < KAttrTop; i++)
   121 		{
   122 		if (set1->IsSet(i) && i != EIsForwardable)
   123 			{
   124 			INFO_PRINTF1(_L("Single test/set(1) test failed."));
   125 			SetTestStepResult(EFail);
   126 			}
   127 		}
   128 	if (!set1->IsSet(EIsForwardable))
   129 		{
   130 		INFO_PRINTF1(_L("Single test/set(2) failed."));
   131 		SetTestStepResult(EFail);
   132 		}
   133 
   134 	set1->Unset(EIsForwardable);
   135 
   136 	// check that none of the bits are set
   137 	for(i = 0; i < KAttrTop; i++)
   138 		{
   139 		if (set1->IsSet(i))
   140 			{
   141 			INFO_PRINTF1(_L("Single test/set(3) failed."));
   142 			SetTestStepResult(EFail);
   143 			}
   144 		}
   145 
   146 	INFO_PRINTF1(_L("Performing setall tests..."));
   147 	set1->SetAll();
   148 
   149 	// check that all bits are set
   150 	for(i = 0; i < KAttrTop; i++)
   151 		{
   152 		if (!set1->IsSet(i))
   153 			{
   154 			INFO_PRINTF1(_L("SetAll test failed."));
   155 			SetTestStepResult(EFail);
   156 			}
   157 		}
   158 	
   159 	set1->Reset();
   160 
   161 	// check all bits are reset
   162 	for(i = 0; i < KAttrTop; i++)
   163 		{
   164 		if (set1->IsSet(i))
   165 			{
   166 			INFO_PRINTF1(_L("Reset test failed."));
   167 			SetTestStepResult(EFail);
   168 			}
   169 		}
   170 
   171 	CleanupStack::PopAndDestroy(set1);	
   172 	
   173 	__UHEAP_MARKEND;
   174 
   175 	return TestStepResult();
   176 	}
   177 
   178 // --------------------------------------------------------------------------
   179 // This step tests the bitset SetList and IsSetList functions
   180 
   181 CBitsetListStep::CBitsetListStep()
   182 	: CBitsetBaseStep(KBitsetListStep)
   183 	{
   184 	}
   185 
   186 TVerdict CBitsetListStep::doTestStepL()
   187 	{
   188 	TInt i;
   189 	SetTestStepResult(EPass); 
   190 
   191 	__UHEAP_MARK;
   192 
   193 	INFO_PRINTF1(_L("Creating set1..."));
   194 
   195 	CBitset *set1 = CBitset::NewLC(KAttrTop); 
   196 
   197 	INFO_PRINTF1(_L("Performing SetList call"));
   198 	set1->SetListL(2, EIsCopyable, EIsModifyable);
   199 
   200 	for(i = 0; i < KAttrTop; i++)
   201 		{
   202 		if (set1->IsSet(i) && i != EIsCopyable && i != EIsModifyable)
   203 			{
   204 			INFO_PRINTF1(_L("SetList(1) failed."));
   205 			SetTestStepResult(EFail);
   206 			}
   207 		}
   208 
   209 	if (!set1->IsSet(EIsModifyable) || !set1->IsSet(EIsCopyable))
   210 		{
   211 		INFO_PRINTF1(_L("SetList(2) failed."));
   212 		SetTestStepResult(EFail);
   213 		}
   214 
   215 	// Now check the IsSetList call
   216 	INFO_PRINTF1(_L("Performing IsSetList calls"));
   217 	if (!set1->IsSetList(2, EIsCopyable, EIsModifyable))
   218 		{
   219 		INFO_PRINTF1(_L("IsSetList call(3) failed."));
   220 		SetTestStepResult(EFail);
   221 		}
   222 
   223 	if (set1->IsSetList(2, EIsProtected, EIsForwardable))
   224 		{
   225 		INFO_PRINTF1(_L("IsSetList call(4) failed."));
   226 		SetTestStepResult(EFail);
   227 		}
   228 
   229 	CleanupStack::PopAndDestroy(set1);	
   230 	__UHEAP_MARKEND;
   231 
   232 	return TestStepResult();
   233 	}
   234 
   235 // --------------------------------------------------------------------------
   236 
   237 CBitsetEqualityStep::CBitsetEqualityStep()
   238 	: CBitsetBaseStep(KBitsetEqualityStep)
   239 	{
   240 	}
   241 
   242 TVerdict CBitsetEqualityStep::doTestStepL()
   243 	{
   244 	SetTestStepResult(EPass); // Default result to EPass
   245 
   246 	__UHEAP_MARK;
   247 	// Get the bitset from the script section
   248 	CBitset* left = GetBitsetLC(_L("left-"));
   249 	CBitset* right = GetBitsetLC(_L("right-"));
   250 
   251 	// Now see whether we expect the result to be equal
   252 	TBool equalExpected = EFalse;
   253 	GetIntFromConfig(ConfigSection(), _L("equal"), equalExpected);
   254 
   255 	if (equalExpected)
   256 		{
   257 		INFO_PRINTF1(_L("Equality expected"));
   258 		}
   259 	else
   260 		{
   261 		INFO_PRINTF1(_L("Inequality expected"));
   262 		}
   263 
   264 	TBool result = (*left == *right);
   265 	if (!result != !equalExpected)
   266 		{
   267 		INFO_PRINTF1(_L("Equality test failed."));
   268 		SetTestStepResult(EFail);
   269 		}
   270 	CleanupStack::PopAndDestroy(2, left);
   271 
   272 	__UHEAP_MARKEND;
   273 
   274 	return TestStepResult();
   275 	}
   276 
   277 // --------------------------------------------------------------------------
   278 
   279 CBitsetCopyStep::CBitsetCopyStep()
   280 	: CBitsetBaseStep(KBitsetCopyStep)
   281 	{
   282 	}
   283 
   284 TVerdict CBitsetCopyStep::doTestStepL()
   285 	{
   286 	SetTestStepResult(EPass); // Default result to EPass
   287 
   288 	__UHEAP_MARK;
   289 	// Get the bitset from the script section
   290 	CBitset* set = GetBitsetLC(KNullDesC);
   291 
   292 	// Now, create a copy
   293 	CBitset* copy = CBitset::NewLC(*set);
   294 
   295 	// Now check the copy
   296 	if (*set != *copy)
   297 		{
   298 		INFO_PRINTF1(_L("Copy constructor return unequal result."));
   299 		SetTestStepResult(EFail);
   300 		}
   301 
   302 	// Now create another bitset of arbitrary length
   303 	CBitset* another = CBitset::NewLC(5);
   304 
   305 	// Perform assignment
   306 	*another = *copy;
   307 
   308 	// Now check another equals the original set
   309 	if (*set != *another)
   310 		{
   311 		INFO_PRINTF1(_L("operator= returned unequal result."));
   312 		SetTestStepResult(EFail);
   313 		}
   314 
   315 	// Now invert another and copy and make sure they are equal
   316 	another->Invert();
   317 	copy->Invert();
   318 
   319 	if (*copy != *another)
   320 		{
   321 		INFO_PRINTF1(_L("Invert returned unequal result."));
   322 		SetTestStepResult(EFail);
   323 		}
   324 
   325 	// Invert the copy again and ensure it is equal to the original
   326 	copy->Invert();
   327 	if (*set != *copy)
   328 		{
   329 		INFO_PRINTF1(_L("Double invert fails."));
   330 		SetTestStepResult(EFail);
   331 		}
   332 
   333 	CleanupStack::PopAndDestroy(3, set);
   334 
   335 	__UHEAP_MARKEND;
   336 
   337 	return TestStepResult();
   338 	}
   339 
   340 // --------------------------------------------------------------------------
   341 
   342 CBitsetSerialiseStep::CBitsetSerialiseStep()
   343 	: CBitsetBaseStep(KBitsetSerialiseStep)
   344 	{
   345 	}
   346 
   347 TVerdict CBitsetSerialiseStep::doTestStepL()
   348 	{
   349 	SetTestStepResult(EPass); // Default result to EPass
   350 
   351 	__UHEAP_MARK;
   352 	// Get the bitset from the script section
   353 	CBitset* set = GetBitsetLC(KNullDesC);
   354 
   355 	// Create a buffer stream
   356 	CBufFlat* buf = CBufFlat::NewL(50);
   357 	CleanupStack::PushL(buf);
   358 	RBufWriteStream stream(*buf);
   359 	CleanupClosePushL(stream);
   360 
   361 	// call the stream function
   362 	stream << *set;
   363 	CleanupStack::PopAndDestroy(&stream);
   364 
   365 	// Now, create an HBufC8 from the stream buf's length, and copy 
   366 	// the stream buffer into this descriptor
   367 	HBufC8* des = HBufC8::NewL(buf->Size());
   368 	TPtr8 ptr(des->Des());
   369 	buf->Read(0, ptr, buf->Size());
   370 
   371 	// destroy the buffer
   372 	CleanupStack::PopAndDestroy(buf);
   373 	CleanupStack::PushL(des);
   374 
   375 	// Now, stream a new bitset from the descriptor
   376 	CBitset* newset = CBitset::NewLC(5);
   377 	RDesReadStream readstream(*des);
   378 	CleanupClosePushL(readstream);
   379 	readstream >> *newset;
   380 	CleanupStack::PopAndDestroy(&readstream);
   381 
   382 	// Now check that the new bitset equals the old one
   383 	if (*set != *newset)
   384 		{
   385 		INFO_PRINTF1(_L("serialisation returned unequal result."));
   386 		SetTestStepResult(EFail);
   387 		}
   388 
   389 	CleanupStack::PopAndDestroy(3, set);
   390 
   391 	__UHEAP_MARKEND;
   392 
   393 	return TestStepResult();
   394 	}
   395 
   396 // --------------------------------------------------------------------------
   397 
   398 CBitsetPanicStep::CBitsetPanicStep()
   399 	: CBitsetBaseStep(KBitsetPanicStep)
   400 	{
   401 	}
   402 
   403 TVerdict CBitsetPanicStep::doTestStepL()
   404 	{
   405 	SetTestStepResult(EPass); // Default result to EPass
   406 
   407 	__UHEAP_MARK;
   408 	// Get the bitset from the script section
   409 	CBitset* panic = GetBitsetLC(_L("panic-"));
   410 
   411 	// Now see whether we expect the result to be equal
   412 	TInt panictest = -1;
   413 	GetIntFromConfig(ConfigSection(), _L("panictest"), panictest);
   414 
   415 	// all of the following cases should panic
   416 	switch(panictest)
   417 		{
   418 	case 1:
   419 		INFO_PRINTF1(_L("IsSet(-1)"));
   420 		panic->IsSet(-1);
   421 		break;
   422 	case 2:
   423 		INFO_PRINTF1(_L("IsSet(MaxBits()+1)"));
   424 		panic->IsSet(panic->MaxBits()+1);
   425 		break;
   426 	case 3:
   427 		INFO_PRINTF1(_L("Set(-1)"));
   428 		panic->Set(-1);
   429 		break;
   430 	case 4:
   431 		INFO_PRINTF1(_L("Set(MaxBits()+1)"));
   432 		panic->Set(panic->MaxBits()+1);
   433 		break;
   434 	case 5:
   435 		INFO_PRINTF1(_L("UnSet(-1)"));
   436 		panic->Unset(-1);
   437 		break;
   438 	case 6:
   439 		INFO_PRINTF1(_L("UnSet(MaxBits()+1)"));
   440 		panic->Unset(panic->MaxBits()+1);
   441 		break;
   442 
   443 	default:
   444 		SetTestStepResult(EFail); 
   445 		};
   446 
   447 	SetTestStepResult(EFail); 
   448 	CleanupStack::PopAndDestroy(panic);
   449 
   450 	__UHEAP_MARKEND;
   451 
   452 	return TestStepResult();
   453 	}
   454