sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Test code for pipes, using dubious WINS extension for multiple processes... sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include // for MAXPATHLEN sl@0: #include sl@0: #include sl@0: #include // for RDebug::Print sl@0: sl@0: #include sl@0: #include // for multi-threading control sl@0: sl@0: extern "C" { sl@0: #include "CTEST.H" sl@0: } sl@0: sl@0: #ifdef _DEBUG sl@0: #define DebugPrint RDebug::Print sl@0: #else sl@0: inline void DebugPrint(const TDesC&, ...) {} sl@0: #endif sl@0: sl@0: // prepare buf for pipe write/read opereations sl@0: void fillbuf(int seed, char* buf, int buflen) sl@0: { sl@0: int j; sl@0: sleep(seed/3); sl@0: seed += 'A'; sl@0: for (j=0; j 127) sl@0: seed = seed - 127 + 'A'; sl@0: } sl@0: } sl@0: sl@0: int fids[3]; sl@0: sl@0: // Producer, writing buf to pipe(fid) sl@0: void producer(int fid) sl@0: { sl@0: sl@0: test_Data; sl@0: char buf[128]; sl@0: int nbytes; sl@0: sl@0: test_Title("Producer"); sl@0: sl@0: fillbuf(1,buf,sizeof(buf)); sl@0: fflush(stdout); sl@0: sl@0: // Pipe Write test sl@0: nbytes=write(fid, buf, sizeof(buf)); sl@0: sl@0: fflush(stdout); sl@0: test(nbytes==sizeof(buf)); sl@0: sl@0: TProcessId id=RProcess().Id(); sl@0: TUint pid=*REINTERPRET_CAST(TUint*,&id); sl@0: DebugPrint(_L("Process %d: Pipe Write success"), pid); sl@0: sl@0: printf("1.\n\n"); sl@0: return; sl@0: } sl@0: sl@0: #define select_test(fid) \ sl@0: mask=E32SELECT_READ+E32SELECT_WRITE; \ sl@0: err=ioctl(fid,E32IOSELECT,(void*)&mask); sl@0: sl@0: // consumer, doing ioctl test and then read from pipe(fid) sl@0: void consumer(int fid) sl@0: { sl@0: sl@0: test_Data; sl@0: char buf[256]; sl@0: char checkbuf[256]; sl@0: int nbytes; sl@0: int mask=E32SELECT_READ; sl@0: int err=0; sl@0: sl@0: test_Title("Consumer"); sl@0: fillbuf(1,checkbuf,128); sl@0: sl@0: test_Next("Simple read, exactly matching write\n"); sl@0: sl@0: // Pipe Ioctl test sl@0: select_test(fid); sl@0: sl@0: test(err==0); sl@0: test(mask==E32SELECT_READ); sl@0: sl@0: // Pipe Read test sl@0: nbytes=read(fid,buf,128); sl@0: sl@0: test(nbytes==128); sl@0: test(memcmp(buf,checkbuf,128)==0); sl@0: sl@0: TProcessId id=RProcess().Id(); sl@0: TUint pid=*REINTERPRET_CAST(TUint*,&id); sl@0: DebugPrint(_L("Process %d: Pipe Read success"), pid); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-UT-1572 sl@0: @SYMTestCaseDesc Tests for cancellation on pipe operations sl@0: @SYMTestPriority High sl@0: @SYMTestActions Cancel an outstanding pipe operation request sl@0: and check if CPosixIPCSession::PipeCancel() handling correct. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void pipeCancel(int fid) sl@0: { sl@0: test_Data; sl@0: int err=0; sl@0: int mask=E32SELECT_READ+E32SELECT_WRITE; sl@0: sl@0: test_Title("Pipe cancellation"); sl@0: DebugPrint(_L("Pipe cancellation test")); sl@0: TRequestStatus aStatus; sl@0: sl@0: // Issue an ansynchronous pipe operation request sl@0: err=ioctl(fid,E32IOSELECT,(void*)&mask, aStatus); sl@0: sl@0: // Cancel pipe ioctl sl@0: err=ioctl_cancel(fid); sl@0: test(err==0); sl@0: sl@0: TProcessId id=RProcess().Id(); sl@0: TUint pid=*REINTERPRET_CAST(TUint*,&id); sl@0: DebugPrint(_L("Process %d: Pipe Cancellation SUCCESS"), pid); sl@0: sl@0: test_Close(); sl@0: } sl@0: sl@0: void do_parent() sl@0: { sl@0: // testing pipe with writing to child stdin sl@0: producer(fids[0]); sl@0: } sl@0: sl@0: void do_child() sl@0: { sl@0: // testing pipe with reading from stdin sl@0: consumer(0); sl@0: sl@0: // pipe cancellation test sl@0: pipeCancel(0); sl@0: sl@0: // close pipe sl@0: close(0); sl@0: } sl@0: sl@0: sl@0: // Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the sl@0: // plan all along. sl@0: sl@0: int main(int argc, char* argv[]) sl@0: { sl@0: void* proc2; sl@0: sl@0: start_redirection_server(); sl@0: sl@0: if (argc==1) sl@0: { sl@0: // create Child process with read/err pipes sl@0: proc2 = create_process(do_child, "CHILD", "r", fids); sl@0: if (proc2) sl@0: start_process(proc2); sl@0: else sl@0: perror("Failed to start process CHILD: "); sl@0: sl@0: if (proc2) sl@0: { sl@0: int exit; sl@0: sl@0: // parent process sl@0: do_parent(); sl@0: exit=wait_for_process(proc2); sl@0: printf("wait_for_process() returned %d\r\n", exit); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // child process sl@0: do_child(); sl@0: } sl@0: sl@0: // exit here, for the moment crt0 libraries panic sl@0: exit(0); sl@0: sl@0: return KErrNone; sl@0: } sl@0: