Bump for 3.6-28
[LibreOffice.git] / sal / workben / testpipe.cxx
blob4b3f1a6a199a7bfd28c3f0b4c44e43ea92838036
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <osl/pipe.h>
36 #include <osl/process.h>
37 #include <rtl/ustring.h>
39 // clear Name for the Pipe
40 const char pszPipeName[] = "TestPipe";
41 const char szTestString[] = "This is a test";
42 char szBuffer[256];
44 const char * cp;
45 size_t n;
46 sal_Int32 nChars;
48 // osl specific variables
49 oslPipe Pipe;
50 oslPipe C1Pipe;
51 oslProcess Process = NULL;
52 oslProcessError ProcessError;
53 oslProcessInfo ProcessInfo;
55 void fail( const char * pszText, int retval )
57 fprintf( stderr, "TestPipe Server: %s", pszText );
58 fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
60 if( Process ) osl_freeProcessHandle( Process );
61 exit( retval );
67 * Test the Pipe-Implementation in osl
70 int main (int argc, const char *argv[])
72 // create the Pipe
73 rtl_uString* ustrPipeName=0;
74 rtl_uString* ustrExeName=0;
77 rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
78 rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
80 Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
82 if( !Pipe )
83 fail( "unable to create Pipe.\n",
84 osl_getLastPipeError(NULL));
86 // start client process
87 ProcessError = osl_executeProcess( ustrExeName,
88 NULL,
90 osl_Process_NORMAL,
92 NULL,
93 NULL,
95 &Process );
97 if( ProcessError != osl_Process_E_None )
98 fail( "unable to start client.\n", ProcessError );
100 // wait for connection
101 C1Pipe = osl_acceptPipe( Pipe );
103 if( !C1Pipe )
104 fail( "unable to connect to client.\n",
105 osl_getLastPipeError( Pipe ));
108 if( argc > 1 )
110 cp = argv[1];
111 n = strlen( cp ) + 1;
113 else
115 cp = szTestString;
116 n = sizeof(szTestString);
119 // send TestString to Client
120 nChars = osl_sendPipe( C1Pipe, cp, n );
122 if( nChars < 0 )
123 fail( "unable to write on pipe.\n",
124 osl_getLastPipeError( Pipe ) );
126 // receive data from the server
127 nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
129 if( nChars < 0 )
130 fail( "unable to read from pipe.\n",
131 osl_getLastPipeError( C1Pipe ) );
133 printf( "TestPipe Server: received data: %s.\n", szBuffer );
135 // wait until the client-program terminates
136 ProcessError = osl_joinProcess( Process );
138 if( ProcessError != osl_Process_E_None )
139 fail( "unable to wait for client.\n",
140 ProcessError );
142 // investigate the return-value of the client-program
143 ProcessInfo.Size = sizeof( ProcessInfo );
145 ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
147 if( ProcessError != osl_Process_E_None )
148 fail( "unable to receive return value of client process.\n",
149 ProcessError );
151 if( ProcessInfo.Code != 0 )
152 fail( "client aborted.\n", ProcessInfo.Code );
154 // give the handle for the free client-process
155 osl_freeProcessHandle( Process );
157 // close the pipes
158 osl_releasePipe( C1Pipe );
159 osl_releasePipe( Pipe );
161 printf( "TestPipe Server: test passed.\n" );
162 return 0;
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */