merge the formfield patch from ooo-build
[ooovba.git] / shell / source / unix / exec / urltest.cxx
blobfc46d331b51a6b95df2d51a6ae3e262d7a7753fd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urltest.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "shellexec.hxx"
33 #include <osl/process.h>
35 #include <stdio.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <strings.h>
40 // -----------------------------------------------------------------------
42 int main(int argc, const char *argv[])
44 int ret = 0;
46 if( argc != 2 )
48 fprintf(stderr, "Usage: urltest <urllist>\n");
49 return -1;
52 FILE * fp = fopen( argv[1], "r" );
53 if( NULL == fp )
55 perror( argv[1] );
56 return -1;
59 // expect urltest.sh beside this binary
60 char line[LINE_MAX];
61 size_t len = strlen(argv[0]);
62 strcpy( line, argv[0] );
63 strcpy( line + len, ".sh " );
64 len += 4;
66 unsigned int errors = 0;
68 // read url(s) to test from file
69 char url[512];
70 while( NULL != fgets(url, sizeof(url), fp))
72 // remove trailing line break
73 strtok( url, "\r\n" );
75 printf( "Passing URL: %s\n", url );
77 // test the encoding functionality from shellexec.cxx
78 rtl::OString aURL( url );
79 rtl::OStringBuffer aBuffer;
80 escapeForShell(aBuffer, aURL);
82 // append encoded URL as (only) parameter to the script
83 strcpy( line + len, aBuffer.getStr() );
85 printf( "Command line: %s\n", line );
87 FILE * pipe = popen( line, "r" );
88 if( NULL != pipe )
90 char buffer[BUFSIZ];
92 // initialize buffer with '\0'
93 memset(buffer, '\0', BUFSIZ);
95 // read the output of the script
96 if(NULL == fgets( buffer, BUFSIZ, pipe))
98 perror("FAILED: output of script could not be read");
99 printf( "\n");
100 ++errors;
101 continue;
104 // remove trailing line break again
105 strtok( buffer, "\r\n" );
107 int n = pclose(pipe);
108 if( 0 != n )
110 printf("FAILED: fclose returned %d\n\n", n );
111 ++errors;
112 continue;
115 if( 0 == strcmp( url, buffer ) )
117 // strings are identical: good !
118 printf( "OK\n\n");
120 else
122 // compare failed
123 printf( "FAILED: returned string is %s\n\n", buffer);
124 ++errors;
128 else
130 perror( line );
131 ret = -2;
132 break;
136 if( ferror( fp ) )
138 perror( argv[1] );
139 ret = -1;
142 fclose( fp );
144 if( errors )
146 printf( "Number of tests failing: %d\n", errors);
147 ret = -3;
149 else
150 printf( "All tests passed OK.\n" );
153 return ret;