PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/test_ext_stream.cpp

https://github.com/diegoIta/hiphop-php
C++ | 440 lines | 356 code | 62 blank | 22 comment | 6 complexity | 31dd3d32f24cc160565e6343af168fca 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_stream.h>
  17. #include <runtime/ext/ext_stream.h>
  18. #include <runtime/ext/ext_socket.h>
  19. #include <runtime/ext/ext_file.h>
  20. using namespace std;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. bool TestExtStream::RunTests(const std::string &which) {
  23. bool ret = true;
  24. RUN_TEST(test_stream_context_create);
  25. RUN_TEST(test_stream_context_get_default);
  26. RUN_TEST(test_stream_context_get_options);
  27. RUN_TEST(test_stream_context_set_option);
  28. RUN_TEST(test_stream_context_set_param);
  29. RUN_TEST(test_stream_copy_to_stream);
  30. RUN_TEST(test_stream_encoding);
  31. RUN_TEST(test_stream_bucket_append);
  32. RUN_TEST(test_stream_bucket_prepend);
  33. RUN_TEST(test_stream_bucket_make_writeable);
  34. RUN_TEST(test_stream_bucket_new);
  35. RUN_TEST(test_stream_filter_register);
  36. RUN_TEST(test_stream_filter_remove);
  37. RUN_TEST(test_stream_filter_append);
  38. RUN_TEST(test_stream_filter_prepend);
  39. RUN_TEST(test_stream_get_contents);
  40. RUN_TEST(test_stream_get_filters);
  41. RUN_TEST(test_stream_get_line);
  42. RUN_TEST(test_stream_get_meta_data);
  43. RUN_TEST(test_stream_get_transports);
  44. RUN_TEST(test_stream_get_wrappers);
  45. RUN_TEST(test_stream_register_wrapper);
  46. RUN_TEST(test_stream_wrapper_register);
  47. RUN_TEST(test_stream_wrapper_restore);
  48. RUN_TEST(test_stream_wrapper_unregister);
  49. RUN_TEST(test_stream_resolve_include_path);
  50. RUN_TEST(test_stream_select);
  51. RUN_TEST(test_stream_set_blocking);
  52. RUN_TEST(test_stream_set_timeout);
  53. RUN_TEST(test_stream_set_write_buffer);
  54. RUN_TEST(test_set_file_buffer);
  55. RUN_TEST(test_stream_socket_accept);
  56. RUN_TEST(test_stream_socket_server);
  57. RUN_TEST(test_stream_socket_client);
  58. RUN_TEST(test_stream_socket_enable_crypto);
  59. RUN_TEST(test_stream_socket_get_name);
  60. RUN_TEST(test_stream_socket_pair);
  61. RUN_TEST(test_stream_socket_recvfrom);
  62. RUN_TEST(test_stream_socket_sendto);
  63. RUN_TEST(test_stream_socket_shutdown);
  64. return ret;
  65. }
  66. // so we run on different range of ports every time
  67. static int get_random_port() {
  68. static int base = -1;
  69. if (base == -1) {
  70. base = 12345 + (int)((time(0) * 100) % 30000);
  71. }
  72. return ++base;
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////
  75. bool TestExtStream::test_stream_context_create() {
  76. f_stream_context_create();
  77. return Count(true);
  78. }
  79. bool TestExtStream::test_stream_context_get_default() {
  80. try {
  81. f_stream_context_get_default();
  82. } catch (NotImplementedException e) {
  83. return Count(true);
  84. }
  85. return Count(false);
  86. }
  87. bool TestExtStream::test_stream_context_get_options() {
  88. try {
  89. f_stream_context_get_options(Object());
  90. } catch (NotImplementedException e) {
  91. return Count(true);
  92. }
  93. return Count(false);
  94. }
  95. bool TestExtStream::test_stream_context_set_option() {
  96. try {
  97. f_stream_context_set_option(Object(), "");
  98. } catch (NotImplementedException e) {
  99. return Count(true);
  100. }
  101. return Count(false);
  102. }
  103. bool TestExtStream::test_stream_context_set_param() {
  104. try {
  105. f_stream_context_set_param(Object(), Array());
  106. } catch (NotImplementedException e) {
  107. return Count(true);
  108. }
  109. return Count(false);
  110. }
  111. bool TestExtStream::test_stream_copy_to_stream() {
  112. Variant src = f_fopen("test/test_ext_file.txt", "r");
  113. Variant dest = f_fopen("test/test_ext_file.tmp", "w");
  114. f_stream_copy_to_stream(src, dest);
  115. f_fclose(dest);
  116. Variant f = f_fopen("test/test_ext_file.tmp", "r");
  117. VS(f_fgets(f), "Testing Ext File\n");
  118. return Count(true);
  119. }
  120. bool TestExtStream::test_stream_encoding() {
  121. try {
  122. f_stream_encoding(Object());
  123. } catch (NotSupportedException e) {
  124. return Count(true);
  125. }
  126. return Count(false);
  127. }
  128. bool TestExtStream::test_stream_bucket_append() {
  129. try {
  130. f_stream_bucket_append(Object(), Object());
  131. } catch (NotSupportedException e) {
  132. return Count(true);
  133. }
  134. return Count(false);
  135. }
  136. bool TestExtStream::test_stream_bucket_prepend() {
  137. try {
  138. f_stream_bucket_prepend(Object(), Object());
  139. } catch (NotSupportedException e) {
  140. return Count(true);
  141. }
  142. return Count(false);
  143. }
  144. bool TestExtStream::test_stream_bucket_make_writeable() {
  145. try {
  146. f_stream_bucket_make_writeable(Object());
  147. } catch (NotSupportedException e) {
  148. return Count(true);
  149. }
  150. return Count(false);
  151. }
  152. bool TestExtStream::test_stream_bucket_new() {
  153. try {
  154. f_stream_bucket_new(Object(), "");
  155. } catch (NotSupportedException e) {
  156. return Count(true);
  157. }
  158. return Count(false);
  159. }
  160. bool TestExtStream::test_stream_filter_register() {
  161. try {
  162. f_stream_filter_register("", "");
  163. } catch (NotSupportedException e) {
  164. return Count(true);
  165. }
  166. return Count(false);
  167. }
  168. bool TestExtStream::test_stream_filter_remove() {
  169. try {
  170. f_stream_filter_remove(Object());
  171. } catch (NotSupportedException e) {
  172. return Count(true);
  173. }
  174. return Count(false);
  175. }
  176. bool TestExtStream::test_stream_filter_append() {
  177. try {
  178. f_stream_filter_append(Object(), "");
  179. } catch (NotSupportedException e) {
  180. return Count(true);
  181. }
  182. return Count(false);
  183. }
  184. bool TestExtStream::test_stream_filter_prepend() {
  185. try {
  186. f_stream_filter_prepend(Object(), "");
  187. } catch (NotSupportedException e) {
  188. return Count(true);
  189. }
  190. return Count(false);
  191. }
  192. bool TestExtStream::test_stream_get_contents() {
  193. {
  194. Variant f = f_fopen("test/test_ext_file.txt", "r");
  195. VS(f_stream_get_contents(f), "Testing Ext File\n");
  196. }
  197. {
  198. Variant f = f_tmpfile();
  199. f_fwrite(f, "fwrite1");
  200. f_fseek(f, 0); VS(f_stream_get_contents(f), "fwrite1");
  201. f_fwrite(f, "fwrite2");
  202. f_fseek(f, 0); VS(f_stream_get_contents(f), "fwrite2");
  203. f_fwrite(f, "fwrite3");
  204. VS(f_stream_get_contents(f), "");
  205. f_fclose(f);
  206. }
  207. return Count(true);
  208. }
  209. bool TestExtStream::test_stream_get_filters() {
  210. try {
  211. f_stream_get_filters();
  212. } catch (NotSupportedException e) {
  213. return Count(true);
  214. }
  215. return Count(false);
  216. }
  217. bool TestExtStream::test_stream_get_line() {
  218. Variant f = f_fopen("test/test_ext_file.txt", "r");
  219. VS(f_stream_get_line(f), "Testing Ext File\n");
  220. return Count(true);
  221. }
  222. bool TestExtStream::test_stream_get_meta_data() {
  223. int port = get_random_port();
  224. string address = string("127.0.0.1:") + boost::lexical_cast<string>(port);
  225. Variant server = f_stream_socket_server(address);
  226. Variant client = f_stream_socket_client(address);
  227. f_stream_set_timeout(client, 0, 500 * 1000); // 500ms
  228. Variant line = f_fgets(client);
  229. Variant meta = f_stream_get_meta_data(client);
  230. VS(meta["timed_out"], true);
  231. return Count(true);
  232. }
  233. bool TestExtStream::test_stream_get_transports() {
  234. VERIFY(f_stream_get_transports().size() > 0);
  235. return Count(true);
  236. }
  237. bool TestExtStream::test_stream_get_wrappers() {
  238. try {
  239. f_stream_get_wrappers();
  240. } catch (NotSupportedException e) {
  241. return Count(true);
  242. }
  243. return Count(false);
  244. }
  245. bool TestExtStream::test_stream_register_wrapper() {
  246. try {
  247. f_stream_register_wrapper("", "");
  248. } catch (NotSupportedException e) {
  249. return Count(true);
  250. }
  251. return Count(false);
  252. }
  253. bool TestExtStream::test_stream_wrapper_register() {
  254. try {
  255. f_stream_wrapper_register("", "");
  256. } catch (NotSupportedException e) {
  257. return Count(true);
  258. }
  259. return Count(false);
  260. }
  261. bool TestExtStream::test_stream_wrapper_restore() {
  262. try {
  263. f_stream_wrapper_restore("");
  264. } catch (NotSupportedException e) {
  265. return Count(true);
  266. }
  267. return Count(false);
  268. }
  269. bool TestExtStream::test_stream_wrapper_unregister() {
  270. try {
  271. f_stream_wrapper_unregister("");
  272. } catch (NotSupportedException e) {
  273. return Count(true);
  274. }
  275. return Count(false);
  276. }
  277. bool TestExtStream::test_stream_resolve_include_path() {
  278. try {
  279. f_stream_resolve_include_path("");
  280. } catch (NotSupportedException e) {
  281. return Count(true);
  282. }
  283. return Count(false);
  284. }
  285. bool TestExtStream::test_stream_select() {
  286. Variant f = f_fopen("test/test_ext_file.txt", "r");
  287. Variant reads = CREATE_VECTOR1(f);
  288. VERIFY(!same(f_stream_select(ref(reads), null, null, 0, 0), false));
  289. return Count(true);
  290. }
  291. bool TestExtStream::test_stream_set_blocking() {
  292. Variant f = f_fopen("test/test_ext_file.txt", "r");
  293. VERIFY(f_stream_set_blocking(f, 0));
  294. return Count(true);
  295. }
  296. bool TestExtStream::test_stream_set_timeout() {
  297. Variant f = f_fopen("test/test_ext_file.txt", "r");
  298. f_stream_set_timeout(f, 0);
  299. return Count(true);
  300. }
  301. bool TestExtStream::test_stream_set_write_buffer() {
  302. Variant f = f_fopen("test/test_ext_file.tmp", "w");
  303. f_stream_set_write_buffer(f, 1 /* PHP_STREAM_BUFFER_LINE */);
  304. f_fputs(f, "testing\nanother line\n");
  305. return Count(true);
  306. }
  307. bool TestExtStream::test_set_file_buffer() {
  308. Variant f = f_fopen("test/test_ext_file.tmp", "w");
  309. f_set_file_buffer(f, 1 /* PHP_STREAM_BUFFER_LINE */);
  310. f_fputs(f, "testing\nanother line\n");
  311. return Count(true);
  312. }
  313. bool TestExtStream::test_stream_socket_accept() {
  314. // tested in test_stream_socket_recvfrom
  315. return Count(true);
  316. }
  317. bool TestExtStream::test_stream_socket_server() {
  318. // tested in test_stream_socket_recvfrom
  319. return Count(true);
  320. }
  321. bool TestExtStream::test_stream_socket_client() {
  322. // tested in test_stream_socket_recvfrom
  323. return Count(true);
  324. }
  325. bool TestExtStream::test_stream_socket_enable_crypto() {
  326. try {
  327. f_stream_socket_enable_crypto(Object(), true);
  328. } catch (NotSupportedException e) {
  329. return Count(true);
  330. }
  331. return Count(false);
  332. }
  333. bool TestExtStream::test_stream_socket_get_name() {
  334. Variant client = f_stream_socket_client("facebook.com:80");
  335. VERIFY(!f_stream_socket_get_name(client, true).toString().empty());
  336. VERIFY(!f_stream_socket_get_name(client, false).toString().empty());
  337. return Count(true);
  338. }
  339. bool TestExtStream::test_stream_socket_pair() {
  340. Variant fds = f_stream_socket_pair(k_AF_UNIX, k_SOCK_STREAM, 0);
  341. VS(fds.toArray().size(), 2);
  342. VERIFY(more(fds[0], 0));
  343. VERIFY(more(fds[1], 0));
  344. return Count(true);
  345. }
  346. bool TestExtStream::test_stream_socket_recvfrom() {
  347. vector<string> addresses;
  348. int port = get_random_port();
  349. addresses.push_back("127.0.0.1:" + boost::lexical_cast<string>(port)); // TCP
  350. addresses.push_back("unix:///tmp/test_stream.sock"); // UNIX domain
  351. unlink("/tmp/test_stream.sock");
  352. for (unsigned int i = 0; i < addresses.size(); i++) {
  353. const string &address = addresses[i];
  354. Variant server = f_stream_socket_server(address);
  355. Variant client = f_stream_socket_client(address);
  356. Variant s = f_stream_socket_accept(server);
  357. String text = "testing";
  358. if (i == 1) {
  359. VERIFY(f_socket_send(client, text, 7, 0));
  360. } else {
  361. VERIFY(f_stream_socket_sendto(client, text, 0, address));
  362. }
  363. Variant buffer = f_stream_socket_recvfrom(s, 100);
  364. VS(buffer, "testing");
  365. }
  366. return Count(true);
  367. }
  368. bool TestExtStream::test_stream_socket_sendto() {
  369. // tested in test_stream_socket_recvfrom
  370. return Count(true);
  371. }
  372. bool TestExtStream::test_stream_socket_shutdown() {
  373. int port = get_random_port();
  374. string address = "127.0.0.1:" + boost::lexical_cast<string>(port);
  375. Variant server = f_stream_socket_server(address);
  376. VERIFY(f_stream_socket_shutdown(server, 0));
  377. return Count(true);
  378. }