blob: 761bf8c64d328a31b2dfb06858333b0d69576366 [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 "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
35static struct Curl_easy *data;
36
37static char input[4096];
38static char result[4096];
39
40int 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 */
48int
49debugf_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
61static CURLcode
62unit_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
77static void
78unit_stop(void)
79{
80 curl_easy_cleanup(data);
81 curl_global_cleanup();
82}
83
84static 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
93UNITTEST_START
94
95/* Injecting a simple short string via a format */
96msnprintf(input, sizeof(input), "Simple Test");
97Curl_infof(data, "%s", input);
98fail_unless(verify(result, input) == 0, "Simple string test");
99
100/* Injecting a few different variables with a format */
101Curl_infof(data, "%s %u testing %lu", input, 42, 43L);
102fail_unless(verify(result, "Simple Test 42 testing 43\n") == 0,
103 "Format string");
104
105/* Variations of empty strings */
106Curl_infof(data, "");
107fail_unless(strlen(result) == 1, "Empty string");
108Curl_infof(data, "%s", NULL);
109fail_unless(verify(result, "(nil)") == 0, "Passing NULL as string");
110
111/* A string just long enough to not be truncated */
112memset(input, '\0', sizeof(input));
113memset(input, 'A', 2047);
114Curl_infof(data, "%s", input);
115fail_unless(strlen(result) == 2048, "No truncation of infof input");
116fail_unless(verify(result, input) == 0, "No truncation of infof input");
117fail_unless(result[sizeof(result) - 1] == '\0',
118 "No truncation of infof input");
119
120/* Just over the limit for truncation without newline */
121memset(input + 2047, 'A', 4);
122Curl_infof(data, "%s", input);
123fail_unless(strlen(result) == 2048, "Truncation of infof input 1");
124fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1");
125
126/* Just over the limit for truncation with newline */
127memset(input + 2047, 'A', 4);
128memset(input + 2047 + 4, '\n', 1);
129Curl_infof(data, "%s", input);
130fail_unless(strlen(result) == 2048, "Truncation of infof input 2");
131fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2");
132
133/* Way over the limit for truncation with newline */
134memset(input, '\0', sizeof(input));
135memset(input, 'A', sizeof(input) - 1);
136Curl_infof(data, "%s", input);
137fail_unless(strlen(result) == 2048, "Truncation of infof input 3");
138fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3");
139
140
141UNITTEST_STOP