xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 | |
| 25 | /* Testing CURLOPT_PROTOCOLS_STR */ |
| 26 | |
| 27 | #include "test.h" |
| 28 | |
| 29 | #include "memdebug.h" |
| 30 | |
| 31 | struct pair { |
| 32 | const char *in; |
| 33 | CURLcode *exp; |
| 34 | }; |
| 35 | |
| 36 | int test(char *URL) |
| 37 | { |
| 38 | CURL *curl = NULL; |
| 39 | int res = 0; |
| 40 | CURLcode result = CURLE_OK; |
| 41 | curl_version_info_data *curlinfo; |
| 42 | const char *const *proto; |
| 43 | int n; |
| 44 | int i; |
| 45 | static CURLcode ok = CURLE_OK; |
| 46 | static CURLcode bad = CURLE_BAD_FUNCTION_ARGUMENT; |
| 47 | static CURLcode unsup = CURLE_UNSUPPORTED_PROTOCOL; |
| 48 | static CURLcode httpcode = CURLE_UNSUPPORTED_PROTOCOL; |
| 49 | static CURLcode httpscode = CURLE_UNSUPPORTED_PROTOCOL; |
| 50 | static char protolist[1024]; |
| 51 | |
| 52 | static const struct pair prots[] = { |
| 53 | {"goobar", &unsup}, |
| 54 | {"http ", &unsup}, |
| 55 | {" http", &unsup}, |
| 56 | {"http", &httpcode}, |
| 57 | {"http,", &httpcode}, |
| 58 | {"https,", &httpscode}, |
| 59 | {"https,http", &httpscode}, |
| 60 | {"http,http", &httpcode}, |
| 61 | {"HTTP,HTTP", &httpcode}, |
| 62 | {",HTTP,HTTP", &httpcode}, |
| 63 | {"http,http,ft", &unsup}, |
| 64 | {"", &bad}, |
| 65 | {",,", &bad}, |
| 66 | {protolist, &ok}, |
| 67 | {"all", &ok}, |
| 68 | {NULL, NULL}, |
| 69 | }; |
| 70 | (void)URL; |
| 71 | |
| 72 | global_init(CURL_GLOBAL_ALL); |
| 73 | |
| 74 | easy_init(curl); |
| 75 | |
| 76 | /* Get enabled protocols.*/ |
| 77 | curlinfo = curl_version_info(CURLVERSION_NOW); |
| 78 | if(!curlinfo) { |
| 79 | fputs("curl_version_info failed\n", stderr); |
| 80 | res = (int) TEST_ERR_FAILURE; |
| 81 | goto test_cleanup; |
| 82 | } |
| 83 | |
| 84 | n = 0; |
| 85 | for(proto = curlinfo->protocols; *proto; proto++) { |
| 86 | if((size_t) n >= sizeof(protolist)) { |
| 87 | puts("protolist buffer too small\n"); |
| 88 | res = (int) TEST_ERR_FAILURE; |
| 89 | goto test_cleanup; |
| 90 | } |
| 91 | n += msnprintf(protolist + n, sizeof(protolist) - n, ",%s", *proto); |
| 92 | if(curl_strequal(*proto, "http")) |
| 93 | httpcode = CURLE_OK; |
| 94 | if(curl_strequal(*proto, "https")) |
| 95 | httpscode = CURLE_OK; |
| 96 | } |
| 97 | |
| 98 | /* Run the tests. */ |
| 99 | for(i = 0; prots[i].in; i++) { |
| 100 | result = curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, prots[i].in); |
| 101 | if(result != *prots[i].exp) { |
| 102 | printf("unexpectedly '%s' returned %u\n", |
| 103 | prots[i].in, result); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | printf("Tested %u strings\n", i); |
| 108 | res = (int)result; |
| 109 | |
| 110 | test_cleanup: |
| 111 | curl_easy_cleanup(curl); |
| 112 | curl_global_cleanup(); |
| 113 | |
| 114 | return (int)result; |
| 115 | } |