stubs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. neon test suite
  3. Copyright (C) 2002-2005, 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. /** These tests show that the stub functions produce appropriate
  17. * results to provide ABI-compatibility when a particular feature is
  18. * not supported by the library.
  19. **/
  20. #include "config.h"
  21. #include <sys/types.h>
  22. #ifdef HAVE_STDLIB_H
  23. #include <stdlib.h>
  24. #endif
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include "ne_request.h"
  29. #include "ne_socket.h"
  30. #include "ne_compress.h"
  31. #include "ne_pkcs11.h"
  32. #include "tests.h"
  33. #include "child.h"
  34. #include "utils.h"
  35. #if defined(NE_HAVE_ZLIB) && defined(NE_HAVE_SSL) && defined(HAVE_PAKCHOIS)
  36. #define NO_TESTS 1
  37. #endif
  38. #define EOL "\r\n"
  39. #ifndef NE_HAVE_ZLIB
  40. static int sd_result = OK;
  41. static int sd_reader(void *ud, const char *block, size_t len)
  42. {
  43. const char *expect = ud;
  44. if (strncmp(expect, block, len) != 0) {
  45. sd_result = FAIL;
  46. t_context("decompress reader got bad data");
  47. }
  48. return 0;
  49. }
  50. static int stub_decompress(void)
  51. {
  52. ne_session *sess;
  53. ne_decompress *dc;
  54. ne_request *req;
  55. int ret;
  56. CALL(make_session(&sess, single_serve_string,
  57. "HTTP/1.1 200 OK" EOL
  58. "Connection: close" EOL EOL
  59. "abcde"));
  60. req = ne_request_create(sess, "GET", "/foo");
  61. dc = ne_decompress_reader(req, ne_accept_2xx, sd_reader, "abcde");
  62. ret = ne_request_dispatch(req);
  63. CALL(await_server());
  64. ONREQ(ret);
  65. ne_decompress_destroy(dc);
  66. ne_request_destroy(req);
  67. ne_session_destroy(sess);
  68. /* This is a skeleton test suite file. */
  69. return sd_result;
  70. }
  71. #endif
  72. #ifndef NE_HAVE_SSL
  73. static int stub_ssl(void)
  74. {
  75. ne_session *sess = ne_session_create("https", "localhost", 1234);
  76. ne_ssl_certificate *cert;
  77. ne_ssl_client_cert *cc;
  78. /* these should all fail when SSL is not supported. */
  79. cert = ne_ssl_cert_read("Makefile");
  80. if (cert) {
  81. char *dn, digest[60], date[NE_SSL_VDATELEN];
  82. const ne_ssl_certificate *issuer;
  83. /* This branch should never be executed, but lets pretend it
  84. * will to prevent the compiler optimising this code away if
  85. * it's placed after the cert != NULL test. And all that
  86. * needs to be tested is that these functions link OK. */
  87. dn = ne_ssl_readable_dname(ne_ssl_cert_subject(cert));
  88. ONN("this code shouldn't run", dn != NULL);
  89. dn = ne_ssl_readable_dname(ne_ssl_cert_issuer(cert));
  90. ONN("this code shouldn't run", dn != NULL);
  91. issuer = ne_ssl_cert_signedby(cert);
  92. ONN("this code shouldn't run", issuer != NULL);
  93. ONN("this code shouldn't run", ne_ssl_cert_digest(cert, digest));
  94. ne_ssl_cert_validity(cert, date, date);
  95. ONN("this code shouldn't run",
  96. ne_ssl_dname_cmp(ne_ssl_cert_subject(cert),
  97. ne_ssl_cert_issuer(cert)));
  98. ONN("this code shouldn't run", ne_ssl_cert_identity(issuer) != NULL);
  99. ONN("this code shouldn't run", ne_ssl_cert_export(cert) != NULL);
  100. ONN("this code shouldn't run", ne_ssl_cert_hdigest(cert, NE_HASH_MD5) != NULL);
  101. }
  102. ONN("this code shouldn't run", ne_ssl_cert_import("foo") != NULL);
  103. ONN("this code shouldn't run", ne_ssl_cert_read("Makefile") != NULL);
  104. ONN("this code shouldn't succeed", ne_ssl_cert_cmp(NULL, NULL) == 0);
  105. ONN("this code shouldn't run", ne_ssl_clicert_fromuri("Makefile", 0) != NULL);
  106. ONN("certificate load succeeded", cert != NULL);
  107. ne_ssl_cert_free(cert);
  108. cc = ne_ssl_clicert_read("Makefile");
  109. if (cc) {
  110. const char *name;
  111. /* dead branch as above. */
  112. cert = (void *)ne_ssl_clicert_owner(cc);
  113. ONN("this code shouldn't run", cert != NULL);
  114. name = ne_ssl_clicert_name(cc);
  115. ONN("this code shouldn't run", name != NULL);
  116. ONN("this code shouldn't run", ne_ssl_clicert_decrypt(cc, "fubar"));
  117. ne_ssl_set_clicert(sess, cc);
  118. }
  119. ONN("client certificate load succeeded", cc != NULL);
  120. ne_ssl_clicert_free(cc);
  121. ne_ssl_trust_default_ca(sess);
  122. ne_session_destroy(sess);
  123. return OK;
  124. }
  125. #endif
  126. #ifndef HAVE_PAKCHOIS
  127. static int stub_pkcs11(void)
  128. {
  129. ne_session *sess = ne_session_create("https", "localhost", 1234);
  130. ne_ssl_pkcs11_provider *prov;
  131. if (ne_ssl_pkcs11_provider_init(&prov, "neon-test-failure-case") == NE_PK11_OK) {
  132. ONN("this code shouldn't run", prov != NULL);
  133. ne_ssl_pkcs11_provider_pin(prov, NULL, NULL); /* noop */
  134. ne_ssl_set_pkcs11_provider(sess, prov); /* noop */
  135. ne_ssl_pkcs11_provider_destroy(prov);
  136. }
  137. ONN("must return NOTIMPL",
  138. ne_ssl_pkcs11_provider_init(&prov, "neon-test-failure-case-2") != NE_PK11_NOTIMPL);
  139. if (ne_ssl_pkcs11_nss_provider_init(&prov, "neon-test-failure-case",
  140. "neon-test", NULL, NULL, NULL) == NE_PK11_OK) {
  141. ONN("this code shouldn't run", prov != NULL);
  142. ne_ssl_pkcs11_provider_destroy(prov);
  143. }
  144. ne_session_destroy(sess);
  145. return OK;
  146. }
  147. #endif
  148. #ifdef NO_TESTS
  149. static int null_test(void) { return OK; }
  150. #endif
  151. ne_test tests[] = {
  152. #ifndef NE_HAVE_ZLIB
  153. T(stub_decompress),
  154. #endif
  155. #ifndef NE_HAVE_SSL
  156. T(stub_ssl),
  157. #endif
  158. #ifndef HAVE_PAKCHOIS
  159. T(stub_pkcs11),
  160. #endif
  161. /* to prevent failure when SSL and zlib are supported. */
  162. #ifdef NO_TESTS
  163. T(null_test),
  164. #endif
  165. T(NULL)
  166. };