1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 // Google Test - The Google C++ Testing and Mocking Framework
32 //
33 // This file implements a universal value printer that can print a
34 // value of any type T:
35 //
36 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
37 //
38 // It uses the << operator when possible, and prints the bytes in the
39 // object otherwise. A user can override its behavior for a class
40 // type Foo by defining either operator<<(::std::ostream&, const Foo&)
41 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
42 // defines Foo.
43
44 #include "gtest/gtest-printers.h"
45 #include <stdio.h>
46 #include <cctype>
47 #include <cstdint>
48 #include <cwchar>
49 #include <ostream> // NOLINT
50 #include <string>
51 #include "gtest/internal/gtest-port.h"
52 #include "src/gtest-internal-inl.h"
53
54 namespace testing {
55
56 namespace {
57
58 using ::std::ostream;
59
60 // Prints a segment of bytes in the given object.
61 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
62 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
63 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
64 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
PrintByteSegmentInObjectTo(const unsigned char * obj_bytes,size_t start,size_t count,ostream * os)65 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
66 size_t count, ostream* os) {
67 char text[5] = "";
68 for (size_t i = 0; i != count; i++) {
69 const size_t j = start + i;
70 if (i != 0) {
71 // Organizes the bytes into groups of 2 for easy parsing by
72 // human.
73 if ((j % 2) == 0)
74 *os << ' ';
75 else
76 *os << '-';
77 }
78 GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
79 *os << text;
80 }
81 }
82
83 // Prints the bytes in the given value to the given ostream.
PrintBytesInObjectToImpl(const unsigned char * obj_bytes,size_t count,ostream * os)84 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
85 ostream* os) {
86 // Tells the user how big the object is.
87 *os << count << "-byte object <";
88
89 const size_t kThreshold = 132;
90 const size_t kChunkSize = 64;
91 // If the object size is bigger than kThreshold, we'll have to omit
92 // some details by printing only the first and the last kChunkSize
93 // bytes.
94 if (count < kThreshold) {
95 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
96 } else {
97 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
98 *os << " ... ";
99 // Rounds up to 2-byte boundary.
100 const size_t resume_pos = (count - kChunkSize + 1)/2*2;
101 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
102 }
103 *os << ">";
104 }
105
106 } // namespace
107
108 namespace internal {
109
110 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
111 // given object. The delegation simplifies the implementation, which
112 // uses the << operator and thus is easier done outside of the
113 // ::testing::internal namespace, which contains a << operator that
114 // sometimes conflicts with the one in STL.
PrintBytesInObjectTo(const unsigned char * obj_bytes,size_t count,ostream * os)115 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
116 ostream* os) {
117 PrintBytesInObjectToImpl(obj_bytes, count, os);
118 }
119
120 // Depending on the value of a char (or wchar_t), we print it in one
121 // of three formats:
122 // - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
123 // - as a hexadecimal escape sequence (e.g. '\x7F'), or
124 // - as a special escape sequence (e.g. '\r', '\n').
125 enum CharFormat {
126 kAsIs,
127 kHexEscape,
128 kSpecialEscape
129 };
130
131 // Returns true if c is a printable ASCII character. We test the
132 // value of c directly instead of calling isprint(), which is buggy on
133 // Windows Mobile.
IsPrintableAscii(wchar_t c)134 inline bool IsPrintableAscii(wchar_t c) {
135 return 0x20 <= c && c <= 0x7E;
136 }
137
138 // Prints a wide or narrow char c as a character literal without the
139 // quotes, escaping it when necessary; returns how c was formatted.
140 // The template argument UnsignedChar is the unsigned version of Char,
141 // which is the type of c.
142 template <typename UnsignedChar, typename Char>
PrintAsCharLiteralTo(Char c,ostream * os)143 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
144 wchar_t w_c = static_cast<wchar_t>(c);
145 switch (w_c) {
146 case L'\0':
147 *os << "\\0";
148 break;
149 case L'\'':
150 *os << "\\'";
151 break;
152 case L'\\':
153 *os << "\\\\";
154 break;
155 case L'\a':
156 *os << "\\a";
157 break;
158 case L'\b':
159 *os << "\\b";
160 break;
161 case L'\f':
162 *os << "\\f";
163 break;
164 case L'\n':
165 *os << "\\n";
166 break;
167 case L'\r':
168 *os << "\\r";
169 break;
170 case L'\t':
171 *os << "\\t";
172 break;
173 case L'\v':
174 *os << "\\v";
175 break;
176 default:
177 if (IsPrintableAscii(w_c)) {
178 *os << static_cast<char>(c);
179 return kAsIs;
180 } else {
181 ostream::fmtflags flags = os->flags();
182 *os << "\\x" << std::hex << std::uppercase
183 << static_cast<int>(static_cast<UnsignedChar>(c));
184 os->flags(flags);
185 return kHexEscape;
186 }
187 }
188 return kSpecialEscape;
189 }
190
191 // Prints a wchar_t c as if it's part of a string literal, escaping it when
192 // necessary; returns how c was formatted.
PrintAsStringLiteralTo(wchar_t c,ostream * os)193 static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
194 switch (c) {
195 case L'\'':
196 *os << "'";
197 return kAsIs;
198 case L'"':
199 *os << "\\\"";
200 return kSpecialEscape;
201 default:
202 return PrintAsCharLiteralTo<wchar_t>(c, os);
203 }
204 }
205
206 // Prints a char c as if it's part of a string literal, escaping it when
207 // necessary; returns how c was formatted.
PrintAsStringLiteralTo(char c,ostream * os)208 static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
209 return PrintAsStringLiteralTo(
210 static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
211 }
212
213 // Prints a wide or narrow character c and its code. '\0' is printed
214 // as "'\\0'", other unprintable characters are also properly escaped
215 // using the standard C++ escape sequence. The template argument
216 // UnsignedChar is the unsigned version of Char, which is the type of c.
217 template <typename UnsignedChar, typename Char>
PrintCharAndCodeTo(Char c,ostream * os)218 void PrintCharAndCodeTo(Char c, ostream* os) {
219 // First, print c as a literal in the most readable form we can find.
220 *os << ((sizeof(c) > 1) ? "L'" : "'");
221 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
222 *os << "'";
223
224 // To aid user debugging, we also print c's code in decimal, unless
225 // it's 0 (in which case c was printed as '\\0', making the code
226 // obvious).
227 if (c == 0)
228 return;
229 *os << " (" << static_cast<int>(c);
230
231 // For more convenience, we print c's code again in hexadecimal,
232 // unless c was already printed in the form '\x##' or the code is in
233 // [1, 9].
234 if (format == kHexEscape || (1 <= c && c <= 9)) {
235 // Do nothing.
236 } else {
237 *os << ", 0x" << String::FormatHexInt(static_cast<int>(c));
238 }
239 *os << ")";
240 }
241
PrintTo(unsigned char c,::std::ostream * os)242 void PrintTo(unsigned char c, ::std::ostream* os) {
243 PrintCharAndCodeTo<unsigned char>(c, os);
244 }
PrintTo(signed char c,::std::ostream * os)245 void PrintTo(signed char c, ::std::ostream* os) {
246 PrintCharAndCodeTo<unsigned char>(c, os);
247 }
248
249 // Prints a wchar_t as a symbol if it is printable or as its internal
250 // code otherwise and also as its code. L'\0' is printed as "L'\\0'".
PrintTo(wchar_t wc,ostream * os)251 void PrintTo(wchar_t wc, ostream* os) {
252 PrintCharAndCodeTo<wchar_t>(wc, os);
253 }
254
PrintTo(char32_t c,::std::ostream * os)255 void PrintTo(char32_t c, ::std::ostream* os) {
256 *os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4)
257 << static_cast<uint32_t>(c);
258 }
259
260 // Prints the given array of characters to the ostream. CharType must be either
261 // char or wchar_t.
262 // The array starts at begin, the length is len, it may include '\0' characters
263 // and may not be NUL-terminated.
264 template <typename CharType>
265 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
266 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
267 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
268 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
PrintCharsAsStringTo(const CharType * begin,size_t len,ostream * os)269 static CharFormat PrintCharsAsStringTo(
270 const CharType* begin, size_t len, ostream* os) {
271 const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
272 *os << kQuoteBegin;
273 bool is_previous_hex = false;
274 CharFormat print_format = kAsIs;
275 for (size_t index = 0; index < len; ++index) {
276 const CharType cur = begin[index];
277 if (is_previous_hex && IsXDigit(cur)) {
278 // Previous character is of '\x..' form and this character can be
279 // interpreted as another hexadecimal digit in its number. Break string to
280 // disambiguate.
281 *os << "\" " << kQuoteBegin;
282 }
283 is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
284 // Remember if any characters required hex escaping.
285 if (is_previous_hex) {
286 print_format = kHexEscape;
287 }
288 }
289 *os << "\"";
290 return print_format;
291 }
292
293 // Prints a (const) char/wchar_t array of 'len' elements, starting at address
294 // 'begin'. CharType must be either char or wchar_t.
295 template <typename CharType>
296 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
297 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
298 GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
299 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
UniversalPrintCharArray(const CharType * begin,size_t len,ostream * os)300 static void UniversalPrintCharArray(
301 const CharType* begin, size_t len, ostream* os) {
302 // The code
303 // const char kFoo[] = "foo";
304 // generates an array of 4, not 3, elements, with the last one being '\0'.
305 //
306 // Therefore when printing a char array, we don't print the last element if
307 // it's '\0', such that the output matches the string literal as it's
308 // written in the source code.
309 if (len > 0 && begin[len - 1] == '\0') {
310 PrintCharsAsStringTo(begin, len - 1, os);
311 return;
312 }
313
314 // If, however, the last element in the array is not '\0', e.g.
315 // const char kFoo[] = { 'f', 'o', 'o' };
316 // we must print the entire array. We also print a message to indicate
317 // that the array is not NUL-terminated.
318 PrintCharsAsStringTo(begin, len, os);
319 *os << " (no terminating NUL)";
320 }
321
322 // Prints a (const) char array of 'len' elements, starting at address 'begin'.
UniversalPrintArray(const char * begin,size_t len,ostream * os)323 void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
324 UniversalPrintCharArray(begin, len, os);
325 }
326
327 // Prints a (const) wchar_t array of 'len' elements, starting at address
328 // 'begin'.
UniversalPrintArray(const wchar_t * begin,size_t len,ostream * os)329 void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
330 UniversalPrintCharArray(begin, len, os);
331 }
332
333 // Prints the given C string to the ostream.
PrintTo(const char * s,ostream * os)334 void PrintTo(const char* s, ostream* os) {
335 if (s == nullptr) {
336 *os << "NULL";
337 } else {
338 *os << ImplicitCast_<const void*>(s) << " pointing to ";
339 PrintCharsAsStringTo(s, strlen(s), os);
340 }
341 }
342
343 // MSVC compiler can be configured to define whar_t as a typedef
344 // of unsigned short. Defining an overload for const wchar_t* in that case
345 // would cause pointers to unsigned shorts be printed as wide strings,
346 // possibly accessing more memory than intended and causing invalid
347 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
348 // wchar_t is implemented as a native type.
349 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
350 // Prints the given wide C string to the ostream.
PrintTo(const wchar_t * s,ostream * os)351 void PrintTo(const wchar_t* s, ostream* os) {
352 if (s == nullptr) {
353 *os << "NULL";
354 } else {
355 *os << ImplicitCast_<const void*>(s) << " pointing to ";
356 PrintCharsAsStringTo(s, wcslen(s), os);
357 }
358 }
359 #endif // wchar_t is native
360
361 namespace {
362
ContainsUnprintableControlCodes(const char * str,size_t length)363 bool ContainsUnprintableControlCodes(const char* str, size_t length) {
364 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
365
366 for (size_t i = 0; i < length; i++) {
367 unsigned char ch = *s++;
368 if (std::iscntrl(ch)) {
369 switch (ch) {
370 case '\t':
371 case '\n':
372 case '\r':
373 break;
374 default:
375 return true;
376 }
377 }
378 }
379 return false;
380 }
381
IsUTF8TrailByte(unsigned char t)382 bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; }
383
IsValidUTF8(const char * str,size_t length)384 bool IsValidUTF8(const char* str, size_t length) {
385 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
386
387 for (size_t i = 0; i < length;) {
388 unsigned char lead = s[i++];
389
390 if (lead <= 0x7f) {
391 continue; // single-byte character (ASCII) 0..7F
392 }
393 if (lead < 0xc2) {
394 return false; // trail byte or non-shortest form
395 } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {
396 ++i; // 2-byte character
397 } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
398 IsUTF8TrailByte(s[i]) &&
399 IsUTF8TrailByte(s[i + 1]) &&
400 // check for non-shortest form and surrogate
401 (lead != 0xe0 || s[i] >= 0xa0) &&
402 (lead != 0xed || s[i] < 0xa0)) {
403 i += 2; // 3-byte character
404 } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
405 IsUTF8TrailByte(s[i]) &&
406 IsUTF8TrailByte(s[i + 1]) &&
407 IsUTF8TrailByte(s[i + 2]) &&
408 // check for non-shortest form
409 (lead != 0xf0 || s[i] >= 0x90) &&
410 (lead != 0xf4 || s[i] < 0x90)) {
411 i += 3; // 4-byte character
412 } else {
413 return false;
414 }
415 }
416 return true;
417 }
418
ConditionalPrintAsText(const char * str,size_t length,ostream * os)419 void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {
420 if (!ContainsUnprintableControlCodes(str, length) &&
421 IsValidUTF8(str, length)) {
422 *os << "\n As Text: \"" << str << "\"";
423 }
424 }
425
426 } // anonymous namespace
427
PrintStringTo(const::std::string & s,ostream * os)428 void PrintStringTo(const ::std::string& s, ostream* os) {
429 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
430 if (GTEST_FLAG(print_utf8)) {
431 ConditionalPrintAsText(s.data(), s.size(), os);
432 }
433 }
434 }
435
436 #if GTEST_HAS_STD_WSTRING
PrintWideStringTo(const::std::wstring & s,ostream * os)437 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
438 PrintCharsAsStringTo(s.data(), s.size(), os);
439 }
440 #endif // GTEST_HAS_STD_WSTRING
441
442 } // namespace internal
443
444 } // namespace testing
445