1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include <cppunit/simpleheader.hxx>
28 #include <osl/process.h>
29 #include <rtl/ustring.hxx>
30 #include <unistd.h>
31 #include <signal.h>
32 
33 #ifdef WNT
34 	const rtl::OUString IMAGE_NAME = rtl::OUString::createFromAscii("ojpx.exe");
35 #else
36 	const rtl::OUString IMAGE_NAME = rtl::OUString::createFromAscii("ojpx");
37 #endif
38 
39 const rtl::OUString CWD        = rtl::OUString::createFromAscii(".");
40 
41 //------------------------------
42 //
43 //------------------------------
44 
45 class Test_osl_Process : public CppUnit::TestFixture
46 {
47 public:
48 
49     /*-------------------------------------
50         Start a process and join with this
51         process specify a timeout so that
52         osl_joinProcessWithTimeout returns
53         osl_Process_E_TimedOut
54      -------------------------------------*/
55 
test_osl_joinProcessWithTimeout_timeout_failure()56     void test_osl_joinProcessWithTimeout_timeout_failure()
57     {
58         oslProcess process;
59         oslProcessError osl_error = osl_executeProcess(
60             IMAGE_NAME.pData,
61             NULL,
62             0,
63             osl_Process_NORMAL,
64             osl_getCurrentSecurity(),
65             CWD.pData,
66             NULL,
67             0,
68             &process);
69 
70         CPPUNIT_ASSERT_MESSAGE
71         (
72             "osl_createProcess failed",
73             osl_error == osl_Process_E_None
74         );
75 
76         TimeValue timeout;
77         timeout.Seconds = 1;
78         timeout.Nanosec = 0;
79 
80         osl_error = osl_joinProcessWithTimeout(process, &timeout);
81 
82         CPPUNIT_ASSERT_MESSAGE
83         (
84             "osl_joinProcessWithTimeout returned without timeout failure",
85             osl_Process_E_TimedOut == osl_error
86         );
87 
88         osl_error = osl_terminateProcess(process);
89 
90         CPPUNIT_ASSERT_MESSAGE
91         (
92             "osl_terminateProcess failed",
93             osl_error == osl_Process_E_None
94         );
95 
96         osl_freeProcessHandle(process);
97     }
98 
99     /*-------------------------------------
100         Start a process and join with this
101         process specify a timeout so that
102         osl_joinProcessWithTimeout returns
103         osl_Process_E_None
104      -------------------------------------*/
105 
test_osl_joinProcessWithTimeout_without_timeout_failure()106     void test_osl_joinProcessWithTimeout_without_timeout_failure()
107     {
108         oslProcess process;
109         oslProcessError osl_error = osl_executeProcess(
110             IMAGE_NAME.pData,
111             NULL,
112             0,
113             osl_Process_NORMAL,
114             osl_getCurrentSecurity(),
115             CWD.pData,
116             NULL,
117             0,
118             &process);
119 
120         CPPUNIT_ASSERT_MESSAGE
121         (
122             "osl_createProcess failed",
123             osl_error == osl_Process_E_None
124         );
125 
126         TimeValue timeout;
127         timeout.Seconds = 10;
128         timeout.Nanosec = 0;
129 
130         osl_error = osl_joinProcessWithTimeout(process, &timeout);
131 
132         CPPUNIT_ASSERT_MESSAGE
133         (
134             "osl_joinProcessWithTimeout returned with failure",
135             osl_Process_E_None == osl_error
136         );
137 
138         osl_freeProcessHandle(process);
139     }
140 
141      /*-------------------------------------
142         Start a process and join with this
143         process specify an infinite timeout
144      -------------------------------------*/
145 
test_osl_joinProcessWithTimeout_infinite()146     void test_osl_joinProcessWithTimeout_infinite()
147     {
148         oslProcess process;
149         oslProcessError osl_error = osl_executeProcess(
150             IMAGE_NAME.pData,
151             NULL,
152             0,
153             osl_Process_NORMAL,
154             osl_getCurrentSecurity(),
155             CWD.pData,
156             NULL,
157             0,
158             &process);
159 
160         CPPUNIT_ASSERT_MESSAGE
161         (
162             "osl_createProcess failed",
163             osl_error == osl_Process_E_None
164         );
165 
166         osl_error = osl_joinProcessWithTimeout(process, NULL);
167 
168         CPPUNIT_ASSERT_MESSAGE
169         (
170             "osl_joinProcessWithTimeout returned with failure",
171             osl_Process_E_None == osl_error
172         );
173 
174         osl_freeProcessHandle(process);
175     }
176 
177      /*-------------------------------------
178         Start a process and join with this
179         process using osl_joinProcess
180      -------------------------------------*/
181 
test_osl_joinProcess()182      void test_osl_joinProcess()
183     {
184         oslProcess process;
185         oslProcessError osl_error = osl_executeProcess(
186             IMAGE_NAME.pData,
187             NULL,
188             0,
189             osl_Process_NORMAL,
190             osl_getCurrentSecurity(),
191             CWD.pData,
192             NULL,
193             0,
194             &process);
195 
196         CPPUNIT_ASSERT_MESSAGE
197         (
198             "osl_createProcess failed",
199             osl_error == osl_Process_E_None
200         );
201 
202         osl_error = osl_joinProcess(process);
203 
204         CPPUNIT_ASSERT_MESSAGE
205         (
206             "osl_joinProcess returned with failure",
207             osl_Process_E_None == osl_error
208         );
209 
210         osl_freeProcessHandle(process);
211     }
212 
213     CPPUNIT_TEST_SUITE(Test_osl_Process);
214     CPPUNIT_TEST(test_osl_joinProcessWithTimeout_timeout_failure);
215     CPPUNIT_TEST(test_osl_joinProcessWithTimeout_without_timeout_failure);
216     CPPUNIT_TEST(test_osl_joinProcessWithTimeout_infinite);
217     CPPUNIT_TEST(test_osl_joinProcess);
218     CPPUNIT_TEST_SUITE_END();
219 };
220 
221 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test_osl_Process, "Test_osl_Process");
222 
223 NOADDITIONAL;
224 
225