authenticationservices/authenticationserver/test/tauthcliserv/step_authexpr_build.cpp
changeset 102 deec7e509f66
parent 94 0e6c5a9328b5
child 108 ca9a0fc2f082
equal deleted inserted replaced
94:0e6c5a9328b5 102:deec7e509f66
     1 /*
       
     2 * Copyright (c) 2005-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 <s32mem.h>
       
    20 #include "tauthcliservstep.h"
       
    21 #include <scs/scsclient.h>
       
    22 using namespace AuthServer;
       
    23 
       
    24 inline void CorruptTypeField(const CAuthExpressionImpl* aExpr)
       
    25 /**
       
    26 	Corrupt the type field of the supplied
       
    27 	expression so the invariant-checking code fails.
       
    28 	
       
    29 	@param	aExpr			Expression to corrupt.  This
       
    30 							is const, even though it is
       
    31 							modified, because Left and
       
    32 							Right return pointers to const
       
    33 							expressions.
       
    34  */
       
    35 	{
       
    36 	// 4 == _FOFF(CAuthExpression, iType), but iType is private
       
    37 	// sizeof(CAuthExpression::iType) == 8
       
    38 	*(((TUint32*)aExpr) + 2) = ~0;
       
    39 	}
       
    40 
       
    41 
       
    42 CTStepAuthExprBuild::CTStepAuthExprBuild()
       
    43 /**
       
    44 	Record this test step's name.
       
    45  */
       
    46 	{
       
    47 	SetTestStepName(KTStepAuthExprBuild);
       
    48 	}
       
    49 
       
    50 
       
    51 TVerdict CTStepAuthExprBuild::doTestStepL()
       
    52 /**
       
    53 	Implement CTestStep by creating simple and
       
    54 	complex expression types.  In debug mode this
       
    55 	function also ensures the operations fail
       
    56 	cleanly in OOM.
       
    57  */
       
    58 	{
       
    59 	RunTestsL();		// create all expression types
       
    60 	TestFailedCombsL();
       
    61 	TestCorruptPersistL();
       
    62 	
       
    63 	// create all expression types in OOM
       
    64 #ifdef _DEBUG
       
    65 	TInt i = 1;
       
    66 	TBool failed = EFalse;
       
    67 	
       
    68 	do
       
    69 		{
       
    70 		__UHEAP_SETFAIL(RAllocator::EDeterministic, i);
       
    71 		TRAPD(r, RunTestsL());
       
    72 		TESTL(r == KErrNone || r == KErrNoMemory);
       
    73 		__UHEAP_RESET;
       
    74 		
       
    75 		failed = (r == KErrNoMemory);
       
    76 		++i;
       
    77 		} while (failed);
       
    78 	
       
    79 	// i is now one greater than the last alloc count.
       
    80 	// run the tests again, to ensure there really was
       
    81 	// enough memory, and this wasn't just an error case
       
    82 	// that wasn't handled properly.
       
    83 	
       
    84 	TInt lim = i * 2;
       
    85 	while (i++ < lim)
       
    86 		{
       
    87 		__UHEAP_SETFAIL(RAllocator::EDeterministic, i);
       
    88 		RunTestsL();
       
    89 		__UHEAP_RESET;
       
    90 		}
       
    91 #endif	
       
    92 	
       
    93 	return EPass;
       
    94 	}
       
    95 
       
    96 
       
    97 void CTStepAuthExprBuild::RunTestsL()
       
    98 /**
       
    99 	Helper function for doTestStepL.  This generates
       
   100 	authentication expressions.
       
   101 	
       
   102 	1. creating simple type and plugin id nodes.
       
   103 	2. creating simple AND and OR combinations
       
   104 	3. creating complex (multi-level) AND and OR combinations.
       
   105  */
       
   106 	{
       
   107 	TestSimpleExprL();
       
   108 	TestSimpleExprCombsConsL();
       
   109 	TestComplexExprCombsConsL();
       
   110 	}
       
   111 
       
   112 
       
   113 void CTStepAuthExprBuild::TestExprLD(
       
   114 	void (CTStepAuthExprBuild::*aTestFuncL)(CAuthExpression*),
       
   115 	CAuthExpression* aExpr)
       
   116 /**
       
   117 	Run the supplied test function on the supplied expression.
       
   118 	This function only exists to place the expression on the
       
   119 	cleanup stand and delete it afterwards.
       
   120 	
       
   121 	This function leaves with KErrNoMemory if the supplied
       
   122 	expression is NULL.
       
   123  */
       
   124 	{
       
   125 	User::LeaveIfNull(aExpr);
       
   126 	
       
   127 	CleanupStack::PushL(aExpr);
       
   128 	(this->*aTestFuncL)(aExpr);
       
   129 	
       
   130 	// test externalization and internalization
       
   131 	// (this can fail because of OOM.)
       
   132 	TSizeStream ss;
       
   133 	RWriteStream ws(&ss);
       
   134 	aExpr->ExternalizeL(ws);
       
   135 	
       
   136 	HBufC8* buf = HBufC8::NewLC(ss.Size());
       
   137 	TPtr8 bufPtr(buf->Des());	
       
   138 	RDesWriteStream dws(bufPtr);
       
   139 	aExpr->ExternalizeL(dws);
       
   140 	dws.CommitL();
       
   141 	
       
   142 	RDesReadStream drs(*buf);
       
   143 	CAuthExpression* aeRestore = CAuthExpressionImpl::NewL(drs);
       
   144 	CleanupStack::PushL(aeRestore);
       
   145 	
       
   146 	(this->*aTestFuncL)(aExpr);
       
   147 	
       
   148 	CleanupStack::PopAndDestroy(3, aExpr);
       
   149 	}
       
   150 
       
   151 
       
   152 void CTStepAuthExprBuild::TestSimpleExprL()
       
   153 /**
       
   154 	Test constructing leaf node expressions from
       
   155 	a plugin ID and a plugin type.
       
   156  */
       
   157 	{
       
   158 	__UHEAP_MARK;
       
   159 	
       
   160 	TestExprLD(&CTStepAuthExprBuild::TestPluginIdL, AuthExpr(KTestPluginId0));
       
   161 	TestExprLD(&CTStepAuthExprBuild::TestPluginTypeL, AuthExpr(EAuthKnowledge));
       
   162 
       
   163 	__UHEAP_MARKEND;
       
   164 	}
       
   165 
       
   166 
       
   167 void CTStepAuthExprBuild::TestPluginIdL(CAuthExpression* aExpr)
       
   168 /**
       
   169 	Tests the supplied expression is a simple expression
       
   170 	for plugin ID KTestPluginId0.
       
   171  */
       
   172 	{
       
   173 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   174 	TESTL(impl->Type() == KPluginId);
       
   175 	TESTL(impl->PluginId() == KTestPluginId0);
       
   176 	}
       
   177 
       
   178 
       
   179 void CTStepAuthExprBuild::TestPluginTypeL(CAuthExpression* aExpr)
       
   180 /**
       
   181 	Tests the supplied expression is a simple expression
       
   182 	for plugin type EAuthKnowledge.
       
   183  */
       
   184 	{
       
   185 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   186 	TESTL(impl->Type() == KPluginType);
       
   187 	TESTL(impl->PluginType() == EAuthKnowledge);
       
   188 	}
       
   189 
       
   190 
       
   191 void CTStepAuthExprBuild::TestSimpleExprCombsConsL()
       
   192 /**
       
   193 	Tests combining two simple types (plugin type or plugin id)
       
   194 	with the AuthAnd and AuthOr functions, and with the overloaded
       
   195 	binary AND (&) and OR (|) operators.
       
   196  */
       
   197 	{
       
   198 	__UHEAP_MARK;
       
   199 	
       
   200 	// type AND type
       
   201 	OpTestExprLD(&CTStepAuthExprBuild::TestTypeAndTypeL, AuthExpr(EAuthKnowledge) & AuthExpr(EAuthBiometric));
       
   202 	TestExprLD(&CTStepAuthExprBuild::TestTypeAndTypeL, AuthAnd(AuthExpr(EAuthKnowledge), AuthExpr(EAuthBiometric)));
       
   203 	
       
   204 	// id OR id
       
   205 	OpTestExprLD(&CTStepAuthExprBuild::TestTypeOrTypeL, AuthExpr(EAuthKnowledge) | AuthExpr(EAuthBiometric));
       
   206 	TestExprLD(&CTStepAuthExprBuild::TestTypeOrTypeL, AuthOr(AuthExpr(EAuthKnowledge), AuthExpr(EAuthBiometric)));
       
   207 	
       
   208 	// ID AND type
       
   209 	OpTestExprLD(&CTStepAuthExprBuild::TestIdAndTypeL, AuthExpr(KTestPluginId0) & AuthExpr(EAuthBiometric));
       
   210 	TestExprLD(&CTStepAuthExprBuild::TestIdAndTypeL, AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(EAuthBiometric)));
       
   211 	
       
   212 	// type AND ID
       
   213 	OpTestExprLD(&CTStepAuthExprBuild::TestTypeAndIdL, AuthExpr(EAuthBiometric) & AuthExpr(KTestPluginId0));
       
   214 	TestExprLD(&CTStepAuthExprBuild::TestTypeAndIdL, AuthAnd(AuthExpr(EAuthBiometric), AuthExpr(KTestPluginId0)));
       
   215 	
       
   216 	// id AND id
       
   217 	OpTestExprLD(&CTStepAuthExprBuild::TestIdAndIdL, AuthExpr(KTestPluginId0) & AuthExpr(KTestPluginId1));
       
   218 	TestExprLD(&CTStepAuthExprBuild::TestIdAndIdL, AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1)));
       
   219 	
       
   220 	__UHEAP_MARKEND;
       
   221 	}
       
   222 
       
   223 
       
   224 void CTStepAuthExprBuild::TestTypeAndTypeL(CAuthExpression* aExpr)
       
   225 /**
       
   226 	Test the supplied expression says know & bio.
       
   227  */
       
   228 	{
       
   229 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   230 	TESTL(impl->Type() == KAnd);
       
   231 	TESTL(impl->Left()->Type() == KPluginType);
       
   232 	TESTL(impl->Left()->PluginType() == EAuthKnowledge);
       
   233 	TESTL(impl->Right()->Type() == KPluginType);
       
   234 	TESTL(impl->Right()->PluginType() == EAuthBiometric);
       
   235 	}
       
   236 
       
   237 
       
   238 void CTStepAuthExprBuild::TestTypeOrTypeL(CAuthExpression* aExpr)
       
   239 /**
       
   240 	Test the supplied expression says know | bio.
       
   241  */
       
   242 	{
       
   243 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   244 	TESTL(impl->Type() == KOr);
       
   245 	TESTL(impl->Left()->Type() == KPluginType);
       
   246 	TESTL(impl->Left()->PluginType() == EAuthKnowledge);
       
   247 	TESTL(impl->Right()->Type() == KPluginType);
       
   248 	TESTL(impl->Right()->PluginType() == EAuthBiometric);
       
   249 	}
       
   250 
       
   251 
       
   252 void CTStepAuthExprBuild::TestIdAndTypeL(CAuthExpression* aExpr)
       
   253 /**
       
   254 	Test the supplied expression says id0 & bio.
       
   255  */
       
   256 	{
       
   257 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   258 	TESTL(impl->Type() == KAnd);
       
   259 	TESTL(impl->Left()->Type() == KPluginId);
       
   260 	TESTL(impl->Left()->PluginId() == KTestPluginId0);
       
   261 	TESTL(impl->Right()->Type() == KPluginType);
       
   262 	TESTL(impl->Right()->PluginType() == EAuthBiometric);
       
   263 	}
       
   264 
       
   265 
       
   266 void CTStepAuthExprBuild::TestTypeAndIdL(CAuthExpression* aExpr)
       
   267 /**
       
   268 	Test the supplied expression says bio & id0.
       
   269  */
       
   270 	{
       
   271 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   272 	TESTL(impl->Type() == KAnd);
       
   273 	TESTL(impl->Left()->Type() == KPluginType);
       
   274 	TESTL(impl->Left()->PluginType() == EAuthBiometric);
       
   275 	TESTL(impl->Right()->Type() == KPluginId);
       
   276 	TESTL(impl->Right()->PluginId() == KTestPluginId0);
       
   277 	}
       
   278 
       
   279 
       
   280 void CTStepAuthExprBuild::TestIdAndIdL(CAuthExpression* aExpr)
       
   281 /**
       
   282 	Test the supplied expression says id0 & id1.
       
   283  */
       
   284 	{
       
   285 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   286 	TESTL(impl->Type() == KAnd);
       
   287 	TESTL(impl->Left()->Type() == KPluginId);
       
   288 	TESTL(impl->Left()->PluginId() == KTestPluginId0);
       
   289 	TESTL(impl->Right()->Type() == KPluginId);
       
   290 	TESTL(impl->Right()->PluginId() == KTestPluginId1);
       
   291 	}
       
   292 
       
   293 
       
   294 void CTStepAuthExprBuild::TestComplexExprCombsConsL()
       
   295 /**
       
   296 	Test complex AND and OR combinations.
       
   297 	For the overloaded operators, this additionally checks
       
   298 	that precedence works as expected.
       
   299  */
       
   300 	{
       
   301 	__UHEAP_MARK;
       
   302 	
       
   303 	// complex combinations - ensure precedence works
       
   304 	
       
   305 	// id AND id AND id == (id AND id) AND id
       
   306 	OpTestExprLD(&CTStepAuthExprBuild::TestAndAndL, AuthExpr(KTestPluginId0) & AuthExpr(KTestPluginId1) & AuthExpr(KTestPluginId2));
       
   307 	TestExprLD(&CTStepAuthExprBuild::TestAndAndL, AuthAnd(AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1)), AuthExpr(KTestPluginId2)));
       
   308 	
       
   309 	// id AND id AND id == (id AND id) OR id
       
   310 	OpTestExprLD(&CTStepAuthExprBuild::TestAndOrL, AuthExpr(KTestPluginId0) & AuthExpr(KTestPluginId1) | AuthExpr(KTestPluginId2));
       
   311 	TestExprLD(&CTStepAuthExprBuild::TestAndOrL, AuthOr(AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1)), AuthExpr(KTestPluginId2)));
       
   312 	
       
   313 	// id OR id AND id == id OR (id AND id)
       
   314 	OpTestExprLD(&CTStepAuthExprBuild::TestOrAndL, AuthExpr(KTestPluginId0) | AuthExpr(KTestPluginId1) & AuthExpr(KTestPluginId2));
       
   315 	TestExprLD(&CTStepAuthExprBuild::TestOrAndL, AuthOr(AuthExpr(KTestPluginId0), AuthAnd(AuthExpr(KTestPluginId1), AuthExpr(KTestPluginId2))));
       
   316 	
       
   317 	// id OR id OR id == (id OR id) OR id
       
   318 	OpTestExprLD(&CTStepAuthExprBuild::TestOrOrL, AuthExpr(KTestPluginId0) | AuthExpr(KTestPluginId1) | AuthExpr(KTestPluginId2));
       
   319 	OpTestExprLD(&CTStepAuthExprBuild::TestOrOrL, AuthOr(AuthOr(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1)), AuthExpr(KTestPluginId2)));
       
   320 	
       
   321 	__UHEAP_MARKEND;
       
   322 	}
       
   323 
       
   324 
       
   325 void CTStepAuthExprBuild::TestAndAndL(CAuthExpression* aExpr)
       
   326 /**
       
   327 	Test the supplied expression says (id0 & id1) & id2.
       
   328  */
       
   329 	{
       
   330 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   331 	TESTL(impl->Type() == KAnd);
       
   332 	TESTL(impl->Left()->Type() == KAnd);
       
   333 	TESTL(impl->Left()->Left()->Type() == KPluginId);
       
   334 	TESTL(impl->Left()->Left()->PluginId() == KTestPluginId0);
       
   335 	TESTL(impl->Left()->Right()->Type() == KPluginId);
       
   336 	TESTL(impl->Left()->Right()->PluginId() == KTestPluginId1);
       
   337 	TESTL(impl->Right()->Type() == KPluginId);
       
   338 	TESTL(impl->Right()->PluginId() == KTestPluginId2);
       
   339 	}
       
   340 
       
   341 
       
   342 void CTStepAuthExprBuild::TestAndOrL(CAuthExpression* aExpr)
       
   343 /**
       
   344 	Test the supplied expression says (id0 & id 1) | id2.
       
   345  */
       
   346 	{
       
   347 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   348 	TESTL(impl->Type() == KOr);
       
   349 	TESTL(impl->Left()->Type() == KAnd);
       
   350 	TESTL(impl->Left()->Left()->Type() == KPluginId);
       
   351 	TESTL(impl->Left()->Left()->PluginId() == KTestPluginId0);
       
   352 	TESTL(impl->Left()->Right()->Type() == KPluginId);
       
   353 	TESTL(impl->Left()->Right()->PluginId() == KTestPluginId1);
       
   354 	TESTL(impl->Right()->Type() == KPluginId);
       
   355 	TESTL(impl->Right()->PluginId() == KTestPluginId2);
       
   356 	}
       
   357 
       
   358 
       
   359 void CTStepAuthExprBuild::TestOrAndL(CAuthExpression* aExpr)
       
   360 /**
       
   361 	Test the supplied expression says id0 | (id1 & id2).
       
   362  */
       
   363 	{
       
   364 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   365 	TESTL(impl->Type() == KOr);
       
   366 	TESTL(impl->Left()->Type() == KPluginId);
       
   367 	TESTL(impl->Left()->PluginId() == KTestPluginId0);
       
   368 	TESTL(impl->Right()->Type() == KAnd);
       
   369 	TESTL(impl->Right()->Left()->Type() == KPluginId);
       
   370 	TESTL(impl->Right()->Left()->PluginId() == KTestPluginId1);
       
   371 	TESTL(impl->Right()->Right()->Type() == KPluginId);
       
   372 	TESTL(impl->Right()->Right()->PluginId() == KTestPluginId2);
       
   373 	}
       
   374 
       
   375 
       
   376 void CTStepAuthExprBuild::TestOrOrL(CAuthExpression* aExpr)
       
   377 /**
       
   378 	Test the supplied expression says (id0 | id1) | id2.
       
   379  */
       
   380 	{
       
   381 	CAuthExpressionImpl* impl = static_cast<CAuthExpressionImpl*>(aExpr);
       
   382 	TESTL(impl->Type() == KOr);
       
   383 	TESTL(impl->Left()->Type() == KOr);
       
   384 	TESTL(impl->Left()->Left()->Type() == KPluginId);
       
   385 	TESTL(impl->Left()->Left()->PluginId() == KTestPluginId0);
       
   386 	TESTL(impl->Left()->Right()->Type() == KPluginId);
       
   387 	TESTL(impl->Left()->Right()->PluginId() == KTestPluginId1);
       
   388 	TESTL(impl->Right()->Type() == KPluginId);
       
   389 	TESTL(impl->Right()->PluginId() == KTestPluginId2);
       
   390 	}
       
   391 
       
   392 
       
   393 void CTStepAuthExprBuild::TestFailedCombsL()
       
   394 /**
       
   395 	Test that complex expression builder fails cleanly
       
   396 	for all permutations of unallocated input.
       
   397  */
       
   398 	{
       
   399 	__UHEAP_MARK;
       
   400 	
       
   401 	// left null only
       
   402 	CAuthExpression* aeRight = AuthExpr(KTestPluginId0);
       
   403 	User::LeaveIfNull(aeRight);
       
   404 	CAuthExpression* aeLeftNull = AuthAnd(0, aeRight);
       
   405 	TESTL(aeLeftNull == 0);
       
   406 	
       
   407 	// right null only
       
   408 	CAuthExpression* aeLeft = AuthExpr(KTestPluginId1);
       
   409 	User::LeaveIfNull(aeRight);
       
   410 	CAuthExpression* aeRightNull = AuthAnd(aeLeft, 0);
       
   411 	TESTL(aeRightNull == 0);
       
   412 	
       
   413 	// both null
       
   414 	CAuthExpression* aeBothNull = AuthAnd(0, 0);
       
   415 	TESTL(aeBothNull == 0);
       
   416 	
       
   417 	__UHEAP_MARKEND;
       
   418 	}
       
   419 
       
   420 
       
   421 void CTStepAuthExprBuild::TestCorruptPersistL()
       
   422 /**
       
   423 	Test get expected errors when externalize or
       
   424 	internalize invalid expressions.
       
   425  */
       
   426 	{
       
   427 	TInt r;
       
   428 	
       
   429 	__UHEAP_MARK;
       
   430 	CAuthExpression* aeTest = AuthExpr(KTestPluginId0);
       
   431 	TESTL(aeTest != 0);
       
   432 	CleanupStack::PushL(aeTest);
       
   433 	
       
   434 	TSizeStream ss;
       
   435 	RWriteStream ws(&ss);
       
   436 	aeTest->ExternalizeL(ws);
       
   437 	
       
   438 	TInt len = ss.Size();
       
   439 	TAny* buf= User::AllocLC(ss.Size());
       
   440 	
       
   441 	// externalize valid expression
       
   442 	RMemWriteStream mwsValid(buf, len);
       
   443 	aeTest->ExternalizeL(mwsValid);
       
   444 	
       
   445 	// internalize unsupported expression stream
       
   446 	TInt8& verByteP = ((TInt8*)buf)[0];
       
   447 	TInt8 origVer = verByteP;
       
   448 	verByteP = KMaxTInt8;
       
   449 	RMemReadStream mrsHighVer(buf, len);
       
   450 	TRAP(r, /* ignore */ CAuthExpressionImpl::NewL(mrsHighVer));
       
   451 	TESTL(r == KErrAuthServUnsupportedExprVersion);
       
   452 	verByteP = origVer;
       
   453 	
       
   454 	// internalize corrupt expression stream
       
   455 	TInt8& typeByteP = ((TInt8*)buf)[1];
       
   456 	TInt8 origType = typeByteP;
       
   457 	typeByteP = ~0;
       
   458 	RMemReadStream mrsCorrupt(buf, len);
       
   459 	TRAP(r, /* ignore */ CAuthExpressionImpl::NewL(mrsCorrupt));
       
   460 	TESTL(r == KErrAuthServInvalidInternalizeExpr);
       
   461 	typeByteP = origType;
       
   462 	
       
   463 	// internalize valid expression stream
       
   464 	RMemReadStream mrsValid(buf, len);
       
   465 	CAuthExpression* aeRestore = CAuthExpressionImpl::NewL(mrsValid);
       
   466 	TESTL(aeRestore != 0);
       
   467 	delete aeRestore;
       
   468 	
       
   469 	CleanupStack::PopAndDestroy(2, aeTest);
       
   470 	__UHEAP_MARKEND;
       
   471 	}
       
   472 
       
   473 
       
   474 // -------- Server fails expressions from client --------
       
   475 
       
   476 
       
   477 namespace {
       
   478 
       
   479 class RTestAuthClient : public RAuthClient
       
   480 /**
       
   481 	This subclass of RAuthClient is used to
       
   482 	send a corrupt externalized expression
       
   483 	to the authentication server.
       
   484 	
       
   485 	The test code can't call SendReceive() on
       
   486 	an instance of RAuthClient because it is
       
   487 	protected.
       
   488  */
       
   489 	{
       
   490 public:
       
   491 	void SendCorruptAuthExprL();
       
   492 	void SendHighVerAuthExprL();
       
   493 
       
   494 	static HBufC8* GetAuthExprExternalizationLC();
       
   495 	};
       
   496 
       
   497 HBufC8* RTestAuthClient::GetAuthExprExternalizationLC()
       
   498 /**
       
   499 	Helper function for SendCorruptAuthExprL and
       
   500 	SendHighVerAuthExprL.  This allocates a buffer
       
   501 	containing an externalized auth expr, so the
       
   502 	calling function can corrupt it.
       
   503 	
       
   504 	@return					Buffer containing externalized
       
   505 							authentication expression.  This
       
   506 							is also placed on the cleanup stack.
       
   507 	@see SendCorruptAuthExprL
       
   508 	@see SendHighVerAuthExprL
       
   509  */
       
   510 	{
       
   511 	CAuthExpression* ae = AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1));
       
   512 	User::LeaveIfNull(ae);
       
   513 	CleanupStack::PushL(ae);
       
   514 	
       
   515 	TSizeStream ss;
       
   516 	RWriteStream ws(&ss);
       
   517 	ae->ExternalizeL(ws);
       
   518 	
       
   519 	HBufC8* buf = HBufC8::NewLC(ss.Size());
       
   520 	TPtr8 bufPtr = buf->Des();
       
   521 	
       
   522 	RDesWriteStream dws(bufPtr);
       
   523 	ae->ExternalizeL(dws);
       
   524 	dws.CommitL();
       
   525 
       
   526 	CleanupStack::Pop(2, ae);
       
   527 	CleanupStack::PushL(buf);
       
   528 	
       
   529 	return buf;
       
   530 	}
       
   531 
       
   532 
       
   533 void RTestAuthClient::SendCorruptAuthExprL()
       
   534 /**
       
   535 	Send an authenticate request to the server,
       
   536 	with a corrupt authentication expression.
       
   537  */
       
   538 	{
       
   539 	HBufC8* buf = GetAuthExprExternalizationLC();
       
   540 	
       
   541 	// corrupt the type field, one byte after the version field
       
   542 	buf->Des()[1] = ~0;
       
   543 	
       
   544 	// the server should panick this thread with
       
   545 	// category "AuthServer" and reason EPanicBadExternalizedAuthExpr (3)
       
   546 	TRequestStatus rs;
       
   547 	CallSessionFunction(EAuthenticate, TIpcArgs(buf), rs);
       
   548 	
       
   549 	// this code will not be reached
       
   550 	User::WaitForRequest(rs);
       
   551 	CleanupStack::PopAndDestroy(buf);
       
   552 	}
       
   553 
       
   554 
       
   555 void RTestAuthClient::SendHighVerAuthExprL()
       
   556 /**
       
   557 	Send an authenticate request to the server,
       
   558 	with an unsupported version number.
       
   559 	
       
   560 	The server should fail the request with
       
   561 	KErrAuthServUnsupportedExprVersion.
       
   562  */
       
   563 	{
       
   564 	HBufC8* buf = GetAuthExprExternalizationLC();
       
   565 	
       
   566 	// change the version field, which is the first byte
       
   567 	buf->Des()[0] = KMaxTInt8;
       
   568 	
       
   569 	TRequestStatus rs;
       
   570 	CallSessionFunction(EAuthenticate, TIpcArgs(buf), rs);
       
   571 	User::WaitForRequest(rs);
       
   572 	
       
   573 	// this isn't semantically the right code to
       
   574 	// fail the test with, because it is expected.
       
   575 	// However, it is easy to find in the test code
       
   576 	// if it occurs.
       
   577 	if (rs != KErrAuthServUnsupportedExprVersion)
       
   578 		User::Leave(KErrAuthServUnsupportedExprVersion);
       
   579 	
       
   580 	CleanupStack::PopAndDestroy(buf);
       
   581 	}
       
   582 
       
   583 
       
   584 void RunTestAuthClientFunctionL(void (RTestAuthClient::* aTacFuncL)())
       
   585 /**
       
   586 	Connect to the authentication server using
       
   587 	RTestAuthClient, and call the supplied function.
       
   588 	
       
   589 	This is a helper function for CTStepBadExtAuthExpr
       
   590 	and CTStepAuthExprHighVer which both use
       
   591 	RTestAuthClient, and only call different functions.
       
   592 	
       
   593 	@param	aTacFuncL		Function to call on RTestAuthClient
       
   594 							once the connection has been made.
       
   595 							If this function leaves then the
       
   596 							leave code is propagated through this
       
   597 							(RunTestAuthClientFunctionL) function.
       
   598  */
       
   599 	{
       
   600 	RTestAuthClient tac;
       
   601 	User::LeaveIfError(tac.Connect());
       
   602 	CleanupClosePushL(tac);
       
   603 	
       
   604 	// this function should not return
       
   605 	(tac.*aTacFuncL)();
       
   606 	
       
   607 	CleanupStack::PopAndDestroy(&tac);
       
   608 	}
       
   609 
       
   610 
       
   611 }	// namespace
       
   612 
       
   613 
       
   614 CTStepBadExtAuthExpr::CTStepBadExtAuthExpr()
       
   615 /**
       
   616 	Record this test step's name.
       
   617  */
       
   618 	{
       
   619 	SetTestStepName(KTStepBadExtAuthExpr);
       
   620 	}
       
   621 
       
   622 
       
   623 TVerdict CTStepBadExtAuthExpr::doTestStepL()
       
   624 /**
       
   625 	Implement CTestStep by calling RAuthClient::Authenticate
       
   626 	with a corrupt externalized authentication expression.
       
   627 	
       
   628 	The current thread should be panicked with
       
   629 	category AuthServer and reason EPanicBadExternalizedAuthExpr.
       
   630  */
       
   631 	{
       
   632 	// this function should not return
       
   633 	RunTestAuthClientFunctionL(&RTestAuthClient::SendCorruptAuthExprL);
       
   634 	return EPass;
       
   635 	}
       
   636 
       
   637 
       
   638 CTStepAuthExprHighVer::CTStepAuthExprHighVer()
       
   639 /**
       
   640 	Record this test step's name.
       
   641  */
       
   642 	{
       
   643 	SetTestStepName(KTStepAuthExprHighVer);
       
   644 	}
       
   645 
       
   646 
       
   647 TVerdict CTStepAuthExprHighVer::doTestStepL()
       
   648 /**
       
   649 	Implement CTestStep by calling RAuthClient::SendHighVerAuthExprL()
       
   650 	and ensuring the server fails the request if
       
   651 	the externalized auth expr uses an unsupported
       
   652 	version number.
       
   653  */
       
   654 	{
       
   655 	RunTestAuthClientFunctionL(&RTestAuthClient::SendHighVerAuthExprL);
       
   656 	return EPass;
       
   657 	}
       
   658 
       
   659 
       
   660 // -------- Debug-mode client-side panicks --------
       
   661 
       
   662 
       
   663 #ifdef _DEBUG
       
   664 
       
   665 
       
   666 // -------- CTStepAuthExprTypePnc --------
       
   667 
       
   668 
       
   669 CTStepAuthExprTypePnc::CTStepAuthExprTypePnc()
       
   670 /**
       
   671 	Record this test step's name.
       
   672  */
       
   673 	{
       
   674 	SetTestStepName(KTStepAuthExprTypePnc);
       
   675 	}
       
   676 
       
   677 
       
   678 TVerdict CTStepAuthExprTypePnc::doTestStepL()
       
   679 /**
       
   680 	Implement CTestStep by creating a plugin id
       
   681 	node, corrupting the type field, and testing
       
   682 	that it is panicked when the type is
       
   683 	retrieved.  Debug only.
       
   684  */
       
   685 	{
       
   686 	CAuthExpression* ae = AuthExpr(KTestPluginId0);
       
   687 	User::LeaveIfNull(ae);
       
   688 	CAuthExpressionImpl* aeImpl = static_cast<CAuthExpressionImpl*>(ae);
       
   689 	CorruptTypeField(aeImpl);
       
   690 	/* ignore */ aeImpl->Type();
       
   691 	
       
   692 	// this code should never be reached
       
   693 	return EPass;
       
   694 	}
       
   695 
       
   696 
       
   697 // -------- CTStepAuthExprTypePncBadLeft --------
       
   698 
       
   699 
       
   700 CTStepAuthExprTypePncBadLeft::CTStepAuthExprTypePncBadLeft()
       
   701 /**
       
   702 	Record this test step's name.
       
   703  */
       
   704 	{
       
   705 	SetTestStepName(KTStepAuthExprTypePncBadLeft);
       
   706 	}
       
   707 
       
   708 
       
   709 TVerdict CTStepAuthExprTypePncBadLeft::doTestStepL()
       
   710 /**
       
   711 	Implement CTestStep by creating a complex
       
   712 	expression where the left subexpression is
       
   713 	corrupt.  This tests that the invariant
       
   714 	correctly validates the left subtree.
       
   715  */
       
   716 	{
       
   717 	CAuthExpression* ae = AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1));
       
   718 	User::LeaveIfNull(ae);
       
   719 	CAuthExpressionImpl* aeImpl = static_cast<CAuthExpressionImpl*>(ae);
       
   720 	CorruptTypeField(aeImpl->Left());	
       
   721 	/* ignore */ aeImpl->Type();
       
   722 	
       
   723 	// this code should never be reached
       
   724 	return EPass;
       
   725 	}
       
   726 
       
   727 
       
   728 // -------- CTStepAuthExprTypePncBadRight --------
       
   729 
       
   730 
       
   731 CTStepAuthExprTypePncBadRight::CTStepAuthExprTypePncBadRight()
       
   732 /**
       
   733 	Record this test step's name.
       
   734  */
       
   735 	{
       
   736 	SetTestStepName(KTStepAuthExprTypePncBadRight);
       
   737 	}
       
   738 
       
   739 
       
   740 TVerdict CTStepAuthExprTypePncBadRight::doTestStepL()
       
   741 /**
       
   742 	Implement CTestStep by creating a complex
       
   743 	expression where the Right subexpression is
       
   744 	corrupt.  This tests that the invariant
       
   745 	correctly validates the Right subtree.
       
   746  */
       
   747 	{
       
   748 	CAuthExpression* ae = AuthAnd(AuthExpr(KTestPluginId0), AuthExpr(KTestPluginId1));
       
   749 	User::LeaveIfNull(ae);
       
   750 	CAuthExpressionImpl* aeImpl = static_cast<CAuthExpressionImpl*>(ae);
       
   751 	CorruptTypeField(aeImpl->Right());	
       
   752 	/* ignore */ aeImpl->Type();
       
   753 	
       
   754 	// this code should never be reached
       
   755 	return EPass;
       
   756 	}
       
   757 
       
   758 
       
   759 #endif	// #ifdef _DEBUG