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