1/*
2   neon test suite
3   Copyright (C) 2002-2005, Joe Orton <joe@manyfish.co.uk>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19*/
20
21/** These tests show that the stub functions produce appropriate
22 * results to provide ABI-compatibility when a particular feature is
23 * not supported by the library.
24 **/
25
26#include "config.h"
27
28#include <sys/types.h>
29
30#ifdef HAVE_STDLIB_H
31#include <stdlib.h>
32#endif
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36
37#include "ne_request.h"
38#include "ne_socket.h"
39#include "ne_compress.h"
40
41#include "tests.h"
42#include "child.h"
43#include "utils.h"
44
45#if defined(NE_HAVE_ZLIB) && defined(NE_HAVE_SSL)
46#define NO_TESTS 1
47#endif
48
49#define EOL "\r\n"
50
51#ifndef NE_HAVE_ZLIB
52static int sd_result = OK;
53
54static int sd_reader(void *ud, const char *block, size_t len)
55{
56    const char *expect = ud;
57    if (strncmp(expect, block, len) != 0) {
58	sd_result = FAIL;
59	t_context("decompress reader got bad data");
60    }
61    return 0;
62}
63
64static int stub_decompress(void)
65{
66    ne_session *sess;
67    ne_decompress *dc;
68    ne_request *req;
69    int ret;
70
71    CALL(make_session(&sess, single_serve_string,
72		      "HTTP/1.1 200 OK" EOL
73		      "Connection: close" EOL EOL
74		      "abcde"));
75
76    req = ne_request_create(sess, "GET", "/foo");
77
78    dc = ne_decompress_reader(req, ne_accept_2xx, sd_reader, "abcde");
79
80    ret = ne_request_dispatch(req);
81
82    CALL(await_server());
83
84    ONREQ(ret);
85
86    ne_decompress_destroy(dc);
87    ne_request_destroy(req);
88    ne_session_destroy(sess);
89
90    /* This is a skeleton test suite file. */
91    return sd_result;
92}
93#endif
94
95#ifndef NE_HAVE_SSL
96static int stub_ssl(void)
97{
98    ne_session *sess = ne_session_create("https", "localhost", 7777);
99    ne_ssl_certificate *cert;
100    ne_ssl_client_cert *cc;
101
102    /* these should all fail when SSL is not supported. */
103    cert = ne_ssl_cert_read("Makefile");
104    if (cert) {
105        char *dn, digest[60], date[NE_SSL_VDATELEN];
106        const ne_ssl_certificate *issuer;
107
108        /* This branch should never be executed, but lets pretend it
109         * will to prevent the compiler optimising this code away if
110         * it's placed after the cert != NULL test.  And all that
111         * needs to be tested is that these functions link OK. */
112        dn = ne_ssl_readable_dname(ne_ssl_cert_subject(cert));
113        ONN("this code shouldn't run", dn != NULL);
114        dn = ne_ssl_readable_dname(ne_ssl_cert_issuer(cert));
115        ONN("this code shouldn't run", dn != NULL);
116        issuer = ne_ssl_cert_signedby(cert);
117        ONN("this code shouldn't run", issuer != NULL);
118        ONN("this code shouldn't run", ne_ssl_cert_digest(cert, digest));
119        ne_ssl_cert_validity(cert, date, date);
120        ONN("this code shouldn't run",
121            ne_ssl_dname_cmp(ne_ssl_cert_subject(cert),
122                             ne_ssl_cert_issuer(cert)));
123        ONN("this code shouldn't run", ne_ssl_cert_identity(issuer) != NULL);
124        ONN("this code shouldn't run", ne_ssl_cert_export(cert) != NULL);
125    }
126
127    ONN("this code shouldn't run", ne_ssl_cert_import("foo") != NULL);
128    ONN("this code shouldn't run", ne_ssl_cert_read("Makefile") != NULL);
129    ONN("this code shouldn't succeed", ne_ssl_cert_cmp(NULL, NULL) == 0);
130
131    ONN("certificate load succeeded", cert != NULL);
132    ne_ssl_cert_free(cert);
133
134    cc = ne_ssl_clicert_read("Makefile");
135    if (cc) {
136        const char *name;
137        /* dead branch as above. */
138        cert = (void *)ne_ssl_clicert_owner(cc);
139        ONN("this code shouldn't run", cert != NULL);
140        name = ne_ssl_clicert_name(cc);
141        ONN("this code shouldn't run", name != NULL);
142        ONN("this code shouldn't run", ne_ssl_clicert_decrypt(cc, "fubar"));
143        ne_ssl_set_clicert(sess, cc);
144    }
145
146    ONN("client certificate load succeeded", cc != NULL);
147    ne_ssl_clicert_free(cc);
148
149    ne_ssl_trust_default_ca(sess);
150
151    ne_session_destroy(sess);
152    return OK;
153}
154#endif
155
156#ifdef NO_TESTS
157static int null_test(void) { return OK; }
158#endif
159
160ne_test tests[] = {
161#ifndef NE_HAVE_ZLIB
162    T(stub_decompress),
163#endif
164#ifndef NE_HAVE_SSL
165    T(stub_ssl),
166#endif
167/* to prevent failure when SSL and zlib are supported. */
168#ifdef NO_TESTS
169    T(null_test),
170#endif
171    T(NULL)
172};
173
174