os/security/crypto/weakcryptospi/test/kms/driver/test/kmslddtest/generate.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-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 * Tests generating a key.
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22 */
    23 
    24 #include "kmslddtest.h"
    25 
    26 
    27 void TestGenerateSingleKey(TInt aLength, RProcess aOwner, TKeyHandle& aHandle, TInt aExpectedResult)
    28 /**
    29 	Generate a key with the global channel object, and test the LDD completes the request
    30 	with the expected result.
    31 
    32 	@param	aLength			Key length in bytes.
    33 	@param	aOwner			Process which will own the newly-created key.
    34 	@param	aHandle			On success this will identify the newly-created key.  Its
    35 							value is undefined on failure.
    36 	@param	aExpectedResult	Expected Symbian OS error code with which the LDD will complete
    37 							the request.
    38  */
    39 	{
    40 	TRequestStatus rs;
    41 	KmsChannel.GenerateKey(aLength, aOwner, aHandle, rs);
    42 	User::WaitForRequest(rs);
    43 	test(rs == aExpectedResult);
    44 	}
    45 
    46 void TestGenerateSingleKey(TInt aLength, TKeyHandle& aHandle, TInt aExpectedResult)
    47 /**
    48 	Generate a key with the global channel object, using the current process as the owner.
    49 
    50 	@param	aLength			Key length in bytes.
    51 	@param	aHandle			On success this will identify the newly-created key.  Its
    52 							value is undefined on failure.
    53 	@param	aExpectedResult	Expected Symbian OS error code with which the LDD will complete
    54 							the request.
    55  */
    56 	{
    57 	TestGenerateSingleKey(aLength, CurrentProcess, aHandle, aExpectedResult);
    58 	}
    59 
    60 void RKmsChannelTest::TestInvalidGenArgsDescriptor(TRequestStatus& aStatus)
    61 /**
    62 	Supply an invalid TGenKeyArgs descriptor to the driver to ensure the current
    63 	thread is panicked as expected.
    64 
    65 	@param	aStatus			This object is passed to the driver, but it should not
    66 							be used because the thread should be panicked.
    67 							It is supplied to this function as a convenience so this
    68 							function does not have to declare its own instance.
    69  */
    70 	{
    71 	DoRequest(KmsLddImpl::EGenerateKey, aStatus, NULL);
    72 	}
    73 
    74 static void TestGenerateKeyBadHandleAddr(RKmsChannel& aChannel, TRequestStatus& aStatus)
    75 /**
    76 	Supply an invalid key handle to the LDD when generating the key, to ensure the
    77 	driver panicks the current thread.
    78 
    79 	@param	aChannel		Custom channel which is used to send generate request to
    80 							the driver.
    81 	@param	aStatus			This object is passed to the driver, but it should not
    82 							be used because the thread should be panicked.
    83 							It is supplied to this function as a convenience so this
    84 							function does not have to declare its own instance.
    85  */
    86 	{
    87 	TKeyHandle* ph = 0;		// in two stages to avoid RVCT #284-D: NULL reference is not allowed
    88 	aChannel.GenerateKey(2, CurrentProcess, *ph, aStatus);
    89 	}
    90 
    91 static void GenKeyOomAlloc(TRequestStatus& aStatus)
    92 /**
    93 	Attempt to generate a key.  This function is called in OOM to test the
    94 	driver cleans up on failure.
    95 
    96 	@param	aStatus			On return, the caller will wait on this object.
    97 							This is provided to save the function from having
    98 							to declare an instance, wait on it, and then return
    99 							return result.
   100 	@see GenKeyOomFree
   101  */
   102 	{
   103 	KmsChannel.GenerateKey(12, CurrentProcess, GenKeyHandle0, aStatus);
   104 	}
   105 
   106 static void GenKeyOomFree()
   107 /**
   108 	Clean up resources allocated for GenKeyOomAlloc.  This is called after the
   109 	operation succeeds.
   110 
   111 	@see GenKeyOomAlloc.
   112  */
   113 	{
   114 	TestDeleteSingleKey(GenKeyHandle0, KErrNone);
   115 	}
   116 
   117 
   118 void TestGenerateKey()
   119 /**
   120 	Test generating keys, successfully and otherwise.  See in-function
   121 	comments for specific test cases.
   122  */
   123 	{
   124 	test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-GENERATE-0001 TestGenerateKey "));
   125 	
   126 	TestGenerateSingleKey(1, GenKeyHandle0, KErrNone);
   127 	TestGenerateSingleKey(13, GenKeyHandle1, KErrNone);
   128 
   129 	// error case - invalid length
   130 	TestGenerateSingleKey(0, GenKeyHandle0, KErrArgument);
   131 
   132 	// error case - invalid arg descriptor
   133 	TestPanicTestChannel(&RKmsChannelTest::TestInvalidGenArgsDescriptor, KmsLddImpl::EGenKeyBadArgDescriptor);
   134 	
   135 	// error case - invalid handle
   136 	TestPanic(TestGenerateKeyBadHandleAddr, KmsLddImpl::EGenKeyInvalidHandleAddr);
   137 
   138 	// OOM
   139 	TestDeleteSingleKey(GenKeyHandle0, KErrNone);
   140 	RunOomTest(GenKeyOomAlloc, GenKeyOomFree);
   141 
   142 	DeleteAllKeys();
   143 	test.End();
   144 	}
   145