genericopenlibs/openenvcore/libc/test/testpipe/src/tpipeblocks.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2002-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 "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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32svr.h>
       
    22 #include <unistd.h> 
       
    23 #include <fcntl.h> 
       
    24 #include <errno.h>
       
    25 #include <string.h>
       
    26 #include "testpipe.h"
       
    27 #include <stdio.h>
       
    28 
       
    29 #include <sys/select.h>
       
    30 #include <sys/wait.h>
       
    31 #include <stdlib.h>
       
    32 #include <sys/types.h>
       
    33 #include <string.h>
       
    34 #include <pthread.h>
       
    35 #include <wchar.h>
       
    36 
       
    37 #include <test/testexecutestepbase.h>
       
    38 #include <test/testexecuteserverbase.h>
       
    39 
       
    40 #define MAX_SIZE 	50
       
    41 #define TEST_PIPE_VAL(val, exp, text)				  {\
       
    42 												if ((val==exp)) \
       
    43 													{ \
       
    44 													INFO_PRINTF1(text); \
       
    45 													INFO_PRINTF1(_L("...passed"));\
       
    46 													} \
       
    47 												else \
       
    48 													{ \
       
    49 													ERR_PRINTF1(text); \
       
    50 													ERR_PRINTF3(_L("...failed with Actual: %d Expected: %d"),(val), (exp));\
       
    51 													return EFail;\
       
    52 													}\
       
    53 												}
       
    54 
       
    55 #define TEST_PIPE_COND(cond, text)				  {\
       
    56 												if ((cond)) \
       
    57 													{ \
       
    58 													INFO_PRINTF1(text); \
       
    59 													INFO_PRINTF1(_L("...passed"));\
       
    60 													} \
       
    61 												else \
       
    62 													{ \
       
    63 													ERR_PRINTF1(text); \
       
    64 													ERR_PRINTF1(_L("...failed"));\
       
    65 													return EFail;\
       
    66 													}\
       
    67 												}
       
    68 												
       
    69 
       
    70 // ============================= LOCAL FUNCTIONS ===============================
       
    71 
       
    72 // ============================ MEMBER FUNCTIONS ===============================
       
    73 // -----------------------------------------------------------------------------
       
    74 // CTestPipe::Pipe
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 TInt CTestPipe::TestPipe(  )
       
    78 	{
       
    79 	INFO_PRINTF1(_L("Test Pipe"));
       
    80 
       
    81 	int fds[2];
       
    82         
       
    83     TInt err = pipe (fds);
       
    84     
       
    85     if ( err < 0)
       
    86         {
       
    87          INFO_PRINTF1(_L("pipe():Error"));        
       
    88         }
       
    89     else
       
    90     	{
       
    91     	err = close(fds[0]);
       
    92     	err = close(fds[1]);	
       
    93     	}
       
    94     
       
    95     return err;
       
    96 	}
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CTestPipe::PipeCheckWriteOnReadfd
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 TInt CTestPipe::PipeCheckWriteOnReadfd(  )
       
   103 	{
       
   104 	_LIT(KFunc, "PipeCheckWriteonReadfd");
       
   105 	INFO_PRINTF1(KFunc);
       
   106 	int fds[2];
       
   107     const char* buf = "somejunk";
       
   108     
       
   109     TInt err = pipe (fds);
       
   110     
       
   111     if ( err < 0)
       
   112         {
       
   113          INFO_PRINTF1(_L("Pipe Error!"));        
       
   114          return err;
       
   115         }
       
   116     
       
   117     err = write(fds[0], buf, 9); // write on the read desc
       
   118     
       
   119     if(err < 0 && errno == EBADF)
       
   120     {
       
   121        err = KErrNone;
       
   122     }
       
   123     else
       
   124     {
       
   125        INFO_PRINTF1( KFunc());
       
   126 	   err = KErrGeneral;
       
   127     }
       
   128     
       
   129     close(fds[0]);
       
   130     close(fds[1]);    
       
   131     INFO_PRINTF1(KFunc());
       
   132     return err;
       
   133 	}
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CTestPipe::PipeCheckReadOnWritefd
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 TInt CTestPipe::PipeCheckReadOnWritefd()
       
   140 	{
       
   141 	_LIT(KFunc, "PipeCheckReadOnWritefd");
       
   142 	INFO_PRINTF1(KFunc);
       
   143 
       
   144 	int fds[2];
       
   145     char buf[128];
       
   146     
       
   147     TInt err = pipe (fds);
       
   148     
       
   149     if ( err < 0)
       
   150         {
       
   151          INFO_PRINTF1(_L("Pipe Error!"));        
       
   152          return err;
       
   153         }
       
   154     
       
   155     err = read(fds[1], buf, 128); // read on the write desc
       
   156     
       
   157     if(err < 0 && errno == EBADF)
       
   158     	{
       
   159        err = KErrNone;
       
   160     	}
       
   161     else
       
   162     	{
       
   163        INFO_PRINTF1( KFunc());
       
   164 	   err = KErrGeneral;
       
   165     	}
       
   166     
       
   167     close(fds[0]);
       
   168     close(fds[1]);    
       
   169     INFO_PRINTF1( KFunc());
       
   170     return err;
       
   171 	}
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CTestPipe::PipeWriteRead
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 TInt CTestPipe::PipeWriteRead(  )
       
   178 	{
       
   179 	_LIT(KFunc, "PipeWriteRead");
       
   180 	INFO_PRINTF1(KFunc);
       
   181 	int fd[2];
       
   182     const char* bufW = "somejunk";
       
   183     char bufR[128];
       
   184     
       
   185     TInt err = pipe (fd);
       
   186     
       
   187     if ( err < 0)
       
   188         {
       
   189          INFO_PRINTF1( _L("Pipe Error!"));        
       
   190          return err;
       
   191         }
       
   192     err = write(fd[1], bufW, 9); 
       
   193     
       
   194     if(err < 0)
       
   195     	{
       
   196        	INFO_PRINTF1(_L("Write Error!"));
       
   197        	close(fd[0]);
       
   198        	close(fd[1]);
       
   199        	return err;
       
   200     	}
       
   201     
       
   202     err = read(fd[0], bufR, 128 );
       
   203     
       
   204     if(err < 0)
       
   205     	{
       
   206        INFO_PRINTF1(_L("Read Error!"));
       
   207        close(fd[0]);
       
   208        close(fd[1]);
       
   209        return err;
       
   210     	}
       
   211     
       
   212     if(strcmp(bufW, bufR) != 0)
       
   213     	{
       
   214     	err = KErrGeneral;
       
   215     	INFO_PRINTF1( KFunc());
       
   216     	}
       
   217     else
       
   218     	{
       
   219     	err = KErrNone;
       
   220     	}
       
   221     close(fd[0]);
       
   222     close(fd[1]);    
       
   223     return err;
       
   224 	}
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CTestPipe::PipeCheckWriteFd
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 TInt CTestPipe::PipeCheckWriteFd(  )
       
   231 	{
       
   232 	_LIT(KFunc, "PipeCheckFd");
       
   233 	INFO_PRINTF1(KFunc);
       
   234 	int fds[2];
       
   235     const char* buf = "somejunk";
       
   236     
       
   237     TInt err = pipe (fds);
       
   238     
       
   239     if ( err < 0)
       
   240         {
       
   241          INFO_PRINTF1 (_L("Pipe() Error!"));        
       
   242          return err;
       
   243         }
       
   244     
       
   245     TInt saved_fd = fds[1];
       
   246     fds[1] = 99; // change value of the returned write fd
       
   247     
       
   248     err = write(fds[1], buf, 9);
       
   249     
       
   250     if(err < 0 && errno == EBADF)
       
   251     	{
       
   252        	err = KErrNone;
       
   253     	}
       
   254     else
       
   255     	{
       
   256 		INFO_PRINTF1(_L("Write Error!")) ;      
       
   257 	    err = KErrGeneral;
       
   258     	}
       
   259     
       
   260     close(fds[0]);
       
   261     close(saved_fd);        
       
   262     return err;
       
   263 	}
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CTestPipe::PipeCheckReadFd
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 TInt CTestPipe::PipeCheckReadFd(  )
       
   270 	{
       
   271 	_LIT(KFunc, "PipeCheckReadFd");
       
   272 	INFO_PRINTF1(KFunc);
       
   273 	int fds[2];
       
   274     char buf[128];
       
   275         
       
   276     TInt err = pipe (fds);
       
   277     if ( err < 0)
       
   278         {
       
   279          INFO_PRINTF1( _L("Pipe() Error!"));        
       
   280          return err;
       
   281         }
       
   282     
       
   283     TInt saved_fd = fds[0];    
       
   284     fds[0] = 99; // change value of the returned read fd
       
   285     
       
   286     err = read(fds[0], buf, 128);
       
   287     
       
   288     if(err < 0 && errno == EBADF)
       
   289     	{
       
   290        err = KErrNone;
       
   291     	}
       
   292     else
       
   293     	{
       
   294        INFO_PRINTF1 (_L("Read Error!"));
       
   295 	   err = KErrGeneral;
       
   296     	}
       
   297 
       
   298     close(saved_fd);
       
   299     close(fds[1]);        
       
   300     return err;
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CTestPipe::PopenPipeCommandRead
       
   305 // -----------------------------------------------------------------------------
       
   306 // Tests the popen() by creating a child process and reading message from it.
       
   307 // -----------------------------------------------------------------------------
       
   308 TInt CTestPipe::PopenPipeCommandRead()
       
   309 	{	
       
   310 	_LIT(KCommand,	"Command");		
       
   311 	TBufC<MAX_SIZE> bcommand;
       
   312 	TPtrC pcommand = bcommand;	
       
   313 	
       
   314 	/*read the command from data file*/
       
   315 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   316 	
       
   317 	TBuf8<MAX_SIZE> buf;
       
   318 	buf.Copy(pcommand);	
       
   319 	char* command=(char*)buf.Ptr();
       
   320 	command[buf.Length()]='\0';
       
   321 	char mode='r';
       
   322 	
       
   323 	char readbuf[100];
       
   324 	FILE* fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   325 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   326 	
       
   327 	/*read data into the buffer using the file pointer*/
       
   328 	fgets(readbuf, sizeof (readbuf), fp);			
       
   329 	
       
   330 	/*close the opened pipe*/
       
   331 	TInt ret=pclose(fp);
       
   332 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));	
       
   333 		
       
   334 	return EPass;
       
   335 	}
       
   336 	
       
   337 // -----------------------------------------------------------------------------
       
   338 // CTestPipe::MultiplePopenPipeCommandRead
       
   339 // -----------------------------------------------------------------------------
       
   340 TInt CTestPipe::MultiplePopenPipeCommandRead()
       
   341 	{	
       
   342 	_LIT(KCommand,	"Command1");		
       
   343 	TBufC<MAX_SIZE> bcommand;
       
   344 	TPtrC pcommand = bcommand;	
       
   345 	
       
   346 	/*read the command from data file*/
       
   347 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   348 	
       
   349 	TBuf8<MAX_SIZE> buf;
       
   350 	buf.Copy(pcommand);	
       
   351 	char* command=(char*)buf.Ptr();
       
   352 	command[buf.Length()]='\0';
       
   353 	char mode='r';
       
   354 	
       
   355 	char readbuf[100];
       
   356 	FILE* fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   357 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   358 	
       
   359 	/*read data into the buffer using the file pointer*/
       
   360 	fgets(readbuf, sizeof (readbuf), fp);			
       
   361 	
       
   362 	/*close the opened pipe*/
       
   363 	TInt ret=pclose(fp);
       
   364 	
       
   365 	fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   366 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   367 	
       
   368 	/*read data into the buffer using the file pointer*/
       
   369 	fgets(readbuf, sizeof (readbuf), fp);				
       
   370 
       
   371 	/*close the opened pipe*/
       
   372 	ret=pclose(fp);
       
   373 	
       
   374 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));	
       
   375 	
       
   376 	fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   377 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   378 	
       
   379 	/*read data into the buffer using the file pointer*/
       
   380 	fgets(readbuf, sizeof (readbuf), fp);				
       
   381 	
       
   382 	/*close the opened pipe*/
       
   383 	ret=pclose(fp);
       
   384 
       
   385 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));	
       
   386 		
       
   387 	fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   388 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   389 	
       
   390 	/*read data into the buffer using the file pointer*/
       
   391 	fgets(readbuf, sizeof (readbuf), fp);			
       
   392 	
       
   393 	/*close the opened pipe*/
       
   394 	ret=pclose(fp);
       
   395 	
       
   396 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));		
       
   397 	
       
   398 	fp = popen(command, &mode); /*create a child process and a pipe for reading*/	
       
   399 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   400 	
       
   401 	/*read data into the buffer using the file pointer*/
       
   402 	fgets(readbuf, sizeof (readbuf), fp);				
       
   403 	
       
   404 	/*close the opened pipe*/
       
   405 	ret=pclose(fp);
       
   406 	
       
   407 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));		
       
   408 	return EPass;
       
   409 	}	
       
   410 	
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CTestPipe::PopenPipeOEExeRead
       
   414 // -----------------------------------------------------------------------------
       
   415 // tests popen for a OE exe(entry thru main()).
       
   416 TInt CTestPipe::PopenPipeOEExeRead()
       
   417 	{	
       
   418 	_LIT(KCommand2,	"Command2");		
       
   419 	TBufC<MAX_SIZE> bcommand2;
       
   420 	TPtrC pcommand2 = bcommand2;	
       
   421 	
       
   422 	/*read the command from data file*/
       
   423 	GetStringFromConfig(ConfigSection(),KCommand2, pcommand2);
       
   424 	
       
   425 	TBuf8<MAX_SIZE> buf2;
       
   426 	buf2.Copy(pcommand2);	
       
   427 	char* command2=(char*)buf2.Ptr();
       
   428 	command2[buf2.Length()]='\0';
       
   429 	char mode='r';	
       
   430 	
       
   431 	char readbuf2[100];
       
   432 	FILE* fp = popen(command2, &mode); /*create a child process and a pipe for reading*/	
       
   433 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   434 	
       
   435 	/*read data into the buffer using the file pointer*/
       
   436 	fgets(readbuf2, sizeof (readbuf2), fp);		
       
   437 	
       
   438 	/*close the opened pipe*/
       
   439 	TInt ret=pclose(fp);
       
   440 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));	
       
   441 	
       
   442 	return EPass;
       
   443 	}
       
   444 	
       
   445 
       
   446 // -----------------------------------------------------------------------------
       
   447 // CTestPipe::PopenPipeInvalidPathRead
       
   448 // -----------------------------------------------------------------------------
       
   449 // tests popen for executable not in //sys//bin
       
   450 
       
   451 TInt CTestPipe::PopenPipeInvalidPathRead()
       
   452 	{	
       
   453 	_LIT(KCommand3,	"Command3");		
       
   454 	TBufC<MAX_SIZE> bcommand3;
       
   455 	TPtrC pcommand3 = bcommand3;	
       
   456 	
       
   457 	/*read the command from data file*/
       
   458 	GetStringFromConfig(ConfigSection(),KCommand3, pcommand3);
       
   459 	
       
   460 	TBuf8<MAX_SIZE> buf3;
       
   461 	buf3.Copy(pcommand3);	
       
   462 	char* command3=(char*)buf3.Ptr();
       
   463 	command3[buf3.Length()]='\0';
       
   464 	char mode='r';	
       
   465 		
       
   466 	FILE* fp = popen(command3, &mode); /*create a child process and a pipe for reading*/	
       
   467 	TEST_PIPE_COND( (fp==NULL), _L("popen()..."));
       
   468 	
       
   469 	/*close the opened pipe*/
       
   470 	TInt ret=pclose(fp);
       
   471 	TEST_PIPE_VAL(ret,-1, _L("pclose()..."));	
       
   472 
       
   473 	return EPass;
       
   474 	}
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // CTestPipe::PopenPipeInvalidCommandAndModeRead
       
   478 // -----------------------------------------------------------------------------	
       
   479 // tests popen with invalid arguments
       
   480 
       
   481 TInt CTestPipe::PopenPipeInvalidCommand()
       
   482 	{
       
   483     /*tests the invalid command*/
       
   484 	_LIT(KInvalidCmd,	"InvalidCommand");			
       
   485 	TBufC<MAX_SIZE> invcommand;
       
   486 	TPtrC pinvcommand = invcommand;			
       
   487 	GetStringFromConfig(ConfigSection(),KInvalidCmd, pinvcommand);	
       
   488 
       
   489 	TBuf8<MAX_SIZE> bufinv;
       
   490 	bufinv.Copy(pinvcommand);	
       
   491 	char* ptrinvcommand=(char*)bufinv.Ptr();
       
   492 	ptrinvcommand[bufinv.Length()]='\0';
       
   493 	char mode='r';
       
   494 	
       
   495 	FILE* fp=popen(ptrinvcommand, &mode); /*create a child process and a pipe for reading*/	
       
   496 	TEST_PIPE_COND(fp==NULL, _L("popen()..."));
       
   497 
       
   498 	TInt ret=pclose(fp);
       
   499 	TEST_PIPE_VAL(ret,-1, _L("pclose()..."));	
       
   500 	
       
   501 	return EPass;
       
   502 	}	
       
   503 
       
   504 // -----------------------------------------------------------------------------
       
   505 // CTestPipe::PopenPipeInvalidMode
       
   506 // -----------------------------------------------------------------------------
       
   507 TInt CTestPipe::PopenPipeInvalidMode()
       
   508 	{
       
   509 	_LIT(KCommand1,	"Command1");		
       
   510 	TBufC<MAX_SIZE> bcommand1;
       
   511 	TPtrC pcommand1 = bcommand1;	
       
   512 	
       
   513 	/*read the command from data file*/
       
   514 	GetStringFromConfig(ConfigSection(),KCommand1, pcommand1);
       
   515 	
       
   516 	TBuf8<MAX_SIZE> buf1;
       
   517 	buf1.Copy(pcommand1);	
       
   518 	char* command1=(char*)buf1.Ptr();
       
   519 	command1[buf1.Length()]='\0';
       
   520 	char* rwmode="x";
       
   521 	
       
   522 	FILE* fp = popen(command1, rwmode); /*create a child process and a pipe for reading*/	
       
   523 	TEST_PIPE_COND( (fp==NULL), _L("popen()..."));
       
   524 	
       
   525     TInt ret=pclose(fp);
       
   526 	TEST_PIPE_VAL(ret,-1, _L("pclose()..."));		
       
   527 	
       
   528 	return EPass;
       
   529 	}
       
   530 
       
   531 // -----------------------------------------------------------------------------
       
   532 // CTestPipe::PopenPipeWrite
       
   533 // -----------------------------------------------------------------------------
       
   534 // test popen's write operation
       
   535 
       
   536 TInt CTestPipe::PopenPipeWrite()
       
   537 	{
       
   538 	_LIT(KCommand4,	"Command4");		
       
   539 	TBufC<MAX_SIZE> bcommand4;
       
   540 	TPtrC pcommand4 = bcommand4;	
       
   541 	
       
   542 	/*read the command from data file*/
       
   543 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand4, pcommand4);
       
   544 	
       
   545 	TBuf8<MAX_SIZE> buf4;
       
   546 	buf4.Copy(pcommand4);	
       
   547 	char* command4=(char*)buf4.Ptr();
       
   548 	command4[buf4.Length()]='\0';
       
   549 	char mode='w';
       
   550 	
       
   551 	char *writebuf="Message from parent\n";
       
   552 	FILE* fp = popen(command4, &mode); /*create a child process and a pipe for writing*/	
       
   553 	TEST_PIPE_COND( (fp!=NULL), _L("popen()..."));
       
   554 	
       
   555 	/*write data into the buffer using the file pointer*/
       
   556 	fputs(writebuf, fp);			
       
   557 
       
   558 	fflush(fp);	
       
   559 	/*close the opened pipe*/
       
   560 	TInt ret=pclose(fp);
       
   561 	TEST_PIPE_VAL(ret,0, _L("pclose()..."));	
       
   562 		
       
   563 	return EPass;
       
   564 	}
       
   565 
       
   566 void* ReadData(void *data)
       
   567 	{	
       
   568 	DATA* dataptr = (DATA*)data;
       
   569 
       
   570 	if(dataptr)
       
   571 		{
       
   572 		char* cptr = (char*)calloc(dataptr->write_size, sizeof(char));
       
   573 					
       
   574 		FILE* ptr=fdopen(dataptr->fds[0], "r"); 					
       
   575 		dataptr->read_size=fread(cptr, sizeof(char), dataptr->write_size, ptr);
       
   576 				
       
   577 		free(cptr);
       
   578 		fclose(ptr);
       
   579 		}
       
   580 	return (void*)0;	
       
   581 	}
       
   582 
       
   583 // -----------------------------------------------------------------------------
       
   584 // CTestPipe::PopenBlockingRead
       
   585 // -----------------------------------------------------------------------------
       
   586 TInt CTestPipe::PopenBlockingRead()
       
   587 	{
       
   588 	DATA data;
       
   589 	pthread_t threadID = 0;
       
   590 	pthread_attr_t threadAttr;
       
   591 	pthread_attr_init( &threadAttr );		
       
   592 	
       
   593 	int fds[2];
       
   594     TInt err=pipe (fds);
       
   595     TEST_PIPE_VAL(err,0, _L("pipe().."));
       
   596     	
       
   597 	char* ptr="Message to child";
       
   598 	data.write_size = strlen(ptr);
       
   599 	data.fds[0]=fds[0];
       
   600 	data.fds[1]=fds[1];
       
   601 	
       
   602 	err=pthread_create( &threadID, &threadAttr, ReadData, (void*)&data);
       
   603 	TEST_PIPE_VAL(err,0, _L("pthread_create()..."));
       
   604 	
       
   605 	size_t ndata=write(fds[1], ptr, strlen(ptr)-5);
       
   606 	TEST_PIPE_VAL(ndata,(strlen(ptr)-5), _L("write()..."));
       
   607 	
       
   608 	err=pthread_create( &threadID, &threadAttr, ReadData, (void*)&data);
       
   609 	TEST_PIPE_VAL(err,0, _L("pthread_create()..."));
       
   610 	
       
   611 	ptr="Another message from the parent to the child";
       
   612 	ndata=write(fds[1], ptr, strlen(ptr));		
       
   613 	TEST_PIPE_VAL(ndata,(strlen(ptr)), _L("write()..."));	
       
   614 
       
   615     close(fds[0]);
       
   616     close(fds[1]);
       
   617     		
       
   618 	return EPass;
       
   619 	}
       
   620 		
       
   621 // -----------------------------------------------------------------------------
       
   622 // CTestPipe::Popen3PipeCommandRead
       
   623 // -----------------------------------------------------------------------------
       
   624 // test popen's read operation
       
   625 
       
   626 TInt CTestPipe::Popen3PipeCommandRead()
       
   627 	{
       
   628 	_LIT(KCommand,	"Command");		
       
   629 	TBufC<MAX_SIZE> bcommand;
       
   630 	TPtrC pcommand = bcommand;	
       
   631 	
       
   632 	/*read command from the data file*/
       
   633 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   634 	
       
   635 	TBuf8<40> buf;
       
   636 	buf.Copy(pcommand);	
       
   637 	char* command=(char*)buf.Ptr();
       
   638 	command[buf.Length()]='\0';
       
   639 
       
   640 	char readdata[15];
       
   641 	
       
   642 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   643 	
       
   644 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   645 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   646 
       
   647 	/*read data and error from the child*/		
       
   648 	size_t bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   649 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   650 
       
   651 	return EPass;				
       
   652 	}
       
   653 
       
   654 // -----------------------------------------------------------------------------
       
   655 // CTestPipe::MultiplePopen3PipeCommandRead
       
   656 // -----------------------------------------------------------------------------
       
   657 TInt CTestPipe::MultiplePopen3PipeCommandRead()
       
   658 	{
       
   659 	_LIT(KCommand,	"Command1");		
       
   660 	TBufC<MAX_SIZE> bcommand;
       
   661 	TPtrC pcommand = bcommand;	
       
   662 	
       
   663 	/*read command from the data file*/
       
   664 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   665 	
       
   666 	TBuf8<MAX_SIZE> buf;
       
   667 	buf.Copy(pcommand);	
       
   668 	char* command=(char*)buf.Ptr();
       
   669 	command[buf.Length()]='\0';
       
   670 
       
   671 	char readdata[15];
       
   672 	
       
   673 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   674 	
       
   675 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   676 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   677 
       
   678 	/*read data and error from the child*/		
       
   679 	size_t bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   680 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   681 
       
   682 	childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   683 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   684 
       
   685 	/*read data and error from the child*/		
       
   686 	bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   687 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   688 
       
   689 	childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   690 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   691 
       
   692 	/*read data and error from the child*/		
       
   693 	bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   694 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   695 
       
   696 	childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   697 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   698 
       
   699 	/*read data and error from the child*/		
       
   700 	bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   701 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   702 	
       
   703 	return EPass;				
       
   704 	}
       
   705 
       
   706 // -----------------------------------------------------------------------------
       
   707 // CTestPipe::Popen3PipeOEExeRead
       
   708 // -----------------------------------------------------------------------------
       
   709 TInt CTestPipe::Popen3PipeOEExeRead()
       
   710 	{
       
   711 	_LIT(KCommand,	"Command2");		
       
   712 	TBufC<MAX_SIZE> bcommand;
       
   713 	TPtrC pcommand = bcommand;	
       
   714 	
       
   715 	/*read command from the data file*/
       
   716 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   717 	
       
   718 	TBuf8<MAX_SIZE> buf;
       
   719 	buf.Copy(pcommand);	
       
   720 	char* command=(char*)buf.Ptr();
       
   721 	command[buf.Length()]='\0';
       
   722 
       
   723 	char readdata[15];
       
   724 	
       
   725 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   726 	
       
   727 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   728 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   729 
       
   730 	/*read data and error from the child*/		
       
   731 	size_t bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   732 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));	
       
   733 	
       
   734 	return EPass;					
       
   735 	}
       
   736 
       
   737 // -----------------------------------------------------------------------------
       
   738 // CTestPipe::Popen3PipeInvalidPathRead
       
   739 // -----------------------------------------------------------------------------
       
   740 TInt CTestPipe::Popen3PipeInvalidPathRead()
       
   741 	{
       
   742 	_LIT(KCommand,	"Command3");		
       
   743 	TBufC<MAX_SIZE> bcommand;
       
   744 	TPtrC pcommand = bcommand;	
       
   745 	
       
   746 	/*read command from the data file*/
       
   747 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   748 	
       
   749 	TBuf8<MAX_SIZE> buf;
       
   750 	buf.Copy(pcommand);	
       
   751 	char* command=(char*)buf.Ptr();
       
   752 	command[buf.Length()]='\0';
       
   753 	
       
   754 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   755 	
       
   756 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   757 	TEST_PIPE_VAL(childid,-1, _L("popen3()..."));
       
   758 
       
   759 	return EPass;					
       
   760 	}
       
   761 
       
   762 // -----------------------------------------------------------------------------
       
   763 // CTestPipe::Popen3PipeInvalidCommandAndModeRead
       
   764 // -----------------------------------------------------------------------------
       
   765 TInt CTestPipe::Popen3PipeInvalidCommand()
       
   766 	{
       
   767 	_LIT(KCommand,	"InvalidCommand");		
       
   768 	TBufC<MAX_SIZE> bcommand;
       
   769 	TPtrC pcommand = bcommand;	
       
   770 	
       
   771 	/*read command from the data file*/
       
   772 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   773 	
       
   774 	TBuf8<MAX_SIZE> buf;
       
   775 	buf.Copy(pcommand);	
       
   776 	char* command=(char*)buf.Ptr();
       
   777 	command[buf.Length()]='\0';
       
   778 
       
   779 	char readdata[15];
       
   780 	
       
   781 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   782 	
       
   783 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   784 	TEST_PIPE_VAL(childid,-1, _L("popen3()..."));
       
   785 
       
   786 	/*read data and error from the child*/		
       
   787 	size_t bytes_read=read(fds[1],readdata, sizeof (readdata));
       
   788 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));
       
   789 			
       
   790 	return EPass;					
       
   791 	}
       
   792 
       
   793 // -----------------------------------------------------------------------------
       
   794 // CTestPipe::TestProcessPopen3ChitChat
       
   795 // -----------------------------------------------------------------------------
       
   796 // Creates a child process(using popen3) and tests the message transfer between parent and 
       
   797 // the child using the file descriptors
       
   798 //
       
   799 TInt CTestPipe::TestProcessPopen3ChitChat()
       
   800 	{
       
   801 	_LIT(KCommand,	"CommandChitChat");		
       
   802 	TBufC<MAX_SIZE> bcommand;
       
   803 	TPtrC pcommand = bcommand;	
       
   804 	
       
   805 	/*read command from the data file*/
       
   806 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   807 	
       
   808 	TBuf8<40> buf;
       
   809 	buf.Copy(pcommand);	
       
   810 	char* command=(char*)buf.Ptr();
       
   811 	command[buf.Length()]='\0';
       
   812 
       
   813 	char writedata[MAX_SIZE];	
       
   814 	char readdata[MAX_SIZE];
       
   815 	char readerror[MAX_SIZE];
       
   816 	
       
   817 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   818 	
       
   819 	strcpy(writedata, "Message from parent\n");
       
   820 	size_t num_bytes=strlen(writedata);
       
   821 	size_t bytes_written, bytes_read, bytes_error;
       
   822 
       
   823 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   824 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   825 			
       
   826 	/*write to the child*/
       
   827 	bytes_written=write(fds[0],writedata, num_bytes);
       
   828 	TEST_PIPE_VAL( bytes_written, num_bytes, _L("write message to child..."));
       
   829 
       
   830 	/*read data and error from the child*/		
       
   831 	bytes_read=read(fds[1],readdata,num_bytes);
       
   832 	TEST_PIPE_COND(bytes_read>0, _L("read message from child..."))
       
   833 	
       
   834 	bytes_error=read(fds[2], readerror, MAX_SIZE);		
       
   835 	TEST_PIPE_COND(bytes_error>0, _L("read error from child ..."))	
       
   836 	
       
   837 	TInt status=-1;	
       
   838 	TEST_PIPE_VAL( waitpid(childid, &status, WUNTRACED), childid, _L("waitpid()..."));
       
   839 	
       
   840 	return EPass;
       
   841 	}
       
   842 
       
   843 
       
   844 // -----------------------------------------------------------------------------
       
   845 // CTestPipe::Popen3ReadWriteTest
       
   846 // -----------------------------------------------------------------------------
       
   847 // Creates a child process(using popen3) and tests the message transfer between parent and 
       
   848 // the child using the file descriptors and also checks if the data recieved is not reapeated
       
   849 // after 512 bytes. In regard to DEF113501
       
   850 //
       
   851 
       
   852 TInt CTestPipe::Popen3ReadWriteTest()
       
   853 	{
       
   854 	_LIT(KCommand,	"Command6");		
       
   855 	TBufC<MAX_SIZE> bcommand;
       
   856 	TPtrC pcommand = bcommand;	
       
   857 	TInt ret = KErrNone;
       
   858 	
       
   859 	/*read command from the data file*/
       
   860 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   861 	
       
   862 	TBuf8<40> buf;
       
   863 	buf.Copy(pcommand);	
       
   864 	char* command=(char*)buf.Ptr();
       
   865 	command[buf.Length()]='\0';
       
   866 
       
   867 	
       
   868 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
   869 	int i,a1;
       
   870 	char tmp;
       
   871 	wchar_t buffer[1000];
       
   872 	char *data = "START_Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead_\
       
   873 					_Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead_\
       
   874 					_Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead_\
       
   875 					_Datalongerthan512repeateswithpiperead__Datalongerthan512repeateswithpiperead__";
       
   876 	size_t bytes_written;	
       
   877 
       
   878 	TInt childid= popen3( command,NULL, NULL, fds); /*create a child*/	
       
   879 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
   880 			
       
   881 	bytes_written=write(fds[0],data, 568);
       
   882 	INFO_PRINTF2(_L("Pipe written: %d"), bytes_written);
       
   883 	TEST_PIPE_COND((bytes_written==568), _L("write() to the pipe..."));
       
   884 	
       
   885 	i = 0;
       
   886       do
       
   887       {
       
   888       	a1=read(fds[1],&tmp,1);
       
   889       	buffer[++i] = tmp;
       
   890       }while (a1 !=0);
       
   891 
       
   892       if(buffer[513]==buffer[1])
       
   893       	{
       
   894       	INFO_PRINTF1(_L("Repeated data after 512 bytes.. Test case failed"));
       
   895       	ret = KErrGeneral;
       
   896       	}
       
   897       	
       
   898       else
       
   899       	INFO_PRINTF1(_L("Non-Repeated data read from pipe.. Test case passed"));
       
   900       	
       
   901       buffer[i] = '\0';
       
   902       write(1,buffer,i);
       
   903 	return ret;
       
   904 	}
       
   905 
       
   906 
       
   907 // -----------------------------------------------------------------------------
       
   908 // CTestPipe::TestSystem
       
   909 // -----------------------------------------------------------------------------
       
   910 // Tests the system interface by passing valid and valid arguments
       
   911 
       
   912 TInt CTestPipe::TestSystem()
       
   913 	{	
       
   914 	_LIT(KCommand,	"Command");		
       
   915 	TBufC<40> bcommand;
       
   916 	TPtrC pcommand = bcommand;	
       
   917 	
       
   918 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
   919 	
       
   920 	TBuf8<40> buf;
       
   921 	buf.Copy(pcommand);	
       
   922 	char* command=(char*)buf.Ptr();
       
   923 	command[buf.Length()]='\0';
       
   924 
       
   925 	/*should return zero for a NULL command*/
       
   926 	TEST_PIPE_COND((command && system(command)) || (!command && !system(command)), _L("system()..."));
       
   927 	
       
   928 	command=NULL;
       
   929 	TEST_PIPE_COND((!command && system(command)), _L("system()..."));
       
   930 	
       
   931 	return EPass;
       
   932 	}
       
   933 
       
   934 //*Start: Test cases for DEF111452*//	
       
   935 TInt CTestPipe::Testwsystem_LongName()
       
   936 {
       
   937 	int ret=EFail;
       
   938 	const wchar_t* exepath= L"testforlongnametestforlongnametestforlongnametestforlongname\
       
   939 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   940 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   941 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   942 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   943 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
   944 	
       
   945 	if((wsystem(exepath)==-1) && 
       
   946 			errno==ENAMETOOLONG)
       
   947 		{
       
   948 		ret=EPass;	
       
   949 		}
       
   950 	else
       
   951 		{
       
   952 		ret=EFail;	
       
   953 		}
       
   954 	
       
   955 	return ret;
       
   956 }
       
   957 
       
   958 TInt CTestPipe::Testsystem_LongName()
       
   959 {
       
   960 	int ret=EFail;
       
   961 	const char* exepath="testforlongnametestforlongnametestforlongnametestforlongname\
       
   962 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   963 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   964 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   965 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   966 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
   967 	
       
   968 	if((system(exepath)==-1) && 
       
   969 			errno==ENAMETOOLONG)
       
   970 		{
       
   971 		ret=EPass;	
       
   972 		}
       
   973 	else
       
   974 		{
       
   975 		ret=EFail;	
       
   976 		}
       
   977 	
       
   978 	return ret;
       
   979 }
       
   980 
       
   981 TInt CTestPipe::Testpopen_LongName()
       
   982 {
       
   983 	int ret=EFail;
       
   984 	const char* exepath="testforlongnametestforlongnametestforlongnametestforlongname\
       
   985 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   986 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   987 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   988 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
   989 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
   990 	
       
   991 	if((popen(exepath, "r")==NULL) && 
       
   992 			errno==ENAMETOOLONG)
       
   993 		{
       
   994 		ret=EPass;	
       
   995 		}
       
   996 	else
       
   997 		{
       
   998 		ret=EFail;	
       
   999 		}
       
  1000 	
       
  1001 	return ret;
       
  1002 }
       
  1003 
       
  1004 TInt CTestPipe::Testwpopen_LongName()
       
  1005 {
       
  1006 	int ret=EFail;
       
  1007 	const wchar_t* exepath=L"testforlongnametestforlongnametestforlongnametestforlongname\
       
  1008 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1009 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1010 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1011 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1012 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
  1013 	
       
  1014 	if((wpopen(exepath, L"r")==NULL) && 
       
  1015 			errno==ENAMETOOLONG)
       
  1016 		{
       
  1017 		ret=EPass;	
       
  1018 		}
       
  1019 	else
       
  1020 		{
       
  1021 		ret=EFail;	
       
  1022 		}
       
  1023 	
       
  1024 	return ret;
       
  1025 }
       
  1026 
       
  1027 TInt CTestPipe::Testpopen3_LongName()
       
  1028 {
       
  1029 	int ret=EFail;
       
  1030 	int fds[3];
       
  1031 	const char* exepath="testforlongnametestforlongnametestforlongnametestforlongname\
       
  1032 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1033 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1034 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1035 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1036 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
  1037 	
       
  1038 	if((popen3(exepath, NULL, NULL, fds)==-1) && 
       
  1039 			errno==ENAMETOOLONG)
       
  1040 		{
       
  1041 		ret=EPass;	
       
  1042 		}
       
  1043 	else
       
  1044 		{
       
  1045 		ret=EFail;	
       
  1046 		}
       
  1047 	
       
  1048 	return ret;
       
  1049 }
       
  1050 
       
  1051 TInt CTestPipe::Testwpopen3_LongName()
       
  1052 {
       
  1053 	int ret=EFail;
       
  1054 	int fds[3];
       
  1055 	const wchar_t* exepath=L"testforlongnametestforlongnametestforlongnametestforlongname\
       
  1056 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1057 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1058 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1059 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname\
       
  1060 	testforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongnametestforlongname.exe";
       
  1061 	
       
  1062 	if((wpopen3(exepath, NULL, NULL, fds)==-1) && 
       
  1063 			errno==ENAMETOOLONG)
       
  1064 		{
       
  1065 		ret=EPass;	
       
  1066 		}
       
  1067 	else
       
  1068 		{
       
  1069 		ret=EFail;	
       
  1070 		}
       
  1071 	
       
  1072 	return ret;
       
  1073 }
       
  1074 //*End: Test cases for DEF111452*//	
       
  1075 
       
  1076 
       
  1077 // -----------------------------------------------------------------------------
       
  1078 // CTestPipe::Lseekpipetest
       
  1079 // -----------------------------------------------------------------------------
       
  1080 // Tests the lseek function on the write descriptor
       
  1081 
       
  1082 TInt CTestPipe::Lseekpipetest()
       
  1083 	{
       
  1084 	int fds[2];
       
  1085     const char* buf = "somedata";
       
  1086       
       
  1087     TInt err = pipe (fds);
       
  1088     
       
  1089     if ( err < 0)
       
  1090         {
       
  1091          INFO_PRINTF1( _L("pipe() failed"));        
       
  1092          return err;
       
  1093         }
       
  1094     TInt len = strlen(buf);
       
  1095     err = write(fds[1], buf, len+1); // write on the read desc
       
  1096         
       
  1097     if(err < 0 )
       
  1098     	{
       
  1099        	INFO_PRINTF1(_L("Pipe write error"));
       
  1100        	}
       
  1101 	else
       
  1102 		{
       
  1103 		INFO_PRINTF1(_L("Write Success!"));
       
  1104 		}
       
  1105 	close(fds[1]);
       
  1106 	
       
  1107 	//int ret = fcntl(fds[0],F_SETFD, 1);
       
  1108 	//seek with the read descriptor
       
  1109 	
       
  1110 	int nCnt= lseek(fds[0],SEEK_SET, 0);
       
  1111 	if (nCnt < 0)
       
  1112 		{
       
  1113 		if(errno == ESPIPE )
       
  1114 			{
       
  1115 			INFO_PRINTF1(_L("lseek failure"));	
       
  1116 			}
       
  1117 		}
       
  1118 	
       
  1119 	char buf1[MAX_SIZE];
       
  1120 	TBuf<MAX_SIZE> dispBuf;
       
  1121     
       
  1122 	err = read(fds[0], buf1, 100 /* len*/ ); // read on the pipe
       
  1123 	buf1[len] = '\0';
       
  1124 	len = strlen(buf1);
       
  1125     for (TInt j =0; j<len;j++)
       
  1126     	{
       
  1127     	dispBuf.Append(buf1[j]);
       
  1128     	}
       
  1129     
       
  1130     if(err < 0 )
       
  1131     	{
       
  1132        	INFO_PRINTF1(_L("Pipe read error"));
       
  1133     	}
       
  1134     else
       
  1135     	{
       
  1136     	INFO_PRINTF2(_L("Pipe read: %S"), &dispBuf);
       
  1137         err = KErrNone;
       
  1138     	}
       
  1139 	close(fds[0]);
       
  1140 	return EPass;	
       
  1141  
       
  1142 	}
       
  1143 
       
  1144 // -----------------------------------------------------------------------------
       
  1145 // CTestPipe::OpenMaxfdPipetest
       
  1146 // -----------------------------------------------------------------------------
       
  1147 // Tests the maximum number of file descriptors that can be opened		
       
  1148 TInt CTestPipe::OpenMaxfdPipetest()
       
  1149 	{
       
  1150 	int fds[2];
       
  1151 	int fdarr[MAX_SIZE];
       
  1152     const char* buf = "somedata";  
       
  1153     TInt i = 0;
       
  1154     TInt j = 0;
       
  1155       do
       
  1156     	{
       
  1157        	TInt err = pipe (fds);
       
  1158        	if ( err < 0)
       
  1159     		{
       
  1160      		INFO_PRINTF1( _L("pipe() failed")); 
       
  1161      		break;
       
  1162     		}
       
  1163     	else
       
  1164     		{
       
  1165     		//Check the descriptors
       
  1166     		//write with the write descriptor
       
  1167     		TInt err = write(fds[1], buf, strlen(buf)+1); // write on the  desc
       
  1168     		TEST_PIPE_COND((err != -1),_L("pipe write"));
       
  1169     
       
  1170 		    //Read descriptor
       
  1171 		    char buf1[10];
       
  1172 			err = read(fds[0], buf1,5); // read on the pipe
       
  1173 			TEST_PIPE_COND((err != -1),_L("pipe read"));
       
  1174     		}
       
  1175     	fdarr[j++]=fds[0];
       
  1176     	fdarr[j++]=fds[1];
       
  1177     	i++;
       
  1178 	    }while(i<21/*MAX_SIZE*/);
       
  1179 	
       
  1180 	INFO_PRINTF2(_L("Maximum File descriptors opened: %d "), 2*i);
       
  1181 	
       
  1182 	INFO_PRINTF1(_L("Creating pipe"));
       
  1183 	TInt err = pipe (fds);
       
  1184     if ( err < 0)
       
  1185     	{
       
  1186      	INFO_PRINTF1( _L("pipe() failed")); 
       
  1187      	}
       
  1188     else
       
  1189     	{
       
  1190     	INFO_PRINTF1( _L("pipe() success"));	
       
  1191     	}
       
  1192     
       
  1193     //Close the descriptors
       
  1194 	for(TInt k =0; k < j ; k++)
       
  1195 		{
       
  1196 		close(fdarr[k]);
       
  1197 		}
       
  1198 	return EPass;
       
  1199 	}
       
  1200 
       
  1201 // -----------------------------------------------------------------------------
       
  1202 // Function Name:SimultaneousOpenfdPipetest
       
  1203 // Description:Tests the maximum number of file descriptors that can be opened		
       
  1204 // Return : EPass if operation is successful , EFail otherwise
       
  1205 // -----------------------------------------------------------------------------
       
  1206 TInt CTestPipe::SimultaneousOpenfdPipetest()
       
  1207 	{
       
  1208 	int fds[2];
       
  1209 	int fdarr[MAX_SIZE];
       
  1210 	TInt i;
       
  1211 	TInt j = 0;
       
  1212 	for (i = 0; i < 1024 ; i++)
       
  1213 		{
       
  1214 		int err = pipe (fds);
       
  1215     	if ( err < 0)
       
  1216     		{
       
  1217      		INFO_PRINTF1( _L("pipe() failed"));        
       
  1218      		break;
       
  1219     		}
       
  1220     	fdarr[j++]=fds[0];
       
  1221     	fdarr[j++]=fds[1];
       
  1222 		}
       
  1223 	//Close the descriptors
       
  1224 	for(int k =0; k < j; k++)
       
  1225 		{
       
  1226 		close(fdarr[k]);
       
  1227 		}
       
  1228 	
       
  1229     INFO_PRINTF2(_L("Descriptors open  :%d"), 2*i);
       
  1230     return EPass;
       
  1231 	}
       
  1232 
       
  1233 // -----------------------------------------------------------------------------
       
  1234 // Function Name:FopenMaxPopenTest
       
  1235 // Description:Tests the maximum number of file descriptors that can be opened
       
  1236 // 			    using popen()
       
  1237 // Return:	EPass if operation is successful , EFail otherwise		
       
  1238 // -----------------------------------------------------------------------------
       
  1239 TInt CTestPipe::FopenMaxPopenTest()
       
  1240 	{
       
  1241 	FILE * fd[MAX_SIZE];
       
  1242 	FILE* fp1;
       
  1243 	
       
  1244 	_LIT(KCommand1,	"Command1");		
       
  1245 	TPtrC pcommand1 ;	
       
  1246 	
       
  1247 	/*read the command from data file*/
       
  1248 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand1, pcommand1);
       
  1249 	
       
  1250 	TBuf8<MAX_SIZE> buf1;
       
  1251 	buf1.Copy(pcommand1);	
       
  1252 	char* command1=(char*)buf1.Ptr();
       
  1253 	command1[buf1.Length()]='\0';
       
  1254 	char mode = 'w';
       
  1255 	
       
  1256 	int i ;
       
  1257 	for (i = 0 ; i < MAX_SIZE ; i++)
       
  1258 		{
       
  1259 		fp1 = popen(command1, &mode);
       
  1260 		if ( fp1 == NULL)
       
  1261 			{
       
  1262 			int n = errno;
       
  1263 			if (n== EMFILE)
       
  1264 				{
       
  1265 				INFO_PRINTF1(_L("Error: EMFILE-The mode parameter is invalid."));
       
  1266 				}
       
  1267 			else if( n== EINVAL)
       
  1268 				{
       
  1269 				INFO_PRINTF1(_L("Error: EINVAL-No more files or streams can be created."));
       
  1270 				}
       
  1271 			break;
       
  1272 			}
       
  1273 		INFO_PRINTF1(_L("popen() success"));
       
  1274 		char *writebuf="Message from parent\n";
       
  1275 		int ret = fputs(writebuf, fp1);	
       
  1276 		if (ret < 0)
       
  1277 			{
       
  1278 			break;
       
  1279 			}
       
  1280 		fflush(fp1);
       
  1281 		fd[i] = fp1;
       
  1282 		}
       
  1283 	for (int k=0; k < i;k++)
       
  1284 		{
       
  1285 		fclose(fd[k]);
       
  1286 		}
       
  1287 	INFO_PRINTF2(_L("No of streams open :%d"), i);
       
  1288 	return EPass;	
       
  1289 	}
       
  1290 
       
  1291 // -----------------------------------------------------------------------------
       
  1292 // Function Name:FopenMaxPopen3Test
       
  1293 // Description:Tests the maximum number of file descriptors that can be opened
       
  1294 // 			    using popen3()
       
  1295 // Return:	EPass if operation is successful , EFail otherwise		
       
  1296 // -----------------------------------------------------------------------------
       
  1297 TInt CTestPipe::FopenMaxPopen3Test()
       
  1298 	{
       
  1299 	_LIT(KCommand,	"Command1");		
       
  1300 	TBufC<MAX_SIZE> bcommand;
       
  1301 	TPtrC pcommand = bcommand;	
       
  1302 	int fdarr[MAX_SIZE];
       
  1303 	/*read command from the data file*/
       
  1304 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
  1305 	
       
  1306 	TBuf8<MAX_SIZE> buf;
       
  1307 	buf.Copy(pcommand);	
       
  1308 	char* command=(char*)buf.Ptr();
       
  1309 	command[buf.Length()]='\0';
       
  1310 
       
  1311 	int i;
       
  1312 	int j=0;
       
  1313 	int fds[3]; /*3 file descriptors, to be returned by popen3*/
       
  1314 	for (i = 0 ; i < MAX_SIZE ; i++)
       
  1315 		{
       
  1316 		TInt childid= popen3(command,NULL, NULL, fds); /*create a child*/
       
  1317 		if (childid == -1)
       
  1318 			{
       
  1319 			TInt n = errno;
       
  1320 			if (n== EMFILE)
       
  1321 				{
       
  1322 				INFO_PRINTF1(_L("Error EMFILE"));
       
  1323 				}
       
  1324 			else if( n== EINVAL)
       
  1325 				{
       
  1326 				INFO_PRINTF1(_L("Error EINVAL"));
       
  1327 				}
       
  1328 			break;
       
  1329 			}
       
  1330 		else
       
  1331 			{
       
  1332 			char *writebuf="Message from parent\n";
       
  1333 			FILE *fp = fdopen(fds[0],"w");
       
  1334 			fputs(writebuf, fp);
       
  1335 			}
       
  1336 		fdarr[j++] = fds[0];
       
  1337 		fdarr[j++] = fds[1];
       
  1338 		fdarr[j++] = fds[2];
       
  1339 		}
       
  1340 	for (int k=0;k<j;k++)
       
  1341 		{
       
  1342 		close(fdarr[k]);
       
  1343 		}
       
  1344 	INFO_PRINTF2(_L("No. of successful calls to popen3:%d"), i);
       
  1345 	return EPass;
       
  1346 	}
       
  1347 	
       
  1348 // -----------------------------------------------------------------------------
       
  1349 // Function Name:TestEnvPopen3
       
  1350 // Description:Tests the maximum number of file descriptors that can be opened
       
  1351 // 			    using popen3()
       
  1352 // Return:	EPass if operation is successful , EFail otherwise		
       
  1353 // -----------------------------------------------------------------------------
       
  1354 TInt CTestPipe::TestEnvPopen3()
       
  1355 	{
       
  1356 	_LIT(KCommand,	"Command5");
       
  1357 	TBufC<MAX_SIZE> bcommand;
       
  1358 	TPtrC pcommand = bcommand;	
       
  1359 	/*read command from the data file*/
       
  1360 	TBool alright = GetStringFromConfig(ConfigSection(),KCommand, pcommand);
       
  1361 	
       
  1362 	TBuf8<MAX_SIZE> buf;
       
  1363 	buf.Copy(pcommand);	
       
  1364 	char* command=(char*)buf.Ptr();
       
  1365 	command[buf.Length()]='\0';
       
  1366 		
       
  1367 	char readdata[MAX_SIZE];
       
  1368 	char **env1p;
       
  1369 	env1p = new char*[2];
       
  1370 	
       
  1371 	env1p[0]= new char[15];
       
  1372 	strcpy(env1p[0],"user = \\home");
       
  1373 	
       
  1374 
       
  1375 	env1p[1]= 0;
       
  1376 		
       
  1377 	TInt fds[3];
       
  1378 	TInt childid= popen3(command, NULL, env1p, fds); /*create a child*/
       
  1379 
       
  1380 	TEST_PIPE_COND((childid!=-1), _L("popen3()..."));
       
  1381 	
       
  1382 	/*read data and error from the child*/		
       
  1383 	size_t bytes_read=read(fds[1],readdata, sizeof (readdata));
       
  1384 	
       
  1385 	TBuf<MAX_SIZE> buf1;
       
  1386 	TInt len = strlen(readdata);
       
  1387 	TInt i;
       
  1388 	for (i=0; i< bytes_read;i++)
       
  1389 		{
       
  1390 		buf1.Append(readdata[i]);
       
  1391 		}
       
  1392 	INFO_PRINTF2(_L("Data read:' %S '"), &buf1);
       
  1393 	TEST_PIPE_COND( (bytes_read>0), _L("read()..."));
       
  1394 	return EPass;	
       
  1395 	}
       
  1396 
       
  1397 // -----------------------------------------------------------------------------
       
  1398 // Function Name:TestPipeWaitForRead
       
  1399 // Description:Tests whether the code waits until there is no data in the pipe.
       
  1400 // Return:	EPass if operation is successful , EFail otherwise		
       
  1401 // -----------------------------------------------------------------------------
       
  1402 TInt CTestPipe::TestPipeWaitForData()
       
  1403 	{
       
  1404 	fd_set read_fds;
       
  1405 	char pipeName[256];
       
  1406 	sprintf(pipeName,"c:\\telnetd_pipe");
       
  1407     unlink(pipeName);
       
  1408 	if (mkfifo(pipeName, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH) < 0)
       
  1409 		{
       
  1410 		INFO_PRINTF1(_L("Failed to create FIFO special file\n"));
       
  1411 		return EFail;
       
  1412 		}
       
  1413 
       
  1414 	TInt pipe = open(pipeName, O_RDWR | O_NONBLOCK);
       
  1415 	if(pipe<0)
       
  1416 		{
       
  1417 		INFO_PRINTF1(_L("Failed to create Pipe\n"));
       
  1418 		return EFail;
       
  1419 		}
       
  1420 
       
  1421 		FD_ZERO(&read_fds);
       
  1422 		FD_SET(pipe, &read_fds);
       
  1423 
       
  1424 		TInt ret=select(pipe+1, &read_fds, NULL, NULL, NULL); 
       
  1425 		if(ret)
       
  1426 			{
       
  1427 			INFO_PRINTF1(_L("Select returned unexpectedly\n"));
       
  1428 			return EFail;
       
  1429 			}
       
  1430 		return EPass;
       
  1431 		}
       
  1432 
       
  1433 //  End of File