xref: /aoo42x/main/sal/qa/osl/process/osl_process.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
32*cdf0e10cSrcweir #include <osl/process.h>
33*cdf0e10cSrcweir #include <osl/file.hxx>
34*cdf0e10cSrcweir #include <osl/thread.h>
35*cdf0e10cSrcweir #include <rtl/ustring.hxx>
36*cdf0e10cSrcweir #include <unistd.h>
37*cdf0e10cSrcweir #include <signal.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <stdio.h>
40*cdf0e10cSrcweir #include <stdlib.h>
41*cdf0e10cSrcweir #include <osl/module.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #if ( defined WNT )                     // Windows
44*cdf0e10cSrcweir #include <tools/prewin.h>
45*cdf0e10cSrcweir #	define WIN32_LEAN_AND_MEAN
46*cdf0e10cSrcweir // #	include <windows.h>
47*cdf0e10cSrcweir #   include <tchar.h>
48*cdf0e10cSrcweir #include <tools/postwin.h>
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #include "rtl/allocator.hxx"
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <iostream>
54*cdf0e10cSrcweir #include <fstream>
55*cdf0e10cSrcweir #include <vector>
56*cdf0e10cSrcweir #include <algorithm>
57*cdf0e10cSrcweir #include <iterator>
58*cdf0e10cSrcweir #include <string>
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
61*cdf0e10cSrcweir 	const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("osl_process_child.exe");
62*cdf0e10cSrcweir #else
63*cdf0e10cSrcweir 	const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("osl_process_child");
64*cdf0e10cSrcweir #endif
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir //########################################
68*cdf0e10cSrcweir std::string OUString_to_std_string(const rtl::OUString& oustr)
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     rtl::OString ostr = rtl::OUStringToOString(oustr, osl_getThreadTextEncoding());
71*cdf0e10cSrcweir     return std::string(ostr.getStr());
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir //########################################
75*cdf0e10cSrcweir using namespace osl;
76*cdf0e10cSrcweir using namespace rtl;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir /** print a UNI_CODE String.
79*cdf0e10cSrcweir */
80*cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir 	rtl::OString aString;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	t_print("#printUString_u# " );
85*cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
86*cdf0e10cSrcweir 	t_print("%s\n", aString.getStr( ) );
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir /** get binary Path.
90*cdf0e10cSrcweir */
91*cdf0e10cSrcweir inline ::rtl::OUString getExecutablePath( void )
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	::rtl::OUString dirPath;
94*cdf0e10cSrcweir 	osl::Module::getUrlFromAddress( ( void* ) &getExecutablePath, dirPath );
95*cdf0e10cSrcweir 	dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') );
96*cdf0e10cSrcweir 	dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
97*cdf0e10cSrcweir 	dirPath += rtl::OUString::createFromAscii("bin");
98*cdf0e10cSrcweir 	return dirPath;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir //rtl::OUString CWD = getExecutablePath();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir //########################################
104*cdf0e10cSrcweir class Test_osl_joinProcess : public CppUnit::TestFixture
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     const OUString join_param_;
107*cdf0e10cSrcweir     const OUString wait_time_;
108*cdf0e10cSrcweir     OUString suCWD;
109*cdf0e10cSrcweir     OUString suExecutableFileURL;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     rtl_uString* parameters_[2];
112*cdf0e10cSrcweir     int          parameters_count_;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir public:
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     Test_osl_joinProcess() :
117*cdf0e10cSrcweir         join_param_(OUString::createFromAscii("-join")),
118*cdf0e10cSrcweir         wait_time_(OUString::createFromAscii("1")),
119*cdf0e10cSrcweir         parameters_count_(2)
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         parameters_[0] = join_param_.pData;
122*cdf0e10cSrcweir         parameters_[1] = wait_time_.pData;
123*cdf0e10cSrcweir         suCWD = getExecutablePath();
124*cdf0e10cSrcweir         suExecutableFileURL = suCWD;
125*cdf0e10cSrcweir         suExecutableFileURL += rtl::OUString::createFromAscii("/");
126*cdf0e10cSrcweir         suExecutableFileURL += EXECUTABLE_NAME;
127*cdf0e10cSrcweir     }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     /*-------------------------------------
130*cdf0e10cSrcweir         Start a process and join with this
131*cdf0e10cSrcweir         process specify a timeout so that
132*cdf0e10cSrcweir         osl_joinProcessWithTimeout returns
133*cdf0e10cSrcweir         osl_Process_E_TimedOut
134*cdf0e10cSrcweir      -------------------------------------*/
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     void osl_joinProcessWithTimeout_timeout_failure()
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         oslProcess process;
139*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
140*cdf0e10cSrcweir             suExecutableFileURL.pData,
141*cdf0e10cSrcweir             parameters_,
142*cdf0e10cSrcweir             parameters_count_,
143*cdf0e10cSrcweir             osl_Process_NORMAL,
144*cdf0e10cSrcweir             osl_getCurrentSecurity(),
145*cdf0e10cSrcweir             suCWD.pData,
146*cdf0e10cSrcweir             NULL,
147*cdf0e10cSrcweir             0,
148*cdf0e10cSrcweir             &process);
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
151*cdf0e10cSrcweir         (
152*cdf0e10cSrcweir             "osl_createProcess failed",
153*cdf0e10cSrcweir             osl_error == osl_Process_E_None
154*cdf0e10cSrcweir         );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         TimeValue timeout;
157*cdf0e10cSrcweir         timeout.Seconds = 1;
158*cdf0e10cSrcweir         timeout.Nanosec = 0;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         osl_error = osl_joinProcessWithTimeout(process, &timeout);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
163*cdf0e10cSrcweir         (
164*cdf0e10cSrcweir             "osl_joinProcessWithTimeout returned without timeout failure",
165*cdf0e10cSrcweir             osl_Process_E_TimedOut == osl_error
166*cdf0e10cSrcweir         );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         osl_error = osl_terminateProcess(process);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
171*cdf0e10cSrcweir         (
172*cdf0e10cSrcweir             "osl_terminateProcess failed",
173*cdf0e10cSrcweir             osl_error == osl_Process_E_None
174*cdf0e10cSrcweir         );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         osl_freeProcessHandle(process);
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     /*-------------------------------------
180*cdf0e10cSrcweir         Start a process and join with this
181*cdf0e10cSrcweir         process specify a timeout so that
182*cdf0e10cSrcweir         osl_joinProcessWithTimeout returns
183*cdf0e10cSrcweir         osl_Process_E_None
184*cdf0e10cSrcweir      -------------------------------------*/
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     void osl_joinProcessWithTimeout_without_timeout_failure()
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         oslProcess process;
189*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
190*cdf0e10cSrcweir             suExecutableFileURL.pData,
191*cdf0e10cSrcweir             parameters_,
192*cdf0e10cSrcweir             parameters_count_,
193*cdf0e10cSrcweir             osl_Process_NORMAL,
194*cdf0e10cSrcweir             osl_getCurrentSecurity(),
195*cdf0e10cSrcweir             suCWD.pData,
196*cdf0e10cSrcweir             NULL,
197*cdf0e10cSrcweir             0,
198*cdf0e10cSrcweir             &process);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
201*cdf0e10cSrcweir         (
202*cdf0e10cSrcweir             "osl_createProcess failed",
203*cdf0e10cSrcweir             osl_error == osl_Process_E_None
204*cdf0e10cSrcweir         );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir         TimeValue timeout;
207*cdf0e10cSrcweir         timeout.Seconds = 10;
208*cdf0e10cSrcweir         timeout.Nanosec = 0;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         osl_error = osl_joinProcessWithTimeout(process, &timeout);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
213*cdf0e10cSrcweir         (
214*cdf0e10cSrcweir             "osl_joinProcessWithTimeout returned with failure",
215*cdf0e10cSrcweir             osl_Process_E_None == osl_error
216*cdf0e10cSrcweir         );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         osl_freeProcessHandle(process);
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir      /*-------------------------------------
222*cdf0e10cSrcweir         Start a process and join with this
223*cdf0e10cSrcweir         process specify an infinite timeout
224*cdf0e10cSrcweir      -------------------------------------*/
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     void osl_joinProcessWithTimeout_infinite()
227*cdf0e10cSrcweir     {
228*cdf0e10cSrcweir         oslProcess process;
229*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
230*cdf0e10cSrcweir             suExecutableFileURL.pData,
231*cdf0e10cSrcweir             parameters_,
232*cdf0e10cSrcweir             parameters_count_,
233*cdf0e10cSrcweir             osl_Process_NORMAL,
234*cdf0e10cSrcweir             osl_getCurrentSecurity(),
235*cdf0e10cSrcweir             suCWD.pData,
236*cdf0e10cSrcweir             NULL,
237*cdf0e10cSrcweir             0,
238*cdf0e10cSrcweir             &process);
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
241*cdf0e10cSrcweir         (
242*cdf0e10cSrcweir             "osl_createProcess failed",
243*cdf0e10cSrcweir             osl_error == osl_Process_E_None
244*cdf0e10cSrcweir         );
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         osl_error = osl_joinProcessWithTimeout(process, NULL);
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
249*cdf0e10cSrcweir         (
250*cdf0e10cSrcweir             "osl_joinProcessWithTimeout returned with failure",
251*cdf0e10cSrcweir             osl_Process_E_None == osl_error
252*cdf0e10cSrcweir         );
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         osl_freeProcessHandle(process);
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir      /*-------------------------------------
258*cdf0e10cSrcweir         Start a process and join with this
259*cdf0e10cSrcweir         process using osl_joinProcess
260*cdf0e10cSrcweir      -------------------------------------*/
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir      void osl_joinProcess()
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         oslProcess process;
265*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
266*cdf0e10cSrcweir             suExecutableFileURL.pData,
267*cdf0e10cSrcweir             parameters_,
268*cdf0e10cSrcweir             parameters_count_,
269*cdf0e10cSrcweir             osl_Process_NORMAL,
270*cdf0e10cSrcweir             osl_getCurrentSecurity(),
271*cdf0e10cSrcweir             suCWD.pData,
272*cdf0e10cSrcweir             NULL,
273*cdf0e10cSrcweir             0,
274*cdf0e10cSrcweir             &process);
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
277*cdf0e10cSrcweir         (
278*cdf0e10cSrcweir             "osl_createProcess failed",
279*cdf0e10cSrcweir             osl_error == osl_Process_E_None
280*cdf0e10cSrcweir         );
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir         osl_error = ::osl_joinProcess(process);
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
285*cdf0e10cSrcweir         (
286*cdf0e10cSrcweir             "osl_joinProcess returned with failure",
287*cdf0e10cSrcweir             osl_Process_E_None == osl_error
288*cdf0e10cSrcweir         );
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir         osl_freeProcessHandle(process);
291*cdf0e10cSrcweir     }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test_osl_joinProcess);
294*cdf0e10cSrcweir     CPPUNIT_TEST(osl_joinProcessWithTimeout_timeout_failure);
295*cdf0e10cSrcweir     CPPUNIT_TEST(osl_joinProcessWithTimeout_without_timeout_failure);
296*cdf0e10cSrcweir     CPPUNIT_TEST(osl_joinProcessWithTimeout_infinite);
297*cdf0e10cSrcweir     CPPUNIT_TEST(osl_joinProcess);
298*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
299*cdf0e10cSrcweir };
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir //#########################################################
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir typedef std::vector<std::string, rtl::Allocator<std::string> >  string_container_t;
304*cdf0e10cSrcweir typedef string_container_t::const_iterator string_container_const_iter_t;
305*cdf0e10cSrcweir typedef string_container_t::iterator       string_container_iter_t;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir //#########################################################
308*cdf0e10cSrcweir class exclude : public std::unary_function<std::string, bool>
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir public:
311*cdf0e10cSrcweir     //------------------------------------------------
312*cdf0e10cSrcweir     exclude(const string_container_t& exclude_list)
313*cdf0e10cSrcweir     {
314*cdf0e10cSrcweir         string_container_const_iter_t iter     = exclude_list.begin();
315*cdf0e10cSrcweir         string_container_const_iter_t iter_end = exclude_list.end();
316*cdf0e10cSrcweir         for (/**/; iter != iter_end; ++iter)
317*cdf0e10cSrcweir             exclude_list_.push_back(env_var_name(*iter));
318*cdf0e10cSrcweir     }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     //------------------------------------------------
321*cdf0e10cSrcweir     bool operator() (const std::string& env_var) const
322*cdf0e10cSrcweir     {
323*cdf0e10cSrcweir         return (exclude_list_.end() !=
324*cdf0e10cSrcweir                 std::find(
325*cdf0e10cSrcweir                     exclude_list_.begin(),
326*cdf0e10cSrcweir                     exclude_list_.end(),
327*cdf0e10cSrcweir                     env_var_name(env_var)));
328*cdf0e10cSrcweir     }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir private:
331*cdf0e10cSrcweir     //-------------------------------------------------
332*cdf0e10cSrcweir     // extract the name from an environment variable
333*cdf0e10cSrcweir     // that is given in the form "NAME=VALUE"
334*cdf0e10cSrcweir     std::string env_var_name(const std::string& env_var) const
335*cdf0e10cSrcweir     {
336*cdf0e10cSrcweir         std::string::size_type pos_equal_sign =
337*cdf0e10cSrcweir             env_var.find_first_of("=");
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir         if (std::string::npos != pos_equal_sign)
340*cdf0e10cSrcweir             return std::string(env_var, 0, pos_equal_sign);
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir         return std::string();
343*cdf0e10cSrcweir     }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir private:
346*cdf0e10cSrcweir     string_container_t exclude_list_;
347*cdf0e10cSrcweir };
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir #ifdef WNT
350*cdf0e10cSrcweir     void read_parent_environment(string_container_t* env_container)
351*cdf0e10cSrcweir     {
352*cdf0e10cSrcweir         LPTSTR env = reinterpret_cast<LPTSTR>(GetEnvironmentStrings());
353*cdf0e10cSrcweir         LPTSTR p   = env;
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir         while (size_t l = _tcslen(p))
356*cdf0e10cSrcweir         {
357*cdf0e10cSrcweir             env_container->push_back(std::string(p));
358*cdf0e10cSrcweir             p += l + 1;
359*cdf0e10cSrcweir         }
360*cdf0e10cSrcweir         FreeEnvironmentStrings(env);
361*cdf0e10cSrcweir     }
362*cdf0e10cSrcweir #else
363*cdf0e10cSrcweir     extern char** environ;
364*cdf0e10cSrcweir     void read_parent_environment(string_container_t* env_container)
365*cdf0e10cSrcweir     {
366*cdf0e10cSrcweir         for (int i = 0; NULL != environ[i]; i++)
367*cdf0e10cSrcweir             env_container->push_back(std::string(environ[i]));
368*cdf0e10cSrcweir     }
369*cdf0e10cSrcweir #endif
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir //#########################################################
372*cdf0e10cSrcweir class Test_osl_executeProcess : public CppUnit::TestFixture
373*cdf0e10cSrcweir {
374*cdf0e10cSrcweir     const OUString env_param_;
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir     OUString     temp_file_path_;
377*cdf0e10cSrcweir     rtl_uString* parameters_[2];
378*cdf0e10cSrcweir     int          parameters_count_;
379*cdf0e10cSrcweir     OUString	suCWD;
380*cdf0e10cSrcweir     OUString	suExecutableFileURL;
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir public:
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir     //------------------------------------------------
385*cdf0e10cSrcweir     // ctor
386*cdf0e10cSrcweir     Test_osl_executeProcess() :
387*cdf0e10cSrcweir         env_param_(OUString::createFromAscii("-env")),
388*cdf0e10cSrcweir         parameters_count_(2)
389*cdf0e10cSrcweir     {
390*cdf0e10cSrcweir         parameters_[0] = env_param_.pData;
391*cdf0e10cSrcweir         suCWD = getExecutablePath();
392*cdf0e10cSrcweir         suExecutableFileURL = suCWD;
393*cdf0e10cSrcweir         suExecutableFileURL += rtl::OUString::createFromAscii("/");
394*cdf0e10cSrcweir         suExecutableFileURL += EXECUTABLE_NAME;
395*cdf0e10cSrcweir     }
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir     //------------------------------------------------
398*cdf0e10cSrcweir     virtual void setUp()
399*cdf0e10cSrcweir     {
400*cdf0e10cSrcweir         temp_file_path_ = create_temp_file();
401*cdf0e10cSrcweir         parameters_[1]  = temp_file_path_.pData;
402*cdf0e10cSrcweir     }
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir     //------------------------------------------------
405*cdf0e10cSrcweir     OUString create_temp_file()
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         OUString temp_file_url;
408*cdf0e10cSrcweir         FileBase::RC rc = FileBase::createTempFile(0, 0, &temp_file_url);
409*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("createTempFile failed", FileBase::E_None == rc);
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir         OUString temp_file_path;
412*cdf0e10cSrcweir         rc = FileBase::getSystemPathFromFileURL(temp_file_url, temp_file_path);
413*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("getSystemPathFromFileURL failed", FileBase::E_None == rc);
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir         return temp_file_path;
416*cdf0e10cSrcweir     }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir    //------------------------------------------------
419*cdf0e10cSrcweir     void read_child_environment(string_container_t* env_container)
420*cdf0e10cSrcweir     {
421*cdf0e10cSrcweir         OString temp_file_name = OUStringToOString(OUString(
422*cdf0e10cSrcweir             parameters_[1]), osl_getThreadTextEncoding());
423*cdf0e10cSrcweir         std::ifstream file(temp_file_name.getStr());
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
426*cdf0e10cSrcweir         (
427*cdf0e10cSrcweir             "I/O error, cannot open child environment file",
428*cdf0e10cSrcweir             file.is_open()
429*cdf0e10cSrcweir         );
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir         std::string line;
432*cdf0e10cSrcweir         while (std::getline(file, line))
433*cdf0e10cSrcweir             env_container->push_back(line);
434*cdf0e10cSrcweir     }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir     //------------------------------------------------
437*cdf0e10cSrcweir     void dump_env(const string_container_t& env, OUString file_name)
438*cdf0e10cSrcweir     {
439*cdf0e10cSrcweir         OString fname = OUStringToOString(file_name, osl_getThreadTextEncoding());
440*cdf0e10cSrcweir         std::ofstream file(fname.getStr());
441*cdf0e10cSrcweir         std::ostream_iterator<std::string> oi(file, "\n");
442*cdf0e10cSrcweir 		std::copy(env.begin(), env.end(), oi);
443*cdf0e10cSrcweir     }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir     //------------------------------------------------
446*cdf0e10cSrcweir     // environment of the child process that was
447*cdf0e10cSrcweir     // started. The child process writes his
448*cdf0e10cSrcweir     // environment into a file
449*cdf0e10cSrcweir     bool compare_environments()
450*cdf0e10cSrcweir     {
451*cdf0e10cSrcweir         string_container_t parent_env;
452*cdf0e10cSrcweir         read_parent_environment(&parent_env);
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir         string_container_t child_env;
455*cdf0e10cSrcweir 		read_child_environment(&child_env);
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 		return ((parent_env.size() == child_env.size()) &&
458*cdf0e10cSrcweir 		        (std::equal(child_env.begin(), child_env.end(), parent_env.begin())));
459*cdf0e10cSrcweir     }
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir     //------------------------------------------------
462*cdf0e10cSrcweir     // compare the equal environment parts and the
463*cdf0e10cSrcweir     // different part of the child environment
464*cdf0e10cSrcweir     bool compare_merged_environments(const string_container_t& different_env_vars)
465*cdf0e10cSrcweir     {
466*cdf0e10cSrcweir         string_container_t parent_env;
467*cdf0e10cSrcweir         read_parent_environment(&parent_env);
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir         //remove the environment variables that we have changed
470*cdf0e10cSrcweir         //in the child environment from the read parent environment
471*cdf0e10cSrcweir         parent_env.erase(
472*cdf0e10cSrcweir             std::remove_if(parent_env.begin(), parent_env.end(), exclude(different_env_vars)),
473*cdf0e10cSrcweir             parent_env.end());
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir         //read the child environment and exclude the variables that
476*cdf0e10cSrcweir         //are different
477*cdf0e10cSrcweir         string_container_t child_env;
478*cdf0e10cSrcweir         read_child_environment(&child_env);
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir         //partition the child environment into the variables that
481*cdf0e10cSrcweir         //are different to the parent environment (they come first)
482*cdf0e10cSrcweir         //and the variables that should be equal between parent
483*cdf0e10cSrcweir         //and child environment
484*cdf0e10cSrcweir         string_container_iter_t iter_logical_end =
485*cdf0e10cSrcweir             std::stable_partition(child_env.begin(), child_env.end(), exclude(different_env_vars));
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir         string_container_t different_child_env_vars(child_env.begin(), iter_logical_end);
488*cdf0e10cSrcweir         child_env.erase(child_env.begin(), iter_logical_end);
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir         bool common_env_size_equals    = (parent_env.size() == child_env.size());
491*cdf0e10cSrcweir         bool common_env_content_equals = std::equal(child_env.begin(), child_env.end(), parent_env.begin());
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir 		bool different_env_size_equals    = (different_child_env_vars.size() == different_env_vars.size());
494*cdf0e10cSrcweir 		bool different_env_content_equals =
495*cdf0e10cSrcweir 		    std::equal(different_env_vars.begin(), different_env_vars.end(), different_child_env_vars.begin());
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir         return (common_env_size_equals && common_env_content_equals &&
498*cdf0e10cSrcweir                 different_env_size_equals && different_env_content_equals);
499*cdf0e10cSrcweir     }
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir     //------------------------------------------------
502*cdf0e10cSrcweir     // test that parent and child process have the
503*cdf0e10cSrcweir     // same environment when osl_executeProcess will
504*cdf0e10cSrcweir     // be called with out setting new environment
505*cdf0e10cSrcweir     // variables
506*cdf0e10cSrcweir    void osl_execProc_parent_equals_child_environment()
507*cdf0e10cSrcweir     {
508*cdf0e10cSrcweir         oslProcess process;
509*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
510*cdf0e10cSrcweir             suExecutableFileURL.pData,
511*cdf0e10cSrcweir             parameters_,
512*cdf0e10cSrcweir             parameters_count_,
513*cdf0e10cSrcweir             osl_Process_NORMAL,
514*cdf0e10cSrcweir             NULL,
515*cdf0e10cSrcweir             suCWD.pData,
516*cdf0e10cSrcweir             NULL,
517*cdf0e10cSrcweir             0,
518*cdf0e10cSrcweir             &process);
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
521*cdf0e10cSrcweir         (
522*cdf0e10cSrcweir             "osl_createProcess failed",
523*cdf0e10cSrcweir             osl_error == osl_Process_E_None
524*cdf0e10cSrcweir         );
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir         osl_error = ::osl_joinProcess(process);
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
529*cdf0e10cSrcweir         (
530*cdf0e10cSrcweir             "osl_joinProcess returned with failure",
531*cdf0e10cSrcweir             osl_Process_E_None == osl_error
532*cdf0e10cSrcweir         );
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir         osl_freeProcessHandle(process);
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
537*cdf0e10cSrcweir         (
538*cdf0e10cSrcweir             "Parent an child environment not equal",
539*cdf0e10cSrcweir             compare_environments()
540*cdf0e10cSrcweir         );
541*cdf0e10cSrcweir     }
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir     //------------------------------------------------
544*cdf0e10cSrcweir     #define ENV1 "PAT=a:\\"
545*cdf0e10cSrcweir     #define ENV2 "PATHb=b:\\"
546*cdf0e10cSrcweir     #define ENV3 "Patha=c:\\"
547*cdf0e10cSrcweir     #define ENV4 "Patha=d:\\"
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir     void osl_execProc_merged_child_environment()
550*cdf0e10cSrcweir     {
551*cdf0e10cSrcweir         rtl_uString* child_env[4];
552*cdf0e10cSrcweir         OUString env1 = OUString::createFromAscii(ENV1);
553*cdf0e10cSrcweir         OUString env2 = OUString::createFromAscii(ENV2);
554*cdf0e10cSrcweir         OUString env3 = OUString::createFromAscii(ENV3);
555*cdf0e10cSrcweir         OUString env4 = OUString::createFromAscii(ENV4);
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir         child_env[0] = env1.pData;
558*cdf0e10cSrcweir         child_env[1] = env2.pData;
559*cdf0e10cSrcweir         child_env[2] = env3.pData;
560*cdf0e10cSrcweir         child_env[3] = env4.pData;
561*cdf0e10cSrcweir 
562*cdf0e10cSrcweir         oslProcess process;
563*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
564*cdf0e10cSrcweir             suExecutableFileURL.pData,
565*cdf0e10cSrcweir             parameters_,
566*cdf0e10cSrcweir             parameters_count_,
567*cdf0e10cSrcweir             osl_Process_NORMAL,
568*cdf0e10cSrcweir             NULL,
569*cdf0e10cSrcweir             suCWD.pData,
570*cdf0e10cSrcweir             child_env,
571*cdf0e10cSrcweir             sizeof(child_env)/sizeof(child_env[0]),
572*cdf0e10cSrcweir             &process);
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
575*cdf0e10cSrcweir         (
576*cdf0e10cSrcweir             "osl_createProcess failed",
577*cdf0e10cSrcweir             osl_error == osl_Process_E_None
578*cdf0e10cSrcweir         );
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir         osl_error = ::osl_joinProcess(process);
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
583*cdf0e10cSrcweir         (
584*cdf0e10cSrcweir             "osl_joinProcess returned with failure",
585*cdf0e10cSrcweir             osl_Process_E_None == osl_error
586*cdf0e10cSrcweir         );
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir         osl_freeProcessHandle(process);
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir         string_container_t different_child_env_vars;
591*cdf0e10cSrcweir         different_child_env_vars.push_back(ENV1);
592*cdf0e10cSrcweir         different_child_env_vars.push_back(ENV2);
593*cdf0e10cSrcweir         different_child_env_vars.push_back(ENV4);
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
596*cdf0e10cSrcweir         (
597*cdf0e10cSrcweir             "osl_execProc_merged_child_environment",
598*cdf0e10cSrcweir             compare_merged_environments(different_child_env_vars)
599*cdf0e10cSrcweir         );
600*cdf0e10cSrcweir     }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir     void osl_execProc_test_batch()
603*cdf0e10cSrcweir     {
604*cdf0e10cSrcweir         oslProcess process;
605*cdf0e10cSrcweir         rtl::OUString suBatch = suCWD + rtl::OUString::createFromAscii("/") + rtl::OUString::createFromAscii("batch.bat");
606*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
607*cdf0e10cSrcweir             suBatch.pData,
608*cdf0e10cSrcweir             NULL,
609*cdf0e10cSrcweir             0,
610*cdf0e10cSrcweir             osl_Process_NORMAL,
611*cdf0e10cSrcweir             NULL,
612*cdf0e10cSrcweir             suCWD.pData,
613*cdf0e10cSrcweir             NULL,
614*cdf0e10cSrcweir             0,
615*cdf0e10cSrcweir             &process);
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
618*cdf0e10cSrcweir         (
619*cdf0e10cSrcweir             "osl_createProcess failed",
620*cdf0e10cSrcweir             osl_error == osl_Process_E_None
621*cdf0e10cSrcweir         );
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir         osl_error = ::osl_joinProcess(process);
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
626*cdf0e10cSrcweir         (
627*cdf0e10cSrcweir             "osl_joinProcess returned with failure",
628*cdf0e10cSrcweir             osl_Process_E_None == osl_error
629*cdf0e10cSrcweir         );
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir         osl_freeProcessHandle(process);
632*cdf0e10cSrcweir     }
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir     void osl_execProc_exe_name_in_argument_list()
635*cdf0e10cSrcweir     {
636*cdf0e10cSrcweir         rtl_uString* params[3];
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir         params[0] = suExecutableFileURL.pData;
639*cdf0e10cSrcweir         params[1] = env_param_.pData;
640*cdf0e10cSrcweir         params[2] = temp_file_path_.pData;
641*cdf0e10cSrcweir         oslProcess process;
642*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
643*cdf0e10cSrcweir             NULL,
644*cdf0e10cSrcweir             params,
645*cdf0e10cSrcweir             3,
646*cdf0e10cSrcweir             osl_Process_NORMAL,
647*cdf0e10cSrcweir             NULL,
648*cdf0e10cSrcweir             suCWD.pData,
649*cdf0e10cSrcweir             NULL,
650*cdf0e10cSrcweir             0,
651*cdf0e10cSrcweir             &process);
652*cdf0e10cSrcweir 
653*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
654*cdf0e10cSrcweir         (
655*cdf0e10cSrcweir             "osl_createProcess failed",
656*cdf0e10cSrcweir             osl_error == osl_Process_E_None
657*cdf0e10cSrcweir         );
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir         osl_error = ::osl_joinProcess(process);
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
662*cdf0e10cSrcweir         (
663*cdf0e10cSrcweir             "osl_joinProcess returned with failure",
664*cdf0e10cSrcweir             osl_Process_E_None == osl_error
665*cdf0e10cSrcweir         );
666*cdf0e10cSrcweir 
667*cdf0e10cSrcweir         osl_freeProcessHandle(process);
668*cdf0e10cSrcweir     }
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test_osl_executeProcess);
671*cdf0e10cSrcweir     CPPUNIT_TEST(osl_execProc_parent_equals_child_environment);
672*cdf0e10cSrcweir     CPPUNIT_TEST(osl_execProc_merged_child_environment);
673*cdf0e10cSrcweir     CPPUNIT_TEST(osl_execProc_test_batch);
674*cdf0e10cSrcweir     CPPUNIT_TEST(osl_execProc_exe_name_in_argument_list);
675*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
676*cdf0e10cSrcweir };
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir //#####################################
679*cdf0e10cSrcweir // register test suites
680*cdf0e10cSrcweir //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test_osl_joinProcess,    "Test_osl_joinProcess");
681*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test_osl_executeProcess, "Test_osl_executeProcess");
682*cdf0e10cSrcweir 
683*cdf0e10cSrcweir NOADDITIONAL;
684*cdf0e10cSrcweir 
685