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