]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/interop-client.php
Avoid warning
[SourceForge/phpwiki.git] / tests / xmlrpc / interop-client.php
1 <?php // #!/usr/local/bin/php -Cq
2 /*
3   This file is part of, or distributed with, libXMLRPC - a C library for 
4   xml-encoded function calls.
5
6   Author: Dan Libby (dan@libby.com)
7   Epinions.com may be contacted at feedback@epinions-inc.com
8 */
9
10 /*  
11   Copyright 2001 Epinions, Inc. 
12
13   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
14   of charge, to (a) use, copy, distribute, modify, perform and display this 
15   software and associated documentation files (the "Software"), and (b) 
16   permit others to whom the Software is furnished to do so as well.  
17
18   1) The above copyright notice and this permission notice shall be included 
19   without modification in all copies or substantial portions of the 
20   Software.  
21
22   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
23   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
24   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
25   PURPOSE OR NONINFRINGEMENT.  
26
27   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
28   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
29   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
30   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
31   DAMAGES.    
32
33 */
34
35 $cur_dir = getcwd();
36 # Add root dir to the path
37 if (substr(PHP_OS,0,3) == 'WIN')
38     $cur_dir = str_replace("\\","/", $cur_dir);
39 $rootdir = $cur_dir . '/../../';
40 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
41 $include_path = ini_get('include_path') . $ini_sep . $rootdir . $ini_sep . $rootdir . "lib/pear";
42 ini_set('include_path', $include_path);
43
44 if ($HTTP_SERVER_VARS["SERVER_NAME"] == 'phpwiki.sourceforge.net') {
45     ini_set('include_path', ini_get('include_path') . ":/usr/share/pear");
46 }
47 define('PHPWIKI_NOMAIN', true);
48 # Quiet warnings in IniConfig.php
49 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
50 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
51 # Other needed files
52 require_once $rootdir.'index.php';
53
54 // fake a POST request to be able to load our interfaces
55 $save = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'];
56 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = "POST";
57 include($rootdir."lib/XmlRpcServer.php");
58 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = $save;
59
60 include("./xmlrpc-servers.php");
61
62 function match($a, $b) {
63    $matches = true;
64    if (gettype($a) === "array") {
65       foreach ($a as $key => $c) {
66          $d = $b[$key];
67          if (!match($c, $d)) {
68             $matches = false;
69             break;
70          }
71       }
72    } else {
73       if ($a !== $b || xmlrpc_get_type($a) !== xmlrpc_get_type($b)) {
74          $matches = false;
75       }
76    }
77    return $matches;
78 }
79
80 function pass($method) {
81    echo "<font color='green'><b>pass</b></font> $method()<br>";
82 }
83
84 function fail($method, $sent, $received) {
85    echo "<font color='red'><b>fail</b></font> $method()<br>";
86    if ($sent) {
87      echo "<h3>sent</h3><xmp>";
88      var_dump($sent);
89      echo "</xmp>";
90    }
91
92    if ($received) {
93        echo "<h3>received</h3><xmp>";
94        var_dump($received);
95        echo "</xmp>";
96    }
97 }
98
99 function check_if_matches($method, $sent, $received) {
100    if (match($sent, $received)) {
101        pass($method);
102    }
103    else {
104        fail($method, $sent, $received);
105    }
106 }
107
108 function foo($method_name, $args) {
109     xmlrpc_encode_request($method_name, $args);
110 }
111
112 function run_test($server, $debug, $output, $method, $args) {
113     echo "<hr>";
114     $params = array($args);
115     $result =  xu_rpc_http_concise(array(method => $method,
116                                          args   => $params, 
117                                          host   => $server[host], 
118                                          uri    => $server[uri], 
119                                          port   => $server[port], 
120                                          debug  => $debug,
121                                          output => $output));
122     check_if_matches($method, $args, $result);
123     echo "</hr>";
124     flush();
125 }
126
127 function run_no_param_test($server, $debug, $output, $method) {
128     echo "<hr>";
129     $result =  xu_rpc_http_concise(array(method => $method,
130                                          host => $server[host], 
131                                          uri  => $server[uri], 
132                                          port => $server[port], 
133                                          debug => $debug,
134                                          output => $output));
135
136     if($result && gettype($result) === "integer") {
137         pass($method);
138     }
139     else {
140         fail($method, false, $result);
141     }
142    
143     flush();
144 }
145
146
147 $decl_1 = "IN CONGRESS, July 4, 1776.";
148 $decl_2 = "The unanimous Declaration of the thirteen united States of America,";
149 $decl_3 = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
150 $decl_4 = "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights,that among these are Life, Liberty and the pursuit of Happiness.--That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, --That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the same Object evinces a design to reduce them under absolute Despotism, it is their right, it is their duty, to throw off such Government, and to provide new Guards for their future security.--Such has been the patient sufferance of these Colonies; and such is now the necessity which constrains them to alter their former Systems of Government. The history of the present King of Great Britain is a history of repeated injuries and usurpations, all having in direct object the establishment of an absolute Tyranny over these States. To prove this, let Facts be submitted to a candid world.";
151
152 // a method to run interop tests against remote server. tests described at bottom.
153 function run_easy_tests($server, $debug=0, $output = null) {
154     global $decl_1, $decl_2, $decl_3, $decl_4;
155     run_test($server, $debug, $output, "interopEchoTests.echoString", "That government is best, which governs least");
156     run_test($server, $debug, $output, "interopEchoTests.echoBoolean", true);
157     run_test($server, $debug, $output, "interopEchoTests.echoInteger", 42);
158     run_test($server, $debug, $output, "interopEchoTests.echoFloat", 3.1416);
159     run_test($server, $debug, $output, "interopEchoTests.echoStruct", array("varFloat" => 1.2345,
160                                                                             "varInt" => 186000,
161                                                                             "varString" => "a string" ));
162     run_test($server, $debug, $output, "interopEchoTests.echoStringArray", array($decl_1, $decl_2, $decl_3, $decl_4));
163     run_test($server, $debug, $output, "interopEchoTests.echoIntegerArray", array(23, 234, 1, 0, -10, 999));
164     run_test($server, $debug, $output, "interopEchoTests.echoFloatArray", array(2.45, 9.9999));
165     run_test($server, $debug, $output, "interopEchoTests.echoStructArray", array(array("varFloat" => 1.2345,
166                                                                                        "varInt" => 186000,
167                                                                                        "varString" => "a string"),
168                                                                                  array("varFloat" => 10.98765,
169                                                                                        "varInt" => 3200,
170                                                                                        "varString" => "happy little string" )
171                                                                                  ));
172                                                                   
173     $foo = "some base64 string";
174     xmlrpc_set_type($foo, "base64");
175     run_test($server, $debug, $output, "interopEchoTests.echoBase64", $foo);
176
177     $foo="19980717T14:08:55";
178     xmlrpc_set_type($foo, "datetime");
179     run_test($server, $debug, $output, "interopEchoTests.echoDate", $foo);
180    
181     run_no_param_test($server, $debug, $output, "interopEchoTests.noInParams");
182 }
183
184 function ident($server, $debug=0, $output=null) {
185     $method = "interopEchoTests.whichToolkit";
186     $result =  xu_rpc_http_concise(array('method' => $method,
187                                          'host'   => $server['host'],
188                                          'uri'    => $server['uri'],
189                                          'port'   => $server['port'], 
190                                          'debug'  => $debug,
191                                          'output' => $output));
192     if ($result && $result['toolkitDocsUrl'] && !$result['faultCode']) {
193         pass($method);
194         echo "<br>";
195         foreach($result as $key => $value) {
196             if(substr($value, 0, 7) === "http://") {
197                 $value = "<a href='$value'>$value</a>";
198             }
199             echo "<b>$key:</b> $value<br>";
200         }
201     }
202     else {
203         fail($method, false, $result);
204     }
205 }
206
207 function run_stress_tests($server, $debug=0, $output=null) {
208     global $decl_1, $decl_2, $decl_3, $decl_4;
209     run_test($server, $debug, $output, "interopEchoTests.echoString", "XML Comment in a string: <!-- A comment -->");
210     run_test($server, $debug, $output, "interopEchoTests.echoInteger", 4200000000);
211     run_test($server, $debug, $output, "interopEchoTests.echoFloat", 1.2);
212     run_test($server, $debug, $output, "interopEchoTests.echoStruct", 
213              array("varFloat" => 1.2345,
214                    "varInt" => 186000,
215                    "varString" => "18 > 2 && 2 < 18 && 42 == the answer to life, the universe, and everything" ));
216     run_test($server, $debug, $output, "interopEchoTests.echoStringArray", 
217              array($decl_1, $decl_2, $decl_3, $decl_4, "non-ascii chars above 127 (165-170): ¥, ¦, §, ¨, ©, ª"));
218     run_test($server, $debug, $output, "interopEchoTests.echoIntegerArray", 
219              array(23, 234, 1, 0, -10, 999));
220     run_test($server, $debug, $output, "interopEchoTests.echoFloatArray", 
221              array(2.45, 9.9999));
222     run_test($server, $debug, $output, "interopEchoTests.echoStructArray", 
223              array(array("varFloat" => 1.2345,
224                          "varInt" => 186000,
225                          "varString" => "non-print char (8): \b"),
226                    array("varFloat" => 10.98765,
227                          "varInt" => 3200,
228                          "varString" => "happy little string" )
229                    ));
230 }
231
232 // a method to display an html form for invoking the script
233 function print_html_form($servers_list) {
234
235    echo <<< END
236 <h1>Choose an xmlrpc server to run interop tests against <i>live!</i></h1>
237 END;
238
239    print_servers_form($servers_list);
240
241 echo <<< END
242 <p>
243 <i>if you know of any other servers that support interop tests, please send a note to
244    <a href='mailto:xmlrpc-epi-devel@lists.sourceforge.net'>xmlrpc-epi-devel@lists.sourceforge.net</a>
245    and we will add it to the list</i>.
246 END;
247
248 }
249
250 // some code which determines if we are in form display or response mode.
251 $server_list = get_interop_servers();
252 $server = get_server_from_user($server_list);
253 if ($server) {
254    $debug = $GLOBALS['HTTP_GET_VARS']['debug'] || $GLOBALS['HTTP_GET_VARS']['start_debug'];
255    $output['version'] = $GLOBALS['HTTP_GET_VARS']['version'];
256    if ($server) {
257       $title = $server['title'];
258       echo "<h2><CENTER>Results for $title</CENTER></H2>";
259       
260       ident($server, $debug, $output);
261       
262       if($GLOBALS['HTTP_GET_VARS']['stress'] == 1) {
263          run_stress_tests($server, $debug, $output);
264       }
265       else {
266          run_easy_tests($server, $debug, $output);
267       }
268    }
269    else {
270       echo "<h3>invalid option</h3>";
271    }
272 }
273 else {
274    print_html_form($server_list);
275 }
276
277 /* Interop tests description:
278
279 interopEchoTests.echoString (inputString) -- Sends a random string to the server, and checks that the
280 response is a string whose value is the same as the string that was sent.
281
282 interopEchoTests.echoInteger (inputInteger) -- Sends a random integer to the server, and checks that
283 the response is an integer whose value is the same as the integer that was sent.
284
285 interopEchoTests.echoFloat (inputFloat) -- Sends a random floating point number to the server, and
286 checks that the response is a float whose value is the same as the number that was sent.
287
288 interopEchoTests.echoStruct (inputStruct) -- Sends a struct to the server, which contains three
289 elements: varString, varInt, and varFloat, a random string, a random integer, and a random float,
290 respectively, and checks that the response is a struct containing the same values that were sent.
291
292 interopEchoTests.echoStringArray (inputStringArray) -- Sends an array of random strings to the server,
293 and checks that the response is an array of strings, whose values are the same as the values that were
294 sent.
295
296 interopEchoTests.echoIntegerArray (inputIntegerArray) -- Sends an array of random integers to the
297 server, and checks that the response is an array of integers, whose values are the same as the values that
298 were sent.
299
300 interopEchoTests.echoFloatArray (inputFloatArray) -- Sends an array of random floating point numbers
301 to the server, and checks that the response is an array of floats, whose values are the same as the values
302 that were sent.
303
304 interopEchoTests.echoStructArray (inputStructArray) -- Sends an array of structs to the server, and
305 checks that the response is an array of structs, which contain the same values that were sent. Each struct
306 contains three elements: varString, varInt, and varFloat, a string, an integer, and a floating point number,
307 respectively.
308
309  */