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 <stdlib.h>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir #include <sal/types.h>
31cdf0e10cSrcweir
32ccd7a7c4SDamjan Jovanovic #include "gtest/gtest.h"
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #include <rtl/string.hxx>
35cdf0e10cSrcweir #include <rtl/process.h>
36cdf0e10cSrcweir #include <osl/process.h>
37cdf0e10cSrcweir #include <osl/module.hxx>
38cdf0e10cSrcweir
39cdf0e10cSrcweir #include "rtl_Process_Const.h"
40cdf0e10cSrcweir
41cdf0e10cSrcweir using namespace osl;
42cdf0e10cSrcweir using namespace rtl;
43cdf0e10cSrcweir
44cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string.
45cdf0e10cSrcweir */
printUString(const::rtl::OUString & str,const sal_Char * msg=NULL)46cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = NULL )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir if ( msg != NULL )
49cdf0e10cSrcweir {
50ccd7a7c4SDamjan Jovanovic printf("#%s #printUString_u# ", msg );
51cdf0e10cSrcweir }
52cdf0e10cSrcweir rtl::OString aString;
53cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
54ccd7a7c4SDamjan Jovanovic printf("%s\n", (char *)aString.getStr( ) );
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
57cdf0e10cSrcweir // -----------------------------------------------------------------------------
getModulePath(void)58cdf0e10cSrcweir inline ::rtl::OUString getModulePath( void )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir ::rtl::OUString suDirPath;
61cdf0e10cSrcweir ::osl::Module::getUrlFromAddress(
62cdf0e10cSrcweir reinterpret_cast< oslGenericFunction >(getModulePath), suDirPath );
63cdf0e10cSrcweir
64cdf0e10cSrcweir printUString(suDirPath, "modulePath:");
65cdf0e10cSrcweir suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
66cdf0e10cSrcweir suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') + 1);
67cdf0e10cSrcweir suDirPath += rtl::OUString::createFromAscii("bin");
68cdf0e10cSrcweir return suDirPath;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir // -----------------------------------------------------------------------------
72cdf0e10cSrcweir
73cdf0e10cSrcweir namespace rtl_Process
74cdf0e10cSrcweir {
75ccd7a7c4SDamjan Jovanovic class getAppCommandArg : public ::testing::Test
76cdf0e10cSrcweir {
77cdf0e10cSrcweir public:
78*95e2fe77Smseidel // initialize your test code values here.
SetUp()79ccd7a7c4SDamjan Jovanovic void SetUp()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
TearDown()83ccd7a7c4SDamjan Jovanovic void TearDown()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86ccd7a7c4SDamjan Jovanovic }; // class getAppCommandArg
87cdf0e10cSrcweir
TEST_F(getAppCommandArg,getAppCommandArg_001)88ccd7a7c4SDamjan Jovanovic TEST_F(getAppCommandArg, getAppCommandArg_001)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
91cdf0e10cSrcweir const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process.exe");
92cdf0e10cSrcweir #else
93cdf0e10cSrcweir const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process");
94cdf0e10cSrcweir #endif
95cdf0e10cSrcweir rtl::OUString suCWD = getModulePath();
96cdf0e10cSrcweir // rtl::OUString suCWD2 = getExecutableDirectory();
97cdf0e10cSrcweir
98cdf0e10cSrcweir printUString(suCWD, "path to the current module");
99cdf0e10cSrcweir // printUString(suCWD2, "suCWD2");
100cdf0e10cSrcweir
101cdf0e10cSrcweir oslProcess hProcess = NULL;
102cdf0e10cSrcweir
103cdf0e10cSrcweir const int nParameterCount = 4;
104cdf0e10cSrcweir rtl_uString* pParameters[ nParameterCount ];
105cdf0e10cSrcweir
106cdf0e10cSrcweir pParameters[0] = suParam0.pData;
107cdf0e10cSrcweir pParameters[1] = suParam1.pData;
108cdf0e10cSrcweir pParameters[2] = suParam2.pData;
109cdf0e10cSrcweir pParameters[3] = suParam3.pData;
110cdf0e10cSrcweir
111cdf0e10cSrcweir rtl::OUString suFileURL = suCWD;
112cdf0e10cSrcweir suFileURL += rtl::OUString::createFromAscii("/");
113cdf0e10cSrcweir suFileURL += EXECUTABLE_NAME;
114cdf0e10cSrcweir
115cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess(
116cdf0e10cSrcweir suFileURL.pData,
117cdf0e10cSrcweir pParameters,
118cdf0e10cSrcweir nParameterCount,
119cdf0e10cSrcweir osl_Process_WAIT,
120cdf0e10cSrcweir 0, /* osl_getCurrentSecurity() */
121cdf0e10cSrcweir suCWD.pData,
122cdf0e10cSrcweir NULL,
123cdf0e10cSrcweir 0,
124cdf0e10cSrcweir &hProcess );
125cdf0e10cSrcweir
126ccd7a7c4SDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None) <<
127ccd7a7c4SDamjan Jovanovic "osl_createProcess failed";
128cdf0e10cSrcweir // we could get return value only after the process terminated
129cdf0e10cSrcweir osl_joinProcess(hProcess);
130ccd7a7c4SDamjan Jovanovic // ASSERT_TRUE(osl_Process_E_None == osl_error)
131ccd7a7c4SDamjan Jovanovic // << "osl_joinProcess returned with failure";
132cdf0e10cSrcweir oslProcessInfo* pInfo = new oslProcessInfo;
133cdf0e10cSrcweir // please pay attention to initial the Size to sizeof(oslProcessInfo), or else
134*95e2fe77Smseidel // you will get unknown error when call osl_getProcessInfo
135cdf0e10cSrcweir pInfo->Size = sizeof(oslProcessInfo);
136cdf0e10cSrcweir osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo );
137ccd7a7c4SDamjan Jovanovic ASSERT_TRUE(osl_Process_E_None == osl_error)
138ccd7a7c4SDamjan Jovanovic << "osl_getProcessInfo returned with failure";
139cdf0e10cSrcweir
140ccd7a7c4SDamjan Jovanovic printf("the exit code is %d.\n", pInfo->Code );
141ccd7a7c4SDamjan Jovanovic ASSERT_TRUE(pInfo->Code == 2) << "rtl_getAppCommandArg or rtl_getAppCommandArgCount error.";
142cdf0e10cSrcweir delete pInfo;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir
146cdf0e10cSrcweir /************************************************************************
147cdf0e10cSrcweir * For diagnostics( from sal/test/testuuid.cxx )
148cdf0e10cSrcweir ************************************************************************/
printUuid(sal_uInt8 * pNode)149cdf0e10cSrcweir void printUuid( sal_uInt8 *pNode )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir printf("# UUID is: ");
152cdf0e10cSrcweir for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir sal_uInt8 nValue = pNode[i1*4 +i2];
157cdf0e10cSrcweir if (nValue < 16)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir printf( "0");
160cdf0e10cSrcweir }
161cdf0e10cSrcweir printf( "%02x" ,nValue );
162cdf0e10cSrcweir }
163cdf0e10cSrcweir if( i1 == 3 )
164cdf0e10cSrcweir break;
165cdf0e10cSrcweir printf( "-" );
166cdf0e10cSrcweir }
167cdf0e10cSrcweir printf("\n");
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir /**************************************************************************
171cdf0e10cSrcweir * output UUID to a string
172cdf0e10cSrcweir **************************************************************************/
printUuidtoBuffer(sal_uInt8 * pNode,sal_Char * pBuffer)173cdf0e10cSrcweir void printUuidtoBuffer( sal_uInt8 *pNode, sal_Char * pBuffer )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir sal_Int8 nPtr = 0;
176cdf0e10cSrcweir for( sal_Int32 i1 = 0 ; i1 < 16 ; i1++ )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir sal_uInt8 nValue = pNode[i1];
179cdf0e10cSrcweir if (nValue < 16)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir sprintf( (sal_Char *)(pBuffer + nPtr), "0");
182cdf0e10cSrcweir nPtr++;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir sprintf( (sal_Char *)(pBuffer + nPtr), "%02x", nValue );
185cdf0e10cSrcweir nPtr += 2 ;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
189ccd7a7c4SDamjan Jovanovic class getGlobalProcessId : public ::testing::Test
190cdf0e10cSrcweir {
191cdf0e10cSrcweir public:
192*95e2fe77Smseidel // initialize your test code values here.
SetUp()193ccd7a7c4SDamjan Jovanovic void SetUp()
194cdf0e10cSrcweir {
195cdf0e10cSrcweir }
196cdf0e10cSrcweir
TearDown()197ccd7a7c4SDamjan Jovanovic void TearDown()
198cdf0e10cSrcweir {
199cdf0e10cSrcweir }
200ccd7a7c4SDamjan Jovanovic }; // class getGlobalProcessId
201ccd7a7c4SDamjan Jovanovic
202cdf0e10cSrcweir // gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
TEST_F(getGlobalProcessId,getGlobalProcessId_001)203ccd7a7c4SDamjan Jovanovic TEST_F(getGlobalProcessId, getGlobalProcessId_001)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir sal_uInt8 pTargetUUID1[16];
206cdf0e10cSrcweir sal_uInt8 pTargetUUID2[16];
207cdf0e10cSrcweir rtl_getGlobalProcessId( pTargetUUID1 );
208cdf0e10cSrcweir rtl_getGlobalProcessId( pTargetUUID2 );
209ccd7a7c4SDamjan Jovanovic ASSERT_TRUE( !memcmp( pTargetUUID1 , pTargetUUID2 , 16 ) )
210ccd7a7c4SDamjan Jovanovic << "getGlobalProcessId: got two same ProcessIds.";
211cdf0e10cSrcweir }
212ccd7a7c4SDamjan Jovanovic
213cdf0e10cSrcweir // different processes different pids
TEST_F(getGlobalProcessId,getGlobalProcessId_002)214ccd7a7c4SDamjan Jovanovic TEST_F(getGlobalProcessId, getGlobalProcessId_002)
215cdf0e10cSrcweir {
216cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
217cdf0e10cSrcweir const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id.exe");
218cdf0e10cSrcweir #else
219cdf0e10cSrcweir const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id");
220cdf0e10cSrcweir #endif
221cdf0e10cSrcweir sal_uInt8 pTargetUUID1[16];
222cdf0e10cSrcweir rtl_getGlobalProcessId( pTargetUUID1 );
223cdf0e10cSrcweir printUuid( pTargetUUID1 );
224cdf0e10cSrcweir sal_Char pUUID1[32];
225cdf0e10cSrcweir printUuidtoBuffer( pTargetUUID1, pUUID1 );
226cdf0e10cSrcweir printf("# UUID to String is %s\n", pUUID1);
227cdf0e10cSrcweir
228cdf0e10cSrcweir rtl::OUString suCWD = getModulePath();
229cdf0e10cSrcweir oslProcess hProcess = NULL;
230cdf0e10cSrcweir rtl::OUString suFileURL = suCWD;
231cdf0e10cSrcweir suFileURL += rtl::OUString::createFromAscii("/");
232cdf0e10cSrcweir suFileURL += EXEC_NAME;
233cdf0e10cSrcweir oslFileHandle* pChildOutputRead = new oslFileHandle();
234cdf0e10cSrcweir oslProcessError osl_error = osl_executeProcess_WithRedirectedIO(
235cdf0e10cSrcweir suFileURL.pData,
236cdf0e10cSrcweir NULL,
237cdf0e10cSrcweir 0,
238cdf0e10cSrcweir osl_Process_WAIT,
239cdf0e10cSrcweir 0,
240cdf0e10cSrcweir suCWD.pData,
241cdf0e10cSrcweir NULL,
242cdf0e10cSrcweir 0,
243cdf0e10cSrcweir &hProcess,
244cdf0e10cSrcweir NULL,
245cdf0e10cSrcweir pChildOutputRead,
246cdf0e10cSrcweir NULL);
247cdf0e10cSrcweir
248ccd7a7c4SDamjan Jovanovic ASSERT_TRUE(osl_error == osl_Process_E_None)
249ccd7a7c4SDamjan Jovanovic << "osl_createProcess failed";
250cdf0e10cSrcweir // we could get return value only after the process terminated
251cdf0e10cSrcweir osl_joinProcess(hProcess);
252cdf0e10cSrcweir
253cdf0e10cSrcweir sal_Char pUUID2[33];
254cdf0e10cSrcweir pUUID2[32] = '\0';
255cdf0e10cSrcweir sal_uInt64 nRead = 0;
256cdf0e10cSrcweir osl_readFile( *pChildOutputRead, pUUID2, 32, &nRead );
257ccd7a7c4SDamjan Jovanovic printf("read buffer is %s, nRead is %lu \n", pUUID2, nRead );
258cdf0e10cSrcweir OUString suUUID2 = OUString::createFromAscii( pUUID2 );
259ccd7a7c4SDamjan Jovanovic ASSERT_TRUE( suUUID2.equalsAsciiL( pUUID1, 32) == sal_False ) << "getGlobalProcessId: got two same ProcessIds.";
260cdf0e10cSrcweir }
261cdf0e10cSrcweir
262cdf0e10cSrcweir } // namespace rtl_Process
263cdf0e10cSrcweir
main(int argc,char ** argv)264ccd7a7c4SDamjan Jovanovic int main(int argc, char **argv)
265ccd7a7c4SDamjan Jovanovic {
266ccd7a7c4SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv);
267ccd7a7c4SDamjan Jovanovic return RUN_ALL_TESTS();
268ccd7a7c4SDamjan Jovanovic }
269*95e2fe77Smseidel
270*95e2fe77Smseidel /* vim: set noet sw=4 ts=4: */
271