1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "test.h"
23
24 #include "memdebug.h"
25
26 static const char *TEST_DATA_STRING = "Test data";
27 static int cb_count = 0;
28
29 static int
resolver_alloc_cb_fail(void * resolver_state,void * reserved,void * userdata)30 resolver_alloc_cb_fail(void *resolver_state, void *reserved, void *userdata)
31 {
32 (void)resolver_state;
33 (void)reserved;
34
35 cb_count++;
36 if(strcmp(userdata, TEST_DATA_STRING)) {
37 fprintf(stderr, "Invalid test data received");
38 exit(1);
39 }
40
41 return 1;
42 }
43
44 static int
resolver_alloc_cb_pass(void * resolver_state,void * reserved,void * userdata)45 resolver_alloc_cb_pass(void *resolver_state, void *reserved, void *userdata)
46 {
47 (void)resolver_state;
48 (void)reserved;
49
50 cb_count++;
51 if(strcmp(userdata, TEST_DATA_STRING)) {
52 fprintf(stderr, "Invalid test data received");
53 exit(1);
54 }
55
56 return 0;
57 }
58
test(char * URL)59 int test(char *URL)
60 {
61 CURL *curl;
62 CURLcode res = CURLE_OK;
63
64 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
65 fprintf(stderr, "curl_global_init() failed\n");
66 return TEST_ERR_MAJOR_BAD;
67 }
68 curl = curl_easy_init();
69 if(!curl) {
70 fprintf(stderr, "curl_easy_init() failed\n");
71 res = TEST_ERR_MAJOR_BAD;
72 goto test_cleanup;
73 }
74
75 /* First set the URL that is about to receive our request. */
76 test_setopt(curl, CURLOPT_URL, URL);
77
78 test_setopt(curl, CURLOPT_RESOLVER_START_DATA, TEST_DATA_STRING);
79 test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_fail);
80
81 /* this should fail */
82 res = curl_easy_perform(curl);
83 if(res != CURLE_COULDNT_RESOLVE_HOST) {
84 fprintf(stderr, "curl_easy_perform should have returned "
85 "CURLE_COULDNT_RESOLVE_HOST but instead returned error %d\n", res);
86 if(res == CURLE_OK)
87 res = TEST_ERR_FAILURE;
88 goto test_cleanup;
89 }
90
91 test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_pass);
92
93 /* this should succeed */
94 res = curl_easy_perform(curl);
95 if(res) {
96 fprintf(stderr, "curl_easy_perform failed.\n");
97 goto test_cleanup;
98 }
99
100 if(cb_count != 2) {
101 fprintf(stderr, "Unexpected number of callbacks: %d\n", cb_count);
102 res = TEST_ERR_FAILURE;
103 goto test_cleanup;
104 }
105
106 test_cleanup:
107 /* always cleanup */
108 curl_easy_cleanup(curl);
109 curl_global_cleanup();
110
111 return (int)res;
112 }
113