xref: /aoo42x/main/shell/source/unix/exec/urltest.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "shellexec.hxx"
29 
30 #include <osl/process.h>
31 
32 #include <stdio.h>
33 #include <limits.h>
34 #include <string.h>
35 #include <strings.h>
36 
37 // -----------------------------------------------------------------------
38 
39 int main(int argc, const char *argv[])
40 {
41     int ret = 0;
42 
43     if( argc != 2 )
44     {
45         fprintf(stderr, "Usage: urltest <urllist>\n");
46         return -1;
47     }
48 
49     FILE * fp = fopen( argv[1], "r" );
50     if( NULL == fp )
51     {
52         perror( argv[1] );
53         return -1;
54     }
55 
56     // expect urltest.sh beside this binary
57     char line[LINE_MAX];
58     size_t len = strlen(argv[0]);
59     strcpy( line, argv[0] );
60     strcpy( line + len, ".sh " );
61     len += 4;
62 
63     unsigned int errors = 0;
64 
65     // read url(s) to test from file
66     char url[512];
67     while( NULL != fgets(url, sizeof(url), fp))
68     {
69         // remove trailing line break
70         strtok( url, "\r\n" );
71 
72         printf( "Passing URL: %s\n", url );
73 
74         // test the encoding functionality from shellexec.cxx
75         rtl::OString aURL( url );
76         rtl::OStringBuffer aBuffer;
77         escapeForShell(aBuffer, aURL);
78 
79         // append encoded URL as (only) parameter to the script
80         strcpy( line + len, aBuffer.getStr() );
81 
82         printf( "Command line: %s\n", line );
83 
84         FILE * pipe = popen( line, "r" );
85         if( NULL != pipe )
86         {
87             char buffer[BUFSIZ];
88 
89             // initialize buffer with '\0'
90             memset(buffer, '\0', BUFSIZ);
91 
92             // read the output of the script
93             if(NULL == fgets( buffer, BUFSIZ, pipe))
94             {
95                 perror("FAILED: output of script could not be read");
96                 printf( "\n");
97                 ++errors;
98                 continue;
99             }
100 
101             // remove trailing line break again
102             strtok( buffer, "\r\n" );
103 
104             int n = pclose(pipe);
105             if( 0 != n )
106             {
107                 printf("FAILED: fclose returned %d\n\n", n );
108                 ++errors;
109                 continue;
110             }
111 
112             if( 0 == strcmp( url, buffer ) )
113             {
114                 // strings are identical: good !
115                 printf( "OK\n\n");
116             }
117             else
118             {
119                 // compare failed
120                 printf( "FAILED: returned string is %s\n\n", buffer);
121                 ++errors;
122             }
123 
124         }
125         else
126         {
127             perror( line );
128             ret = -2;
129             break;
130         }
131     }
132 
133     if( ferror( fp ) )
134     {
135         perror( argv[1] );
136         ret = -1;
137     }
138 
139     fclose( fp );
140 
141     if( errors )
142     {
143         printf( "Number of tests failing: %d\n", errors);
144         ret = -3;
145     }
146     else
147         printf( "All tests passed OK.\n" );
148 
149 
150     return ret;
151 }
152