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