• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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  ***************************************************************************/
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     curl_global_cleanup();
68     return CURLE_OUT_OF_MEMORY;
69   }
70   curl_easy_setopt(data, CURLOPT_DEBUGFUNCTION, debugf_cb);
71   curl_easy_setopt(data, CURLOPT_VERBOSE, 1L);
72   return CURLE_OK;
73 }
74 
75 static void
unit_stop(void)76 unit_stop(void)
77 {
78   curl_easy_cleanup(data);
79   curl_global_cleanup();
80 }
81 
verify(const char * info,const char * two)82 static int verify(const char *info, const char *two)
83 {
84   /* the 'info' one has a newline appended */
85   char *nl = strchr(info, '\n');
86   if(!nl)
87     return 1; /* nope */
88   return strncmp(info, two, nl - info);
89 }
90 
91 UNITTEST_START
92 
93 /* Injecting a simple short string via a format */
94 msnprintf(input, sizeof(input), "Simple Test");
95 Curl_infof(data, "%s", input);
96 fail_unless(verify(result, input) == 0, "Simple string test");
97 
98 /* Injecting a few different variables with a format */
99 Curl_infof(data, "%s %u testing %lu", input, 42, 43L);
100 fail_unless(verify(result, "Simple Test 42 testing 43\n") == 0,
101             "Format string");
102 
103 /* Variations of empty strings */
104 Curl_infof(data, "");
105 fail_unless(strlen(result) == 1, "Empty string");
106 Curl_infof(data, "%s", NULL);
107 fail_unless(verify(result, "(nil)") == 0, "Passing NULL as string");
108 
109 /* A string just long enough to not be truncated */
110 memset(input, '\0', sizeof(input));
111 memset(input, 'A', 2047);
112 Curl_infof(data, "%s", input);
113 fail_unless(strlen(result) == 2048, "No truncation of infof input");
114 fail_unless(verify(result, input) == 0, "No truncation of infof input");
115 fail_unless(result[sizeof(result) - 1] == '\0',
116             "No truncation of infof input");
117 
118 /* Just over the limit for truncation without newline */
119 memset(input + 2047, 'A', 4);
120 Curl_infof(data, "%s", input);
121 fail_unless(strlen(result) == 2048, "Truncation of infof input 1");
122 fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1");
123 
124 /* Just over the limit for truncation with newline */
125 memset(input + 2047, 'A', 4);
126 memset(input + 2047 + 4, '\n', 1);
127 Curl_infof(data, "%s", input);
128 fail_unless(strlen(result) == 2048, "Truncation of infof input 2");
129 fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2");
130 
131 /* Way over the limit for truncation with newline */
132 memset(input, '\0', sizeof(input));
133 memset(input, 'A', sizeof(input) - 1);
134 Curl_infof(data, "%s", input);
135 fail_unless(strlen(result) == 2048, "Truncation of infof input 3");
136 fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3");
137 
138 
139 UNITTEST_STOP
140