| 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 | #include "curlcheck.h" | 
|  | 25 |  | 
|  | 26 | #include "urldata.h" | 
|  | 27 | #include "sendf.h" | 
|  | 28 |  | 
|  | 29 | /* | 
|  | 30 | * This test hardcodes the knowledge of the buffer size which is internal to | 
|  | 31 | * Curl_infof(). If that buffer is changed in size, this tests needs to be | 
|  | 32 | * updated to still be valid. | 
|  | 33 | */ | 
|  | 34 |  | 
|  | 35 | static struct Curl_easy *data; | 
|  | 36 |  | 
|  | 37 | static char input[4096]; | 
|  | 38 | static char result[4096]; | 
|  | 39 |  | 
|  | 40 | int debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size, | 
|  | 41 | void *userptr); | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * This debugf callback is simply dumping the string into the static buffer | 
|  | 45 | * for the unit test to inspect. Since we know that we're only dealing with | 
|  | 46 | * text we can afford the luxury of skipping the type check here. | 
|  | 47 | */ | 
|  | 48 | int | 
|  | 49 | debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size, | 
|  | 50 | void *userptr) | 
|  | 51 | { | 
|  | 52 | (void)handle; | 
|  | 53 | (void)type; | 
|  | 54 | (void)userptr; | 
|  | 55 |  | 
|  | 56 | memset(result, '\0', sizeof(result)); | 
|  | 57 | memcpy(result, buf, size); | 
|  | 58 | return 0; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | static CURLcode | 
|  | 62 | unit_setup(void) | 
|  | 63 | { | 
|  | 64 | CURLcode res = CURLE_OK; | 
|  | 65 |  | 
|  | 66 | global_init(CURL_GLOBAL_ALL); | 
|  | 67 | data = curl_easy_init(); | 
|  | 68 | if(!data) { | 
|  | 69 | curl_global_cleanup(); | 
|  | 70 | return CURLE_OUT_OF_MEMORY; | 
|  | 71 | } | 
|  | 72 | curl_easy_setopt(data, CURLOPT_DEBUGFUNCTION, debugf_cb); | 
|  | 73 | curl_easy_setopt(data, CURLOPT_VERBOSE, 1L); | 
|  | 74 | return res; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static void | 
|  | 78 | unit_stop(void) | 
|  | 79 | { | 
|  | 80 | curl_easy_cleanup(data); | 
|  | 81 | curl_global_cleanup(); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static int verify(const char *info, const char *two) | 
|  | 85 | { | 
|  | 86 | /* the 'info' one has a newline appended */ | 
|  | 87 | char *nl = strchr(info, '\n'); | 
|  | 88 | if(!nl) | 
|  | 89 | return 1; /* nope */ | 
|  | 90 | return strncmp(info, two, nl - info); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | UNITTEST_START | 
|  | 94 |  | 
|  | 95 | /* Injecting a simple short string via a format */ | 
|  | 96 | msnprintf(input, sizeof(input), "Simple Test"); | 
|  | 97 | Curl_infof(data, "%s", input); | 
|  | 98 | fail_unless(verify(result, input) == 0, "Simple string test"); | 
|  | 99 |  | 
|  | 100 | /* Injecting a few different variables with a format */ | 
|  | 101 | Curl_infof(data, "%s %u testing %lu", input, 42, 43L); | 
|  | 102 | fail_unless(verify(result, "Simple Test 42 testing 43\n") == 0, | 
|  | 103 | "Format string"); | 
|  | 104 |  | 
|  | 105 | /* Variations of empty strings */ | 
|  | 106 | Curl_infof(data, ""); | 
|  | 107 | fail_unless(strlen(result) == 1, "Empty string"); | 
|  | 108 | Curl_infof(data, "%s", NULL); | 
|  | 109 | fail_unless(verify(result, "(nil)") == 0, "Passing NULL as string"); | 
|  | 110 |  | 
|  | 111 | /* A string just long enough to not be truncated */ | 
|  | 112 | memset(input, '\0', sizeof(input)); | 
|  | 113 | memset(input, 'A', 2047); | 
|  | 114 | Curl_infof(data, "%s", input); | 
|  | 115 | fail_unless(strlen(result) == 2048, "No truncation of infof input"); | 
|  | 116 | fail_unless(verify(result, input) == 0, "No truncation of infof input"); | 
|  | 117 | fail_unless(result[sizeof(result) - 1] == '\0', | 
|  | 118 | "No truncation of infof input"); | 
|  | 119 |  | 
|  | 120 | /* Just over the limit for truncation without newline */ | 
|  | 121 | memset(input + 2047, 'A', 4); | 
|  | 122 | Curl_infof(data, "%s", input); | 
|  | 123 | fail_unless(strlen(result) == 2048, "Truncation of infof input 1"); | 
|  | 124 | fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1"); | 
|  | 125 |  | 
|  | 126 | /* Just over the limit for truncation with newline */ | 
|  | 127 | memset(input + 2047, 'A', 4); | 
|  | 128 | memset(input + 2047 + 4, '\n', 1); | 
|  | 129 | Curl_infof(data, "%s", input); | 
|  | 130 | fail_unless(strlen(result) == 2048, "Truncation of infof input 2"); | 
|  | 131 | fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2"); | 
|  | 132 |  | 
|  | 133 | /* Way over the limit for truncation with newline */ | 
|  | 134 | memset(input, '\0', sizeof(input)); | 
|  | 135 | memset(input, 'A', sizeof(input) - 1); | 
|  | 136 | Curl_infof(data, "%s", input); | 
|  | 137 | fail_unless(strlen(result) == 2048, "Truncation of infof input 3"); | 
|  | 138 | fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3"); | 
|  | 139 |  | 
|  | 140 |  | 
|  | 141 | UNITTEST_STOP |