|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2019 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include <ucx/string.h> |
|
33 #include <ucx/utils.h> |
|
34 |
|
35 #include "../util/pblock.h" |
|
36 |
|
37 #include "../util/io.h" |
|
38 |
|
39 #include "testutils.h" |
|
40 |
|
41 Session* testutil_session(void) { |
|
42 pool_handle_t *pool = pool_create(); |
|
43 NSAPISession *sn = nsapisession_create(pool); |
|
44 sn->connection = testutil_dummy_connection(pool); |
|
45 |
|
46 return &sn->sn; |
|
47 } |
|
48 |
|
49 Request* testutil_request(pool_handle_t *pool, const char *method, const char *uri) { |
|
50 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
|
51 ZERO(rq, sizeof(NSAPIRequest)); |
|
52 |
|
53 HTTPRequest httprequest; |
|
54 ZERO(&httprequest, sizeof(HTTPRequest)); |
|
55 request_initialize(pool, &httprequest, rq); |
|
56 |
|
57 sstr_t clf = ucx_sprintf("%s %s HTTP/1.1", method, uri); |
|
58 pblock_kvinsert( |
|
59 pb_key_clf_request, |
|
60 clf.ptr, |
|
61 clf.length, |
|
62 rq->rq.reqpb); |
|
63 free(clf.ptr); |
|
64 |
|
65 pblock_nvinsert( |
|
66 "method", |
|
67 method, |
|
68 rq->rq.reqpb); |
|
69 |
|
70 pblock_nvinsert( |
|
71 "protocol", |
|
72 "HTTP/1.1", |
|
73 rq->rq.reqpb); |
|
74 |
|
75 pblock_nvinsert("uri", uri, rq->rq.reqpb); |
|
76 |
|
77 return &rq->rq; |
|
78 } |
|
79 |
|
80 static int dummyconn_read(Connection *conn, void *buf, int len) { |
|
81 return len; |
|
82 } |
|
83 |
|
84 static int dummyconn_write(Connection *conn, const void *buf, int len) { |
|
85 return len; |
|
86 } |
|
87 |
|
88 static void dummyconn_close(Connection *conn) { |
|
89 |
|
90 } |
|
91 |
|
92 |
|
93 Connection* testutil_dummy_connection(pool_handle_t *pool) { |
|
94 Connection *conn = pool_malloc(pool, sizeof(Connection)); |
|
95 ZERO(conn, sizeof(Connection)); |
|
96 conn->read = dummyconn_read; |
|
97 conn->write = dummyconn_write; |
|
98 conn->close = dummyconn_close; |
|
99 return conn; |
|
100 } |
|
101 |
|
102 void testutil_request_body(Session *sn, Request *rq, const char *body, size_t len) { |
|
103 sstr_t cl = ucx_sprintf("%d", (int)len); |
|
104 pblock_nvreplace("content-length", cl.ptr, rq->headers); |
|
105 free(cl.ptr); |
|
106 |
|
107 netbuf *inbuf = pool_malloc(sn->pool, sizeof(netbuf)); |
|
108 inbuf->sd = NULL; |
|
109 inbuf->inbuf = pool_malloc(sn->pool, len); |
|
110 inbuf->pos = 0; |
|
111 inbuf->maxsize = len; |
|
112 inbuf->cursize = len; |
|
113 sn->inbuf = inbuf; |
|
114 |
|
115 memcpy(inbuf->inbuf, body, len); |
|
116 } |
|
117 |
|
118 void testutil_destroy_session(Session *sn) { |
|
119 pool_destroy(sn->pool); |
|
120 } |
|
121 |
|
122 |
|
123 static ssize_t test_io_write(IOStream *io, void *buf, size_t size) { |
|
124 TestIOStream *st = (TestIOStream*)io; |
|
125 return ucx_buffer_write(buf, 1, size, st->buf); |
|
126 } |
|
127 |
|
128 static ssize_t test_io_writev(IOStream *io, struct iovec *iovec, int iovctn) { |
|
129 return -1; |
|
130 } |
|
131 |
|
132 static ssize_t test_io_read(IOStream *io, void *buf, size_t size) { |
|
133 return -1; |
|
134 } |
|
135 |
|
136 static void test_io_close(IOStream *io) { |
|
137 |
|
138 } |
|
139 |
|
140 static void test_io_finish(IOStream *io) { |
|
141 |
|
142 } |
|
143 |
|
144 static void test_io_setmode(IOStream *io, int mode) { |
|
145 |
|
146 } |
|
147 |
|
148 static int test_io_poll(IOStream *io, EventHandler *ev, int events , Event *event) { |
|
149 return 1; |
|
150 } |
|
151 |
|
152 TestIOStream* testutil_iostream(size_t size, int autoextend) { |
|
153 TestIOStream *stream = calloc(1, sizeof(TestIOStream)); |
|
154 int flags = 0; |
|
155 if(autoextend) { |
|
156 flags = UCX_BUFFER_AUTOEXTEND; |
|
157 } |
|
158 stream->buf = ucx_buffer_new(NULL, size, flags); |
|
159 |
|
160 stream->io.st.write = test_io_write; |
|
161 stream->io.st.writev = test_io_writev; |
|
162 stream->io.st.close = test_io_close; |
|
163 stream->io.st.finish = test_io_finish; |
|
164 |
|
165 return stream; |
|
166 } |
|
167 |
|
168 void testutil_iostream_destroy(TestIOStream *stream) { |
|
169 ucx_buffer_free(stream->buf); |
|
170 free(stream); |
|
171 } |