1 /* ====================================================================
2  *    Licensed to the Apache Software Foundation (ASF) under one
3  *    or more contributor license agreements.  See the NOTICE file
4  *    distributed with this work for additional information
5  *    regarding copyright ownership.  The ASF licenses this file
6  *    to you under the Apache License, Version 2.0 (the
7  *    "License"); you may not use this file except in compliance
8  *    with the License.  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing,
13  *    software distributed under the License is distributed on an
14  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  *    KIND, either express or implied.  See the License for the
16  *    specific language governing permissions and limitations
17  *    under the License.
18  * ====================================================================
19  */
20 
21 #ifndef TEST_SERF_H
22 #define TEST_SERF_H
23 
24 #include "CuTest.h"
25 
26 #include <apr.h>
27 #include <apr_pools.h>
28 #include <apr_uri.h>
29 
30 #include "serf.h"
31 #include "server/test_server.h"
32 
33 /** These macros are provided by APR itself from version 1.3.
34  * Definitions are provided here for when using older versions of APR.
35  */
36 
37 /** index into an apr_array_header_t */
38 #ifndef APR_ARRAY_IDX
39 #define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
40 #endif
41 
42 /** easier array-pushing syntax */
43 #ifndef APR_ARRAY_PUSH
44 #define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary)))
45 #endif
46 
47 /* CuTest declarations */
48 CuSuite *getsuite(void);
49 
50 CuSuite *test_context(void);
51 CuSuite *test_buckets(void);
52 CuSuite *test_ssl(void);
53 CuSuite *test_auth(void);
54 CuSuite *test_mock_bucket(void);
55 
56 /* Test setup declarations */
57 #define CRLF "\r\n"
58 #define CR "\r"
59 #define LF "\n"
60 
61 #define CHUNKED_REQUEST(len, body)\
62         "GET / HTTP/1.1" CRLF\
63         "Host: localhost:12345" CRLF\
64         "Transfer-Encoding: chunked" CRLF\
65         CRLF\
66         #len CRLF\
67         body CRLF\
68         "0" CRLF\
69         CRLF
70 
71 #define CHUNKED_REQUEST_URI(uri, len, body)\
72         "GET " uri " HTTP/1.1" CRLF\
73         "Host: localhost:12345" CRLF\
74         "Transfer-Encoding: chunked" CRLF\
75         CRLF\
76         #len CRLF\
77         body CRLF\
78         "0" CRLF\
79         CRLF
80 
81 #define CHUNKED_RESPONSE(len, body)\
82         "HTTP/1.1 200 OK" CRLF\
83         "Transfer-Encoding: chunked" CRLF\
84         CRLF\
85         #len CRLF\
86         body CRLF\
87         "0" CRLF\
88         CRLF
89 
90 #define CHUNKED_EMPTY_RESPONSE\
91         "HTTP/1.1 200 OK" CRLF\
92         "Transfer-Encoding: chunked" CRLF\
93         CRLF\
94         "0" CRLF\
95         CRLF
96 
97 typedef struct {
98     /* Pool for resource allocation. */
99     apr_pool_t *pool;
100 
101     serf_context_t *context;
102     serf_connection_t *connection;
103     serf_bucket_alloc_t *bkt_alloc;
104 
105     serv_ctx_t *serv_ctx;
106     apr_sockaddr_t *serv_addr;
107 
108     serv_ctx_t *proxy_ctx;
109     apr_sockaddr_t *proxy_addr;
110 
111     /* Cache connection params here so it gets user for a test to switch to a
112        new connection. */
113     const char *serv_url;
114     serf_connection_setup_t conn_setup;
115 
116     /* Extra batons which can be freely used by tests. */
117     void *user_baton;
118     long user_baton_l;
119 
120     /* Flags that can be used to report situations, e.g. that a callback was
121        called. */
122     int result_flags;
123 
124     apr_array_header_t *accepted_requests, *handled_requests, *sent_requests;
125 
126     serf_ssl_context_t *ssl_context;
127     serf_ssl_need_server_cert_t server_cert_cb;
128 } test_baton_t;
129 
130 apr_status_t default_https_conn_setup(apr_socket_t *skt,
131                                       serf_bucket_t **input_bkt,
132                                       serf_bucket_t **output_bkt,
133                                       void *setup_baton,
134                                       apr_pool_t *pool);
135 
136 apr_status_t use_new_connection(test_baton_t *tb,
137                                 apr_pool_t *pool);
138 
139 apr_status_t test_https_server_setup(test_baton_t **tb_p,
140                                      test_server_message_t *message_list,
141                                      apr_size_t message_count,
142                                      test_server_action_t *action_list,
143                                      apr_size_t action_count,
144                                      apr_int32_t options,
145                                      serf_connection_setup_t conn_setup,
146                                      const char *keyfile,
147                                      const char **certfile,
148                                      const char *client_cn,
149                                      serf_ssl_need_server_cert_t server_cert_cb,
150                                      apr_pool_t *pool);
151 
152 apr_status_t test_http_server_setup(test_baton_t **tb_p,
153                                     test_server_message_t *message_list,
154                                     apr_size_t message_count,
155                                     test_server_action_t *action_list,
156                                     apr_size_t action_count,
157                                     apr_int32_t options,
158                                     serf_connection_setup_t conn_setup,
159                                     apr_pool_t *pool);
160 
161 apr_status_t test_server_proxy_setup(
162                  test_baton_t **tb_p,
163                  test_server_message_t *serv_message_list,
164                  apr_size_t serv_message_count,
165                  test_server_action_t *serv_action_list,
166                  apr_size_t serv_action_count,
167                  test_server_message_t *proxy_message_list,
168                  apr_size_t proxy_message_count,
169                  test_server_action_t *proxy_action_list,
170                  apr_size_t proxy_action_count,
171                  apr_int32_t options,
172                  serf_connection_setup_t conn_setup,
173                  apr_pool_t *pool);
174 
175 apr_status_t test_https_server_proxy_setup(
176                  test_baton_t **tb_p,
177                  test_server_message_t *serv_message_list,
178                  apr_size_t serv_message_count,
179                  test_server_action_t *serv_action_list,
180                  apr_size_t serv_action_count,
181                  test_server_message_t *proxy_message_list,
182                  apr_size_t proxy_message_count,
183                  test_server_action_t *proxy_action_list,
184                  apr_size_t proxy_action_count,
185                  apr_int32_t options,
186                  serf_connection_setup_t conn_setup,
187                  const char *keyfile,
188                  const char **certfiles,
189                  const char *client_cn,
190                  serf_ssl_need_server_cert_t server_cert_cb,
191                  apr_pool_t *pool);
192 
193 void *test_setup(void *baton);
194 void *test_teardown(void *baton);
195 
196 typedef struct {
197     serf_response_acceptor_t acceptor;
198     void *acceptor_baton;
199 
200     serf_response_handler_t handler;
201 
202     apr_array_header_t *sent_requests;
203     apr_array_header_t *accepted_requests;
204     apr_array_header_t *handled_requests;
205     int req_id;
206 
207     const char *method;
208     const char *path;
209     /* Use this for a raw request message */
210     const char *request;
211     int done;
212 
213     test_baton_t *tb;
214 } handler_baton_t;
215 
216 /* These defines are used with the test_baton_t result_flags variable. */
217 #define TEST_RESULT_SERVERCERTCB_CALLED      0x0001
218 #define TEST_RESULT_SERVERCERTCHAINCB_CALLED 0x0002
219 #define TEST_RESULT_CLIENT_CERTCB_CALLED     0x0004
220 #define TEST_RESULT_CLIENT_CERTPWCB_CALLED   0x0008
221 #define TEST_RESULT_AUTHNCB_CALLED           0x001A
222 
223 apr_status_t
224 test_helper_run_requests_no_check(CuTest *tc, test_baton_t *tb,
225                                   int num_requests,
226                                   handler_baton_t handler_ctx[],
227                                   apr_pool_t *pool);
228 void
229 test_helper_run_requests_expect_ok(CuTest *tc, test_baton_t *tb,
230                                    int num_requests,
231                                    handler_baton_t handler_ctx[],
232                                    apr_pool_t *pool);
233 serf_bucket_t* accept_response(serf_request_t *request,
234                                serf_bucket_t *stream,
235                                void *acceptor_baton,
236                                apr_pool_t *pool);
237 apr_status_t setup_request(serf_request_t *request,
238                            void *setup_baton,
239                            serf_bucket_t **req_bkt,
240                            serf_response_acceptor_t *acceptor,
241                            void **acceptor_baton,
242                            serf_response_handler_t *handler,
243                            void **handler_baton,
244                            apr_pool_t *pool);
245 apr_status_t handle_response(serf_request_t *request,
246                              serf_bucket_t *response,
247                              void *handler_baton,
248                              apr_pool_t *pool);
249 void setup_handler(test_baton_t *tb, handler_baton_t *handler_ctx,
250                    const char *method, const char *path,
251                    int req_id,
252                    serf_response_handler_t handler);
253 void create_new_prio_request(test_baton_t *tb,
254                              handler_baton_t *handler_ctx,
255                              const char *method, const char *path,
256                              int req_id);
257 void create_new_request(test_baton_t *tb,
258                         handler_baton_t *handler_ctx,
259                         const char *method, const char *path,
260                         int req_id);
261 void
262 create_new_request_with_resp_hdlr(test_baton_t *tb,
263                                   handler_baton_t *handler_ctx,
264                                   const char *method, const char *path,
265                                   int req_id,
266                                   serf_response_handler_t handler);
267 
268 /* Mock bucket type and constructor */
269 typedef struct {
270     int times;
271     const char *data;
272     apr_status_t status;
273 } mockbkt_action;
274 
275 void read_and_check_bucket(CuTest *tc, serf_bucket_t *bkt,
276                            const char *expected);
277 void readlines_and_check_bucket(CuTest *tc, serf_bucket_t *bkt,
278                                 int acceptable,
279                                 const char *expected,
280                                 int expected_nr_of_lines);
281 
282 extern const serf_bucket_type_t serf_bucket_type_mock;
283 #define SERF_BUCKET_IS_MOCK(b) SERF_BUCKET_CHECK((b), mock)
284 
285 serf_bucket_t *serf_bucket_mock_create(mockbkt_action *actions,
286                                        int len,
287                                        serf_bucket_alloc_t *allocator);
288 apr_status_t serf_bucket_mock_more_data_arrived(serf_bucket_t *bucket);
289 
290 /* Helper to get a file relative to our source directory by looking at
291  * 'srcdir' env variable. */
292 const char * get_srcdir_file(apr_pool_t *pool, const char * file);
293 
294 #endif /* TEST_SERF_H */
295