xref: /third_party/curl/tests/libtest/lib1535.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
28/* Test CURLINFO_PROTOCOL */
29
30int test(char *URL)
31{
32  CURL *curl, *dupe = NULL;
33  long protocol;
34  CURLcode res = CURLE_OK;
35
36  global_init(CURL_GLOBAL_ALL);
37
38  easy_init(curl);
39
40  /* Test that protocol is properly initialized on curl_easy_init.
41  */
42
43  CURL_IGNORE_DEPRECATION(
44    res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
45  )
46  if(res) {
47    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
48            __FILE__, __LINE__, res, curl_easy_strerror(res));
49    goto test_cleanup;
50  }
51  if(protocol) {
52    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
53            __FILE__, __LINE__, protocol);
54    res = CURLE_FAILED_INIT;
55    goto test_cleanup;
56  }
57
58  easy_setopt(curl, CURLOPT_URL, URL);
59
60  res = curl_easy_perform(curl);
61  if(res) {
62    fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
63            __FILE__, __LINE__, res, curl_easy_strerror(res));
64    goto test_cleanup;
65  }
66
67  /* Test that a protocol is properly set after receiving an HTTP resource.
68  */
69
70  CURL_IGNORE_DEPRECATION(
71    res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
72  )
73  if(res) {
74    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
75            __FILE__, __LINE__, res, curl_easy_strerror(res));
76    goto test_cleanup;
77  }
78  if(protocol != CURLPROTO_HTTP) {
79    fprintf(stderr, "%s:%d protocol of http resource is incorrect; "
80            "expected %d but is %ld\n",
81            __FILE__, __LINE__, CURLPROTO_HTTP, protocol);
82    res = CURLE_HTTP_RETURNED_ERROR;
83    goto test_cleanup;
84  }
85
86  /* Test that a protocol is properly initialized on curl_easy_duphandle.
87  */
88
89  dupe = curl_easy_duphandle(curl);
90  if(!dupe) {
91    fprintf(stderr, "%s:%d curl_easy_duphandle() failed\n",
92            __FILE__, __LINE__);
93    res = CURLE_FAILED_INIT;
94    goto test_cleanup;
95  }
96
97  CURL_IGNORE_DEPRECATION(
98    res = curl_easy_getinfo(dupe, CURLINFO_PROTOCOL, &protocol);
99  )
100  if(res) {
101    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
102            __FILE__, __LINE__, res, curl_easy_strerror(res));
103    goto test_cleanup;
104  }
105  if(protocol) {
106    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
107            __FILE__, __LINE__, protocol);
108    res = CURLE_FAILED_INIT;
109    goto test_cleanup;
110  }
111
112
113  /* Test that a protocol is properly initialized on curl_easy_reset.
114  */
115
116  curl_easy_reset(curl);
117
118  CURL_IGNORE_DEPRECATION(
119    res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
120  )
121  if(res) {
122    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
123            __FILE__, __LINE__, res, curl_easy_strerror(res));
124    goto test_cleanup;
125  }
126  if(protocol) {
127    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
128            __FILE__, __LINE__, protocol);
129    res = CURLE_FAILED_INIT;
130    goto test_cleanup;
131  }
132
133test_cleanup:
134  curl_easy_cleanup(curl);
135  curl_easy_cleanup(dupe);
136  curl_global_cleanup();
137  return (int)res;
138}
139