blob: e8cf67f9cd2ac6eeadab644901e7e0725e2ec8ae [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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 res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
44 if(res) {
45 fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
46 __FILE__, __LINE__, res, curl_easy_strerror(res));
47 goto test_cleanup;
48 }
49 if(protocol) {
50 fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
51 __FILE__, __LINE__, protocol);
52 res = CURLE_FAILED_INIT;
53 goto test_cleanup;
54 }
55
56 easy_setopt(curl, CURLOPT_URL, URL);
57
58 res = curl_easy_perform(curl);
59 if(res) {
60 fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
61 __FILE__, __LINE__, res, curl_easy_strerror(res));
62 goto test_cleanup;
63 }
64
65 /* Test that a protocol is properly set after receiving an HTTP resource.
66 */
67
68 res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
69 if(res) {
70 fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
71 __FILE__, __LINE__, res, curl_easy_strerror(res));
72 goto test_cleanup;
73 }
74 if(protocol != CURLPROTO_HTTP) {
75 fprintf(stderr, "%s:%d protocol of http resource is incorrect; "
76 "expected %d but is %ld\n",
77 __FILE__, __LINE__, CURLPROTO_HTTP, protocol);
78 res = CURLE_HTTP_RETURNED_ERROR;
79 goto test_cleanup;
80 }
81
82 /* Test that a protocol is properly initialized on curl_easy_duphandle.
83 */
84
85 dupe = curl_easy_duphandle(curl);
86 if(!dupe) {
87 fprintf(stderr, "%s:%d curl_easy_duphandle() failed\n",
88 __FILE__, __LINE__);
89 res = CURLE_FAILED_INIT;
90 goto test_cleanup;
91 }
92
93 res = curl_easy_getinfo(dupe, CURLINFO_PROTOCOL, &protocol);
94 if(res) {
95 fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
96 __FILE__, __LINE__, res, curl_easy_strerror(res));
97 goto test_cleanup;
98 }
99 if(protocol) {
100 fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
101 __FILE__, __LINE__, protocol);
102 res = CURLE_FAILED_INIT;
103 goto test_cleanup;
104 }
105
106
107 /* Test that a protocol is properly initialized on curl_easy_reset.
108 */
109
110 curl_easy_reset(curl);
111
112 res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
113 if(res) {
114 fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
115 __FILE__, __LINE__, res, curl_easy_strerror(res));
116 goto test_cleanup;
117 }
118 if(protocol) {
119 fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
120 __FILE__, __LINE__, protocol);
121 res = CURLE_FAILED_INIT;
122 goto test_cleanup;
123 }
124
125test_cleanup:
126 curl_easy_cleanup(curl);
127 curl_easy_cleanup(dupe);
128 curl_global_cleanup();
129 return (int)res;
130}