session.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Tests for session handling
  3. Copyright (C) 2002-2006, 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. #include "config.h"
  17. #include <sys/types.h>
  18. #ifdef HAVE_STDLIB_H
  19. #include <stdlib.h>
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include "ne_session.h"
  25. #include "tests.h"
  26. static int fill_uri(void)
  27. {
  28. ne_uri uri = {0};
  29. ne_session *sess = ne_session_create("http", "localhost", 1234);
  30. ne_fill_server_uri(sess, &uri);
  31. ONCMP("localhost", uri.host, "fill_uri", "host");
  32. ONN("port mis-match", uri.port != 1234);
  33. ONCMP("http", uri.scheme, "fill_uri", "scheme");
  34. ne_session_destroy(sess);
  35. ne_uri_free(&uri);
  36. return OK;
  37. }
  38. static int fill_proxy_uri(void)
  39. {
  40. ne_uri uri = {0};
  41. ne_session *sess = ne_session_create("http", "localhost", 1234);
  42. ne_fill_proxy_uri(sess, &uri);
  43. ONN("no proxy host should be set", uri.host != NULL);
  44. ONN("no proxy port should be set", uri.port != 0);
  45. ne_session_proxy(sess, "www.example.com", 345);
  46. ne_fill_proxy_uri(sess, &uri);
  47. ONCMP("www.example.com", uri.host, "fill_proxy_uri", "host");
  48. ONN("proxy port mis-match", uri.port != 345);
  49. ne_session_destroy(sess);
  50. ne_uri_free(&uri);
  51. return OK;
  52. }
  53. static int match_hostport(const char *scheme, const char *hostname, int port,
  54. const char *hostport)
  55. {
  56. ne_session *sess = ne_session_create(scheme, hostname, port);
  57. const char *hp = ne_get_server_hostport(sess);
  58. ONV(strcmp(hp, hostport),
  59. ("hostport incorrect for %s: `%s' not `%s'", scheme, hp, hostport));
  60. ne_session_destroy(sess);
  61. return OK;
  62. }
  63. static int hostports(void)
  64. {
  65. static const struct {
  66. const char *scheme, *hostname;
  67. int port;
  68. const char *hostport;
  69. } hps[] = {
  70. { "http", "host.name", 80, "host.name" },
  71. { "http", "host.name", 555, "host.name:555" },
  72. { "http", "host.name", 443, "host.name:443" },
  73. { "https", "host.name", 80, "host.name:80" },
  74. { "https", "host.name", 443, "host.name" },
  75. { "https", "host.name", 700, "host.name:700" },
  76. { NULL }
  77. };
  78. int n;
  79. for (n = 0; hps[n].scheme; n++) {
  80. CALL(match_hostport(hps[n].scheme, hps[n].hostname,
  81. hps[n].port, hps[n].hostport));
  82. }
  83. return OK;
  84. }
  85. /* Check that ne_set_error is passing through to printf correctly. */
  86. static int errors(void)
  87. {
  88. ne_session *sess = ne_session_create("http", "foo.com", 80);
  89. #define EXPECT "foo, hello world, 100, bar!"
  90. ne_set_error(sess, "foo, %s, %d, bar!", "hello world", 100);
  91. ONV(strcmp(ne_get_error(sess), EXPECT),
  92. ("session error was `%s' not `%s'",
  93. ne_get_error(sess), EXPECT));
  94. #undef EXPECT
  95. ne_session_destroy(sess);
  96. return OK;
  97. }
  98. #define ID1 "foo"
  99. #define ID2 "bar"
  100. static int privates(void)
  101. {
  102. ne_session *sess = ne_session_create("http", "localhost", 80);
  103. char *v1 = "hello", *v2 = "world";
  104. ne_set_session_private(sess, ID1, v1);
  105. ne_set_session_private(sess, ID2, v2);
  106. #define PRIV(msg, id, val) \
  107. ONN(msg, ne_get_session_private(sess, id) != val)
  108. PRIV("private #1 wrong", ID1, v1);
  109. PRIV("private #2 wrong", ID2, v2);
  110. PRIV("unknown id wrong", "no such ID", NULL);
  111. ne_session_destroy(sess);
  112. return OK;
  113. }
  114. /* test that ne_session_create doesn't really care what scheme you
  115. * give it, and that ne_get_scheme() works. */
  116. static int get_scheme(void)
  117. {
  118. static const char *schemes[] = {
  119. "http", "https", "ftp", "ldap", "foobar", NULL
  120. };
  121. int n;
  122. for (n = 0; schemes[n]; n++) {
  123. ne_session *sess = ne_session_create(schemes[n], "localhost", 80);
  124. ONV(strcmp(ne_get_scheme(sess), schemes[n]),
  125. ("scheme was `%s' not `%s'!", ne_get_scheme(sess), schemes[n]));
  126. ne_session_destroy(sess);
  127. }
  128. return OK;
  129. }
  130. static int flags(void)
  131. {
  132. ne_session *sess = ne_session_create("https", "localhost", 443);
  133. ne_set_session_flag(sess, NE_SESSFLAG_PERSIST, 1);
  134. ONN("persist flag was not set",
  135. ne_get_session_flag(sess, NE_SESSFLAG_PERSIST) != 1);
  136. ne_set_session_flag(sess, NE_SESSFLAG_LAST, 1);
  137. ONN("unsupported flag was recognized",
  138. ne_get_session_flag(sess, NE_SESSFLAG_LAST) != -1);
  139. ne_session_destroy(sess);
  140. return OK;
  141. }
  142. static int proxies(void)
  143. {
  144. ne_session *sess = ne_session_create("https", "localhost", 443);
  145. ne_session_proxy(sess, "http", 80);
  146. ne_set_addrlist2(sess, 80, NULL, 0);
  147. ne_session_destroy(sess);
  148. return OK;
  149. }
  150. static int tls_names(void)
  151. {
  152. static const struct {
  153. unsigned int proto;
  154. const char *expected;
  155. } ts[] = {
  156. { NE_SSL_PROTO_SSL_3, "SSLv3" },
  157. { NE_SSL_PROTO_TLS_1_0, "TLSv1.0" },
  158. { NE_SSL_PROTO_TLS_1_1, "TLSv1.1" },
  159. { NE_SSL_PROTO_TLS_1_2, "TLSv1.2" },
  160. { NE_SSL_PROTO_TLS_1_3, "TLSv1.3" },
  161. { 0 },
  162. };
  163. unsigned n;
  164. for (n = 0; ts[n].proto; n++) {
  165. const char *actual = ne_ssl_proto_name(ts[n].proto);
  166. ONCMP(ts[n].expected, actual, "TLS", "protocol name");
  167. }
  168. return OK;
  169. }
  170. ne_test tests[] = {
  171. T(fill_uri),
  172. T(fill_proxy_uri),
  173. T(hostports),
  174. T(errors),
  175. T(privates),
  176. T(get_scheme),
  177. T(flags),
  178. T(proxies),
  179. T(tls_names),
  180. T(NULL)
  181. };