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
2744e8df1fSDamjan Jovanovic #include "gtest/gtest.h"
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
39*e6348c9cSDamjan Jovanovic #ifdef WNT // Windows
40cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
41cdf0e10cSrcweir #include <tchar.h>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include <iostream>
45cdf0e10cSrcweir #include <fstream>
46cdf0e10cSrcweir #include <vector>
47cdf0e10cSrcweir #include <algorithm>
48cdf0e10cSrcweir #include <iterator>
49cdf0e10cSrcweir #include <string>
50cdf0e10cSrcweir
51cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
52cdf0e10cSrcweir const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("osl_process_child.exe");
53cdf0e10cSrcweir #else
54cdf0e10cSrcweir const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("osl_process_child");
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir
57cdf0e10cSrcweir
58cdf0e10cSrcweir //########################################
OUString_to_std_string(const rtl::OUString & oustr)59cdf0e10cSrcweir std::string OUString_to_std_string(const rtl::OUString& oustr)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir rtl::OString ostr = rtl::OUStringToOString(oustr, osl_getThreadTextEncoding());
62cdf0e10cSrcweir return std::string(ostr.getStr());
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
65cdf0e10cSrcweir //########################################
66cdf0e10cSrcweir using namespace osl;
67cdf0e10cSrcweir using namespace rtl;
68cdf0e10cSrcweir
69cdf0e10cSrcweir /** print a UNI_CODE String.
70cdf0e10cSrcweir */
printUString(const::rtl::OUString & str)71cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir rtl::OString aString;
74cdf0e10cSrcweir
7544e8df1fSDamjan Jovanovic printf("#printUString_u# " );
76cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
7744e8df1fSDamjan Jovanovic printf("%s\n", aString.getStr( ) );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir /** get binary Path.
81cdf0e10cSrcweir */
getExecutablePath(void)82cdf0e10cSrcweir inline ::rtl::OUString getExecutablePath( void )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir ::rtl::OUString dirPath;
85cdf0e10cSrcweir osl::Module::getUrlFromAddress( ( void* ) &getExecutablePath, dirPath );
86cdf0e10cSrcweir dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') );
87cdf0e10cSrcweir dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
88cdf0e10cSrcweir dirPath += rtl::OUString::createFromAscii("bin");
89cdf0e10cSrcweir return dirPath;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir //rtl::OUString CWD = getExecutablePath();
93cdf0e10cSrcweir
94cdf0e10cSrcweir //########################################
9544e8df1fSDamjan Jovanovic class Test_osl_joinProcess : public ::testing::Test
96cdf0e10cSrcweir {
9744e8df1fSDamjan Jovanovic protected:
98cdf0e10cSrcweir const OUString join_param_;
99cdf0e10cSrcweir const OUString wait_time_;
100cdf0e10cSrcweir OUString suCWD;
101cdf0e10cSrcweir OUString suExecutableFileURL;
102cdf0e10cSrcweir
103cdf0e10cSrcweir rtl_uString* parameters_[2];
104cdf0e10cSrcweir int parameters_count_;
105cdf0e10cSrcweir
106cdf0e10cSrcweir public:
107cdf0e10cSrcweir
Test_osl_joinProcess()108cdf0e10cSrcweir Test_osl_joinProcess() :
109cdf0e10cSrcweir join_param_(OUString::createFromAscii("-join")),
110cdf0e10cSrcweir wait_time_(OUString::createFromAscii("1")),
111cdf0e10cSrcweir parameters_count_(2)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir parameters_[0] = join_param_.pData;
114cdf0e10cSrcweir parameters_[1] = wait_time_.pData;
115cdf0e10cSrcweir suCWD = getExecutablePath();
116cdf0e10cSrcweir suExecutableFileURL = suCWD;
117cdf0e10cSrcweir suExecutableFileURL += rtl::OUString::createFromAscii("/");
118cdf0e10cSrcweir suExecutableFileURL += EXECUTABLE_NAME;
119cdf0e10cSrcweir }
12044e8df1fSDamjan Jovanovic };
121cdf0e10cSrcweir
122cdf0e10cSrcweir /*-------------------------------------
123cdf0e10cSrcweir Start a process and join with this
124cdf0e10cSrcweir process specify a timeout so that
125cdf0e10cSrcweir osl_joinProcessWithTimeout returns
126cdf0e10cSrcweir osl_Process_E_TimedOut
127cdf0e10cSrcweir -------------------------------------*/
128cdf0e10cSrcweir
TEST_F(Test_osl_joinProcess,osl_joinProcessWithTimeout_timeout_failure)12944e8df1fSDamjan Jovanovic TEST_F(Test_osl_joinProcess, osl_joinProcessWithTimeout_timeout_failure)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir oslProcess process;
132cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
133cdf0e10cSrcweir suExecutableFileURL.pData,
134cdf0e10cSrcweir parameters_,
135cdf0e10cSrcweir parameters_count_,
136cdf0e10cSrcweir osl_Process_NORMAL,
137cdf0e10cSrcweir osl_getCurrentSecurity(),
138cdf0e10cSrcweir suCWD.pData,
139cdf0e10cSrcweir NULL,
140cdf0e10cSrcweir 0,
141cdf0e10cSrcweir &process);
142cdf0e10cSrcweir
14344e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
144cdf0e10cSrcweir
145cdf0e10cSrcweir TimeValue timeout;
146cdf0e10cSrcweir timeout.Seconds = 1;
147cdf0e10cSrcweir timeout.Nanosec = 0;
148cdf0e10cSrcweir
149cdf0e10cSrcweir osl_error = osl_joinProcessWithTimeout(process, &timeout);
150cdf0e10cSrcweir
15144e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_TimedOut == osl_error) << "osl_joinProcessWithTimeout returned without timeout failure";
152cdf0e10cSrcweir
153cdf0e10cSrcweir osl_error = osl_terminateProcess(process);
154cdf0e10cSrcweir
15544e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_terminateProcess failed";
156cdf0e10cSrcweir
157cdf0e10cSrcweir osl_freeProcessHandle(process);
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
160cdf0e10cSrcweir /*-------------------------------------
161cdf0e10cSrcweir Start a process and join with this
162cdf0e10cSrcweir process specify a timeout so that
163cdf0e10cSrcweir osl_joinProcessWithTimeout returns
164cdf0e10cSrcweir osl_Process_E_None
165cdf0e10cSrcweir -------------------------------------*/
166cdf0e10cSrcweir
TEST_F(Test_osl_joinProcess,osl_joinProcessWithTimeout_without_timeout_failure)16744e8df1fSDamjan Jovanovic TEST_F(Test_osl_joinProcess, osl_joinProcessWithTimeout_without_timeout_failure)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir oslProcess process;
170cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
171cdf0e10cSrcweir suExecutableFileURL.pData,
172cdf0e10cSrcweir parameters_,
173cdf0e10cSrcweir parameters_count_,
174cdf0e10cSrcweir osl_Process_NORMAL,
175cdf0e10cSrcweir osl_getCurrentSecurity(),
176cdf0e10cSrcweir suCWD.pData,
177cdf0e10cSrcweir NULL,
178cdf0e10cSrcweir 0,
179cdf0e10cSrcweir &process);
180cdf0e10cSrcweir
18144e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
182cdf0e10cSrcweir
183cdf0e10cSrcweir TimeValue timeout;
184cdf0e10cSrcweir timeout.Seconds = 10;
185cdf0e10cSrcweir timeout.Nanosec = 0;
186cdf0e10cSrcweir
187cdf0e10cSrcweir osl_error = osl_joinProcessWithTimeout(process, &timeout);
188cdf0e10cSrcweir
18944e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcessWithTimeout returned with failure";
190cdf0e10cSrcweir
191cdf0e10cSrcweir osl_freeProcessHandle(process);
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir /*-------------------------------------
195cdf0e10cSrcweir Start a process and join with this
196cdf0e10cSrcweir process specify an infinite timeout
197cdf0e10cSrcweir -------------------------------------*/
198cdf0e10cSrcweir
TEST_F(Test_osl_joinProcess,osl_joinProcessWithTimeout_infinite)19944e8df1fSDamjan Jovanovic TEST_F(Test_osl_joinProcess, osl_joinProcessWithTimeout_infinite)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir oslProcess process;
202cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
203cdf0e10cSrcweir suExecutableFileURL.pData,
204cdf0e10cSrcweir parameters_,
205cdf0e10cSrcweir parameters_count_,
206cdf0e10cSrcweir osl_Process_NORMAL,
207cdf0e10cSrcweir osl_getCurrentSecurity(),
208cdf0e10cSrcweir suCWD.pData,
209cdf0e10cSrcweir NULL,
210cdf0e10cSrcweir 0,
211cdf0e10cSrcweir &process);
212cdf0e10cSrcweir
21344e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
214cdf0e10cSrcweir
215cdf0e10cSrcweir osl_error = osl_joinProcessWithTimeout(process, NULL);
216cdf0e10cSrcweir
21744e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcessWithTimeout returned with failure";
218cdf0e10cSrcweir
219cdf0e10cSrcweir osl_freeProcessHandle(process);
220cdf0e10cSrcweir }
221cdf0e10cSrcweir
222cdf0e10cSrcweir /*-------------------------------------
223cdf0e10cSrcweir Start a process and join with this
224cdf0e10cSrcweir process using osl_joinProcess
225cdf0e10cSrcweir -------------------------------------*/
226cdf0e10cSrcweir
TEST_F(Test_osl_joinProcess,osl_joinProcess)22744e8df1fSDamjan Jovanovic TEST_F(Test_osl_joinProcess, osl_joinProcess)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir oslProcess process;
230cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
231cdf0e10cSrcweir suExecutableFileURL.pData,
232cdf0e10cSrcweir parameters_,
233cdf0e10cSrcweir parameters_count_,
234cdf0e10cSrcweir osl_Process_NORMAL,
235cdf0e10cSrcweir osl_getCurrentSecurity(),
236cdf0e10cSrcweir suCWD.pData,
237cdf0e10cSrcweir NULL,
238cdf0e10cSrcweir 0,
239cdf0e10cSrcweir &process);
240cdf0e10cSrcweir
24144e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
242cdf0e10cSrcweir
243cdf0e10cSrcweir osl_error = ::osl_joinProcess(process);
244cdf0e10cSrcweir
24544e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
246cdf0e10cSrcweir
247cdf0e10cSrcweir osl_freeProcessHandle(process);
248cdf0e10cSrcweir }
249cdf0e10cSrcweir
250cdf0e10cSrcweir
251cdf0e10cSrcweir //#########################################################
252cdf0e10cSrcweir
25322076bf1SHerbert Dürr typedef std::vector<std::string> string_container_t;
254cdf0e10cSrcweir typedef string_container_t::const_iterator string_container_const_iter_t;
255cdf0e10cSrcweir typedef string_container_t::iterator string_container_iter_t;
256cdf0e10cSrcweir
257cdf0e10cSrcweir //#########################################################
258cdf0e10cSrcweir class exclude : public std::unary_function<std::string, bool>
259cdf0e10cSrcweir {
260cdf0e10cSrcweir public:
261cdf0e10cSrcweir //------------------------------------------------
exclude(const string_container_t & exclude_list)262cdf0e10cSrcweir exclude(const string_container_t& exclude_list)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir string_container_const_iter_t iter = exclude_list.begin();
265cdf0e10cSrcweir string_container_const_iter_t iter_end = exclude_list.end();
266cdf0e10cSrcweir for (/**/; iter != iter_end; ++iter)
267cdf0e10cSrcweir exclude_list_.push_back(env_var_name(*iter));
268cdf0e10cSrcweir }
269cdf0e10cSrcweir
270cdf0e10cSrcweir //------------------------------------------------
operator ()(const std::string & env_var) const271cdf0e10cSrcweir bool operator() (const std::string& env_var) const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir return (exclude_list_.end() !=
274cdf0e10cSrcweir std::find(
275cdf0e10cSrcweir exclude_list_.begin(),
276cdf0e10cSrcweir exclude_list_.end(),
277cdf0e10cSrcweir env_var_name(env_var)));
278cdf0e10cSrcweir }
279cdf0e10cSrcweir
280cdf0e10cSrcweir private:
281cdf0e10cSrcweir //-------------------------------------------------
282cdf0e10cSrcweir // extract the name from an environment variable
283cdf0e10cSrcweir // that is given in the form "NAME=VALUE"
env_var_name(const std::string & env_var) const284cdf0e10cSrcweir std::string env_var_name(const std::string& env_var) const
285cdf0e10cSrcweir {
286cdf0e10cSrcweir std::string::size_type pos_equal_sign =
287cdf0e10cSrcweir env_var.find_first_of("=");
288cdf0e10cSrcweir
289cdf0e10cSrcweir if (std::string::npos != pos_equal_sign)
290cdf0e10cSrcweir return std::string(env_var, 0, pos_equal_sign);
291cdf0e10cSrcweir
292cdf0e10cSrcweir return std::string();
293cdf0e10cSrcweir }
294cdf0e10cSrcweir
295cdf0e10cSrcweir private:
296cdf0e10cSrcweir string_container_t exclude_list_;
297cdf0e10cSrcweir };
298cdf0e10cSrcweir
299cdf0e10cSrcweir #ifdef WNT
read_parent_environment(string_container_t * env_container)300cdf0e10cSrcweir void read_parent_environment(string_container_t* env_container)
301cdf0e10cSrcweir {
302cdf0e10cSrcweir LPTSTR env = reinterpret_cast<LPTSTR>(GetEnvironmentStrings());
303cdf0e10cSrcweir LPTSTR p = env;
304cdf0e10cSrcweir
305cdf0e10cSrcweir while (size_t l = _tcslen(p))
306cdf0e10cSrcweir {
307cdf0e10cSrcweir env_container->push_back(std::string(p));
308cdf0e10cSrcweir p += l + 1;
309cdf0e10cSrcweir }
310cdf0e10cSrcweir FreeEnvironmentStrings(env);
311cdf0e10cSrcweir }
312cdf0e10cSrcweir #else
313cdf0e10cSrcweir extern char** environ;
read_parent_environment(string_container_t * env_container)314cdf0e10cSrcweir void read_parent_environment(string_container_t* env_container)
315cdf0e10cSrcweir {
316cdf0e10cSrcweir for (int i = 0; NULL != environ[i]; i++)
317cdf0e10cSrcweir env_container->push_back(std::string(environ[i]));
318cdf0e10cSrcweir }
319cdf0e10cSrcweir #endif
320cdf0e10cSrcweir
321cdf0e10cSrcweir //#########################################################
32244e8df1fSDamjan Jovanovic class Test_osl_executeProcess : public ::testing::Test
323cdf0e10cSrcweir {
32444e8df1fSDamjan Jovanovic protected:
325cdf0e10cSrcweir const OUString env_param_;
326cdf0e10cSrcweir
327cdf0e10cSrcweir OUString temp_file_path_;
328cdf0e10cSrcweir rtl_uString* parameters_[2];
329cdf0e10cSrcweir int parameters_count_;
330cdf0e10cSrcweir OUString suCWD;
331cdf0e10cSrcweir OUString suExecutableFileURL;
332cdf0e10cSrcweir
333cdf0e10cSrcweir public:
334cdf0e10cSrcweir
335cdf0e10cSrcweir //------------------------------------------------
336cdf0e10cSrcweir // ctor
Test_osl_executeProcess()337cdf0e10cSrcweir Test_osl_executeProcess() :
338cdf0e10cSrcweir env_param_(OUString::createFromAscii("-env")),
339cdf0e10cSrcweir parameters_count_(2)
340cdf0e10cSrcweir {
341cdf0e10cSrcweir parameters_[0] = env_param_.pData;
342cdf0e10cSrcweir suCWD = getExecutablePath();
343cdf0e10cSrcweir suExecutableFileURL = suCWD;
344cdf0e10cSrcweir suExecutableFileURL += rtl::OUString::createFromAscii("/");
345cdf0e10cSrcweir suExecutableFileURL += EXECUTABLE_NAME;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir
348cdf0e10cSrcweir //------------------------------------------------
SetUp()34944e8df1fSDamjan Jovanovic virtual void SetUp()
350cdf0e10cSrcweir {
351cdf0e10cSrcweir temp_file_path_ = create_temp_file();
352cdf0e10cSrcweir parameters_[1] = temp_file_path_.pData;
353cdf0e10cSrcweir }
354cdf0e10cSrcweir
355cdf0e10cSrcweir //------------------------------------------------
create_temp_file()356cdf0e10cSrcweir OUString create_temp_file()
357cdf0e10cSrcweir {
358cdf0e10cSrcweir OUString temp_file_url;
359cdf0e10cSrcweir FileBase::RC rc = FileBase::createTempFile(0, 0, &temp_file_url);
36044e8df1fSDamjan Jovanovic EXPECT_TRUE(FileBase::E_None == rc) << "createTempFile failed";
361cdf0e10cSrcweir
362cdf0e10cSrcweir OUString temp_file_path;
363cdf0e10cSrcweir rc = FileBase::getSystemPathFromFileURL(temp_file_url, temp_file_path);
36444e8df1fSDamjan Jovanovic EXPECT_TRUE(FileBase::E_None == rc) << "getSystemPathFromFileURL failed";
365cdf0e10cSrcweir
366cdf0e10cSrcweir return temp_file_path;
367cdf0e10cSrcweir }
368cdf0e10cSrcweir
369cdf0e10cSrcweir //------------------------------------------------
read_child_environment(string_container_t * env_container)370cdf0e10cSrcweir void read_child_environment(string_container_t* env_container)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir OString temp_file_name = OUStringToOString(OUString(
373cdf0e10cSrcweir parameters_[1]), osl_getThreadTextEncoding());
374cdf0e10cSrcweir std::ifstream file(temp_file_name.getStr());
375cdf0e10cSrcweir
37644e8df1fSDamjan Jovanovic ASSERT_TRUE(file.is_open()) << "I/O error, cannot open child environment file";
377cdf0e10cSrcweir
378cdf0e10cSrcweir std::string line;
379cdf0e10cSrcweir while (std::getline(file, line))
380cdf0e10cSrcweir env_container->push_back(line);
381cdf0e10cSrcweir }
382cdf0e10cSrcweir
383cdf0e10cSrcweir //------------------------------------------------
dump_env(const string_container_t & env,OUString file_name)384cdf0e10cSrcweir void dump_env(const string_container_t& env, OUString file_name)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir OString fname = OUStringToOString(file_name, osl_getThreadTextEncoding());
387cdf0e10cSrcweir std::ofstream file(fname.getStr());
388cdf0e10cSrcweir std::ostream_iterator<std::string> oi(file, "\n");
389cdf0e10cSrcweir std::copy(env.begin(), env.end(), oi);
390cdf0e10cSrcweir }
391cdf0e10cSrcweir
392cdf0e10cSrcweir //------------------------------------------------
393cdf0e10cSrcweir // environment of the child process that was
394cdf0e10cSrcweir // started. The child process writes his
395cdf0e10cSrcweir // environment into a file
compare_environments()396cdf0e10cSrcweir bool compare_environments()
397cdf0e10cSrcweir {
398cdf0e10cSrcweir string_container_t parent_env;
399cdf0e10cSrcweir read_parent_environment(&parent_env);
400cdf0e10cSrcweir
401cdf0e10cSrcweir string_container_t child_env;
402cdf0e10cSrcweir read_child_environment(&child_env);
403cdf0e10cSrcweir
404cdf0e10cSrcweir return ((parent_env.size() == child_env.size()) &&
405cdf0e10cSrcweir (std::equal(child_env.begin(), child_env.end(), parent_env.begin())));
406cdf0e10cSrcweir }
407cdf0e10cSrcweir
408cdf0e10cSrcweir //------------------------------------------------
409cdf0e10cSrcweir // compare the equal environment parts and the
410cdf0e10cSrcweir // different part of the child environment
compare_merged_environments(const string_container_t & different_env_vars)411cdf0e10cSrcweir bool compare_merged_environments(const string_container_t& different_env_vars)
412cdf0e10cSrcweir {
413cdf0e10cSrcweir string_container_t parent_env;
414cdf0e10cSrcweir read_parent_environment(&parent_env);
415cdf0e10cSrcweir
416cdf0e10cSrcweir //remove the environment variables that we have changed
417cdf0e10cSrcweir //in the child environment from the read parent environment
418cdf0e10cSrcweir parent_env.erase(
419cdf0e10cSrcweir std::remove_if(parent_env.begin(), parent_env.end(), exclude(different_env_vars)),
420cdf0e10cSrcweir parent_env.end());
421cdf0e10cSrcweir
422cdf0e10cSrcweir //read the child environment and exclude the variables that
423cdf0e10cSrcweir //are different
424cdf0e10cSrcweir string_container_t child_env;
425cdf0e10cSrcweir read_child_environment(&child_env);
426cdf0e10cSrcweir
427cdf0e10cSrcweir //partition the child environment into the variables that
428cdf0e10cSrcweir //are different to the parent environment (they come first)
429cdf0e10cSrcweir //and the variables that should be equal between parent
430cdf0e10cSrcweir //and child environment
431cdf0e10cSrcweir string_container_iter_t iter_logical_end =
432cdf0e10cSrcweir std::stable_partition(child_env.begin(), child_env.end(), exclude(different_env_vars));
433cdf0e10cSrcweir
434cdf0e10cSrcweir string_container_t different_child_env_vars(child_env.begin(), iter_logical_end);
435cdf0e10cSrcweir child_env.erase(child_env.begin(), iter_logical_end);
436cdf0e10cSrcweir
437cdf0e10cSrcweir bool common_env_size_equals = (parent_env.size() == child_env.size());
438cdf0e10cSrcweir bool common_env_content_equals = std::equal(child_env.begin(), child_env.end(), parent_env.begin());
439cdf0e10cSrcweir
440cdf0e10cSrcweir bool different_env_size_equals = (different_child_env_vars.size() == different_env_vars.size());
441cdf0e10cSrcweir bool different_env_content_equals =
442cdf0e10cSrcweir std::equal(different_env_vars.begin(), different_env_vars.end(), different_child_env_vars.begin());
443cdf0e10cSrcweir
444cdf0e10cSrcweir return (common_env_size_equals && common_env_content_equals &&
445cdf0e10cSrcweir different_env_size_equals && different_env_content_equals);
446cdf0e10cSrcweir }
44744e8df1fSDamjan Jovanovic };
448cdf0e10cSrcweir
449cdf0e10cSrcweir //------------------------------------------------
450cdf0e10cSrcweir // test that parent and child process have the
451cdf0e10cSrcweir // same environment when osl_executeProcess will
452cdf0e10cSrcweir // be called with out setting new environment
453cdf0e10cSrcweir // variables
TEST_F(Test_osl_executeProcess,osl_execProc_parent_equals_child_environment)45444e8df1fSDamjan Jovanovic TEST_F(Test_osl_executeProcess, osl_execProc_parent_equals_child_environment)
455cdf0e10cSrcweir {
456cdf0e10cSrcweir oslProcess process;
457cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
458cdf0e10cSrcweir suExecutableFileURL.pData,
459cdf0e10cSrcweir parameters_,
460cdf0e10cSrcweir parameters_count_,
461cdf0e10cSrcweir osl_Process_NORMAL,
462cdf0e10cSrcweir NULL,
463cdf0e10cSrcweir suCWD.pData,
464cdf0e10cSrcweir NULL,
465cdf0e10cSrcweir 0,
466cdf0e10cSrcweir &process);
467cdf0e10cSrcweir
46844e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
469cdf0e10cSrcweir
470cdf0e10cSrcweir osl_error = ::osl_joinProcess(process);
471cdf0e10cSrcweir
47244e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
473cdf0e10cSrcweir
474cdf0e10cSrcweir osl_freeProcessHandle(process);
475cdf0e10cSrcweir
47644e8df1fSDamjan Jovanovic ASSERT_TRUE(compare_environments()) << "Parent an child environment not equal";
477cdf0e10cSrcweir }
478cdf0e10cSrcweir
479cdf0e10cSrcweir //------------------------------------------------
480cdf0e10cSrcweir #define ENV1 "PAT=a:\\"
481cdf0e10cSrcweir #define ENV2 "PATHb=b:\\"
482cdf0e10cSrcweir #define ENV3 "Patha=c:\\"
483cdf0e10cSrcweir #define ENV4 "Patha=d:\\"
484cdf0e10cSrcweir
TEST_F(Test_osl_executeProcess,osl_execProc_merged_child_environment)48544e8df1fSDamjan Jovanovic TEST_F(Test_osl_executeProcess, osl_execProc_merged_child_environment)
486cdf0e10cSrcweir {
487cdf0e10cSrcweir rtl_uString* child_env[4];
488cdf0e10cSrcweir OUString env1 = OUString::createFromAscii(ENV1);
489cdf0e10cSrcweir OUString env2 = OUString::createFromAscii(ENV2);
490cdf0e10cSrcweir OUString env3 = OUString::createFromAscii(ENV3);
491cdf0e10cSrcweir OUString env4 = OUString::createFromAscii(ENV4);
492cdf0e10cSrcweir
493cdf0e10cSrcweir child_env[0] = env1.pData;
494cdf0e10cSrcweir child_env[1] = env2.pData;
495cdf0e10cSrcweir child_env[2] = env3.pData;
496cdf0e10cSrcweir child_env[3] = env4.pData;
497cdf0e10cSrcweir
498cdf0e10cSrcweir oslProcess process;
499cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
500cdf0e10cSrcweir suExecutableFileURL.pData,
501cdf0e10cSrcweir parameters_,
502cdf0e10cSrcweir parameters_count_,
503cdf0e10cSrcweir osl_Process_NORMAL,
504cdf0e10cSrcweir NULL,
505cdf0e10cSrcweir suCWD.pData,
506cdf0e10cSrcweir child_env,
507cdf0e10cSrcweir sizeof(child_env)/sizeof(child_env[0]),
508cdf0e10cSrcweir &process);
509cdf0e10cSrcweir
51044e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
511cdf0e10cSrcweir
512cdf0e10cSrcweir osl_error = ::osl_joinProcess(process);
513cdf0e10cSrcweir
51444e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
515cdf0e10cSrcweir
516cdf0e10cSrcweir osl_freeProcessHandle(process);
517cdf0e10cSrcweir
518cdf0e10cSrcweir string_container_t different_child_env_vars;
519cdf0e10cSrcweir different_child_env_vars.push_back(ENV1);
520cdf0e10cSrcweir different_child_env_vars.push_back(ENV2);
521cdf0e10cSrcweir different_child_env_vars.push_back(ENV4);
522cdf0e10cSrcweir
52344e8df1fSDamjan Jovanovic ASSERT_TRUE(compare_merged_environments(different_child_env_vars)) << "osl_execProc_merged_child_environment";
524cdf0e10cSrcweir }
525cdf0e10cSrcweir
TEST_F(Test_osl_executeProcess,osl_execProc_test_batch)52644e8df1fSDamjan Jovanovic TEST_F(Test_osl_executeProcess, osl_execProc_test_batch)
527cdf0e10cSrcweir {
528cdf0e10cSrcweir oslProcess process;
529cdf0e10cSrcweir rtl::OUString suBatch = suCWD + rtl::OUString::createFromAscii("/") + rtl::OUString::createFromAscii("batch.bat");
530cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
531cdf0e10cSrcweir suBatch.pData,
532cdf0e10cSrcweir NULL,
533cdf0e10cSrcweir 0,
534cdf0e10cSrcweir osl_Process_NORMAL,
535cdf0e10cSrcweir NULL,
536cdf0e10cSrcweir suCWD.pData,
537cdf0e10cSrcweir NULL,
538cdf0e10cSrcweir 0,
539cdf0e10cSrcweir &process);
540cdf0e10cSrcweir
54144e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
542cdf0e10cSrcweir
543cdf0e10cSrcweir osl_error = ::osl_joinProcess(process);
544cdf0e10cSrcweir
54544e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
546cdf0e10cSrcweir
547cdf0e10cSrcweir osl_freeProcessHandle(process);
548cdf0e10cSrcweir }
549cdf0e10cSrcweir
TEST_F(Test_osl_executeProcess,osl_execProc_exe_name_in_argument_list)55044e8df1fSDamjan Jovanovic TEST_F(Test_osl_executeProcess, osl_execProc_exe_name_in_argument_list)
551cdf0e10cSrcweir {
552cdf0e10cSrcweir rtl_uString* params[3];
553cdf0e10cSrcweir
554cdf0e10cSrcweir params[0] = suExecutableFileURL.pData;
555cdf0e10cSrcweir params[1] = env_param_.pData;
556cdf0e10cSrcweir params[2] = temp_file_path_.pData;
557cdf0e10cSrcweir oslProcess process;
558cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
559cdf0e10cSrcweir NULL,
560cdf0e10cSrcweir params,
561cdf0e10cSrcweir 3,
562cdf0e10cSrcweir osl_Process_NORMAL,
563cdf0e10cSrcweir NULL,
564cdf0e10cSrcweir suCWD.pData,
565cdf0e10cSrcweir NULL,
566cdf0e10cSrcweir 0,
567cdf0e10cSrcweir &process);
568cdf0e10cSrcweir
56944e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
570cdf0e10cSrcweir
571cdf0e10cSrcweir osl_error = ::osl_joinProcess(process);
572cdf0e10cSrcweir
57344e8df1fSDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
574cdf0e10cSrcweir
575cdf0e10cSrcweir osl_freeProcessHandle(process);
576cdf0e10cSrcweir }
577cdf0e10cSrcweir
main(int argc,char ** argv)57844e8df1fSDamjan Jovanovic int main(int argc, char **argv)
57944e8df1fSDamjan Jovanovic {
58044e8df1fSDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv);
58144e8df1fSDamjan Jovanovic return RUN_ALL_TESTS();
58244e8df1fSDamjan Jovanovic }
583