utils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. neon-specific test utils
  3. Copyright (C) 2001-2009, Joe Orton <[email protected]>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef UTILS_H
  17. #define UTILS_H 1
  18. #include "ne_request.h"
  19. #include "child.h"
  20. #define ONREQ(x) do { int _ret = (x); if (_ret) { t_context("line %d: HTTP error:\n%s", __LINE__, ne_get_error(sess)); return FAIL; } } while (0);
  21. int single_serve_string(ne_socket *s, void *userdata);
  22. int serve_response(ne_socket *s, const char *response);
  23. struct many_serve_args {
  24. int count;
  25. const char *str;
  26. };
  27. /* Serves args->str response args->count times down a single
  28. * connection. */
  29. int many_serve_string(ne_socket *s, void *userdata);
  30. /* Run a request using URI on the session. */
  31. int any_request(ne_session *sess, const char *uri);
  32. /* Run a request using URI on the session; fail on a non-2xx response.
  33. */
  34. int any_2xx_request(ne_session *sess, const char *uri);
  35. /* As above but with a request body. */
  36. int any_2xx_request_body(ne_session *sess, const char *uri);
  37. /* As any_2xx_request but with a specified method. */
  38. int any_2xx_request_method(ne_session *sess, const char *method,
  39. const char *uri);
  40. /* makes *session, spawns server which will run 'fn(userdata,
  41. * socket)'. sets error context if returns non-zero, i.e use like:
  42. * CALL(make_session(...)); */
  43. int make_session(ne_session **sess, server_fn fn, void *userdata);
  44. /* Returns hostname used for make_session(). */
  45. const char *get_session_host(void);
  46. /* Server which sleeps for 10 seconds then closes the socket. */
  47. int sleepy_server(ne_socket *sock, void *userdata);
  48. struct string {
  49. char *data;
  50. size_t len;
  51. };
  52. struct double_serve_args {
  53. struct string first, second;
  54. };
  55. /* Serve a struct string. */
  56. int serve_sstring(ne_socket *sock, void *ud);
  57. /* Discards an HTTP request, serves response ->first, discards another
  58. * HTTP request, then serves response ->second. */
  59. int double_serve_sstring(ne_socket *s, void *userdata);
  60. /* Serve a struct string slowly. */
  61. int serve_sstring_slowly(ne_socket *sock, void *ud);
  62. struct infinite {
  63. const char *header, *repeat;
  64. };
  65. /* Pass a "struct infinite *" as userdata, this function sends
  66. * ->header and then loops sending ->repeat forever. */
  67. int serve_infinite(ne_socket *sock, void *ud);
  68. /* SOCKS server stuff. */
  69. struct socks_server {
  70. enum ne_sock_sversion version;
  71. enum socks_failure {
  72. fail_none = 0,
  73. fail_init_vers,
  74. fail_init_close,
  75. fail_init_trunc,
  76. fail_no_auth,
  77. fail_bogus_auth,
  78. fail_auth_close,
  79. fail_auth_denied
  80. } failure;
  81. unsigned int expect_port;
  82. ne_inet_addr *expect_addr;
  83. const char *expect_fqdn;
  84. const char *username;
  85. const char *password;
  86. int say_hello;
  87. server_fn server;
  88. void *userdata;
  89. };
  90. int socks_server(ne_socket *sock, void *userdata);
  91. int full_write(ne_socket *sock, const char *data, size_t len);
  92. /* Create a session with server process running fn(userdata). Sets
  93. * test suite error on failure; initializes *sess with a new session
  94. * on success. Uses an unspecified hostname/port for the server. */
  95. int session_server(ne_session **sess, server_fn fn, void *userdata);
  96. /* Create a session for scheme with server process running count
  97. * multiple iterations fn(userdata). Sets test suite error on
  98. * failure; initializes *sess with a new session on success. Uses an
  99. * unspecified hostname/port for the server. */
  100. int multi_session_server(ne_session **sess, const char *scheme,
  101. const char *hostname,
  102. int count, server_fn fn, void *userdata);
  103. /* Create a session with server process running fn(userdata). Sets
  104. * test suite error on failure; initializes *sess with a new session
  105. * on success. Uses an unspecified hostname/port for the server;
  106. * session is created as if using origin 'host:fakeport' via HTTP
  107. * proxy to spawned server. */
  108. int proxied_session_server(ne_session **sess, const char *scheme,
  109. const char *host, unsigned int fakeport,
  110. server_fn fn, void *userdata);
  111. int proxied_multi_session_server(int count, ne_session **sess,
  112. const char *scheme, const char *host,
  113. unsigned int fakeport,
  114. server_fn fn, void *userdata);
  115. /* As per proxied_session_server, but uses a "fake" (direct) TCP proxy
  116. * rather than an HTTP proxy. */
  117. int fakeproxied_session_server(ne_session **sess, const char *scheme,
  118. const char *host, unsigned int fakeport,
  119. server_fn fn, void *userdata);
  120. /* As per fakeproxied_session_server, but also takes an iteration
  121. * count. */
  122. int fakeproxied_multi_session_server(int count,
  123. ne_session **sess, const char *scheme,
  124. const char *host, unsigned int fakeport,
  125. server_fn fn, void *userdata);
  126. /* Read contents of file 'filename' into buffer 'buf'. */
  127. int file_to_buffer(const char *filename, ne_buffer *buf);
  128. /* Notifier callback which serializes notifier invocations.
  129. * ne_buffer * must be passed as userdata. */
  130. void sess_notifier(void *userdata, ne_session_status status,
  131. const ne_session_status_info *info);
  132. #define MULTI_207(x) "HTTP/1.0 207 Foo\r\nConnection: close\r\n\r\n" \
  133. "<?xml version='1.0'?>\r\n" \
  134. "<D:multistatus xmlns:D='DAV:'>" x "</D:multistatus>"
  135. #define RESP_207(href, x) "<D:response><D:href>" href "</D:href>" x \
  136. "</D:response>"
  137. #define PSTAT_207(x) "<D:propstat>" x "</D:propstat>"
  138. #define STAT_207(s) "<D:status>HTTP/1.1 " s "</D:status>"
  139. #define DESCR_207(d) "<D:responsedescription>" d "</D:responsedescription>"
  140. #define DESCR_REM "The end of the world, as we know it"
  141. #define PROPS_207(x) "<D:prop>" x "</D:prop>"
  142. #define APROP_207(n, c) "<D:" n ">" c "</D:" n ">"
  143. #endif /* UTILS_H */