genericopenlibs/cstdlib/TSTLIB/T_UCRT0P3.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2008-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 // *IMPORTANT*: This should only be run when called from T_UCRT0P1.
       
    15 // See TestCase SYSLIB-STDLIB-UT-4003 in T_UCRT0P1.CPP for more information.
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <stdlib.h>
       
    20 #include <e32debug.h>
       
    21 #include <e32test.h>
       
    22 #include <estlib.h>
       
    23 
       
    24 
       
    25 RTest TheTest(_L("T_UCRT0P3"));
       
    26 
       
    27 
       
    28 //Test macros and functions
       
    29 LOCAL_C void Check(TInt aValue, TInt aLine)
       
    30 	{
       
    31 	if(!aValue)
       
    32 		TheTest(EFalse, aLine);
       
    33 	}
       
    34 
       
    35 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    36 	{
       
    37 	if(aValue != aExpected)
       
    38 		{
       
    39 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    40 		TheTest(EFalse, aLine);
       
    41 		}
       
    42 	}
       
    43 
       
    44 #define TEST(arg) ::Check((arg), __LINE__)
       
    45 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    46 
       
    47 
       
    48 
       
    49 //DEF124477: [coverity] RESOURCE_LEAK - stdlib.
       
    50 void Defect_DEF124477_Part2()
       
    51 	{
       
    52 	int argc=0;
       
    53 	char** char_argv=0;
       
    54 	char** char_envp=0;
       
    55 
       
    56 	TheTest.Printf(_L("Retrieving the arguments passed in from T_UCRT0P1\n"));
       
    57 	
       
    58 	// Set heap memory allocation failure for 'char_argv'.
       
    59 	TheTest.Printf(_L("Set RHeap::EFailNext on the 4th memory allocation event.\n"));
       
    60 	__UHEAP_SETFAIL(RHeap::EFailNext, 4);
       
    61 
       
    62 	TheTest.Printf(_L("Try to get args and environment.\n"));
       
    63 	__crt0(argc,char_argv,char_envp);			// get args & environment from somewhere
       
    64 	
       
    65 	// Memory allocation failure should result in the following variables being NULL.
       
    66 	TheTest.Printf(_L("Check argc is NULL.\n"));
       
    67 	TEST(argc==0);
       
    68 	TheTest.Printf(_L("Check char_argv is NULL.\n"));
       
    69 	TEST(char_argv==0);
       
    70 	TheTest.Printf(_L("Check char_envp is NULL.\n"));
       
    71 	TEST(char_envp==0);
       
    72 
       
    73 
       
    74 	//Reset values.
       
    75 	argc=0;
       
    76 	char_argv=0;
       
    77 	char_envp=0;
       
    78 	
       
    79 	// Set heap memory allocation failure for 'cmdbuf' in __crt0 to fail, resulting in routine
       
    80 	// returning early, so 'char_argc' and 'char_envp' should still be NULL.
       
    81 	TheTest.Printf(_L("Set RHeap::EFailNext on the 1st memory allocation event.\n"));
       
    82 	__UHEAP_SETFAIL(RHeap::EFailNext, 1);
       
    83 
       
    84 	TheTest.Printf(_L("Try to get args and environment.\n"));
       
    85 	__crt0(argc,char_argv,char_envp);			// get args & environment from somewhere
       
    86 	
       
    87 	// Memory allocation failure should result in the following variables being NULL.
       
    88 	TheTest.Printf(_L("Check argc is NULL.\n"));
       
    89 	TEST(argc==0);
       
    90 	TheTest.Printf(_L("Check char_envp is NULL.\n"));
       
    91 	TEST(char_envp==0);
       
    92 
       
    93 
       
    94 	wchar_t** wchar_t_argv=0;
       
    95 	wchar_t** wchar_t_envp=0;
       
    96 	
       
    97 	TheTest.Printf(_L("Retrieving the arguments passed in from T_UCRT0P1\n"));
       
    98 	
       
    99 	// Will set the memory allocation for wargv in __crt0 to fail.
       
   100 	TheTest.Printf(_L("Set RHeap::EFailNext on the 3rd memory allocation event.\n"));
       
   101 	__UHEAP_SETFAIL(RHeap::EFailNext, 3);
       
   102 
       
   103 	TheTest.Printf(_L("Now try to get args and environment.\n"));
       
   104 	__crt0(argc,wchar_t_argv,wchar_t_envp);			// get args & environment from somewhere
       
   105 	
       
   106 	// The arguements passed into __crt0 should still be NULL.
       
   107 	TheTest.Printf(_L("Check argc is NULL.\n"));
       
   108 	TEST(argc==0);
       
   109 	TheTest.Printf(_L("Check wchar_t_argv is NULL.\n"));
       
   110 	TEST(wchar_t_argv==0);
       
   111 	TheTest.Printf(_L("Check wchar_t_envp is NULL.\n"));
       
   112 	TEST(wchar_t_envp==0);
       
   113 	
       
   114 	exit(0);
       
   115 	}
       
   116 
       
   117 /**
       
   118 Invoke the tests
       
   119 */
       
   120 LOCAL_C void RunTestsL ()
       
   121     {
       
   122 	Defect_DEF124477_Part2();
       
   123 	}
       
   124 
       
   125 /**
       
   126 /This should only be called from T_UCRT0P1, as it is part of the same test.
       
   127 */
       
   128 GLDEF_C TInt E32Main()
       
   129 	{
       
   130 	CTrapCleanup* tc = CTrapCleanup::New();
       
   131 	TheTest(tc != NULL);
       
   132 	__UHEAP_MARK;
       
   133 
       
   134 	TheTest.Title();
       
   135 	TheTest.Printf(_L("** Starting the tests in the child process T_UCRT0P3 ...\n"));
       
   136 	TheTest.Start(_L(" @SYMTestCaseID: "));
       
   137 	TRAPD(error,RunTestsL());
       
   138 	TEST2(error,KErrNone);
       
   139 
       
   140 	TheTest.End();
       
   141 	TheTest.Close();
       
   142 	__UHEAP_MARKEND;
       
   143 	delete tc;
       
   144 	return 0;
       
   145 	}
       
   146