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