1*f8e2c85aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*f8e2c85aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*f8e2c85aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*f8e2c85aSAndrew Rist * distributed with this work for additional information 6*f8e2c85aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*f8e2c85aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*f8e2c85aSAndrew Rist * "License"); you may not use this file except in compliance 9*f8e2c85aSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*f8e2c85aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*f8e2c85aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*f8e2c85aSAndrew Rist * software distributed under the License is distributed on an 15*f8e2c85aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*f8e2c85aSAndrew Rist * KIND, either express or implied. See the License for the 17*f8e2c85aSAndrew Rist * specific language governing permissions and limitations 18*f8e2c85aSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*f8e2c85aSAndrew Rist *************************************************************/ 21*f8e2c85aSAndrew Rist 22*f8e2c85aSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "shellexec.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <osl/process.h> 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <stdio.h> 29cdf0e10cSrcweir #include <limits.h> 30cdf0e10cSrcweir #include <string.h> 31cdf0e10cSrcweir #include <strings.h> 32cdf0e10cSrcweir 33cdf0e10cSrcweir // ----------------------------------------------------------------------- 34cdf0e10cSrcweir 35cdf0e10cSrcweir int main(int argc, const char *argv[]) 36cdf0e10cSrcweir { 37cdf0e10cSrcweir int ret = 0; 38cdf0e10cSrcweir 39cdf0e10cSrcweir if( argc != 2 ) 40cdf0e10cSrcweir { 41cdf0e10cSrcweir fprintf(stderr, "Usage: urltest <urllist>\n"); 42cdf0e10cSrcweir return -1; 43cdf0e10cSrcweir } 44cdf0e10cSrcweir 45cdf0e10cSrcweir FILE * fp = fopen( argv[1], "r" ); 46cdf0e10cSrcweir if( NULL == fp ) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir perror( argv[1] ); 49cdf0e10cSrcweir return -1; 50cdf0e10cSrcweir } 51cdf0e10cSrcweir 52cdf0e10cSrcweir // expect urltest.sh beside this binary 53cdf0e10cSrcweir char line[LINE_MAX]; 54cdf0e10cSrcweir size_t len = strlen(argv[0]); 55cdf0e10cSrcweir strcpy( line, argv[0] ); 56cdf0e10cSrcweir strcpy( line + len, ".sh " ); 57cdf0e10cSrcweir len += 4; 58cdf0e10cSrcweir 59cdf0e10cSrcweir unsigned int errors = 0; 60cdf0e10cSrcweir 61cdf0e10cSrcweir // read url(s) to test from file 62cdf0e10cSrcweir char url[512]; 63cdf0e10cSrcweir while( NULL != fgets(url, sizeof(url), fp)) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir // remove trailing line break 66cdf0e10cSrcweir strtok( url, "\r\n" ); 67cdf0e10cSrcweir 68cdf0e10cSrcweir printf( "Passing URL: %s\n", url ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir // test the encoding functionality from shellexec.cxx 71cdf0e10cSrcweir rtl::OString aURL( url ); 72cdf0e10cSrcweir rtl::OStringBuffer aBuffer; 73cdf0e10cSrcweir escapeForShell(aBuffer, aURL); 74cdf0e10cSrcweir 75cdf0e10cSrcweir // append encoded URL as (only) parameter to the script 76cdf0e10cSrcweir strcpy( line + len, aBuffer.getStr() ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir printf( "Command line: %s\n", line ); 79cdf0e10cSrcweir 80cdf0e10cSrcweir FILE * pipe = popen( line, "r" ); 81cdf0e10cSrcweir if( NULL != pipe ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir char buffer[BUFSIZ]; 84cdf0e10cSrcweir 85cdf0e10cSrcweir // initialize buffer with '\0' 86cdf0e10cSrcweir memset(buffer, '\0', BUFSIZ); 87cdf0e10cSrcweir 88cdf0e10cSrcweir // read the output of the script 89cdf0e10cSrcweir if(NULL == fgets( buffer, BUFSIZ, pipe)) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir perror("FAILED: output of script could not be read"); 92cdf0e10cSrcweir printf( "\n"); 93cdf0e10cSrcweir ++errors; 94cdf0e10cSrcweir continue; 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir // remove trailing line break again 98cdf0e10cSrcweir strtok( buffer, "\r\n" ); 99cdf0e10cSrcweir 100cdf0e10cSrcweir int n = pclose(pipe); 101cdf0e10cSrcweir if( 0 != n ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir printf("FAILED: fclose returned %d\n\n", n ); 104cdf0e10cSrcweir ++errors; 105cdf0e10cSrcweir continue; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir if( 0 == strcmp( url, buffer ) ) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir // strings are identical: good ! 111cdf0e10cSrcweir printf( "OK\n\n"); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir else 114cdf0e10cSrcweir { 115cdf0e10cSrcweir // compare failed 116cdf0e10cSrcweir printf( "FAILED: returned string is %s\n\n", buffer); 117cdf0e10cSrcweir ++errors; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir } 121cdf0e10cSrcweir else 122cdf0e10cSrcweir { 123cdf0e10cSrcweir perror( line ); 124cdf0e10cSrcweir ret = -2; 125cdf0e10cSrcweir break; 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir if( ferror( fp ) ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir perror( argv[1] ); 132cdf0e10cSrcweir ret = -1; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir fclose( fp ); 136cdf0e10cSrcweir 137cdf0e10cSrcweir if( errors ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir printf( "Number of tests failing: %d\n", errors); 140cdf0e10cSrcweir ret = -3; 141cdf0e10cSrcweir } 142cdf0e10cSrcweir else 143cdf0e10cSrcweir printf( "All tests passed OK.\n" ); 144cdf0e10cSrcweir 145cdf0e10cSrcweir 146cdf0e10cSrcweir return ret; 147cdf0e10cSrcweir } 148