kerneltest/e32test/property/t_security_slave.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-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 the License "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 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include "t_property.h"
       
    18 
       
    19 TSecureId TestSecureId;
       
    20 TVendorId TestVendorId;
       
    21 TCapability TestCaps[ECapability_Limit];
       
    22 TCapability TestMissingCaps[ECapability_Limit];
       
    23 
       
    24 TSecurityPolicy CorrectCapPolicy;
       
    25 TSecurityPolicy IncorrectCapPolicy;
       
    26 
       
    27 TUint TestPolicyCount=0;
       
    28 
       
    29 #include <e32svr.h>
       
    30 
       
    31 TBool MakeTestSecurityPolicy()
       
    32 	{
       
    33 	TCapability* c =TestCaps;
       
    34 	TCapability* m =TestMissingCaps;
       
    35 	TUint t=++TestPolicyCount;
       
    36 	RDebug::Print(_L("Test policy %d\n"),t);
       
    37 	if(t==1)
       
    38 		{
       
    39 		_LIT_SECURITY_POLICY_PASS(KSecurityPolicyPass);
       
    40 		CorrectCapPolicy = KSecurityPolicyPass;
       
    41 		_LIT_SECURITY_POLICY_FAIL(KSecurityPolicyFail);
       
    42 		IncorrectCapPolicy = KSecurityPolicyFail;
       
    43 		return ETrue;
       
    44 		}
       
    45 	t -= 1;
       
    46 	if(t<7)
       
    47 		{
       
    48 		switch(t)
       
    49 			{
       
    50 		case 0:
       
    51 			CorrectCapPolicy = TSecurityPolicy(c[0]);
       
    52 			IncorrectCapPolicy = TSecurityPolicy(m[0]);
       
    53 			break;
       
    54 		case 1:
       
    55 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1]);
       
    56 			IncorrectCapPolicy = TSecurityPolicy(m[0],c[1]);
       
    57 			break;
       
    58 		case 2:
       
    59 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2]);
       
    60 			IncorrectCapPolicy = TSecurityPolicy(c[0],m[1],c[2]);
       
    61 			break;
       
    62 		case 3:
       
    63 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3]);
       
    64 			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],m[2],c[3]);
       
    65 			break;
       
    66 		case 4:
       
    67 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4]);
       
    68 			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],m[3],c[4]);
       
    69 			break;
       
    70 		case 5:
       
    71 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],c[5]);
       
    72 			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],m[4],c[5]);
       
    73 			break;
       
    74 		case 6:
       
    75 			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],c[5],c[6]);
       
    76 			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],m[5],c[6]);
       
    77 			break;
       
    78 			}
       
    79 		return ETrue;
       
    80 		}
       
    81 	t -= 7;
       
    82 	if(t<4)
       
    83 		{
       
    84 		TSecureId id = TestSecureId;
       
    85 		TSecureId id2 = TSecureId((TUint32)TestVendorId);
       
    86 		switch(t)
       
    87 			{
       
    88 		case 0:
       
    89 			CorrectCapPolicy = TSecurityPolicy(id);
       
    90 			IncorrectCapPolicy = TSecurityPolicy(id2);
       
    91 			break;
       
    92 		case 1:
       
    93 			CorrectCapPolicy = TSecurityPolicy(id,c[0]);
       
    94 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0]);
       
    95 			break;
       
    96 		case 2:
       
    97 			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1]);
       
    98 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1]);
       
    99 			break;
       
   100 		case 3:
       
   101 			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1],c[2]);
       
   102 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1],c[2]);
       
   103 			break;
       
   104 			}
       
   105 		return ETrue;
       
   106 		}
       
   107 	t -= 4;
       
   108 	if(t<4)
       
   109 		{
       
   110 		TVendorId id = TestVendorId;
       
   111 		TVendorId id2 = TVendorId((TUint32)TestSecureId);
       
   112 		switch(t)
       
   113 			{
       
   114 		case 0:
       
   115 			CorrectCapPolicy = TSecurityPolicy(id);
       
   116 			IncorrectCapPolicy = TSecurityPolicy(id2);
       
   117 			break;
       
   118 		case 1:
       
   119 			CorrectCapPolicy = TSecurityPolicy(id,c[0]);
       
   120 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0]);
       
   121 			break;
       
   122 		case 2:
       
   123 			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1]);
       
   124 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1]);
       
   125 			break;
       
   126 		case 3:
       
   127 			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1],c[2]);
       
   128 			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1],c[2]);
       
   129 			break;
       
   130 			}
       
   131 		return ETrue;
       
   132 		}
       
   133 	t -= 4;
       
   134 	TestPolicyCount = 0;
       
   135 	return EFalse;
       
   136 	}
       
   137 
       
   138 _LIT(KSecurityReadCapabilityName, "RProperty Security: Read Capability Basics");
       
   139 
       
   140 class CPropSecurityReadCapability : public CTestProgram
       
   141 	{
       
   142 public:
       
   143 	CPropSecurityReadCapability(TUid aCategory, TUint aKey1, TUint aKey2, RProperty::TType aType) :
       
   144 		CTestProgram(KSecurityReadCapabilityName), iCategory(aCategory), iKey1(aKey1), iKey2(aKey2), iType(aType)
       
   145 		{
       
   146 		}
       
   147 
       
   148 	void Run(TUint aCount);
       
   149 
       
   150 private:
       
   151 	TUid				iCategory;
       
   152 	TUint				iKey1;
       
   153 	TUint				iKey2;
       
   154 	RProperty::TType	iType;
       
   155 	};
       
   156 
       
   157 void CPropSecurityReadCapability::Run(TUint aCount)
       
   158 	{
       
   159 	while(MakeTestSecurityPolicy())
       
   160 	for (TUint i = 0; i < aCount; ++i)
       
   161 		{
       
   162 		RProperty	prop1;
       
   163 		RProperty	prop2;
       
   164 
       
   165 		TInt r = prop1.Attach(iCategory, iKey1);
       
   166 		TF_ERROR(r, r == KErrNone);
       
   167 		r = prop2.Attach(iCategory, iKey2);
       
   168 		TF_ERROR(r, r == KErrNone);
       
   169 
       
   170 		// If the property has not been defined, the request will not complete until the property
       
   171 		// is defined and published.
       
   172 		// When defined if the caller does not have read capabilities the request completes
       
   173 		// with KErrPermissionDenied.
       
   174 		TRequestStatus status1;
       
   175 		prop1.Subscribe(status1);
       
   176 		TF_ERROR(status1.Int(), status1.Int() == KRequestPending);
       
   177 		r = prop1.Define(iCategory, iKey1, iType, IncorrectCapPolicy, CorrectCapPolicy);
       
   178 		TF_ERROR(r, r == KErrNone);
       
   179 		User::WaitForRequest(status1);
       
   180 		TF_ERROR(status1.Int(), status1.Int() == KErrPermissionDenied);
       
   181 
       
   182 		TRequestStatus status2;
       
   183 		prop2.Subscribe(status2);
       
   184 		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
       
   185 		r = prop2.Define(iCategory, iKey2, iType, CorrectCapPolicy, IncorrectCapPolicy);
       
   186 		TF_ERROR(r, r == KErrNone);
       
   187 		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
       
   188 		prop2.Cancel();
       
   189 		User::WaitForRequest(status2);
       
   190 		TF_ERROR(status2.Int(), status2.Int() == KErrCancel);
       
   191 
       
   192 		// If the Subscribe() caller does not have the read capabilities the request completes immediately
       
   193 		// with KErrPermissionDenied.
       
   194 		prop1.Subscribe(status1);
       
   195 		User::WaitForRequest(status1);
       
   196 		TF_ERROR(status1.Int(), status1.Int() == KErrPermissionDenied);
       
   197 
       
   198 		prop2.Subscribe(status2);
       
   199 		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
       
   200 		prop2.Cancel();
       
   201 		User::WaitForRequest(status2);
       
   202 		TF_ERROR(status2.Int(), status2.Int() == KErrCancel);
       
   203 
       
   204 		// If the caller does not have the read capabilities Get() fails with KErrPermissionDenied.
       
   205 		if (iType == RProperty::EInt)
       
   206 			{
       
   207 			TInt value;
       
   208 			r = prop1.Get(iCategory, iKey1, value);
       
   209 			TF_ERROR(r, r == KErrPermissionDenied);
       
   210 			r = prop2.Get(iCategory, iKey2, value);
       
   211 			TF_ERROR(r, r == KErrNone);
       
   212 			r = prop1.Get(value);
       
   213 			TF_ERROR(r, r == KErrPermissionDenied);
       
   214 			r = prop2.Get(value);
       
   215 			TF_ERROR(r, r == KErrNone);
       
   216 			}
       
   217 		else 
       
   218 			{
       
   219 			TBuf<16> buf;
       
   220 			TBuf8<16> buf8;
       
   221 			r = prop1.Get(iCategory, iKey1, buf);
       
   222 			TF_ERROR(r, r == KErrPermissionDenied);
       
   223 			r = prop2.Get(iCategory, iKey2, buf);
       
   224 			TF_ERROR(r, r == KErrNone);
       
   225 			r = prop1.Get(iCategory, iKey1, buf8);
       
   226 			TF_ERROR(r, r == KErrPermissionDenied);
       
   227 			r = prop2.Get(iCategory, iKey2, buf8);
       
   228 			TF_ERROR(r, r == KErrNone);
       
   229 			r = prop1.Get(buf);
       
   230 			TF_ERROR(r, r == KErrPermissionDenied);
       
   231 			r = prop2.Get(buf);
       
   232 			TF_ERROR(r, r == KErrNone);
       
   233 			r = prop1.Get(buf8);
       
   234 			TF_ERROR(r, r == KErrPermissionDenied);
       
   235 			r = prop2.Get(buf8);
       
   236 			TF_ERROR(r, r == KErrNone);
       
   237 			}
       
   238 
       
   239 		r = prop1.Delete(iCategory, iKey1);
       
   240 		TF_ERROR(r, r == KErrNone);
       
   241 		r = prop2.Delete(iCategory, iKey2);
       
   242 		TF_ERROR(r, r == KErrNone);
       
   243 		prop1.Close();
       
   244 		prop2.Close();
       
   245 		}
       
   246 	}
       
   247 
       
   248 _LIT(KSecurityWriteCapabilityName, "RProperty Security: Write Cpability Basics");
       
   249 
       
   250 class CPropSecurityWriteCapability : public CTestProgram
       
   251 	{
       
   252 public:
       
   253 	CPropSecurityWriteCapability(TUid aCategory, TUint aKey1, TUint aKey2, RProperty::TType aType) :
       
   254 		CTestProgram(KSecurityWriteCapabilityName), iCategory(aCategory), iKey1(aKey1), iKey2(aKey2), iType(aType)
       
   255 		{
       
   256 		}
       
   257 
       
   258 	void Run(TUint aCount);
       
   259 
       
   260 private:
       
   261 	TUid				iCategory;
       
   262 	TUint				iKey1;
       
   263 	TUint				iKey2;
       
   264 	RProperty::TType	iType;
       
   265 	};
       
   266 
       
   267 void CPropSecurityWriteCapability::Run(TUint aCount)
       
   268 	{
       
   269 	while(MakeTestSecurityPolicy())
       
   270 	for (TUint i = 0; i < aCount; ++i)
       
   271 		{
       
   272 		RProperty	prop1;
       
   273 		RProperty	prop2;
       
   274 
       
   275 		TInt r = prop1.Attach(iCategory, iKey1);
       
   276 		TF_ERROR(r, r == KErrNone);
       
   277 		r = prop2.Attach(iCategory, iKey2);
       
   278 		TF_ERROR(r, r == KErrNone);
       
   279 
       
   280 		r = prop1.Define(iCategory, iKey1, iType, CorrectCapPolicy, IncorrectCapPolicy);
       
   281 		TF_ERROR(r, r == KErrNone);
       
   282 		r = prop2.Define(iCategory, iKey2, iType, IncorrectCapPolicy, CorrectCapPolicy);
       
   283 		TF_ERROR(r, r == KErrNone);
       
   284 
       
   285 		// If the caller does not have the write capabilities Set() fails with KErrPermissionDenied.
       
   286 		if (iType == RProperty::EInt)
       
   287 			{
       
   288 			TInt value = 1;
       
   289 			r = prop1.Set(iCategory, iKey1, value);
       
   290 			TF_ERROR(r, r == KErrPermissionDenied);
       
   291 			r = prop2.Set(iCategory, iKey2, value);
       
   292 			TF_ERROR(r, r == KErrNone);
       
   293 			r = prop1.Set(value);
       
   294 			TF_ERROR(r, r == KErrPermissionDenied);
       
   295 			r = prop2.Set(value);
       
   296 			TF_ERROR(r, r == KErrNone);
       
   297 			}
       
   298 		else 
       
   299 			{
       
   300 			TBuf<16> buf(_L("Foo"));
       
   301 			TBuf8<16> buf8((TUint8*) "Foo");
       
   302 			r = prop1.Set(iCategory, iKey1, buf);
       
   303 			TF_ERROR(r, r == KErrPermissionDenied);
       
   304 			r = prop2.Set(iCategory, iKey2, buf);
       
   305 			TF_ERROR(r, r == KErrNone);
       
   306 			r = prop1.Set(iCategory, iKey1, buf8);
       
   307 			TF_ERROR(r, r == KErrPermissionDenied);
       
   308 			r = prop2.Set(iCategory, iKey2, buf8);
       
   309 			TF_ERROR(r, r == KErrNone);
       
   310 			r = prop1.Set(buf);
       
   311 			TF_ERROR(r, r == KErrPermissionDenied);
       
   312 			r = prop2.Set(buf);
       
   313 			TF_ERROR(r, r == KErrNone);
       
   314 			r = prop1.Set(buf8);
       
   315 			TF_ERROR(r, r == KErrPermissionDenied);
       
   316 			r = prop2.Set(buf8);
       
   317 			TF_ERROR(r, r == KErrNone);
       
   318 			}
       
   319 		
       
   320 		r = prop1.Delete(iCategory, iKey1);
       
   321 		TF_ERROR(r, r == KErrNone);
       
   322 		r = prop2.Delete(iCategory, iKey2);
       
   323 		TF_ERROR(r, r == KErrNone);
       
   324 		prop1.Close();
       
   325 		prop2.Close();
       
   326 		}
       
   327 	}
       
   328 
       
   329 GLDEF_C TInt E32Main()
       
   330 	{
       
   331 
       
   332 	TSecurityInfo info;
       
   333 	info.Set(RProcess());
       
   334 	TestSecureId = info.iSecureId;
       
   335 	TestVendorId = info.iVendorId;
       
   336 	{
       
   337 	TInt c=0;
       
   338 	TInt m=0;
       
   339 	for(TInt i=0; i<ECapability_Limit; i++)
       
   340 		{
       
   341 		if(info.iCaps.HasCapability((TCapability)i))
       
   342 			TestCaps[c++] = (TCapability)i;
       
   343 		else
       
   344 			TestMissingCaps[m++] = (TCapability)i;
       
   345 		}
       
   346 	__ASSERT_ALWAYS(c>=7,User::Panic(_L("not enough caps"), 1));
       
   347 	__ASSERT_ALWAYS(m>=6,User::Panic(_L("missing caps <6"), 1));
       
   348 	}
       
   349 		
       
   350 	TInt len = User::CommandLineLength();
       
   351 	__ASSERT_ALWAYS(len, User::Panic(_L("t_prop_sec: bad args"), 0));
       
   352 
       
   353 	// Get arguments for the command line
       
   354 	TInt size = len * sizeof(TUint16);
       
   355 	HBufC8* hb = HBufC8::NewMax(size);
       
   356 	__ASSERT_ALWAYS(hb, User::Panic(_L("t_prop_sec: no memory"), 0));
       
   357 	TPtr cmd((TUint16*) hb->Ptr(), len);
       
   358 	User::CommandLine(cmd);
       
   359 	CPropSecurity::TArgs* args = (CPropSecurity::TArgs*) hb->Ptr();	
       
   360 
       
   361 	CTestProgram::Start();
       
   362 
       
   363 	CTestProgram* progs[] = 
       
   364 		{ 
       
   365 		new CPropSecurityReadCapability(args->iCategory, args->iSlaveKeySlot + 0, args->iSlaveKeySlot + 1, 
       
   366 										RProperty::EInt),
       
   367 		new CPropSecurityReadCapability(args->iCategory, args->iSlaveKeySlot + 2, args->iSlaveKeySlot + 3, 
       
   368 										RProperty::EByteArray),
       
   369 		new CPropSecurityWriteCapability(args->iCategory, args->iSlaveKeySlot + 4, args->iSlaveKeySlot + 5, 
       
   370 										RProperty::EInt),
       
   371 		new CPropSecurityWriteCapability(args->iCategory, args->iSlaveKeySlot + 6, args->iSlaveKeySlot + 7, 
       
   372 										RProperty::EByteArray),
       
   373 		NULL 
       
   374 		};
       
   375 
       
   376 	TInt i;
       
   377 	TInt n = (sizeof(progs)/sizeof(*progs)) - 1;
       
   378 	for (i = 0; i < n; ++i)
       
   379 		{
       
   380 		__ASSERT_ALWAYS(progs[i], User::Panic(_L("t_property: no memory"), 0));
       
   381 		}
       
   382 	
       
   383 	CTestProgram::LaunchGroup(progs, 2);
       
   384 
       
   385 	for (i = 0; i < n; ++i)
       
   386 		{
       
   387 		delete progs[i];
       
   388 		}
       
   389 
       
   390 	CTestProgram::End();
       
   391 
       
   392 	return KErrNone;
       
   393 	}