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 "gtest/gtest.h"
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 ::testing::Test
46 {
47 public:
48 };
49 
50 /*-------------------------------------
51     Start a process and join with this
52     process specify a timeout so that
53     osl_joinProcessWithTimeout returns
54     osl_Process_E_TimedOut
55  -------------------------------------*/
56 
TEST_F(Test_osl_Process,test_osl_joinProcessWithTimeout_timeout_failure)57 TEST_F(Test_osl_Process, test_osl_joinProcessWithTimeout_timeout_failure)
58 {
59     oslProcess process;
60     oslProcessError osl_error = osl_executeProcess(
61         IMAGE_NAME.pData,
62         NULL,
63         0,
64         osl_Process_NORMAL,
65         osl_getCurrentSecurity(),
66         CWD.pData,
67         NULL,
68         0,
69         &process);
70 
71     ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
72 
73     TimeValue timeout;
74     timeout.Seconds = 1;
75     timeout.Nanosec = 0;
76 
77     osl_error = osl_joinProcessWithTimeout(process, &timeout);
78 
79     ASSERT_TRUE(osl_Process_E_TimedOut == osl_error) << "osl_joinProcessWithTimeout returned without timeout failure";
80 
81     osl_error = osl_terminateProcess(process);
82 
83     ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_terminateProcess failed";
84 
85     osl_freeProcessHandle(process);
86 }
87 
88 /*-------------------------------------
89     Start a process and join with this
90     process specify a timeout so that
91     osl_joinProcessWithTimeout returns
92     osl_Process_E_None
93  -------------------------------------*/
94 
TEST_F(Test_osl_Process,test_osl_joinProcessWithTimeout_without_timeout_failure)95 TEST_F(Test_osl_Process, test_osl_joinProcessWithTimeout_without_timeout_failure)
96 {
97     oslProcess process;
98     oslProcessError osl_error = osl_executeProcess(
99         IMAGE_NAME.pData,
100         NULL,
101         0,
102         osl_Process_NORMAL,
103         osl_getCurrentSecurity(),
104         CWD.pData,
105         NULL,
106         0,
107         &process);
108 
109     ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
110 
111     TimeValue timeout;
112     timeout.Seconds = 10;
113     timeout.Nanosec = 0;
114 
115     osl_error = osl_joinProcessWithTimeout(process, &timeout);
116 
117     ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcessWithTimeout returned with failure";
118 
119     osl_freeProcessHandle(process);
120 }
121 
122 /*-------------------------------------
123     Start a process and join with this
124     process specify an infinite timeout
125  -------------------------------------*/
126 
TEST_F(Test_osl_Process,test_osl_joinProcessWithTimeout_infinite)127 TEST_F(Test_osl_Process, test_osl_joinProcessWithTimeout_infinite)
128 {
129     oslProcess process;
130     oslProcessError osl_error = osl_executeProcess(
131         IMAGE_NAME.pData,
132         NULL,
133         0,
134         osl_Process_NORMAL,
135         osl_getCurrentSecurity(),
136         CWD.pData,
137         NULL,
138         0,
139         &process);
140 
141     ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
142 
143     osl_error = osl_joinProcessWithTimeout(process, NULL);
144 
145     ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcessWithTimeout returned with failure";
146 
147     osl_freeProcessHandle(process);
148 }
149 
150  /*-------------------------------------
151     Start a process and join with this
152     process using osl_joinProcess
153    -------------------------------------*/
154 
TEST_F(Test_osl_Process,test_osl_joinProcess)155 TEST_F(Test_osl_Process, test_osl_joinProcess)
156 {
157     oslProcess process;
158     oslProcessError osl_error = osl_executeProcess(
159         IMAGE_NAME.pData,
160         NULL,
161         0,
162         osl_Process_NORMAL,
163         osl_getCurrentSecurity(),
164         CWD.pData,
165         NULL,
166         0,
167         &process);
168 
169     ASSERT_TRUE(osl_error == osl_Process_E_None) << "osl_createProcess failed";
170 
171     osl_error = osl_joinProcess(process);
172 
173     ASSERT_TRUE(osl_Process_E_None == osl_error) << "osl_joinProcess returned with failure";
174 
175     osl_freeProcessHandle(process);
176 }
177 
main(int argc,char ** argv)178 int main(int argc, char **argv)
179 {
180     ::testing::InitGoogleTest(&argc, argv);
181     return RUN_ALL_TESTS();
182 }
183 
184