PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/test_ext_network.cpp

https://github.com/tmjnaid/hiphop-php
C++ | 266 lines | 210 code | 39 blank | 17 comment | 1 complexity | e1e7f8f9d681dca52f9605e6354681e4 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <test/test_ext_network.h>
  17. #include <runtime/ext/ext_network.h>
  18. #include <runtime/ext/ext_file.h>
  19. #include <runtime/ext/ext_string.h>
  20. #include <runtime/base/util/string_buffer.h>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. bool TestExtNetwork::RunTests(const std::string &which) {
  23. bool ret = true;
  24. RUN_TEST(test_gethostbyaddr);
  25. RUN_TEST(test_gethostbyname);
  26. RUN_TEST(test_gethostbynamel);
  27. RUN_TEST(test_getprotobyname);
  28. RUN_TEST(test_getprotobynumber);
  29. RUN_TEST(test_getservbyname);
  30. RUN_TEST(test_getservbyport);
  31. RUN_TEST(test_inet_ntop);
  32. RUN_TEST(test_inet_pton);
  33. RUN_TEST(test_ip2long);
  34. RUN_TEST(test_long2ip);
  35. RUN_TEST(test_dns_check_record);
  36. RUN_TEST(test_checkdnsrr);
  37. RUN_TEST(test_dns_get_record);
  38. RUN_TEST(test_dns_get_mx);
  39. RUN_TEST(test_getmxrr);
  40. RUN_TEST(test_fsockopen);
  41. RUN_TEST(test_pfsockopen);
  42. RUN_TEST(test_socket_get_status);
  43. RUN_TEST(test_socket_set_blocking);
  44. RUN_TEST(test_socket_set_timeout);
  45. RUN_TEST(test_header);
  46. RUN_TEST(test_headers_list);
  47. RUN_TEST(test_headers_sent);
  48. RUN_TEST(test_header_remove);
  49. RUN_TEST(test_setcookie);
  50. RUN_TEST(test_setrawcookie);
  51. RUN_TEST(test_define_syslog_variables);
  52. RUN_TEST(test_openlog);
  53. RUN_TEST(test_closelog);
  54. RUN_TEST(test_syslog);
  55. return ret;
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////
  58. bool TestExtNetwork::test_gethostbyaddr() {
  59. VS(f_gethostbyaddr("127.0.0.1"), "localhost.localdomain");
  60. return Count(true);
  61. }
  62. bool TestExtNetwork::test_gethostbyname() {
  63. VS(f_gethostbyname("localhost"), "127.0.0.1");
  64. return Count(true);
  65. }
  66. bool TestExtNetwork::test_gethostbynamel() {
  67. VS(f_gethostbynamel("localhost"), CREATE_VECTOR1("127.0.0.1"));
  68. return Count(true);
  69. }
  70. bool TestExtNetwork::test_getprotobyname() {
  71. VS(f_getprotobyname("tcp"), 6);
  72. return Count(true);
  73. }
  74. bool TestExtNetwork::test_getprotobynumber() {
  75. VS(f_getprotobynumber(6), "tcp");
  76. return Count(true);
  77. }
  78. bool TestExtNetwork::test_getservbyname() {
  79. VS(f_getservbyname("http", "tcp"), 80);
  80. return Count(true);
  81. }
  82. bool TestExtNetwork::test_getservbyport() {
  83. VS(f_getservbyport(80, "tcp"), "http");
  84. return Count(true);
  85. }
  86. bool TestExtNetwork::test_inet_ntop() {
  87. String packed = f_chr(127) + f_chr(0) + f_chr(0) + f_chr(1);
  88. VS(f_inet_ntop(packed), "127.0.0.1");
  89. packed = f_str_repeat(f_chr(0), 15) + f_chr(1);
  90. VS(f_inet_ntop(packed), "::1");
  91. return Count(true);
  92. }
  93. bool TestExtNetwork::test_inet_pton() {
  94. String packed = f_chr(127) + f_chr(0) + f_chr(0) + f_chr(1);
  95. VS(f_inet_pton("127.0.0.1"), packed);
  96. packed = f_str_repeat(f_chr(0), 15) + f_chr(1);
  97. VS(f_inet_pton("::1"), packed);
  98. return Count(true);
  99. }
  100. bool TestExtNetwork::test_ip2long() {
  101. VS(f_ip2long("127.0.0.1"), 2130706433);
  102. return Count(true);
  103. }
  104. bool TestExtNetwork::test_long2ip() {
  105. VS(f_long2ip(2130706433), "127.0.0.1");
  106. return Count(true);
  107. }
  108. bool TestExtNetwork::test_dns_check_record() {
  109. VERIFY(f_dns_check_record("facebook.com"));
  110. return Count(true);
  111. }
  112. bool TestExtNetwork::test_checkdnsrr() {
  113. VERIFY(f_checkdnsrr("facebook.com"));
  114. return Count(true);
  115. }
  116. bool TestExtNetwork::test_dns_get_record() {
  117. VERIFY(!f_dns_get_record("facebook.com", k_DNS_A).toArray().empty());
  118. return Count(true);
  119. }
  120. bool TestExtNetwork::test_dns_get_mx() {
  121. Variant hosts;
  122. VERIFY(f_dns_get_mx("facebook.com", ref(hosts)));
  123. VERIFY(!hosts.toArray().empty());
  124. return Count(true);
  125. }
  126. bool TestExtNetwork::test_getmxrr() {
  127. Variant hosts;
  128. VERIFY(f_getmxrr("facebook.com", ref(hosts)));
  129. VERIFY(!hosts.toArray().empty());
  130. return Count(true);
  131. }
  132. bool TestExtNetwork::test_fsockopen() {
  133. {
  134. Variant f = f_fsockopen("facebook.com", 80);
  135. VERIFY(!same(f, false));
  136. f_fputs(f, "GET / HTTP/1.0\n\n");
  137. VERIFY(!f_fread(f, 15).toString().empty());
  138. }
  139. {
  140. Variant f = f_fsockopen("ssl://www.facebook.com", 443);
  141. VERIFY(!same(f, false));
  142. f_fwrite(f,
  143. "GET / HTTP/1.1\r\n"
  144. "Host: www.facebook.com\r\n"
  145. "Connection: Close\r\n"
  146. "\r\n");
  147. StringBuffer response;
  148. while (!same(f_feof(f), true)) {
  149. Variant line = f_fgets(f, 128);
  150. response.append(line.toString());
  151. }
  152. VERIFY(!response.detach().empty());
  153. }
  154. return Count(true);
  155. }
  156. bool TestExtNetwork::test_pfsockopen() {
  157. Variant f = f_pfsockopen("facebook.com", 80);
  158. VERIFY(!same(f, false));
  159. f_fputs(f, "GET / HTTP/1.0\n\n");
  160. VERIFY(!f_fread(f, 15).toString().empty());
  161. return Count(true);
  162. }
  163. bool TestExtNetwork::test_socket_get_status() {
  164. VS(f_socket_get_status(Object()), false);
  165. return Count(true);
  166. }
  167. bool TestExtNetwork::test_socket_set_blocking() {
  168. Variant f = f_fsockopen("facebook.com", 80);
  169. VERIFY(!same(f, false));
  170. f_socket_set_blocking(f, 0);
  171. return Count(true);
  172. }
  173. bool TestExtNetwork::test_socket_set_timeout() {
  174. Variant f = f_fsockopen("facebook.com", 80);
  175. VERIFY(!same(f, false));
  176. f_socket_set_timeout(f, 0);
  177. return Count(true);
  178. }
  179. bool TestExtNetwork::test_header() {
  180. f_header("Location: http://www.facebook.com");
  181. return Count(true);
  182. }
  183. bool TestExtNetwork::test_headers_list() {
  184. f_header("Location: http://www.facebook.com");
  185. VS(f_headers_list(), Array());
  186. return Count(true);
  187. }
  188. bool TestExtNetwork::test_headers_sent() {
  189. f_header("Location: http://www.facebook.com");
  190. VERIFY(!f_headers_sent());
  191. return Count(true);
  192. }
  193. bool TestExtNetwork::test_header_remove() {
  194. f_header_remove("name");
  195. f_header_remove();
  196. return Count(true);
  197. }
  198. bool TestExtNetwork::test_setcookie() {
  199. VERIFY(!f_setcookie("name", "value"));
  200. return Count(true);
  201. }
  202. bool TestExtNetwork::test_setrawcookie() {
  203. VERIFY(!f_setrawcookie("name", "value"));
  204. return Count(true);
  205. }
  206. bool TestExtNetwork::test_define_syslog_variables() {
  207. f_define_syslog_variables();
  208. return Count(true);
  209. }
  210. bool TestExtNetwork::test_openlog() {
  211. f_openlog("TestExtNetwork", k_LOG_ODELAY, k_LOG_USER);
  212. f_syslog(k_LOG_INFO, "testing");
  213. f_closelog();
  214. return Count(true);
  215. }
  216. bool TestExtNetwork::test_closelog() {
  217. f_openlog("TestExtNetwork", k_LOG_ODELAY, k_LOG_USER);
  218. f_syslog(k_LOG_INFO, "testing");
  219. f_closelog();
  220. return Count(true);
  221. }
  222. bool TestExtNetwork::test_syslog() {
  223. f_openlog("TestExtNetwork", k_LOG_ODELAY, k_LOG_USER);
  224. f_syslog(k_LOG_INFO, "testing");
  225. f_closelog();
  226. return Count(true);
  227. }