LCOV - code coverage report
Current view: top level - sal/qa/osl/process - osl_process.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 143 153 93.5 %
Date: 2012-08-25 Functions: 30 34 88.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 178 358 49.7 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifdef IOS
      30                 :            : #define CPPUNIT_PLUGIN_EXPORTED_NAME cppunitTest_osl_process
      31                 :            : #endif
      32                 :            : 
      33                 :            : #include <sal/types.h>
      34                 :            : #include <cppunit/TestFixture.h>
      35                 :            : #include <cppunit/extensions/HelperMacros.h>
      36                 :            : #include <cppunit/plugin/TestPlugIn.h>
      37                 :            : 
      38                 :            : #define t_print printf
      39                 :            : 
      40                 :            : #include <osl/process.h>
      41                 :            : #include <osl/file.hxx>
      42                 :            : #include <osl/thread.h>
      43                 :            : #include <rtl/ustring.hxx>
      44                 :            : #include <unistd.h>
      45                 :            : #include <signal.h>
      46                 :            : 
      47                 :            : #include <stdio.h>
      48                 :            : #include <stdlib.h>
      49                 :            : #include <osl/module.hxx>
      50                 :            : #include <sal/macros.h>
      51                 :            : 
      52                 :            : #if ( defined WNT )                     // Windows
      53                 :            : #   include <windows.h>
      54                 :            : #   include <tchar.h>
      55                 :            : #endif
      56                 :            : 
      57                 :            : #include "rtl/allocator.hxx"
      58                 :            : 
      59                 :            : #include <iostream>
      60                 :            : #include <fstream>
      61                 :            : #include <vector>
      62                 :            : #include <algorithm>
      63                 :            : #include <iterator>
      64                 :            : #include <string>
      65                 :            : 
      66                 :            : #ifdef UNX
      67                 :            : #if defined( MACOSX )
      68                 :            : # include <crt_externs.h>
      69                 :            : # define environ (*_NSGetEnviron())
      70                 :            : # else
      71                 :            :     extern char** environ;
      72                 :            : # endif
      73                 :            : #endif
      74                 :            : 
      75                 :            : #if defined(WNT)
      76                 :            :     const rtl::OUString EXECUTABLE_NAME ("osl_process_child.exe");
      77                 :            : #else
      78                 :          5 :     const rtl::OUString EXECUTABLE_NAME ("osl_process_child");
      79                 :            : #endif
      80                 :            : 
      81                 :            : //########################################
      82                 :            : using namespace osl;
      83                 :            : 
      84                 :            : using ::rtl::OUString;
      85                 :            : using ::rtl::OUStringToOString;
      86                 :            : using ::rtl::OString;
      87                 :            : 
      88                 :            : /** print a UNI_CODE String.
      89                 :            : */
      90                 :            : inline void printUString( const ::rtl::OUString & str )
      91                 :            : {
      92                 :            :     rtl::OString aString;
      93                 :            : 
      94                 :            :     t_print("#printUString_u# " );
      95                 :            :     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
      96                 :            :     t_print("%s\n", aString.getStr( ) );
      97                 :            : }
      98                 :            : 
      99                 :            : /** get binary Path.
     100                 :            : */
     101                 :         15 : inline ::rtl::OUString getExecutablePath( void )
     102                 :            : {
     103                 :         15 :     ::rtl::OUString dirPath;
     104         [ +  - ]:         15 :     osl::Module::getUrlFromAddress( ( void* ) &getExecutablePath, dirPath );
     105                 :         15 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') );
     106                 :         15 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
     107                 :         15 :     dirPath += rtl::OUString("Executable");
     108                 :         15 :     return dirPath;
     109                 :            : }
     110                 :            : 
     111                 :            : //rtl::OUString CWD = getExecutablePath();
     112                 :            : 
     113                 :            : //########################################
     114         [ #  # ]:          0 : class Test_osl_joinProcess : public CppUnit::TestFixture
     115                 :            : {
     116                 :            :     const OUString join_param_;
     117                 :            :     const OUString wait_time_;
     118                 :            :     OUString suCWD;
     119                 :            :     OUString suExecutableFileURL;
     120                 :            : 
     121                 :            :     rtl_uString* parameters_[2];
     122                 :            :     int          parameters_count_;
     123                 :            : 
     124                 :            : public:
     125                 :            : 
     126                 :          0 :     Test_osl_joinProcess() :
     127                 :            :         join_param_(OUString("-join")),
     128                 :            :         wait_time_(OUString("1")),
     129                 :          0 :         parameters_count_(2)
     130                 :            :     {
     131                 :          0 :         parameters_[0] = join_param_.pData;
     132                 :          0 :         parameters_[1] = wait_time_.pData;
     133         [ #  # ]:          0 :         suCWD = getExecutablePath();
     134                 :          0 :         suExecutableFileURL = suCWD;
     135                 :          0 :         suExecutableFileURL += rtl::OUString("/");
     136                 :          0 :         suExecutableFileURL += EXECUTABLE_NAME;
     137                 :          0 :     }
     138                 :            : 
     139                 :            :     /*-------------------------------------
     140                 :            :         Start a process and join with this
     141                 :            :         process specify a timeout so that
     142                 :            :         osl_joinProcessWithTimeout returns
     143                 :            :         osl_Process_E_TimedOut
     144                 :            :      -------------------------------------*/
     145                 :            : 
     146                 :            :     void osl_joinProcessWithTimeout_timeout_failure()
     147                 :            :     {
     148                 :            :         oslProcess process;
     149                 :            :         oslProcessError osl_error = osl_executeProcess(
     150                 :            :             suExecutableFileURL.pData,
     151                 :            :             parameters_,
     152                 :            :             parameters_count_,
     153                 :            :             osl_Process_NORMAL,
     154                 :            :             osl_getCurrentSecurity(),
     155                 :            :             suCWD.pData,
     156                 :            :             NULL,
     157                 :            :             0,
     158                 :            :             &process);
     159                 :            : 
     160                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     161                 :            :         (
     162                 :            :             "osl_createProcess failed",
     163                 :            :             osl_Process_E_None, osl_error
     164                 :            :         );
     165                 :            : 
     166                 :            :         TimeValue timeout;
     167                 :            :         timeout.Seconds = 1;
     168                 :            :         timeout.Nanosec = 0;
     169                 :            : 
     170                 :            :         osl_error = osl_joinProcessWithTimeout(process, &timeout);
     171                 :            : 
     172                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     173                 :            :         (
     174                 :            :             "osl_joinProcessWithTimeout returned without timeout failure",
     175                 :            :             osl_Process_E_TimedOut, osl_error
     176                 :            :         );
     177                 :            : 
     178                 :            :         osl_error = osl_terminateProcess(process);
     179                 :            : 
     180                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     181                 :            :         (
     182                 :            :             "osl_terminateProcess failed",
     183                 :            :             osl_Process_E_None, osl_error
     184                 :            :         );
     185                 :            : 
     186                 :            :         osl_freeProcessHandle(process);
     187                 :            :     }
     188                 :            : 
     189                 :            :     /*-------------------------------------
     190                 :            :         Start a process and join with this
     191                 :            :         process specify a timeout so that
     192                 :            :         osl_joinProcessWithTimeout returns
     193                 :            :         osl_Process_E_None
     194                 :            :      -------------------------------------*/
     195                 :            : 
     196                 :            :     void osl_joinProcessWithTimeout_without_timeout_failure()
     197                 :            :     {
     198                 :            :         oslProcess process;
     199                 :            :         oslProcessError osl_error = osl_executeProcess(
     200                 :            :             suExecutableFileURL.pData,
     201                 :            :             parameters_,
     202                 :            :             parameters_count_,
     203                 :            :             osl_Process_NORMAL,
     204                 :            :             osl_getCurrentSecurity(),
     205                 :            :             suCWD.pData,
     206                 :            :             NULL,
     207                 :            :             0,
     208                 :            :             &process);
     209                 :            : 
     210                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     211                 :            :         (
     212                 :            :             "osl_createProcess failed",
     213                 :            :             osl_Process_E_None, osl_error
     214                 :            :         );
     215                 :            : 
     216                 :            :         TimeValue timeout;
     217                 :            :         timeout.Seconds = 10;
     218                 :            :         timeout.Nanosec = 0;
     219                 :            : 
     220                 :            :         osl_error = osl_joinProcessWithTimeout(process, &timeout);
     221                 :            : 
     222                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     223                 :            :         (
     224                 :            :             "osl_joinProcessWithTimeout returned with failure",
     225                 :            :             osl_Process_E_None, osl_error
     226                 :            :         );
     227                 :            : 
     228                 :            :         osl_freeProcessHandle(process);
     229                 :            :     }
     230                 :            : 
     231                 :            :      /*-------------------------------------
     232                 :            :         Start a process and join with this
     233                 :            :         process specify an infinite timeout
     234                 :            :      -------------------------------------*/
     235                 :            : 
     236                 :            :     void osl_joinProcessWithTimeout_infinite()
     237                 :            :     {
     238                 :            :         oslProcess process;
     239                 :            :         oslProcessError osl_error = osl_executeProcess(
     240                 :            :             suExecutableFileURL.pData,
     241                 :            :             parameters_,
     242                 :            :             parameters_count_,
     243                 :            :             osl_Process_NORMAL,
     244                 :            :             osl_getCurrentSecurity(),
     245                 :            :             suCWD.pData,
     246                 :            :             NULL,
     247                 :            :             0,
     248                 :            :             &process);
     249                 :            : 
     250                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     251                 :            :         (
     252                 :            :             "osl_createProcess failed",
     253                 :            :             osl_Process_E_None, osl_error
     254                 :            :         );
     255                 :            : 
     256                 :            :         osl_error = osl_joinProcessWithTimeout(process, NULL);
     257                 :            : 
     258                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     259                 :            :         (
     260                 :            :             "osl_joinProcessWithTimeout returned with failure",
     261                 :            :             osl_Process_E_None, osl_error
     262                 :            :         );
     263                 :            : 
     264                 :            :         osl_freeProcessHandle(process);
     265                 :            :     }
     266                 :            : 
     267                 :            :      /*-------------------------------------
     268                 :            :         Start a process and join with this
     269                 :            :         process using osl_joinProcess
     270                 :            :      -------------------------------------*/
     271                 :            : 
     272                 :            :      void osl_joinProcess()
     273                 :            :     {
     274                 :            :         oslProcess process;
     275                 :            :         oslProcessError osl_error = osl_executeProcess(
     276                 :            :             suExecutableFileURL.pData,
     277                 :            :             parameters_,
     278                 :            :             parameters_count_,
     279                 :            :             osl_Process_NORMAL,
     280                 :            :             osl_getCurrentSecurity(),
     281                 :            :             suCWD.pData,
     282                 :            :             NULL,
     283                 :            :             0,
     284                 :            :             &process);
     285                 :            : 
     286                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     287                 :            :         (
     288                 :            :             "osl_createProcess failed",
     289                 :            :             osl_Process_E_None, osl_error
     290                 :            :         );
     291                 :            : 
     292                 :            :         osl_error = ::osl_joinProcess(process);
     293                 :            : 
     294                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     295                 :            :         (
     296                 :            :             "osl_joinProcess returned with failure",
     297                 :            :             osl_Process_E_None, osl_error
     298                 :            :         );
     299                 :            : 
     300                 :            :         osl_freeProcessHandle(process);
     301                 :            :     }
     302                 :            : 
     303                 :            :     CPPUNIT_TEST_SUITE(Test_osl_joinProcess);
     304                 :            :     CPPUNIT_TEST(osl_joinProcessWithTimeout_timeout_failure);
     305                 :            :     CPPUNIT_TEST(osl_joinProcessWithTimeout_without_timeout_failure);
     306                 :            :     CPPUNIT_TEST(osl_joinProcessWithTimeout_infinite);
     307                 :            :     CPPUNIT_TEST(osl_joinProcess);
     308                 :            :     CPPUNIT_TEST_SUITE_END();
     309                 :            : };
     310                 :            : 
     311                 :            : //#########################################################
     312                 :            : 
     313                 :            : typedef std::vector<std::string, rtl::Allocator<std::string> >  string_container_t;
     314                 :            : typedef string_container_t::const_iterator string_container_const_iter_t;
     315                 :            : typedef string_container_t::iterator       string_container_iter_t;
     316                 :            : 
     317                 :            : //#########################################################
     318                 :         60 : class exclude : public std::unary_function<std::string, bool>
     319                 :            : {
     320                 :            : public:
     321                 :            :     //------------------------------------------------
     322                 :         10 :     exclude(const string_container_t& exclude_list)
     323                 :         10 :     {
     324                 :         10 :         string_container_const_iter_t iter     = exclude_list.begin();
     325                 :         10 :         string_container_const_iter_t iter_end = exclude_list.end();
     326 [ +  - ][ +  + ]:         40 :         for (/**/; iter != iter_end; ++iter)
     327 [ +  - ][ +  - ]:         30 :             exclude_list_.push_back(env_var_name(*iter));
     328                 :         10 :     }
     329                 :            : 
     330                 :            :     //------------------------------------------------
     331                 :       6497 :     bool operator() (const std::string& env_var) const
     332                 :            :     {
     333                 :       6497 :         return (exclude_list_.end() !=
     334                 :            :                 std::find(
     335                 :            :                     exclude_list_.begin(),
     336                 :            :                     exclude_list_.end(),
     337 [ +  - ][ +  - ]:       6497 :                     env_var_name(env_var)));
     338                 :            :     }
     339                 :            : 
     340                 :            : private:
     341                 :            :     //-------------------------------------------------
     342                 :            :     // extract the name from an environment variable
     343                 :            :     // that is given in the form "NAME=VALUE"
     344                 :       6527 :     std::string env_var_name(const std::string& env_var) const
     345                 :            :     {
     346                 :            :         std::string::size_type pos_equal_sign =
     347                 :       6527 :             env_var.find_first_of("=");
     348                 :            : 
     349         [ +  - ]:       6527 :         if (std::string::npos != pos_equal_sign)
     350                 :       6527 :             return std::string(env_var, 0, pos_equal_sign);
     351                 :            : 
     352                 :       6527 :         return std::string();
     353                 :            :     }
     354                 :            : 
     355                 :            : private:
     356                 :            :     string_container_t exclude_list_;
     357                 :            : };
     358                 :            : 
     359                 :            : namespace
     360                 :            : {
     361                 :        100 :     class starts_with
     362                 :            :         : public std::unary_function<const std::string&, bool>
     363                 :            :     {
     364                 :            :     private:
     365                 :            :         const std::string m_rString;
     366                 :            :     public:
     367         [ +  - ]:         20 :         starts_with(const char *pString) : m_rString(pString) {}
     368                 :      12979 :         bool operator()(const std::string &rEntry) const
     369                 :            :         {
     370                 :      12979 :             return rEntry.find(m_rString) == 0;
     371                 :            :         }
     372                 :            :     };
     373                 :            : 
     374                 :         20 :     void tidy_container(string_container_t &env_container)
     375                 :            :     {
     376                 :            :         //sort them because there are no guarantees to ordering
     377                 :         20 :         std::sort(env_container.begin(), env_container.end());
     378                 :            :         //remove LD_PRELOAD because valgrind injects that into the
     379                 :            :         //parent process
     380                 :            :         env_container.erase(std::remove_if(env_container.begin(), env_container.end(),
     381 [ +  - ][ +  - ]:         20 :             starts_with("LD_PRELOAD=")), env_container.end());
     382                 :         20 :     }
     383                 :            : }
     384                 :            : 
     385                 :            : #ifdef WNT
     386                 :            :     void read_parent_environment(string_container_t* env_container)
     387                 :            :     {
     388                 :            :         LPTSTR env = reinterpret_cast<LPTSTR>(GetEnvironmentStrings());
     389                 :            :         LPTSTR p   = env;
     390                 :            : 
     391                 :            :         while (size_t l = _tcslen(p))
     392                 :            :         {
     393                 :            :             env_container->push_back(std::string(p));
     394                 :            :             p += l + 1;
     395                 :            :         }
     396                 :            :         FreeEnvironmentStrings(env);
     397                 :            :         tidy_container(*env_container);
     398                 :            :     }
     399                 :            : #else
     400                 :         10 :     void read_parent_environment(string_container_t* env_container)
     401                 :            :     {
     402         [ +  + ]:       6492 :         for (int i = 0; NULL != environ[i]; i++)
     403 [ +  - ][ +  - ]:       6482 :             env_container->push_back(std::string(environ[i]));
     404                 :         10 :         tidy_container(*env_container);
     405                 :         10 :     }
     406                 :            : #endif
     407                 :            : 
     408                 :            : //#########################################################
     409         [ -  + ]:         30 : class Test_osl_executeProcess : public CppUnit::TestFixture
     410                 :            : {
     411                 :            :     const OUString env_param_;
     412                 :            : 
     413                 :            :     OUString     temp_file_url_;
     414                 :            :     OUString     temp_file_path_;
     415                 :            :     rtl_uString* parameters_[2];
     416                 :            :     int          parameters_count_;
     417                 :            :     OUString    suCWD;
     418                 :            :     OUString    suExecutableFileURL;
     419                 :            : 
     420                 :            : public:
     421                 :            : 
     422                 :            :     //------------------------------------------------
     423                 :            :     // ctor
     424                 :         15 :     Test_osl_executeProcess() :
     425                 :            :         env_param_(OUString("-env")),
     426                 :         15 :         parameters_count_(2)
     427                 :            :     {
     428                 :         15 :         parameters_[0] = env_param_.pData;
     429         [ +  - ]:         15 :         suCWD = getExecutablePath();
     430                 :         15 :         suExecutableFileURL = suCWD;
     431                 :         15 :         suExecutableFileURL += rtl::OUString("/");
     432                 :         15 :         suExecutableFileURL += EXECUTABLE_NAME;
     433                 :         15 :     }
     434                 :            : 
     435                 :            :     //------------------------------------------------
     436                 :         15 :     virtual void setUp()
     437                 :            :     {
     438                 :         15 :         temp_file_path_ = create_temp_file(temp_file_url_);
     439                 :         15 :         parameters_[1]  = temp_file_path_.pData;
     440                 :         15 :     }
     441                 :            : 
     442                 :         15 :     virtual void tearDown()
     443                 :            :     {
     444                 :         15 :         osl::File::remove(temp_file_url_);
     445                 :         15 :     }
     446                 :            : 
     447                 :            :     //------------------------------------------------
     448                 :         15 :     OUString create_temp_file(OUString &temp_file_url)
     449                 :            :     {
     450         [ +  - ]:         15 :         FileBase::RC rc = FileBase::createTempFile(0, 0, &temp_file_url);
     451 [ +  - ][ +  - ]:         15 :         CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile failed", FileBase::E_None, rc);
         [ +  - ][ +  - ]
                 [ +  - ]
     452                 :            : 
     453                 :         15 :         OUString temp_file_path;
     454         [ +  - ]:         15 :         rc = FileBase::getSystemPathFromFileURL(temp_file_url, temp_file_path);
     455 [ +  - ][ +  - ]:         15 :         CPPUNIT_ASSERT_EQUAL_MESSAGE("getSystemPathFromFileURL failed", FileBase::E_None, rc);
         [ +  - ][ +  - ]
                 [ +  - ]
     456                 :            : 
     457                 :         15 :         return temp_file_path;
     458                 :            :     }
     459                 :            : 
     460                 :            :    //------------------------------------------------
     461                 :         10 :     void read_child_environment(string_container_t* env_container)
     462                 :            :     {
     463                 :            :         OString temp_file_name = OUStringToOString(OUString(
     464 [ +  - ][ +  - ]:         10 :             parameters_[1]), osl_getThreadTextEncoding());
     465         [ +  - ]:         10 :         std::ifstream file(temp_file_name.getStr());
     466                 :            : 
     467 [ +  - ][ +  - ]:         20 :         CPPUNIT_ASSERT_MESSAGE
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     468                 :            :         (
     469                 :            :             "I/O error, cannot open child environment file",
     470                 :            :             file.is_open()
     471         [ +  - ]:         10 :         );
     472                 :            : 
     473         [ +  - ]:         10 :         std::string line;
     474         [ +  - ]:         10 :         line.reserve(1024);
     475 [ +  - ][ +  - ]:       6507 :         while (std::getline(file, line, '\0'))
                 [ +  + ]
     476         [ +  - ]:       6497 :             env_container->push_back(line);
     477 [ +  - ][ +  - ]:         10 :         tidy_container(*env_container);
     478                 :         10 :     }
     479                 :            : 
     480                 :            :     //------------------------------------------------
     481                 :            :     // environment of the child process that was
     482                 :            :     // started. The child process writes his
     483                 :            :     // environment into a file
     484                 :          5 :     bool compare_environments()
     485                 :            :     {
     486         [ +  - ]:          5 :         string_container_t parent_env;
     487         [ +  - ]:          5 :         read_parent_environment(&parent_env);
     488                 :            : 
     489         [ +  - ]:          5 :         string_container_t child_env;
     490         [ +  - ]:          5 :         read_child_environment(&child_env);
     491                 :            : 
     492                 :          5 :         return ((parent_env.size() == child_env.size()) &&
     493 [ +  - ][ +  - ]:          5 :                 (std::equal(child_env.begin(), child_env.end(), parent_env.begin())));
                 [ +  - ]
     494                 :            :     }
     495                 :            : 
     496                 :            :     //------------------------------------------------
     497                 :            :     // compare the equal environment parts and the
     498                 :            :     // different part of the child environment
     499                 :          5 :     bool compare_merged_environments(const string_container_t& different_env_vars)
     500                 :            :     {
     501         [ +  - ]:          5 :         string_container_t parent_env;
     502         [ +  - ]:          5 :         read_parent_environment(&parent_env);
     503                 :            : 
     504                 :            : #if OSL_DEBUG_LEVEL > 1
     505                 :            :         for (string_container_t::const_iterator iter = parent_env.begin(), end = parent_env.end(); iter != end; ++iter)
     506                 :            :             std::cerr << "initially parent env: " << *iter << std::endl;
     507                 :            : #endif
     508                 :            : 
     509                 :            :         //remove the environment variables that we have changed
     510                 :            :         //in the child environment from the read parent environment
     511                 :            :         parent_env.erase(
     512                 :            :             std::remove_if(parent_env.begin(), parent_env.end(), exclude(different_env_vars)),
     513 [ +  - ][ +  - ]:          5 :             parent_env.end());
                 [ +  - ]
     514                 :            : 
     515                 :            : #if OSL_DEBUG_LEVEL > 1
     516                 :            :         for (string_container_t::const_iterator iter = parent_env.begin(), end = parent_env.end(); iter != end; ++iter)
     517                 :            :             std::cerr << "stripped parent env: " << *iter << std::endl;
     518                 :            : #endif
     519                 :            : 
     520                 :            :         //read the child environment and exclude the variables that
     521                 :            :         //are different
     522         [ +  - ]:          5 :         string_container_t child_env;
     523         [ +  - ]:          5 :         read_child_environment(&child_env);
     524                 :            : 
     525                 :            : #if OSL_DEBUG_LEVEL > 1
     526                 :            :         for (string_container_t::const_iterator iter = child_env.begin(), end = child_env.end(); iter != end; ++iter)
     527                 :            :             std::cerr << "initial child env: " << *iter << std::endl;
     528                 :            : #endif
     529                 :            :         //partition the child environment into the variables that
     530                 :            :         //are different to the parent environment (they come first)
     531                 :            :         //and the variables that should be equal between parent
     532                 :            :         //and child environment
     533                 :            :         string_container_iter_t iter_logical_end =
     534 [ +  - ][ +  - ]:          5 :             std::stable_partition(child_env.begin(), child_env.end(), exclude(different_env_vars));
     535                 :            : 
     536         [ +  - ]:          5 :         string_container_t different_child_env_vars(child_env.begin(), iter_logical_end);
     537         [ +  - ]:          5 :         child_env.erase(child_env.begin(), iter_logical_end);
     538                 :            : 
     539                 :            : #if OSL_DEBUG_LEVEL > 1
     540                 :            :         for (string_container_t::const_iterator iter = child_env.begin(), end = child_env.end(); iter != end; ++iter)
     541                 :            :             std::cerr << "stripped child env: " << *iter << std::endl;
     542                 :            : #endif
     543                 :            : 
     544                 :          5 :         bool common_env_size_equals    = (parent_env.size() == child_env.size());
     545         [ +  - ]:          5 :         bool common_env_content_equals = std::equal(child_env.begin(), child_env.end(), parent_env.begin());
     546                 :            : 
     547                 :            : #if OSL_DEBUG_LEVEL > 1
     548                 :            :         for (string_container_t::const_iterator iter = different_env_vars.begin(), end = different_env_vars.end(); iter != end; ++iter)
     549                 :            :             std::cerr << "different should be: " << *iter << std::endl;
     550                 :            : #endif
     551                 :            : 
     552                 :            : 
     553                 :            : #if OSL_DEBUG_LEVEL > 1
     554                 :            :         for (string_container_t::const_iterator iter = different_child_env_vars.begin(), end = different_child_env_vars.end(); iter != end; ++iter)
     555                 :            :             std::cerr << "different are: " << *iter << std::endl;
     556                 :            : #endif
     557                 :            : 
     558                 :          5 :         bool different_env_size_equals    = (different_child_env_vars.size() == different_env_vars.size());
     559                 :            :         bool different_env_content_equals =
     560         [ +  - ]:          5 :             std::equal(different_env_vars.begin(), different_env_vars.end(), different_child_env_vars.begin());
     561                 :            : 
     562                 :            :         return (common_env_size_equals && common_env_content_equals &&
     563 [ +  - ][ +  - ]:          5 :                 different_env_size_equals && different_env_content_equals);
         [ +  - ][ +  - ]
     564                 :            :     }
     565                 :            : 
     566                 :            :     //------------------------------------------------
     567                 :            :     // test that parent and child process have the
     568                 :            :     // same environment when osl_executeProcess will
     569                 :            :     // be called with out setting new environment
     570                 :            :     // variables
     571                 :          5 :    void osl_execProc_parent_equals_child_environment()
     572                 :            :     {
     573                 :            :         oslProcess process;
     574                 :            :         oslProcessError osl_error = osl_executeProcess(
     575                 :            :             suExecutableFileURL.pData,
     576                 :            :             parameters_,
     577                 :            :             parameters_count_,
     578                 :            :             osl_Process_NORMAL,
     579                 :            :             NULL,
     580                 :            :             suCWD.pData,
     581                 :            :             NULL,
     582                 :            :             0,
     583         [ +  - ]:          5 :             &process);
     584                 :            : 
     585 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     586                 :            :         (
     587                 :            :             "osl_createProcess failed",
     588                 :            :             osl_Process_E_None, osl_error
     589         [ +  - ]:          5 :         );
     590                 :            : 
     591         [ +  - ]:          5 :         osl_error = ::osl_joinProcess(process);
     592                 :            : 
     593 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     594                 :            :         (
     595                 :            :             "osl_joinProcess returned with failure",
     596                 :            :             osl_Process_E_None, osl_error
     597         [ +  - ]:          5 :         );
     598                 :            : 
     599         [ +  - ]:          5 :         osl_freeProcessHandle(process);
     600                 :            : 
     601 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_MESSAGE
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     602                 :            :         (
     603                 :            :             "Parent and child environment not equal",
     604                 :            :             compare_environments()
     605         [ +  - ]:          5 :         );
     606                 :          5 :     }
     607                 :            : 
     608                 :            :     //------------------------------------------------
     609                 :            :     #define ENV1 "PAT=a:\\"
     610                 :            :     #define ENV2 "PATHb=b:\\"
     611                 :            :     #define ENV3 "Patha=c:\\"
     612                 :            :     #define ENV4 "Patha=d:\\"
     613                 :            : 
     614                 :          5 :     void osl_execProc_merged_child_environment()
     615                 :            :     {
     616                 :            :         rtl_uString* child_env[4];
     617                 :          5 :         OUString env1(ENV1);
     618                 :          5 :         OUString env2(ENV2);
     619                 :          5 :         OUString env3(ENV3);
     620                 :          5 :         OUString env4(ENV4);
     621                 :            : 
     622                 :          5 :         child_env[0] = env1.pData;
     623                 :          5 :         child_env[1] = env2.pData;
     624                 :          5 :         child_env[2] = env3.pData;
     625                 :          5 :         child_env[3] = env4.pData;
     626                 :            : 
     627                 :            :         oslProcess process;
     628                 :            :         oslProcessError osl_error = osl_executeProcess(
     629                 :            :             suExecutableFileURL.pData,
     630                 :            :             parameters_,
     631                 :            :             parameters_count_,
     632                 :            :             osl_Process_NORMAL,
     633                 :            :             NULL,
     634                 :            :             suCWD.pData,
     635                 :            :             child_env,
     636                 :            :             SAL_N_ELEMENTS(child_env),
     637         [ +  - ]:          5 :             &process);
     638                 :            : 
     639 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     640                 :            :         (
     641                 :            :             "osl_createProcess failed",
     642                 :            :             osl_Process_E_None, osl_error
     643         [ +  - ]:          5 :         );
     644                 :            : 
     645         [ +  - ]:          5 :         osl_error = ::osl_joinProcess(process);
     646                 :            : 
     647 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     648                 :            :         (
     649                 :            :             "osl_joinProcess returned with failure",
     650                 :            :             osl_Process_E_None, osl_error
     651         [ +  - ]:          5 :         );
     652                 :            : 
     653         [ +  - ]:          5 :         osl_freeProcessHandle(process);
     654                 :            : 
     655         [ +  - ]:          5 :         string_container_t different_child_env_vars;
     656 [ +  - ][ +  - ]:          5 :         different_child_env_vars.push_back(ENV1);
     657 [ +  - ][ +  - ]:          5 :         different_child_env_vars.push_back(ENV2);
     658 [ +  - ][ +  - ]:          5 :         different_child_env_vars.push_back(ENV4);
     659                 :            : 
     660 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_MESSAGE
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     661                 :            :         (
     662                 :            :             "osl_execProc_merged_child_environment",
     663                 :            :             compare_merged_environments(different_child_env_vars)
     664         [ +  - ]:         10 :         );
     665                 :          5 :     }
     666                 :            : 
     667                 :          5 :     void osl_execProc_test_batch()
     668                 :            :     {
     669                 :            :         oslProcess process;
     670                 :            : #if defined(WNT)
     671                 :            :         rtl::OUString suBatch = suCWD + rtl::OUString("/") + rtl::OUString("batch.bat");
     672                 :            : #else
     673                 :          5 :         rtl::OUString suBatch = suCWD + rtl::OUString("/") + rtl::OUString("batch.sh");
     674                 :            : #endif
     675                 :            :         oslProcessError osl_error = osl_executeProcess(
     676                 :            :             suBatch.pData,
     677                 :            :             NULL,
     678                 :            :             0,
     679                 :            :             osl_Process_NORMAL,
     680                 :            :             NULL,
     681                 :            :             suCWD.pData,
     682                 :            :             NULL,
     683                 :            :             0,
     684         [ +  - ]:          5 :             &process);
     685                 :            : 
     686 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     687                 :            :         (
     688                 :            :             "osl_createProcess failed",
     689                 :            :             osl_Process_E_None, osl_error
     690         [ +  - ]:          5 :         );
     691                 :            : 
     692         [ +  - ]:          5 :         osl_error = ::osl_joinProcess(process);
     693                 :            : 
     694 [ +  - ][ +  - ]:         10 :         CPPUNIT_ASSERT_EQUAL_MESSAGE
         [ +  - ][ +  - ]
     695                 :            :         (
     696                 :            :             "osl_joinProcess returned with failure",
     697                 :            :             osl_Process_E_None, osl_error
     698         [ +  - ]:          5 :         );
     699                 :            : 
     700         [ +  - ]:          5 :         osl_freeProcessHandle(process);
     701                 :          5 :     }
     702                 :            : 
     703                 :            :     void osl_execProc_exe_name_in_argument_list()
     704                 :            :     {
     705                 :            :         rtl_uString* params[3];
     706                 :            : 
     707                 :            :         params[0] = suExecutableFileURL.pData;
     708                 :            :         params[1] = env_param_.pData;
     709                 :            :         params[2] = temp_file_path_.pData;
     710                 :            :         oslProcess process;
     711                 :            :         oslProcessError osl_error = osl_executeProcess(
     712                 :            :             NULL,
     713                 :            :             params,
     714                 :            :             3,
     715                 :            :             osl_Process_NORMAL,
     716                 :            :             NULL,
     717                 :            :             suCWD.pData,
     718                 :            :             NULL,
     719                 :            :             0,
     720                 :            :             &process);
     721                 :            : 
     722                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     723                 :            :         (
     724                 :            :             "osl_createProcess failed",
     725                 :            :             osl_Process_E_None, osl_error
     726                 :            :         );
     727                 :            : 
     728                 :            :         osl_error = ::osl_joinProcess(process);
     729                 :            : 
     730                 :            :         CPPUNIT_ASSERT_EQUAL_MESSAGE
     731                 :            :         (
     732                 :            :             "osl_joinProcess returned with failure",
     733                 :            :             osl_Process_E_None, osl_error
     734                 :            :         );
     735                 :            : 
     736                 :            :         osl_freeProcessHandle(process);
     737                 :            :     }
     738                 :            : 
     739 [ +  - ][ +  - ]:         10 :     CPPUNIT_TEST_SUITE(Test_osl_executeProcess);
         [ +  - ][ +  - ]
                 [ #  # ]
     740                 :            :     //TODO: Repair these under windows.
     741                 :            : #ifndef WNT
     742 [ +  - ][ +  - ]:          5 :     CPPUNIT_TEST(osl_execProc_parent_equals_child_environment);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     743 [ +  - ][ +  - ]:          5 :     CPPUNIT_TEST(osl_execProc_merged_child_environment);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     744                 :            : #endif
     745 [ +  - ][ +  - ]:          5 :     CPPUNIT_TEST(osl_execProc_test_batch);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     746                 :            :     ///TODO: Repair test (or tested function ;-) - test fails.
     747                 :            :     // CPPUNIT_TEST(osl_execProc_exe_name_in_argument_list);
     748 [ +  - ][ +  - ]:         10 :     CPPUNIT_TEST_SUITE_END();
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     749                 :            : };
     750                 :            : 
     751                 :            : //#####################################
     752                 :            : // register test suites
     753                 :            : //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test_osl_joinProcess,    "Test_osl_joinProcess");
     754                 :          5 : CPPUNIT_TEST_SUITE_REGISTRATION(Test_osl_executeProcess);
     755                 :            : 
     756 [ +  - ][ +  - ]:         20 : CPPUNIT_PLUGIN_IMPLEMENT();
         [ +  - ][ +  - ]
         [ +  - ][ #  # ]
     757                 :            : 
     758                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10