os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_StdlibDefect.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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 //
    15 
    16 #include <string.h>
    17 #include <e32test.h> //this includes e32cmn.h
    18 #include <e32debug.h>
    19 
    20 #include <sys/types.h>
    21 #include <sys/socket.h>
    22 #include <sys/ioctl.h>
    23 #include <libc/netinet/in.h>
    24 #include <libc/arpa/inet.h>
    25 #include <limits.h>
    26 
    27 #include <sys/errno.h>
    28 #include <stdlib.h>
    29 #include <math.h>
    30 
    31 LOCAL_D RTest				TheTest (_L("T_StdlibDefect"));
    32 //
    33 //
    34 //Test macroses and functions
    35 
    36 static void Check(TInt aValue, TInt aLine)
    37 	{
    38 	if(!aValue)
    39 		{
    40 		TheTest(EFalse, aLine);
    41 		}
    42 	}
    43 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
    44 	{
    45 	if(aValue != aExpected)
    46 		{
    47 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    48 		TheTest(EFalse, aLine);
    49 		}
    50 	}
    51 #define TEST(arg) ::Check((arg), __LINE__)
    52 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__) 
    53 
    54 
    55 
    56 const TInt KTestDataLen = 10000;
    57 const TInt KTestIterations = 1000;
    58 
    59 #ifdef __ARMCC__
    60 const TInt KMinCharARMCC = 0;
    61 const TInt KMaxCharARMCC = 255;
    62 #else
    63 const TInt KMinCharNoARMCC = -128;
    64 const TInt KMaxCharNoARMCC = 127;
    65 #endif
    66 
    67 /**
    68 DEF062679  : memcpy in stdlib is slow
    69 */ 
    70 void Defect_DEF062679_memcpy()
    71 	{
    72 
    73 	TUint8* src1 = new TUint8[KTestDataLen];
    74 	TEST(src1 != NULL);
    75 	Mem::Fill(src1, KTestDataLen, 'A');
    76 	
    77 	TUint8* dst1 = new TUint8[KTestDataLen];
    78 	TEST(dst1 != NULL);
    79 
    80 	TTime startTime, stopTime;
    81 	TInt i;
    82 	//Loop to check time spent using Mem::Copy
    83 	startTime.UniversalTime();
    84 	for(i=0; i<KTestIterations ; i++)
    85 		Mem::Copy(dst1, src1, KTestDataLen );
    86 	stopTime.UniversalTime();
    87 	TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
    88 	TheTest.Printf(_L("Time taken using Mem::Copy :%d microseconds\n"), timeTaken.Int64() );
    89 	
    90 	//Loop to check the time spent using memcpy after the change
    91 	startTime.UniversalTime();
    92 	for(i=0; i<KTestIterations ; i++)
    93 		memcpy(dst1, src1, KTestDataLen );
    94 	stopTime.UniversalTime();
    95 	TTimeIntervalMicroSeconds timeTaken2 = stopTime.MicroSecondsFrom(startTime);
    96 	TheTest.Printf(_L("Time taken using  memcpy: %d microseconds\n"), timeTaken2.Int64() );
    97 	TheTest.Printf(_L("Time taken using  memcpy before the change about 613125 microseconds\n"));
    98 
    99 	//Test memcpy works fine
   100 	for(i=0; i<KTestIterations ; i++)
   101 		TEST(dst1[i] == src1[i]);
   102 
   103 	delete [] src1;
   104 	delete [] dst1;
   105 	
   106 	}
   107 	
   108 	
   109 	
   110 	
   111 /**
   112 DEF062679  : memcpy in stdlib is slow
   113 */ 
   114 void Defect_DEF062679_memcmp()
   115 	{
   116 
   117 	TUint8* str1 = new TUint8[KTestDataLen];
   118 	TEST(str1 != NULL);
   119 	Mem::Fill(str1, KTestDataLen, 'A');
   120 	
   121 	TUint8* str2 = new TUint8[KTestDataLen];
   122 	TEST(str2 != NULL);
   123 	Mem::Fill(str2, KTestDataLen, 'A');
   124 	
   125 	TTime startTime, stopTime;
   126 	
   127 	//Loop to check the time using Mem::Copy
   128 	startTime.UniversalTime();
   129 	TInt i,ret;
   130 	for(i=0; i<KTestIterations ; i++)
   131 		ret = Mem::Compare(str2,KTestDataLen, str1, KTestDataLen );
   132 	stopTime.UniversalTime();
   133 	TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
   134 	TheTest.Printf(_L("Time taken using Mem::Compare :%ld microseconds\n"), timeTaken.Int64() );
   135 	
   136 	//Loop to check the time spent using memcpy after the change
   137 	startTime.UniversalTime();
   138 	
   139 	for(i=0; i<KTestIterations ; i++)
   140 		ret = memcmp(str2,str1, KTestDataLen );
   141 	TEST(ret==0); //check that memcmp works fine	
   142 	stopTime.UniversalTime();
   143 	TTimeIntervalMicroSeconds timeTaken2 = stopTime.MicroSecondsFrom(startTime);
   144 	TheTest.Printf(_L("Time taken using  memcmp: %ld microseconds\n"), timeTaken2.Int64() );
   145 	
   146 	TheTest.Printf(_L("Time taken using  memcmp before changes 1007000 microseconds\n"));
   147 	
   148 
   149 	//Test memcmp works fine
   150 	
   151 	TUint8* str3 = new TUint8[KTestDataLen];
   152 	TEST(str3 != NULL);
   153 	Mem::Fill(str3, KTestDataLen, 'B');
   154 	ret = memcmp(str3, str1, KTestDataLen);
   155 	TEST(ret>0);
   156 	ret = memcmp(str1, str3, KTestDataLen);
   157 	TEST(ret<0);
   158 	
   159 	delete str1;
   160 	delete str2;
   161 	delete str3;
   162 	}	
   163 
   164 /**
   165 INC073740: inet_addr and inet_aton returns wrong results with invalid input on STDLIB
   166 */ 
   167 void Defect_INC073740()
   168 	{
   169 	TheTest.Next(_L("INC073740: inet_addr and inet_aton returns wrong results with invalid input on STDLIB"));
   170 	
   171 	int err;
   172 	struct in_addr iaddr;	
   173 	char* good_addr="16.33.50.67";
   174 	char* bad_addr="256.33.50.67";
   175 	char* worse_addr="16.1456.50.67";
   176 	char* worst_addr="16.33.333333.67";
   177 	
   178 	err=inet_aton(good_addr, &iaddr);
   179 	TEST2(err, 1);
   180 
   181 	err=inet_aton(bad_addr, &iaddr);
   182 	TEST2(err, 0);
   183 
   184 	err=inet_aton(worse_addr, &iaddr);
   185 	TEST2(err, 0);
   186 
   187 	err=inet_aton(worst_addr, &iaddr);
   188 	TEST2(err, 0);
   189 	}
   190 
   191 /**
   192 @SYMTestCaseID         	SYSLIB-STDLIB-CT-1863
   193 @SYMTestCaseDesc        Tests for minimum and maximum values type "char" with ARMCC macro
   194 @SYMTestPriority        High
   195 @SYMTestActions         Tests for checking minimum and maximum values for a variable of type "char"
   196 @SYMTestExpectedResults Tests must not fail
   197 @SYMDEF                 PDEF091928
   198 */ 
   199 void Defect_PDEF091928()
   200 	{
   201 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-1863 PDEF091928: limits.h not correct for ARM RVCT compiler "));
   202 	
   203 	char charmn=CHAR_MIN;
   204 	char charmx=CHAR_MAX;
   205 	
   206 #ifdef __ARMCC__
   207 	
   208 	TEST2(charmn, KMinCharARMCC);
   209 	TEST2(charmx, KMaxCharARMCC);
   210 	
   211 #else
   212 
   213 	TEST2(charmn, KMinCharNoARMCC);
   214 	TEST2(charmx, KMaxCharNoARMCC);
   215 
   216 #endif
   217 	}
   218 
   219 /**
   220 @SYMTestCaseID         	SYSLIB-STDLIB-UT-3612
   221 @SYMTestCaseDesc        Tests for __errno().
   222 @SYMTestPriority        Normal
   223 @SYMTestActions         Tests for __errno(). Test whether it is correctly exported and functioning 
   224 						as expected after being declared with IMPORT_C. 
   225 @SYMTestExpectedResults Tests must not fail
   226 @SYMDEF                 DEF110593
   227 */ 
   228 void Defect_DEF110593()
   229 	{
   230 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-3612 DEF110593: IMPORT_C/EXPORT_C: ERRNO.H "));
   231 
   232 	// randomly selected 24 macros from errno.h
   233 	const int errMacros[24] =
   234 		{
   235 		EPERM,		ENOENT,		ESRCH,		EINTR,		EIO,
   236 		EBADF,		ENOMEM,		EFAULT,		EBUSY,		ENOTDIR,
   237 		ENFILE,		ESPIPE,		ERANGE,		ENOMSG,		EDEADLK,
   238 		ENOLCK,		ENOTSOCK,	ENOLINK,	EBADMSG,	ENOTUNIQ,
   239 		EBADFD,		ENOSYS,		EILSEQ, 	__ELASTERROR
   240 		};
   241 	
   242 	// Step 1: setting errno using assign "=", test value returned by errno
   243 	errno = 0;
   244 	TEST(errno == 0);
   245 	
   246 	int i;
   247 	for (i = 0; i < 24; i++)
   248 		{
   249 		errno = errMacros[i];
   250 		TEST(errno == errMacros[i]);
   251 		}
   252 	
   253 	// Step 2: setting errno using the library globals struct, test value returned by errno
   254 	struct _reent *r = _REENT2;
   255 	r->_errno = 0;
   256 	TEST(errno == 0);
   257 	
   258 	for (i = 0; i < 24; i++)
   259 		{
   260 		r->_errno = errMacros[i];
   261 		TEST(errno == errMacros[i]);
   262 		}
   263 	
   264 	r->_errno = 0;
   265 	TEST(errno == 0);
   266 	
   267 	// Step3: Test errno by using other C function in STDLIB
   268 	// Test using ldexp(double value, int exp);
   269 	// Giving val a huge number to make res overflow. errno should return ERANGE
   270 	double val = 1.5E+308;
   271 	int exp = 10;
   272 	ldexp(val, exp);
   273 	TEST(errno == ERANGE);
   274 		
   275 	//finish
   276 	CloseSTDLIB();	
   277 	}
   278 
   279 /**
   280 @SYMTestCaseID          SYSLIB-STDLIB-CT-4001
   281 @SYMTestCaseDesc        Test strtoul() with a string whose first character is '-' or '+'. 
   282 @SYMTestPriority        3. Medium
   283 @SYMTestActions         Test strtoul() with a string whose first character is '-' or '+'. 
   284 @SYMTestExpectedResults Tests must not fail
   285 @SYMDEF                 PDEF114447 
   286 */ 
   287 void Defect_PDEF114447 ()
   288     {
   289 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-4001 PDEF114447 : add the processing for '-' and '+' in a string passed to strtoul() "));
   290     
   291     unsigned long result;
   292     
   293     result = strtoul("+80", NULL, 10);    
   294     TEST2(result, 80);
   295 
   296     result = strtoul("-80", NULL, 10);    
   297     TEST2(result, -80);    
   298     }
   299 
   300 /**
   301 Calls ImpurePtr2() to allocate memory for the library globals struct then calls CloseSTDLIB() 
   302 to release it.
   303 Leaves if system-wide error occurs.
   304 */
   305 LOCAL_C void TestImpurePtrL()
   306 {
   307 	struct _reent * p = ImpurePtr2();
   308 	User::LeaveIfNull(p);
   309 	CloseSTDLIB();
   310 }
   311 
   312 /**
   313 @SYMTestCaseID         	SYSLIB-STDLIB-UT-4002
   314 @SYMTestCaseDesc        Test checks the constructor of CLocalSystemInterface does not panics in OOM test
   315 						or when error KErrNoMemory occurs.
   316 @SYMTestPriority        Normal
   317 @SYMTestActions         In an OOM test, repeats calling ImpurePtr2(), which creates CLocalSystemInterface instance. 
   318 @SYMTestExpectedResults The test program should not panic or fail.
   319 @SYMDEF                 DEF114383
   320 */ 
   321 LOCAL_C void Defect_DEF114383()
   322 	{
   323 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-4002 DEF114383: STDLIB, CLocalSystemInterface::CLocalSystemInterface() panics in OOM "));		
   324 	
   325 	TInt err=KErrNone;
   326 	TInt tryCount = 0;
   327 	do
   328 		{
   329 		__UHEAP_MARK;
   330 
   331 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   332 		TRAP(err, TestImpurePtrL());
   333 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   334 		
   335 		if (err!=KErrNoMemory)
   336 			TEST(err == KErrNone);
   337 		
   338 		__UHEAP_MARKEND;
   339 		} while(err == KErrNoMemory);
   340 		
   341 	TEST(err == KErrNone);
   342 		
   343 	TheTest.Printf(_L("- ImpurePtr2() succeeded at heap failure rate of %i\n"), tryCount);
   344 	} 
   345 
   346 /**
   347 Invoke the tests
   348 */
   349 LOCAL_C void RunTestsL ()
   350     {
   351 	Defect_DEF062679_memcpy();
   352 	Defect_DEF062679_memcmp();
   353 
   354 	Defect_INC073740();
   355 	
   356 	Defect_PDEF091928();
   357 	
   358 	Defect_DEF110593(); 
   359 	
   360 	Defect_PDEF114447();
   361 
   362 	Defect_DEF114383();
   363 	}
   364 
   365 
   366 
   367 GLDEF_C TInt E32Main()
   368  {		
   369  		
   370 	CTrapCleanup* tc = CTrapCleanup::New();
   371 	TEST(tc != NULL);
   372 	
   373 	__UHEAP_MARK;
   374 
   375 	TheTest.Title();
   376 	TheTest.Start (_L("Defect Tests"));
   377 	TInt err;
   378 	TRAP(err, ::RunTestsL())
   379 	TEST2(err, KErrNone);
   380 
   381 	TheTest.End();
   382 	TheTest.Close();
   383 
   384 	delete tc;
   385 	__UHEAP_MARKEND;
   386 	
   387 	
   388 	
   389 	return(KErrNone);
   390 	
   391 	}
   392