]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/interop-client.php
trailing_spaces
[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   $Id$
9 */
10
11 /*
12   Copyright 2001 Epinions, Inc.
13
14   Subject to the following 3 conditions, Epinions, Inc.  permits you, free
15   of charge, to (a) use, copy, distribute, modify, perform and display this
16   software and associated documentation files (the "Software"), and (b)
17   permit others to whom the Software is furnished to do so as well.
18
19   1) The above copyright notice and this permission notice shall be included
20   without modification in all copies or substantial portions of the
21   Software.
22
23   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
24   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
25   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
26   PURPOSE OR NONINFRINGEMENT.
27
28   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
29   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
30   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
31   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH
32   DAMAGES.
33
34 */
35
36 $cur_dir = getcwd();
37 # Add root dir to the path
38 if (substr(PHP_OS,0,3) == 'WIN')
39     $cur_dir = str_replace("\\","/", $cur_dir);
40 $rootdir = $cur_dir . '/../../';
41 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
42 $include_path = ini_get('include_path') . $ini_sep . $rootdir . $ini_sep . $rootdir . "lib/pear";
43 ini_set('include_path', $include_path);
44
45 if ($HTTP_SERVER_VARS["SERVER_NAME"] == 'phpwiki.sourceforge.net') {
46     ini_set('include_path', ini_get('include_path') . ":/usr/share/pear");
47 }
48 define('PHPWIKI_NOMAIN', true);
49 # Quiet warnings in IniConfig.php
50 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
51 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
52 # Other needed files
53 require_once $rootdir.'index.php';
54
55 // fake a POST request to be able to load our interfaces
56 $save = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'];
57 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = "POST";
58 include($rootdir."lib/XmlRpcServer.php");
59 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = $save;
60
61 include("./xmlrpc-servers.php");
62
63 function match($a, $b) {
64    $matches = true;
65    if (gettype($a) === "array") {
66       foreach ($a as $key => $c) {
67          $d = $b[$key];
68          if (!match($c, $d)) {
69             $matches = false;
70             break;
71          }
72       }
73    } else {
74       if ($a !== $b || xmlrpc_get_type($a) !== xmlrpc_get_type($b)) {
75          $matches = false;
76       }
77    }
78    return $matches;
79 }
80
81 function pass($method) {
82    echo "<font color='green'><b>pass</b></font> $method()<br>";
83 }
84
85 function fail($method, $sent, $received) {
86    echo "<font color='red'><b>fail</b></font> $method()<br>";
87    if ($sent) {
88      echo "<h3>sent</h3><xmp>";
89      var_dump($sent);
90      echo "</xmp>";
91    }
92
93    if ($received) {
94        echo "<h3>received</h3><xmp>";
95        var_dump($received);
96        echo "</xmp>";
97    }
98 }
99
100 function check_if_matches($method, $sent, $received) {
101    if (match($sent, $received)) {
102        pass($method);
103    }
104    else {
105        fail($method, $sent, $received);
106    }
107 }
108
109 function foo($method_name, $args) {
110     xmlrpc_encode_request($method_name, $args);
111 }
112
113 function run_test($server, $debug, $output, $method, $args) {
114     echo "<hr>";
115     $params = array($args);
116     $result =  xu_rpc_http_concise(array(method => $method,
117                                          args   => $params,
118                                          host   => $server[host],
119                                          uri    => $server[uri],
120                                          port   => $server[port],
121                                          debug  => $debug,
122                                          output => $output));
123     check_if_matches($method, $args, $result);
124     echo "</hr>";
125     flush();
126 }
127
128 function run_no_param_test($server, $debug, $output, $method) {
129     echo "<hr>";
130     $result =  xu_rpc_http_concise(array(method => $method,
131                                          host => $server[host],
132                                          uri  => $server[uri],
133                                          port => $server[port],
134                                          debug => $debug,
135                                          output => $output));
136
137     if($result && gettype($result) === "integer") {
138         pass($method);
139     }
140     else {
141         fail($method, false, $result);
142     }
143
144     flush();
145 }
146
147
148 $decl_1 = "IN CONGRESS, July 4, 1776.";
149 $decl_2 = "The unanimous Declaration of the thirteen united States of America,";
150 $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.";
151 $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.";
152
153 // a method to run interop tests against remote server. tests described at bottom.
154 function run_easy_tests($server, $debug=0, $output = null) {
155     global $decl_1, $decl_2, $decl_3, $decl_4;
156     run_test($server, $debug, $output, "interopEchoTests.echoString", "That government is best, which governs least");
157     run_test($server, $debug, $output, "interopEchoTests.echoBoolean", true);
158     run_test($server, $debug, $output, "interopEchoTests.echoInteger", 42);
159     run_test($server, $debug, $output, "interopEchoTests.echoFloat", 3.1416);
160     run_test($server, $debug, $output, "interopEchoTests.echoStruct", array("varFloat" => 1.2345,
161                                                                             "varInt" => 186000,
162                                                                             "varString" => "a string" ));
163     run_test($server, $debug, $output, "interopEchoTests.echoStringArray", array($decl_1, $decl_2, $decl_3, $decl_4));
164     run_test($server, $debug, $output, "interopEchoTests.echoIntegerArray", array(23, 234, 1, 0, -10, 999));
165     run_test($server, $debug, $output, "interopEchoTests.echoFloatArray", array(2.45, 9.9999));
166     run_test($server, $debug, $output, "interopEchoTests.echoStructArray", array(array("varFloat" => 1.2345,
167                                                                                        "varInt" => 186000,
168                                                                                        "varString" => "a string"),
169                                                                                  array("varFloat" => 10.98765,
170                                                                                        "varInt" => 3200,
171                                                                                        "varString" => "happy little string" )
172                                                                                  ));
173
174     $foo = "some base64 string";
175     xmlrpc_set_type($foo, "base64");
176     run_test($server, $debug, $output, "interopEchoTests.echoBase64", $foo);
177
178     $foo="19980717T14:08:55";
179     xmlrpc_set_type($foo, "datetime");
180     run_test($server, $debug, $output, "interopEchoTests.echoDate", $foo);
181
182     run_no_param_test($server, $debug, $output, "interopEchoTests.noInParams");
183 }
184
185 function ident($server, $debug=0, $output=null) {
186     $method = "interopEchoTests.whichToolkit";
187     $result =  xu_rpc_http_concise(array('method' => $method,
188                                          'host'   => $server['host'],
189                                          'uri'    => $server['uri'],
190                                          'port'   => $server['port'],
191                                          'debug'  => $debug,
192                                          'output' => $output));
193     if ($result && $result['toolkitDocsUrl'] && !$result['faultCode']) {
194         pass($method);
195         echo "<br>";
196         foreach($result as $key => $value) {
197             if(substr($value, 0, 7) === "http://") {
198                 $value = "<a href='$value'>$value</a>";
199             }
200             echo "<b>$key:</b> $value<br>";
201         }
202     }
203     else {
204         fail($method, false, $result);
205     }
206 }
207
208 function run_stress_tests($server, $debug=0, $output=null) {
209     global $decl_1, $decl_2, $decl_3, $decl_4;
210     run_test($server, $debug, $output, "interopEchoTests.echoString", "XML Comment in a string: <!-- A comment -->");
211     run_test($server, $debug, $output, "interopEchoTests.echoInteger", 4200000000);
212     run_test($server, $debug, $output, "interopEchoTests.echoFloat", 1.2);
213     run_test($server, $debug, $output, "interopEchoTests.echoStruct",
214              array("varFloat" => 1.2345,
215                    "varInt" => 186000,
216                    "varString" => "18 > 2 && 2 < 18 && 42 == the answer to life, the universe, and everything" ));
217     run_test($server, $debug, $output, "interopEchoTests.echoStringArray",
218              array($decl_1, $decl_2, $decl_3, $decl_4, "non-ascii chars above 127 (165-170): ¥, ¦, §, ¨, ©, ª"));
219     run_test($server, $debug, $output, "interopEchoTests.echoIntegerArray",
220              array(23, 234, 1, 0, -10, 999));
221     run_test($server, $debug, $output, "interopEchoTests.echoFloatArray",
222              array(2.45, 9.9999));
223     run_test($server, $debug, $output, "interopEchoTests.echoStructArray",
224              array(array("varFloat" => 1.2345,
225                          "varInt" => 186000,
226                          "varString" => "non-print char (8): \b"),
227                    array("varFloat" => 10.98765,
228                          "varInt" => 3200,
229                          "varString" => "happy little string" )
230                    ));
231 }
232
233 // a method to display an html form for invoking the script
234 function print_html_form($servers_list) {
235
236    echo <<< END
237 <h1>Choose an xmlrpc server to run interop tests against <i>live!</i></h1>
238 END;
239
240    print_servers_form($servers_list);
241
242 echo <<< END
243 <p>
244 <i>if you know of any other servers that support interop tests, please send a note to
245    <a href='mailto:xmlrpc-epi-devel@lists.sourceforge.net'>xmlrpc-epi-devel@lists.sourceforge.net</a>
246    and we will add it to the list</i>.
247 END;
248
249 }
250
251 // some code which determines if we are in form display or response mode.
252 $server_list = get_interop_servers();
253 $server = get_server_from_user($server_list);
254 if ($server) {
255    $debug = $GLOBALS['HTTP_GET_VARS']['debug'] || $GLOBALS['HTTP_GET_VARS']['start_debug'];
256    $output['version'] = $GLOBALS['HTTP_GET_VARS']['version'];
257    if ($server) {
258       $title = $server['title'];
259       echo "<h2><CENTER>Results for $title</CENTER></H2>";
260
261       ident($server, $debug, $output);
262
263       if($GLOBALS['HTTP_GET_VARS']['stress'] == 1) {
264          run_stress_tests($server, $debug, $output);
265       }
266       else {
267          run_easy_tests($server, $debug, $output);
268       }
269    }
270    else {
271       echo "<h3>invalid option</h3>";
272    }
273 }
274 else {
275    print_html_form($server_list);
276 }
277
278 /* Interop tests description:
279
280 interopEchoTests.echoString (inputString) -- Sends a random string to the server, and checks that the
281 response is a string whose value is the same as the string that was sent.
282
283 interopEchoTests.echoInteger (inputInteger) -- Sends a random integer to the server, and checks that
284 the response is an integer whose value is the same as the integer that was sent.
285
286 interopEchoTests.echoFloat (inputFloat) -- Sends a random floating point number to the server, and
287 checks that the response is a float whose value is the same as the number that was sent.
288
289 interopEchoTests.echoStruct (inputStruct) -- Sends a struct to the server, which contains three
290 elements: varString, varInt, and varFloat, a random string, a random integer, and a random float,
291 respectively, and checks that the response is a struct containing the same values that were sent.
292
293 interopEchoTests.echoStringArray (inputStringArray) -- Sends an array of random strings to the server,
294 and checks that the response is an array of strings, whose values are the same as the values that were
295 sent.
296
297 interopEchoTests.echoIntegerArray (inputIntegerArray) -- Sends an array of random integers to the
298 server, and checks that the response is an array of integers, whose values are the same as the values that
299 were sent.
300
301 interopEchoTests.echoFloatArray (inputFloatArray) -- Sends an array of random floating point numbers
302 to the server, and checks that the response is an array of floats, whose values are the same as the values
303 that were sent.
304
305 interopEchoTests.echoStructArray (inputStructArray) -- Sends an array of structs to the server, and
306 checks that the response is an array of structs, which contain the same values that were sent. Each struct
307 contains three elements: varString, varInt, and varFloat, a string, an integer, and a floating point number,
308 respectively.
309
310  */