| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef IOS |
| 21 |
#define CPPUNIT_PLUGIN_EXPORTED_NAME cppunitTest_osl_process |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <sal/types.h> |
| 25 |
#include <cppunit/TestFixture.h> |
| 26 |
#include <cppunit/extensions/HelperMacros.h> |
| 27 |
|
| 28 |
#include <osl/process.h> |
| 29 |
#include <osl/file.hxx> |
| 30 |
#include <osl/thread.h> |
| 31 |
#include <rtl/ustring.hxx> |
| 32 |
|
| 33 |
#include <osl/module.hxx> |
| 34 |
#include <sal/macros.h> |
| 35 |
|
| 36 |
#if defined HAVE_VALGRIND_HEADERS |
| 37 |
#include <valgrind/valgrind.h> |
| 38 |
#elif !defined _WIN32 |
| 39 |
#define RUNNING_ON_VALGRIND false |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <iostream> |
| 43 |
#include <vector> |
| 44 |
#include <algorithm> |
| 45 |
#include <string> |
| 46 |
|
| 47 |
#ifdef UNX |
| 48 |
#if defined( MACOSX ) |
| 49 |
# include <crt_externs.h> |
| 50 |
# define environ (*_NSGetEnviron()) |
| 51 |
# else |
| 52 |
extern char** environ; |
| 53 |
# endif |
| 54 |
#endif |
| 55 |
|
| 56 |
using namespace osl; |
| 57 |
|
| 58 |
/** get binary Path. |
| 59 |
*/ |
| 60 |
static OUString getExecutablePath() |
| 61 |
{ |
| 62 |
OUString dirPath; |
| 63 |
osl::Module::getUrlFromAddress( |
| 64 |
reinterpret_cast<oslGenericFunction>(&getExecutablePath), dirPath); |
| 65 |
dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') ); |
| 66 |
dirPath = OUString::Concat(dirPath.subView( 0, dirPath.lastIndexOf('/') + 1)) + |
| 67 |
"Executable"; |
| 68 |
return dirPath; |
| 69 |
} |
| 70 |
|
| 71 |
#if !defined _WIN32 |
| 72 |
|
| 73 |
namespace { |
| 74 |
|
| 75 |
class exclude |
| 76 |
{ |
| 77 |
public: |
| 78 |
|
| 79 |
explicit exclude(const std::vector<OString>& exclude_list) |
| 80 |
{ |
| 81 |
for (auto& exclude_list_item : exclude_list) |
| 82 |
exclude_list_.push_back(env_var_name(exclude_list_item)); |
| 83 |
} |
| 84 |
|
| 85 |
bool operator() (const OString& env_var) const |
| 86 |
{ |
| 87 |
return (exclude_list_.end() != |
| 88 |
std::find( |
| 89 |
exclude_list_.begin(), |
| 90 |
exclude_list_.end(), |
| 91 |
env_var_name(env_var))); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
|
| 96 |
// extract the name from an environment variable |
| 97 |
// that is given in the form "NAME=VALUE" |
| 98 |
static OString env_var_name(const OString& env_var) |
| 99 |
{ |
| 100 |
sal_Int32 pos_equal_sign = |
| 101 |
env_var.indexOf('='); |
| 102 |
|
| 103 |
if (pos_equal_sign != -1) |
| 104 |
return env_var.copy(0, pos_equal_sign); |
| 105 |
|
| 106 |
return OString(); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
std::vector<OString> exclude_list_; |
| 111 |
}; |
| 112 |
|
| 113 |
void tidy_container(std::vector<OString> &env_container) |
| 114 |
{ |
| 115 |
//sort them because there are no guarantees to ordering |
| 116 |
std::sort(env_container.begin(), env_container.end()); |
| 117 |
if (RUNNING_ON_VALGRIND) |
| 118 |
{ |
| 119 |
std::erase_if( |
| 120 |
env_container, |
| 121 |
[](OString const & s) { |
| 122 |
return s.startsWith("LD_PRELOAD=") |
| 123 |
|| s.startsWith("VALGRIND_LIB="); }); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static void read_parent_environment(std::vector<OString>* env_container) |
| 129 |
{ |
| 130 |
for (int i = 0; environ[i] != nullptr; i++) |
| 131 |
env_container->push_back(OString(environ[i])); |
| 132 |
tidy_container(*env_container); |
| 133 |
} |
| 134 |
|
| 135 |
#endif |
| 136 |
|
| 137 |
class Test_osl_executeProcess : public CppUnit::TestFixture |
| 138 |
{ |
| 139 |
const OUString env_param_; |
| 140 |
|
| 141 |
OUString temp_file_url_; |
| 142 |
OUString temp_file_path_; |
| 143 |
rtl_uString* parameters_[2]; |
| 144 |
static const int parameters_count_ = 2; |
| 145 |
OUString suCWD; |
| 146 |
OUString suExecutableFileURL; |
| 147 |
|
| 148 |
public: |
| 149 |
|
| 150 |
// ctor |
| 151 |
Test_osl_executeProcess() : |
| 152 |
env_param_(u"-env"_ustr), suCWD(getExecutablePath()) |
| 153 |
{ |
| 154 |
parameters_[0] = env_param_.pData; |
| 155 |
|
| 156 |
#if defined(_WIN32) |
| 157 |
suExecutableFileURL = suCWD + "/" "osl_process_child.exe"; |
| 158 |
#else |
| 159 |
suExecutableFileURL = suCWD + "/" "osl_process_child"; |
| 160 |
#endif |
| 161 |
} |
| 162 |
|
| 163 |
virtual void setUp() override |
| 164 |
{ |
| 165 |
temp_file_path_ = create_temp_file(temp_file_url_); |
| 166 |
parameters_[1] = temp_file_path_.pData; |
| 167 |
} |
| 168 |
|
| 169 |
virtual void tearDown() override |
| 170 |
{ |
| 171 |
osl::File::remove(temp_file_url_); |
| 172 |
} |
| 173 |
|
| 174 |
OUString create_temp_file(OUString &temp_file_url) |
| 175 |
{ |
| 176 |
FileBase::RC rc = FileBase::createTempFile(nullptr, nullptr, &temp_file_url); |
| 177 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile failed", FileBase::E_None, rc); |
| 178 |
|
| 179 |
OUString temp_file_path; |
| 180 |
rc = FileBase::getSystemPathFromFileURL(temp_file_url, temp_file_path); |
| 181 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("getSystemPathFromFileURL failed", FileBase::E_None, rc); |
| 182 |
|
| 183 |
return temp_file_path; |
| 184 |
} |
| 185 |
|
| 186 |
#if !defined _WIN32 |
| 187 |
|
| 188 |
void read_child_environment(std::vector<OString>* env_container) |
| 189 |
{ |
| 190 |
OString temp_file_name = OUStringToOString(OUString( |
| 191 |
parameters_[1]), osl_getThreadTextEncoding()); |
| 192 |
std::ifstream file(temp_file_name.getStr()); |
| 193 |
|
| 194 |
CPPUNIT_ASSERT_MESSAGE |
| 195 |
( |
| 196 |
"I/O error, cannot open child environment file", |
| 197 |
file.is_open() |
| 198 |
); |
| 199 |
|
| 200 |
std::string line; |
| 201 |
line.reserve(1024); |
| 202 |
while (std::getline(file, line, '\0')) |
| 203 |
env_container->push_back(OString(line.c_str())); |
| 204 |
tidy_container(*env_container); |
| 205 |
} |
| 206 |
|
| 207 |
// environment of the child process that was |
| 208 |
// started. The child process writes his |
| 209 |
// environment into a file |
| 210 |
void compare_environments() |
| 211 |
{ |
| 212 |
std::vector<OString> parent_env; |
| 213 |
read_parent_environment(&parent_env); |
| 214 |
|
| 215 |
std::vector<OString> child_env; |
| 216 |
read_child_environment(&child_env); |
| 217 |
|
| 218 |
OString msg( |
| 219 |
OString::number(parent_env.size()) + "/" |
| 220 |
+ OString::number(child_env.size())); |
| 221 |
auto min = std::min(parent_env.size(), child_env.size()); |
| 222 |
for (decltype(min) i = 0; i != min; ++i) { |
| 223 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( |
| 224 |
msg.getStr(), parent_env[i], child_env[i]); |
| 225 |
} |
| 226 |
if (parent_env.size() != child_env.size()) { |
| 227 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( |
| 228 |
(parent_env.size() >= child_env.size() |
| 229 |
? parent_env.back() : child_env.back()).getStr(), |
| 230 |
parent_env.size(), child_env.size()); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// compare the equal environment parts and the |
| 235 |
// different part of the child environment |
| 236 |
bool compare_merged_environments(const std::vector<OString>& different_env_vars) |
| 237 |
{ |
| 238 |
std::vector<OString> parent_env; |
| 239 |
read_parent_environment(&parent_env); |
| 240 |
|
| 241 |
for (auto& env : parent_env) |
| 242 |
std::cout << "initially parent env: " << env << "\n"; |
| 243 |
|
| 244 |
//remove the environment variables that we have changed |
| 245 |
//in the child environment from the read parent environment |
| 246 |
std::erase_if(parent_env, exclude(different_env_vars)); |
| 247 |
|
| 248 |
for (auto& env : parent_env) |
| 249 |
std::cout << "stripped parent env: " << env << "\n"; |
| 250 |
|
| 251 |
//read the child environment and exclude the variables that |
| 252 |
//are different |
| 253 |
std::vector<OString> child_env; |
| 254 |
read_child_environment(&child_env); |
| 255 |
|
| 256 |
for (auto& env : child_env) |
| 257 |
std::cout << "initial child env: " << env << "\n"; |
| 258 |
//partition the child environment into the variables that |
| 259 |
//are different to the parent environment (they come first) |
| 260 |
//and the variables that should be equal between parent |
| 261 |
//and child environment |
| 262 |
auto iter_logical_end = |
| 263 |
std::stable_partition(child_env.begin(), child_env.end(), exclude(different_env_vars)); |
| 264 |
|
| 265 |
std::vector<OString> different_child_env_vars(child_env.begin(), iter_logical_end); |
| 266 |
child_env.erase(child_env.begin(), iter_logical_end); |
| 267 |
|
| 268 |
for (auto& env : child_env) |
| 269 |
std::cout << "stripped child env: " << env << "\n"; |
| 270 |
|
| 271 |
bool common_env_size_equals = (parent_env.size() == child_env.size()); |
| 272 |
bool common_env_content_equals = std::equal(child_env.begin(), child_env.end(), parent_env.begin()); |
| 273 |
|
| 274 |
for (auto& env_var : different_env_vars) |
| 275 |
std::cout << "different should be: " << env_var << "\n"; |
| 276 |
|
| 277 |
for (auto& env_var : different_child_env_vars) |
| 278 |
std::cout << "different are: " << env_var << "\n"; |
| 279 |
|
| 280 |
bool different_env_size_equals = (different_child_env_vars.size() == different_env_vars.size()); |
| 281 |
bool different_env_content_equals = |
| 282 |
std::equal(different_env_vars.begin(), different_env_vars.end(), different_child_env_vars.begin()); |
| 283 |
|
| 284 |
return (common_env_size_equals && common_env_content_equals && |
| 285 |
different_env_size_equals && different_env_content_equals); |
| 286 |
} |
| 287 |
|
| 288 |
// test that parent and child process have the |
| 289 |
// same environment when osl_executeProcess will |
| 290 |
// be called without setting new environment |
| 291 |
// variables |
| 292 |
void osl_execProc_parent_equals_child_environment() |
| 293 |
{ |
| 294 |
oslProcess process; |
| 295 |
oslProcessError osl_error = osl_executeProcess( |
| 296 |
suExecutableFileURL.pData, |
| 297 |
parameters_, |
| 298 |
parameters_count_, |
| 299 |
osl_Process_NORMAL, |
| 300 |
nullptr, |
| 301 |
suCWD.pData, |
| 302 |
nullptr, |
| 303 |
0, |
| 304 |
&process); |
| 305 |
|
| 306 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 307 |
( |
| 308 |
"osl_createProcess failed", |
| 309 |
osl_Process_E_None, osl_error |
| 310 |
); |
| 311 |
|
| 312 |
osl_error = ::osl_joinProcess(process); |
| 313 |
|
| 314 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 315 |
( |
| 316 |
"osl_joinProcess returned with failure", |
| 317 |
osl_Process_E_None, osl_error |
| 318 |
); |
| 319 |
|
| 320 |
osl_freeProcessHandle(process); |
| 321 |
|
| 322 |
compare_environments(); |
| 323 |
} |
| 324 |
|
| 325 |
#define ENV1 "PAT=a:\\" |
| 326 |
#define ENV2 "PATHb=b:\\" |
| 327 |
#define ENV3 "Patha=c:\\" |
| 328 |
#define ENV4 "Patha=d:\\" |
| 329 |
|
| 330 |
void osl_execProc_merged_child_environment() |
| 331 |
{ |
| 332 |
rtl_uString* child_env[4]; |
| 333 |
OUString env1(u"" ENV1 ""_ustr); |
| 334 |
OUString env2(u"" ENV2 ""_ustr); |
| 335 |
OUString env3(u"" ENV3 ""_ustr); |
| 336 |
OUString env4(u"" ENV4 ""_ustr); |
| 337 |
|
| 338 |
child_env[0] = env1.pData; |
| 339 |
child_env[1] = env2.pData; |
| 340 |
child_env[2] = env3.pData; |
| 341 |
child_env[3] = env4.pData; |
| 342 |
|
| 343 |
oslProcess process; |
| 344 |
oslProcessError osl_error = osl_executeProcess( |
| 345 |
suExecutableFileURL.pData, |
| 346 |
parameters_, |
| 347 |
parameters_count_, |
| 348 |
osl_Process_NORMAL, |
| 349 |
nullptr, |
| 350 |
suCWD.pData, |
| 351 |
child_env, |
| 352 |
SAL_N_ELEMENTS(child_env), |
| 353 |
&process); |
| 354 |
|
| 355 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 356 |
( |
| 357 |
"osl_createProcess failed", |
| 358 |
osl_Process_E_None, osl_error |
| 359 |
); |
| 360 |
|
| 361 |
osl_error = ::osl_joinProcess(process); |
| 362 |
|
| 363 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 364 |
( |
| 365 |
"osl_joinProcess returned with failure", |
| 366 |
osl_Process_E_None, osl_error |
| 367 |
); |
| 368 |
|
| 369 |
osl_freeProcessHandle(process); |
| 370 |
|
| 371 |
std::vector<OString> different_child_env_vars |
| 372 |
{ |
| 373 |
ENV1 ""_ostr, |
| 374 |
ENV2 ""_ostr, |
| 375 |
ENV4 ""_ostr |
| 376 |
}; |
| 377 |
|
| 378 |
CPPUNIT_ASSERT_MESSAGE |
| 379 |
( |
| 380 |
"osl_execProc_merged_child_environment", |
| 381 |
compare_merged_environments(different_child_env_vars) |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
#endif |
| 386 |
|
| 387 |
void osl_execProc_test_batch() |
| 388 |
{ |
| 389 |
oslProcess process; |
| 390 |
#if defined(_WIN32) |
| 391 |
OUString suBatch = suCWD + "/batch.bat"; |
| 392 |
#else |
| 393 |
OUString suBatch = suCWD + "/batch.sh"; |
| 394 |
#endif |
| 395 |
oslProcessError osl_error = osl_executeProcess( |
| 396 |
suBatch.pData, |
| 397 |
nullptr, |
| 398 |
0, |
| 399 |
osl_Process_HIDDEN, |
| 400 |
nullptr, |
| 401 |
suCWD.pData, |
| 402 |
nullptr, |
| 403 |
0, |
| 404 |
&process); |
| 405 |
|
| 406 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 407 |
( |
| 408 |
"osl_createProcess failed", |
| 409 |
osl_Process_E_None, osl_error |
| 410 |
); |
| 411 |
|
| 412 |
osl_error = ::osl_joinProcess(process); |
| 413 |
|
| 414 |
CPPUNIT_ASSERT_EQUAL_MESSAGE |
| 415 |
( |
| 416 |
"osl_joinProcess returned with failure", |
| 417 |
osl_Process_E_None, osl_error |
| 418 |
); |
| 419 |
|
| 420 |
osl_freeProcessHandle(process); |
| 421 |
} |
| 422 |
|
| 423 |
CPPUNIT_TEST_SUITE(Test_osl_executeProcess); |
| 424 |
//TODO: Repair these (at least under Windows) |
| 425 |
#if !defined(_WIN32) |
| 426 |
CPPUNIT_TEST(osl_execProc_parent_equals_child_environment); |
| 427 |
CPPUNIT_TEST(osl_execProc_merged_child_environment); |
| 428 |
#endif |
| 429 |
CPPUNIT_TEST(osl_execProc_test_batch); |
| 430 |
///TODO: Repair test (or tested function ;-) - test fails. |
| 431 |
CPPUNIT_TEST_SUITE_END(); |
| 432 |
}; |
| 433 |
|
| 434 |
CPPUNIT_TEST_SUITE_REGISTRATION(Test_osl_executeProcess); |
| 435 |
|
| 436 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |