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