LCOV - code coverage report
Current view: top level - sal/qa/osl/process - osl_process.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 145 152 95.4 %
Date: 2015-06-13 12:38:46 Functions: 29 31 93.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include <cppunit/plugin/TestPlugIn.h>
      28             : 
      29             : #include <osl/process.h>
      30             : #include <osl/file.hxx>
      31             : #include <osl/thread.h>
      32             : #include <rtl/ustring.hxx>
      33             : #include <signal.h>
      34             : 
      35             : #include <stdio.h>
      36             : #include <stdlib.h>
      37             : #include <osl/module.hxx>
      38             : #include <sal/macros.h>
      39             : 
      40             : #if defined HAVE_VALGRIND_HEADERS
      41             : #include <valgrind/valgrind.h>
      42             : #else
      43             : #define RUNNING_ON_VALGRIND false
      44             : #endif
      45             : 
      46             : #if ( defined WNT )                     // Windows
      47             : #   include <windows.h>
      48             : #   include <tchar.h>
      49             : #else
      50             : #include <unistd.h>
      51             : #endif
      52             : 
      53             : #include <iostream>
      54             : #include <fstream>
      55             : #include <vector>
      56             : #include <algorithm>
      57             : #include <iterator>
      58             : #include <string>
      59             : 
      60             : #ifdef UNX
      61             : #if defined( MACOSX )
      62             : # include <crt_externs.h>
      63             : # define environ (*_NSGetEnviron())
      64             : # else
      65             :     extern char** environ;
      66             : # endif
      67             : #endif
      68             : 
      69             : #if defined(WNT)
      70             :     const rtl::OUString EXECUTABLE_NAME ("osl_process_child.exe");
      71             : #else
      72           1 :     const rtl::OUString EXECUTABLE_NAME ("osl_process_child");
      73             : #endif
      74             : 
      75             : using namespace osl;
      76             : 
      77             : using ::rtl::OUString;
      78             : using ::rtl::OUStringToOString;
      79             : using ::rtl::OString;
      80             : 
      81             : /** get binary Path.
      82             : */
      83           3 : inline ::rtl::OUString getExecutablePath()
      84             : {
      85           3 :     ::rtl::OUString dirPath;
      86             :     osl::Module::getUrlFromAddress(
      87           3 :         reinterpret_cast<oslGenericFunction>(&getExecutablePath), dirPath);
      88           3 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') );
      89           3 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
      90           3 :     dirPath += rtl::OUString("Executable");
      91           3 :     return dirPath;
      92             : }
      93             : 
      94             : //rtl::OUString CWD = getExecutablePath();
      95             : 
      96             : typedef std::vector<OString> string_container_t;
      97             : typedef string_container_t::const_iterator string_container_const_iter_t;
      98             : typedef string_container_t::iterator       string_container_iter_t;
      99             : 
     100          12 : class exclude : public std::unary_function<OString, bool>
     101             : {
     102             : public:
     103             : 
     104           2 :     exclude(const string_container_t& exclude_list)
     105           2 :     {
     106           2 :         string_container_const_iter_t iter     = exclude_list.begin();
     107           2 :         string_container_const_iter_t iter_end = exclude_list.end();
     108           8 :         for (/**/; iter != iter_end; ++iter)
     109           6 :             exclude_list_.push_back(env_var_name(*iter));
     110           2 :     }
     111             : 
     112        1725 :     bool operator() (const OString& env_var) const
     113             :     {
     114        3450 :         return (exclude_list_.end() !=
     115             :                 std::find(
     116             :                     exclude_list_.begin(),
     117             :                     exclude_list_.end(),
     118        5175 :                     env_var_name(env_var)));
     119             :     }
     120             : 
     121             : private:
     122             : 
     123             :     // extract the name from an environment variable
     124             :     // that is given in the form "NAME=VALUE"
     125        1731 :     static OString env_var_name(const OString& env_var)
     126             :     {
     127             :         sal_Int32 pos_equal_sign =
     128        1731 :             env_var.indexOf('=');
     129             : 
     130        1731 :         if (-1 != pos_equal_sign)
     131        1731 :             return env_var.copy(0, pos_equal_sign);
     132             : 
     133           0 :         return OString();
     134             :     }
     135             : 
     136             : private:
     137             :     string_container_t exclude_list_;
     138             : };
     139             : 
     140             : namespace
     141             : {
     142           4 :     void tidy_container(string_container_t &env_container)
     143             :     {
     144             :         //sort them because there are no guarantees to ordering
     145           4 :         std::sort(env_container.begin(), env_container.end());
     146           4 :         if (RUNNING_ON_VALGRIND)
     147             :         {
     148             :             env_container.erase(
     149             :                 std::remove_if(
     150             :                     env_container.begin(), env_container.end(),
     151           0 :                     [](OString const & s) {
     152           0 :                         return s.startsWith("LD_PRELOAD=")
     153           0 :                             || s.startsWith("VALGRIND_LIB="); }),
     154           0 :                 env_container.end());
     155             :         }
     156           4 :     }
     157             : }
     158             : 
     159             : #ifdef WNT
     160             :     void read_parent_environment(string_container_t* env_container)
     161             :     {
     162             :         LPTSTR env = reinterpret_cast<LPTSTR>(GetEnvironmentStrings());
     163             :         LPTSTR p   = env;
     164             : 
     165             :         while (size_t l = _tcslen(p))
     166             :         {
     167             :             env_container->push_back(OString(p));
     168             :             p += l + 1;
     169             :         }
     170             :         FreeEnvironmentStrings(env);
     171             :         tidy_container(*env_container);
     172             :     }
     173             : #else
     174           2 :     void read_parent_environment(string_container_t* env_container)
     175             :     {
     176        1724 :         for (int i = 0; NULL != environ[i]; i++)
     177        1722 :             env_container->push_back(OString(environ[i]));
     178           2 :         tidy_container(*env_container);
     179           2 :     }
     180             : #endif
     181             : 
     182           6 : class Test_osl_executeProcess : public CppUnit::TestFixture
     183             : {
     184             :     const OUString env_param_;
     185             : 
     186             :     OUString     temp_file_url_;
     187             :     OUString     temp_file_path_;
     188             :     rtl_uString* parameters_[2];
     189             :     int          parameters_count_;
     190             :     OUString    suCWD;
     191             :     OUString    suExecutableFileURL;
     192             : 
     193             : public:
     194             : 
     195             :     // ctor
     196           3 :     Test_osl_executeProcess() :
     197             :         env_param_(OUString("-env")),
     198           3 :         parameters_count_(2)
     199             :     {
     200           3 :         parameters_[0] = env_param_.pData;
     201           3 :         suCWD = getExecutablePath();
     202           3 :         suExecutableFileURL = suCWD;
     203           3 :         suExecutableFileURL += rtl::OUString("/");
     204           3 :         suExecutableFileURL += EXECUTABLE_NAME;
     205           3 :     }
     206             : 
     207           3 :     virtual void setUp() SAL_OVERRIDE
     208             :     {
     209           3 :         temp_file_path_ = create_temp_file(temp_file_url_);
     210           3 :         parameters_[1]  = temp_file_path_.pData;
     211           3 :     }
     212             : 
     213           3 :     virtual void tearDown() SAL_OVERRIDE
     214             :     {
     215           3 :         osl::File::remove(temp_file_url_);
     216           3 :     }
     217             : 
     218           3 :     OUString create_temp_file(OUString &temp_file_url)
     219             :     {
     220           3 :         FileBase::RC rc = FileBase::createTempFile(0, 0, &temp_file_url);
     221           3 :         CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile failed", FileBase::E_None, rc);
     222             : 
     223           3 :         OUString temp_file_path;
     224           3 :         rc = FileBase::getSystemPathFromFileURL(temp_file_url, temp_file_path);
     225           3 :         CPPUNIT_ASSERT_EQUAL_MESSAGE("getSystemPathFromFileURL failed", FileBase::E_None, rc);
     226             : 
     227           3 :         return temp_file_path;
     228             :     }
     229             : 
     230           2 :     void read_child_environment(string_container_t* env_container)
     231             :     {
     232             :         OString temp_file_name = OUStringToOString(OUString(
     233           2 :             parameters_[1]), osl_getThreadTextEncoding());
     234           4 :         std::ifstream file(temp_file_name.getStr());
     235             : 
     236           4 :         CPPUNIT_ASSERT_MESSAGE
     237             :         (
     238             :             "I/O error, cannot open child environment file",
     239             :             file.is_open()
     240           2 :         );
     241             : 
     242           4 :         std::string line;
     243           2 :         line.reserve(1024);
     244        1729 :         while (std::getline(file, line, '\0'))
     245        1725 :             env_container->push_back(OString(line.c_str()));
     246           4 :         tidy_container(*env_container);
     247           2 :     }
     248             : 
     249             :     // environment of the child process that was
     250             :     // started. The child process writes his
     251             :     // environment into a file
     252           1 :     void compare_environments()
     253             :     {
     254           1 :         string_container_t parent_env;
     255           1 :         read_parent_environment(&parent_env);
     256             : 
     257           2 :         string_container_t child_env;
     258           1 :         read_child_environment(&child_env);
     259             : 
     260             :         OString msg(
     261           2 :             OString::number(parent_env.size()) + "/"
     262           4 :             + OString::number(child_env.size()));
     263           1 :         auto min = std::min(parent_env.size(), child_env.size());
     264         862 :         for (decltype(min) i = 0; i != min; ++i) {
     265        1722 :             CPPUNIT_ASSERT_EQUAL_MESSAGE(
     266         861 :                 msg.getStr(), parent_env[i], child_env[i]);
     267             :         }
     268           1 :         if (parent_env.size() != child_env.size()) {
     269           0 :             CPPUNIT_ASSERT_EQUAL_MESSAGE(
     270             :                 (parent_env.size() >= child_env.size()
     271             :                  ? parent_env.back() : child_env.back()).getStr(),
     272           0 :                 parent_env.size(), child_env.size());
     273           1 :         }
     274           1 :     }
     275             : 
     276             :     // compare the equal environment parts and the
     277             :     // different part of the child environment
     278           1 :     bool compare_merged_environments(const string_container_t& different_env_vars)
     279             :     {
     280           1 :         string_container_t parent_env;
     281           1 :         read_parent_environment(&parent_env);
     282             : 
     283             : #if OSL_DEBUG_LEVEL > 1
     284             :         for (string_container_t::const_iterator iter = parent_env.begin(), end = parent_env.end(); iter != end; ++iter)
     285             :             std::cerr << "initially parent env: " << *iter << std::endl;
     286             : #endif
     287             : 
     288             :         //remove the environment variables that we have changed
     289             :         //in the child environment from the read parent environment
     290             :         parent_env.erase(
     291             :             std::remove_if(parent_env.begin(), parent_env.end(), exclude(different_env_vars)),
     292           1 :             parent_env.end());
     293             : 
     294             : #if OSL_DEBUG_LEVEL > 1
     295             :         for (string_container_t::const_iterator iter = parent_env.begin(), end = parent_env.end(); iter != end; ++iter)
     296             :             std::cerr << "stripped parent env: " << *iter << std::endl;
     297             : #endif
     298             : 
     299             :         //read the child environment and exclude the variables that
     300             :         //are different
     301           2 :         string_container_t child_env;
     302           1 :         read_child_environment(&child_env);
     303             : 
     304             : #if OSL_DEBUG_LEVEL > 1
     305             :         for (string_container_t::const_iterator iter = child_env.begin(), end = child_env.end(); iter != end; ++iter)
     306             :             std::cerr << "initial child env: " << *iter << std::endl;
     307             : #endif
     308             :         //partition the child environment into the variables that
     309             :         //are different to the parent environment (they come first)
     310             :         //and the variables that should be equal between parent
     311             :         //and child environment
     312             :         string_container_iter_t iter_logical_end =
     313           1 :             std::stable_partition(child_env.begin(), child_env.end(), exclude(different_env_vars));
     314             : 
     315           2 :         string_container_t different_child_env_vars(child_env.begin(), iter_logical_end);
     316           1 :         child_env.erase(child_env.begin(), iter_logical_end);
     317             : 
     318             : #if OSL_DEBUG_LEVEL > 1
     319             :         for (string_container_t::const_iterator iter = child_env.begin(), end = child_env.end(); iter != end; ++iter)
     320             :             std::cerr << "stripped child env: " << *iter << std::endl;
     321             : #endif
     322             : 
     323           1 :         bool common_env_size_equals    = (parent_env.size() == child_env.size());
     324           1 :         bool common_env_content_equals = std::equal(child_env.begin(), child_env.end(), parent_env.begin());
     325             : 
     326             : #if OSL_DEBUG_LEVEL > 1
     327             :         for (string_container_t::const_iterator iter = different_env_vars.begin(), end = different_env_vars.end(); iter != end; ++iter)
     328             :             std::cerr << "different should be: " << *iter << std::endl;
     329             : #endif
     330             : 
     331             : #if OSL_DEBUG_LEVEL > 1
     332             :         for (string_container_t::const_iterator iter = different_child_env_vars.begin(), end = different_child_env_vars.end(); iter != end; ++iter)
     333             :             std::cerr << "different are: " << *iter << std::endl;
     334             : #endif
     335             : 
     336           1 :         bool different_env_size_equals    = (different_child_env_vars.size() == different_env_vars.size());
     337             :         bool different_env_content_equals =
     338           1 :             std::equal(different_env_vars.begin(), different_env_vars.end(), different_child_env_vars.begin());
     339             : 
     340           1 :         return (common_env_size_equals && common_env_content_equals &&
     341           3 :                 different_env_size_equals && different_env_content_equals);
     342             :     }
     343             : 
     344             :     // test that parent and child process have the
     345             :     // same environment when osl_executeProcess will
     346             :     // be called with out setting new environment
     347             :     // variables
     348           1 :    void osl_execProc_parent_equals_child_environment()
     349             :     {
     350             :         oslProcess process;
     351             :         oslProcessError osl_error = osl_executeProcess(
     352             :             suExecutableFileURL.pData,
     353             :             parameters_,
     354             :             parameters_count_,
     355             :             osl_Process_NORMAL,
     356             :             NULL,
     357             :             suCWD.pData,
     358             :             NULL,
     359             :             0,
     360           1 :             &process);
     361             : 
     362           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     363             :         (
     364             :             "osl_createProcess failed",
     365             :             osl_Process_E_None, osl_error
     366           1 :         );
     367             : 
     368           1 :         osl_error = ::osl_joinProcess(process);
     369             : 
     370           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     371             :         (
     372             :             "osl_joinProcess returned with failure",
     373             :             osl_Process_E_None, osl_error
     374           1 :         );
     375             : 
     376           1 :         osl_freeProcessHandle(process);
     377             : 
     378           1 :         compare_environments();
     379           1 :     }
     380             : 
     381             :     #define ENV1 "PAT=a:\\"
     382             :     #define ENV2 "PATHb=b:\\"
     383             :     #define ENV3 "Patha=c:\\"
     384             :     #define ENV4 "Patha=d:\\"
     385             : 
     386           1 :     void osl_execProc_merged_child_environment()
     387             :     {
     388             :         rtl_uString* child_env[4];
     389           1 :         OUString env1(ENV1);
     390           2 :         OUString env2(ENV2);
     391           2 :         OUString env3(ENV3);
     392           2 :         OUString env4(ENV4);
     393             : 
     394           1 :         child_env[0] = env1.pData;
     395           1 :         child_env[1] = env2.pData;
     396           1 :         child_env[2] = env3.pData;
     397           1 :         child_env[3] = env4.pData;
     398             : 
     399             :         oslProcess process;
     400             :         oslProcessError osl_error = osl_executeProcess(
     401             :             suExecutableFileURL.pData,
     402             :             parameters_,
     403             :             parameters_count_,
     404             :             osl_Process_NORMAL,
     405             :             NULL,
     406             :             suCWD.pData,
     407             :             child_env,
     408             :             SAL_N_ELEMENTS(child_env),
     409           1 :             &process);
     410             : 
     411           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     412             :         (
     413             :             "osl_createProcess failed",
     414             :             osl_Process_E_None, osl_error
     415           1 :         );
     416             : 
     417           1 :         osl_error = ::osl_joinProcess(process);
     418             : 
     419           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     420             :         (
     421             :             "osl_joinProcess returned with failure",
     422             :             osl_Process_E_None, osl_error
     423           1 :         );
     424             : 
     425           1 :         osl_freeProcessHandle(process);
     426             : 
     427           2 :         string_container_t different_child_env_vars;
     428           1 :         different_child_env_vars.push_back(ENV1);
     429           1 :         different_child_env_vars.push_back(ENV2);
     430           1 :         different_child_env_vars.push_back(ENV4);
     431             : 
     432           2 :         CPPUNIT_ASSERT_MESSAGE
     433             :         (
     434             :             "osl_execProc_merged_child_environment",
     435             :             compare_merged_environments(different_child_env_vars)
     436           2 :         );
     437           1 :     }
     438             : 
     439           1 :     void osl_execProc_test_batch()
     440             :     {
     441             :         oslProcess process;
     442             : #if defined(WNT)
     443             :         rtl::OUString suBatch = suCWD + "/batch.bat";
     444             : #else
     445           1 :         rtl::OUString suBatch = suCWD + "/batch.sh";
     446             : #endif
     447             :         oslProcessError osl_error = osl_executeProcess(
     448             :             suBatch.pData,
     449             :             NULL,
     450             :             0,
     451             :             osl_Process_NORMAL,
     452             :             NULL,
     453             :             suCWD.pData,
     454             :             NULL,
     455             :             0,
     456           1 :             &process);
     457             : 
     458           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     459             :         (
     460             :             "osl_createProcess failed",
     461             :             osl_Process_E_None, osl_error
     462           1 :         );
     463             : 
     464           1 :         osl_error = ::osl_joinProcess(process);
     465             : 
     466           2 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     467             :         (
     468             :             "osl_joinProcess returned with failure",
     469             :             osl_Process_E_None, osl_error
     470           1 :         );
     471             : 
     472           1 :         osl_freeProcessHandle(process);
     473           1 :     }
     474             : 
     475           2 :     CPPUNIT_TEST_SUITE(Test_osl_executeProcess);
     476             :     //TODO: Repair these (at least under Windows)
     477             : #if !defined(WNT)
     478           1 :     CPPUNIT_TEST(osl_execProc_parent_equals_child_environment);
     479           1 :     CPPUNIT_TEST(osl_execProc_merged_child_environment);
     480             : #endif
     481           1 :     CPPUNIT_TEST(osl_execProc_test_batch);
     482             :     ///TODO: Repair test (or tested function ;-) - test fails.
     483           5 :     CPPUNIT_TEST_SUITE_END();
     484             : };
     485             : 
     486           1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test_osl_executeProcess);
     487             : 
     488           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     489             : 
     490             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11