xref: /third_party/curl/tests/libtest/lib655.c (revision 13498266)
1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "test.h"
25
26#include "memdebug.h"
27
28static const char *TEST_DATA_STRING = "Test data";
29static int cb_count = 0;
30
31static int
32resolver_alloc_cb_fail(void *resolver_state, void *reserved, void *userdata)
33{
34  (void)resolver_state;
35  (void)reserved;
36
37  cb_count++;
38  if(strcmp(userdata, TEST_DATA_STRING)) {
39    fprintf(stderr, "Invalid test data received");
40    exit(1);
41  }
42
43  return 1;
44}
45
46static int
47resolver_alloc_cb_pass(void *resolver_state, void *reserved, void *userdata)
48{
49  (void)resolver_state;
50  (void)reserved;
51
52  cb_count++;
53  if(strcmp(userdata, TEST_DATA_STRING)) {
54    fprintf(stderr, "Invalid test data received");
55    exit(1);
56  }
57
58  return 0;
59}
60
61int test(char *URL)
62{
63  CURL *curl;
64  CURLcode res = CURLE_OK;
65
66  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
67    fprintf(stderr, "curl_global_init() failed\n");
68    return TEST_ERR_MAJOR_BAD;
69  }
70  curl = curl_easy_init();
71  if(!curl) {
72    fprintf(stderr, "curl_easy_init() failed\n");
73    res = TEST_ERR_MAJOR_BAD;
74    goto test_cleanup;
75  }
76
77  /* First set the URL that is about to receive our request. */
78  test_setopt(curl, CURLOPT_URL, URL);
79
80  test_setopt(curl, CURLOPT_RESOLVER_START_DATA, TEST_DATA_STRING);
81  test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_fail);
82
83  /* this should fail */
84  res = curl_easy_perform(curl);
85  if(res != CURLE_COULDNT_RESOLVE_HOST) {
86    fprintf(stderr, "curl_easy_perform should have returned "
87            "CURLE_COULDNT_RESOLVE_HOST but instead returned error %d\n", res);
88    if(res == CURLE_OK)
89      res = TEST_ERR_FAILURE;
90    goto test_cleanup;
91  }
92
93  test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_pass);
94
95  /* this should succeed */
96  res = curl_easy_perform(curl);
97  if(res) {
98    fprintf(stderr, "curl_easy_perform failed.\n");
99    goto test_cleanup;
100  }
101
102  if(cb_count != 2) {
103    fprintf(stderr, "Unexpected number of callbacks: %d\n", cb_count);
104    res = TEST_ERR_FAILURE;
105    goto test_cleanup;
106  }
107
108test_cleanup:
109  /* always cleanup */
110  curl_easy_cleanup(curl);
111  curl_global_cleanup();
112
113  return (int)res;
114}
115