xref: /curl/tests/unit/unit1652.c (revision fb711b50)
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 /*
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 *testdata;
36 
37 static char input[4096];
38 static char output[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
debugf_cb(CURL * handle,curl_infotype type,char * buf,size_t size,void * userptr)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(output, '\0', sizeof(output));
57   memcpy(output, buf, size);
58   return 0;
59 }
60 
61 static CURLcode
unit_setup(void)62 unit_setup(void)
63 {
64   CURLcode res = CURLE_OK;
65 
66   global_init(CURL_GLOBAL_ALL);
67   testdata = curl_easy_init();
68   if(!testdata) {
69     curl_global_cleanup();
70     return CURLE_OUT_OF_MEMORY;
71   }
72   curl_easy_setopt(testdata, CURLOPT_DEBUGFUNCTION, debugf_cb);
73   curl_easy_setopt(testdata, CURLOPT_VERBOSE, 1L);
74   return res;
75 }
76 
77 static void
unit_stop(void)78 unit_stop(void)
79 {
80   curl_easy_cleanup(testdata);
81   curl_global_cleanup();
82 }
83 
verify(const char * info,const char * two)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 #if defined(__GNUC__) && !defined(__clang__)
96 #pragma GCC diagnostic push
97 #pragma GCC diagnostic ignored "-Wformat"
98 #pragma GCC diagnostic ignored "-Wformat-zero-length"
99 #if __GNUC__ >= 7
100 #pragma GCC diagnostic ignored "-Wformat-overflow"
101 #endif
102 #endif
103 
104 /* Injecting a simple short string via a format */
105 msnprintf(input, sizeof(input), "Simple Test");
106 Curl_infof(testdata, "%s", input);
107 fail_unless(verify(output, input) == 0, "Simple string test");
108 
109 /* Injecting a few different variables with a format */
110 Curl_infof(testdata, "%s %u testing %lu", input, 42, 43L);
111 fail_unless(verify(output, "Simple Test 42 testing 43\n") == 0,
112             "Format string");
113 
114 /* Variations of empty strings */
115 Curl_infof(testdata, "");
116 fail_unless(strlen(output) == 1, "Empty string");
117 Curl_infof(testdata, "%s", (char *)NULL);
118 fail_unless(verify(output, "(nil)") == 0, "Passing NULL as string");
119 
120 /* A string just long enough to not be truncated */
121 memset(input, '\0', sizeof(input));
122 memset(input, 'A', 2047);
123 Curl_infof(testdata, "%s", input);
124 fail_unless(strlen(output) == 2048, "No truncation of infof input");
125 fail_unless(verify(output, input) == 0, "No truncation of infof input");
126 fail_unless(output[sizeof(output) - 1] == '\0',
127             "No truncation of infof input");
128 
129 /* Just over the limit without newline for truncation via '...' */
130 memset(input + 2047, 'A', 4);
131 Curl_infof(testdata, "%s", input);
132 fail_unless(strlen(output) == 2051, "Truncation of infof input 1");
133 fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 1");
134 
135 /* Just over the limit with newline for truncation via '...' */
136 memset(input + 2047, 'A', 4);
137 memset(input + 2047 + 4, '\n', 1);
138 Curl_infof(testdata, "%s", input);
139 fail_unless(strlen(output) == 2051, "Truncation of infof input 2");
140 fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 2");
141 
142 /* Way over the limit for truncation via '...' */
143 memset(input, '\0', sizeof(input));
144 memset(input, 'A', sizeof(input) - 1);
145 Curl_infof(testdata, "%s", input);
146 fail_unless(strlen(output) == 2051, "Truncation of infof input 3");
147 fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 3");
148 
149 #if defined(__GNUC__) && !defined(__clang__)
150 #pragma GCC diagnostic pop
151 #endif
152 
153 UNITTEST_STOP
154