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

Generated by: LCOV version 1.10