xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 2019 - 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 "curlcheck.h" |
| 25 | |
| 26 | #include "urldata.h" |
| 27 | #include "altsvc.h" |
| 28 | |
| 29 | static CURLcode |
| 30 | unit_setup(void) |
| 31 | { |
| 32 | return CURLE_OK; |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | unit_stop(void) |
| 37 | { |
| 38 | curl_global_cleanup(); |
| 39 | } |
| 40 | |
| 41 | #if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_ALTSVC) |
| 42 | UNITTEST_START |
| 43 | { |
| 44 | return 0; /* nothing to do when HTTP or alt-svc is disabled */ |
| 45 | } |
| 46 | UNITTEST_STOP |
| 47 | #else |
| 48 | UNITTEST_START |
| 49 | { |
| 50 | char outname[256]; |
| 51 | CURL *curl; |
| 52 | CURLcode result; |
| 53 | struct altsvcinfo *asi = Curl_altsvc_init(); |
| 54 | if(!asi) |
| 55 | return 1; |
| 56 | result = Curl_altsvc_load(asi, arg); |
| 57 | if(result) { |
| 58 | Curl_altsvc_cleanup(&asi); |
| 59 | return result; |
| 60 | } |
| 61 | curl_global_init(CURL_GLOBAL_ALL); |
| 62 | curl = curl_easy_init(); |
| 63 | if(!curl) |
| 64 | goto fail; |
| 65 | fail_unless(asi->list.size == 4, "wrong number of entries"); |
| 66 | msnprintf(outname, sizeof(outname), "%s-out", arg); |
| 67 | |
| 68 | result = Curl_altsvc_parse(curl, asi, "h2=\"example.com:8080\"\r\n", |
| 69 | ALPN_h1, "example.org", 8080); |
| 70 | if(result) { |
| 71 | fprintf(stderr, "Curl_altsvc_parse() failed!\n"); |
| 72 | unitfail++; |
| 73 | } |
| 74 | fail_unless(asi->list.size == 5, "wrong number of entries"); |
| 75 | |
| 76 | result = Curl_altsvc_parse(curl, asi, "h3=\":8080\"\r\n", |
| 77 | ALPN_h1, "2.example.org", 8080); |
| 78 | if(result) { |
| 79 | fprintf(stderr, "Curl_altsvc_parse(2) failed!\n"); |
| 80 | unitfail++; |
| 81 | } |
| 82 | fail_unless(asi->list.size == 6, "wrong number of entries"); |
| 83 | |
| 84 | result = Curl_altsvc_parse(curl, asi, |
| 85 | "h2=\"example.com:8080\", h3=\"yesyes.com\"\r\n", |
| 86 | ALPN_h1, "3.example.org", 8080); |
| 87 | if(result) { |
| 88 | fprintf(stderr, "Curl_altsvc_parse(3) failed!\n"); |
| 89 | unitfail++; |
| 90 | } |
| 91 | /* that one should make two entries */ |
| 92 | fail_unless(asi->list.size == 8, "wrong number of entries"); |
| 93 | |
| 94 | result = Curl_altsvc_parse(curl, asi, |
| 95 | "h2=\"example.com:443\"; ma = 120;\r\n", |
| 96 | ALPN_h2, "example.org", 80); |
| 97 | if(result) { |
| 98 | fprintf(stderr, "Curl_altsvc_parse(4) failed!\n"); |
| 99 | unitfail++; |
| 100 | } |
| 101 | fail_unless(asi->list.size == 9, "wrong number of entries"); |
| 102 | |
| 103 | /* quoted 'ma' value */ |
| 104 | result = Curl_altsvc_parse(curl, asi, |
| 105 | "h2=\"example.net:443\"; ma=\"180\";\r\n", |
| 106 | ALPN_h2, "example.net", 80); |
| 107 | if(result) { |
| 108 | fprintf(stderr, "Curl_altsvc_parse(4) failed!\n"); |
| 109 | unitfail++; |
| 110 | } |
| 111 | fail_unless(asi->list.size == 10, "wrong number of entries"); |
| 112 | |
| 113 | result = |
| 114 | Curl_altsvc_parse(curl, asi, |
| 115 | "h2=\":443\", h3=\":443\"; ma = 120; persist = 1\r\n", |
| 116 | ALPN_h1, "curl.se", 80); |
| 117 | if(result) { |
| 118 | fprintf(stderr, "Curl_altsvc_parse(5) failed!\n"); |
| 119 | unitfail++; |
| 120 | } |
| 121 | fail_unless(asi->list.size == 12, "wrong number of entries"); |
| 122 | |
| 123 | /* clear that one again and decrease the counter */ |
| 124 | result = Curl_altsvc_parse(curl, asi, "clear;\r\n", |
| 125 | ALPN_h1, "curl.se", 80); |
| 126 | if(result) { |
| 127 | fprintf(stderr, "Curl_altsvc_parse(6) failed!\n"); |
| 128 | unitfail++; |
| 129 | } |
| 130 | fail_unless(asi->list.size == 10, "wrong number of entries"); |
| 131 | |
| 132 | Curl_altsvc_save(curl, asi, outname); |
| 133 | |
| 134 | curl_easy_cleanup(curl); |
| 135 | curl_global_cleanup(); |
| 136 | fail: |
| 137 | Curl_altsvc_cleanup(&asi); |
| 138 | curl_global_cleanup(); |
| 139 | return unitfail; |
| 140 | } |
| 141 | UNITTEST_STOP |
| 142 | #endif |