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