Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / workben / testpipe.cxx
blob10ccbe337676040d68d3543434a5858dceee9189
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include <osl/pipe.h>
25 #include <osl/process.h>
26 #include <rtl/ustring.h>
28 // clear Name for the Pipe
29 const char pszPipeName[] = "TestPipe";
30 const char szTestString[] = "This is a test";
31 char szBuffer[256];
33 const char * cp;
34 size_t n;
35 sal_Int32 nChars;
37 // osl specific variables
38 oslPipe Pipe;
39 oslPipe C1Pipe;
40 oslProcess Process = NULL;
41 oslProcessError ProcessError;
42 oslProcessInfo ProcessInfo;
44 void fail( const char * pszText, int retval )
46 fprintf( stderr, "TestPipe Server: %s", pszText );
47 fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
49 if( Process ) osl_freeProcessHandle( Process );
50 exit( retval );
56 * Test the Pipe-Implementation in osl
59 int main (int argc, const char *argv[])
61 // create the Pipe
62 rtl_uString* ustrPipeName=0;
63 rtl_uString* ustrExeName=0;
66 rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
67 rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
69 Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
71 if( !Pipe )
72 fail( "unable to create Pipe.\n",
73 osl_getLastPipeError(NULL));
75 // start client process
76 ProcessError = osl_executeProcess( ustrExeName,
77 NULL,
79 osl_Process_NORMAL,
81 NULL,
82 NULL,
84 &Process );
86 if( ProcessError != osl_Process_E_None )
87 fail( "unable to start client.\n", ProcessError );
89 // wait for connection
90 C1Pipe = osl_acceptPipe( Pipe );
92 if( !C1Pipe )
93 fail( "unable to connect to client.\n",
94 osl_getLastPipeError( Pipe ));
97 if( argc > 1 )
99 cp = argv[1];
100 n = strlen( cp ) + 1;
102 else
104 cp = szTestString;
105 n = sizeof(szTestString);
108 // send TestString to Client
109 nChars = osl_sendPipe( C1Pipe, cp, n );
111 if( nChars < 0 )
112 fail( "unable to write on pipe.\n",
113 osl_getLastPipeError( Pipe ) );
115 // receive data from the server
116 nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
118 if( nChars < 0 )
119 fail( "unable to read from pipe.\n",
120 osl_getLastPipeError( C1Pipe ) );
122 printf( "TestPipe Server: received data: %s.\n", szBuffer );
124 // wait until the client-program terminates
125 ProcessError = osl_joinProcess( Process );
127 if( ProcessError != osl_Process_E_None )
128 fail( "unable to wait for client.\n",
129 ProcessError );
131 // investigate the return-value of the client-program
132 ProcessInfo.Size = sizeof( ProcessInfo );
134 ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
136 if( ProcessError != osl_Process_E_None )
137 fail( "unable to receive return value of client process.\n",
138 ProcessError );
140 if( ProcessInfo.Code != 0 )
141 fail( "client aborted.\n", ProcessInfo.Code );
143 // give the handle for the free client-process
144 osl_freeProcessHandle( Process );
146 // close the pipes
147 osl_releasePipe( C1Pipe );
148 osl_releasePipe( Pipe );
150 printf( "TestPipe Server: test passed.\n" );
151 return 0;
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */