build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / sal / workben / testpipe.cxx
blob2e405aaa81e0020c17f7e5afb306ca4b25abb029
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 );
54 * Test the Pipe-Implementation in osl
57 int main (int argc, const char *argv[])
59 // create the Pipe
60 rtl_uString* ustrPipeName=0;
61 rtl_uString* ustrExeName=0;
63 rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
64 rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
66 Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
68 if( !Pipe )
69 fail( "unable to create Pipe.\n",
70 osl_getLastPipeError(NULL));
72 // start client process
73 ProcessError = osl_executeProcess( ustrExeName,
74 NULL,
76 osl_Process_NORMAL,
78 NULL,
79 NULL,
81 &Process );
83 if( ProcessError != osl_Process_E_None )
84 fail( "unable to start client.\n", ProcessError );
86 // wait for connection
87 C1Pipe = osl_acceptPipe( Pipe );
89 if( !C1Pipe )
90 fail( "unable to connect to client.\n",
91 osl_getLastPipeError( Pipe ));
93 if( argc > 1 )
95 cp = argv[1];
96 n = strlen( cp ) + 1;
98 else
100 cp = szTestString;
101 n = sizeof(szTestString);
104 // send TestString to Client
105 nChars = osl_sendPipe( C1Pipe, cp, n );
107 if( nChars < 0 )
108 fail( "unable to write on pipe.\n",
109 osl_getLastPipeError( Pipe ) );
111 // receive data from the server
112 nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
114 if( nChars < 0 )
115 fail( "unable to read from pipe.\n",
116 osl_getLastPipeError( C1Pipe ) );
118 printf( "TestPipe Server: received data: %s.\n", szBuffer );
120 // wait until the client-program terminates
121 ProcessError = osl_joinProcess( Process );
123 if( ProcessError != osl_Process_E_None )
124 fail( "unable to wait for client.\n",
125 ProcessError );
127 // investigate the return-value of the client-program
128 ProcessInfo.Size = sizeof( ProcessInfo );
130 ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
132 if( ProcessError != osl_Process_E_None )
133 fail( "unable to receive return value of client process.\n",
134 ProcessError );
136 if( ProcessInfo.Code != 0 )
137 fail( "client aborted.\n", ProcessInfo.Code );
139 // give the handle for the free client-process
140 osl_freeProcessHandle( Process );
142 // close the pipes
143 osl_releasePipe( C1Pipe );
144 osl_releasePipe( Pipe );
146 printf( "TestPipe Server: test passed.\n" );
147 return 0;
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */