Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: base/debug/format_unittest.cc

Issue 18656004: Added a new SafeSPrintf() function that implements snprintf() in an async-safe-fashion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed jln's comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/debug/format.cc ('K') | « base/debug/format.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Author: markus@chromium.org
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include <limits>
11
12 #include "base/debug/format.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 #if defined(GTEST_HAS_DEATH_TEST)
18 #define ALLOW_DEATH_TEST
19 #endif
20
21 namespace base {
22 namespace debug {
23
24 TEST(FormatTest, Empty) {
25 char buf[2] = { 'X', 'X' };
26
27 // Negative buffer size should always result in an error.
28 EXPECT_EQ(-1, FormatN(buf, -1, ""));
29 EXPECT_EQ('X', buf[0]);
30 EXPECT_EQ('X', buf[1]);
31
32 // Zero buffer size should always result in an error.
33 EXPECT_EQ(-1, FormatN(buf, 0, ""));
34 EXPECT_EQ('X', buf[0]);
35 EXPECT_EQ('X', buf[1]);
36
37 // A one-byte buffer should always print a single NUL byte.
38 EXPECT_EQ(0, FormatN(buf, 1, ""));
39 EXPECT_EQ(0, buf[0]);
40 EXPECT_EQ('X', buf[1]);
41 buf[0] = 'X';
42
43 // A larger buffer should leave the trailing bytes unchanged.
44 EXPECT_EQ(0, FormatN(buf, 2, ""));
45 EXPECT_EQ(0, buf[0]);
46 EXPECT_EQ('X', buf[1]);
47 buf[0] = 'X';
48
49 // The same test using Format() instead of FormatN().
50 EXPECT_EQ(0, Format(buf, ""));
51 EXPECT_EQ(0, buf[0]);
52 EXPECT_EQ('X', buf[1]);
53 buf[0] = 'X';
54 }
55
56 TEST(FormatTest, NoArguments) {
57 // Output a text message that doesn't require any substitutions. This
58 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does
59 // always add a trailing NUL; it always deduplicates '%' characters).
60 const char text[] = "hello world";
61 char ref[20], buf[20];
62 memset(ref, 'X', sizeof(buf));
63 memcpy(buf, ref, sizeof(buf));
64
65 // A negative buffer size should always result in an error.
66 EXPECT_EQ(-1, FormatN(buf, -1, text));
67 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
68
69 // Zero buffer size should always result in an error.
70 EXPECT_EQ(-1, FormatN(buf, 0, text));
71 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
72
73 // A one-byte buffer should always print a single NUL byte.
74 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 1, text));
75 EXPECT_EQ(0, buf[0]);
76 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1));
77 memcpy(buf, ref, sizeof(buf));
78
79 // A larger (but limited) buffer should always leave the trailing bytes
80 // unchanged.
81 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 2, text));
82 EXPECT_EQ(text[0], buf[0]);
83 EXPECT_EQ(0, buf[1]);
84 EXPECT_TRUE(!memcmp(buf+2, ref+2, sizeof(buf)-2));
85 memcpy(buf, ref, sizeof(buf));
86
87 // A unrestricted buffer length should always leave the trailing bytes
88 // unchanged.
89 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1,
90 FormatN(buf, sizeof(buf), text));
91 EXPECT_EQ(std::string(text), std::string(buf));
92 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
93 sizeof(buf) - sizeof(text)));
94 memcpy(buf, ref, sizeof(buf));
95
96 // The same test using Format() instead of FormatN().
97 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, Format(buf, text));
98 EXPECT_EQ(std::string(text), std::string(buf));
99 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
100 sizeof(buf) - sizeof(text)));
101 memcpy(buf, ref, sizeof(buf));
102
103 // Check for deduplication of '%' percent characters.
104 EXPECT_EQ(1, Format(buf, "%%"));
105 EXPECT_EQ(2, Format(buf, "%%%%"));
106 EXPECT_EQ(2, Format(buf, "%%X"));
107 EXPECT_EQ(3, Format(buf, "%%%%X"));
108 #if defined(NDEBUG)
109 EXPECT_EQ(1, Format(buf, "%"));
110 EXPECT_EQ(2, Format(buf, "%%%"));
111 EXPECT_EQ(2, Format(buf, "%X"));
112 EXPECT_EQ(3, Format(buf, "%%%X"));
113 #elif defined(ALLOW_DEATH_TEST)
114 EXPECT_DEATH(Format(buf, "%"), "src.1. == '%'");
115 EXPECT_DEATH(Format(buf, "%%%"), "src.1. == '%'");
116 EXPECT_DEATH(Format(buf, "%X"), "src.1. == '%'");
117 EXPECT_DEATH(Format(buf, "%%%X"), "src.1. == '%'");
118 #endif
119 }
120
121 TEST(FormatTest, OneArgument) {
122 // Test basic single-argument single-character substitution.
123 const char text[] = "hello world";
124 const char fmt[] = "hello%cworld";
125 char ref[20], buf[20];
126 memset(ref, 'X', sizeof(buf));
127 memcpy(buf, ref, sizeof(buf));
128
129 // A negative buffer size should always result in an error.
130 EXPECT_EQ(-1, FormatN(buf, -1, fmt, ' '));
131 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
132
133 // Zero buffer size should always result in an error.
134 EXPECT_EQ(-1, FormatN(buf, 0, fmt, ' '));
135 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
136
137 // A one-byte buffer should always print a single NUL byte.
138 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 1, fmt, ' '));
139 EXPECT_EQ(0, buf[0]);
140 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1));
141 memcpy(buf, ref, sizeof(buf));
142
143 // A larger (but limited) buffer should always leave the trailing bytes
144 // unchanged.
145 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 2, fmt, ' '));
146 EXPECT_EQ(text[0], buf[0]);
147 EXPECT_EQ(0, buf[1]);
148 EXPECT_TRUE(!memcmp(buf+2, ref+2, sizeof(buf)-2));
149 memcpy(buf, ref, sizeof(buf));
150
151 // A unrestricted buffer length should always leave the trailing bytes
152 // unchanged.
153 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1,
154 FormatN(buf, sizeof(buf), fmt, ' '));
155 EXPECT_EQ(std::string(text), std::string(buf));
156 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
157 sizeof(buf) - sizeof(text)));
158 memcpy(buf, ref, sizeof(buf));
159
160 // The same test using Format() instead of FormatN().
161 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, Format(buf, fmt, ' '));
162 EXPECT_EQ(std::string(text), std::string(buf));
163 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
164 sizeof(buf) - sizeof(text)));
165 memcpy(buf, ref, sizeof(buf));
166
167 // Check for deduplication of '%' percent characters.
168 EXPECT_EQ(1, Format(buf, "%%", 0));
169 EXPECT_EQ(2, Format(buf, "%%%%", 0));
170 EXPECT_EQ(2, Format(buf, "%Y", 0));
171 EXPECT_EQ(2, Format(buf, "%%Y", 0));
172 EXPECT_EQ(3, Format(buf, "%%%Y", 0));
173 EXPECT_EQ(3, Format(buf, "%%%%Y", 0));
174 #if defined(NDEBUG)
175 EXPECT_EQ(1, Format(buf, "%", 0));
176 EXPECT_EQ(2, Format(buf, "%%%", 0));
177 #elif defined(ALLOW_DEATH_TEST)
178 EXPECT_DEATH(Format(buf, "%", 0), "ch");
179 EXPECT_DEATH(Format(buf, "%%%", 0), "ch");
180 #endif
181 }
182
183 #if defined(NDEBUG)
184 TEST(FormatTest, MissingArg) {
185 char buf[20];
186 EXPECT_EQ(3, Format(buf, "%c%c", 'A'));
187 EXPECT_EQ("A%c", std::string(buf));
188 }
189 #endif
190
191 TEST(FormatTest, NArgs) {
192 // Pre-C++11 compilers have a different code path, that can only print
193 // up to ten distinct arguments.
194 // We test both Format() and FormatN(). This makes sure we don't have
195 // typos in the copy-n-pasted code that is needed to deal with various
196 // numbers of arguments.
197 char buf[12];
198 EXPECT_EQ(1, Format(buf, "%c", 1));
199 EXPECT_EQ("\1", std::string(buf));
200 EXPECT_EQ(2, Format(buf, "%c%c", 1, 2));
201 EXPECT_EQ("\1\2", std::string(buf));
202 EXPECT_EQ(3, Format(buf, "%c%c%c", 1, 2, 3));
203 EXPECT_EQ("\1\2\3", std::string(buf));
204 EXPECT_EQ(4, Format(buf, "%c%c%c%c", 1, 2, 3, 4));
205 EXPECT_EQ("\1\2\3\4", std::string(buf));
206 EXPECT_EQ(5, Format(buf, "%c%c%c%c%c", 1, 2, 3, 4, 5));
207 EXPECT_EQ("\1\2\3\4\5", std::string(buf));
208 EXPECT_EQ(6, Format(buf, "%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6));
209 EXPECT_EQ("\1\2\3\4\5\6", std::string(buf));
210 EXPECT_EQ(7, Format(buf, "%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7));
211 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf));
212 EXPECT_EQ(8, Format(buf, "%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8));
213 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf));
214 EXPECT_EQ(9, Format(buf, "%c%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8, 9));
215 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf));
216 EXPECT_EQ(10, Format(buf, "%c%c%c%c%c%c%c%c%c%c",
217 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
218
219 // Repeat all the tests with FormatN() instead of Format().
220 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf));
221 EXPECT_EQ(1, FormatN(buf, 11, "%c", 1));
222 EXPECT_EQ("\1", std::string(buf));
223 EXPECT_EQ(2, FormatN(buf, 11, "%c%c", 1, 2));
224 EXPECT_EQ("\1\2", std::string(buf));
225 EXPECT_EQ(3, FormatN(buf, 11, "%c%c%c", 1, 2, 3));
226 EXPECT_EQ("\1\2\3", std::string(buf));
227 EXPECT_EQ(4, FormatN(buf, 11, "%c%c%c%c", 1, 2, 3, 4));
228 EXPECT_EQ("\1\2\3\4", std::string(buf));
229 EXPECT_EQ(5, FormatN(buf, 11, "%c%c%c%c%c", 1, 2, 3, 4, 5));
230 EXPECT_EQ("\1\2\3\4\5", std::string(buf));
231 EXPECT_EQ(6, FormatN(buf, 11, "%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6));
232 EXPECT_EQ("\1\2\3\4\5\6", std::string(buf));
233 EXPECT_EQ(7, FormatN(buf, 11, "%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7));
234 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf));
235 EXPECT_EQ(8, FormatN(buf, 11, "%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8));
236 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf));
237 EXPECT_EQ(9, FormatN(buf, 11, "%c%c%c%c%c%c%c%c%c",
238 1, 2, 3, 4, 5, 6, 7, 8, 9));
239 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf));
240 EXPECT_EQ(10, FormatN(buf, 11, "%c%c%c%c%c%c%c%c%c%c",
241 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
242 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf));
243
244
245 // C++11 is smart enough to handle variadic template arguments. It can
246 // deal with arbitrary numbers of arguments.
247 #if __cplusplus >= 201103 // C++11
248 EXPECT_EQ(11, Format(buf, "%c%c%c%c%c%c%c%c%c%c%c",
249 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
250 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
251 EXPECT_EQ(11, FormatN(buf, 12, "%c%c%c%c%c%c%c%c%c%c%c",
252 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
253 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
254 #endif
255 }
256
257 TEST(FormatTest, DataTypes) {
258 char buf[40];
259
260 // Bytes
261 EXPECT_EQ(1, Format(buf, "%d", (uint8_t)1));
262 EXPECT_EQ("1", std::string(buf));
263 EXPECT_EQ(3, Format(buf, "%d", (uint8_t)-1));
264 EXPECT_EQ("255", std::string(buf));
265 EXPECT_EQ(1, Format(buf, "%d", (int8_t)1));
266 EXPECT_EQ("1", std::string(buf));
267 EXPECT_EQ(2, Format(buf, "%d", (int8_t)-1));
268 EXPECT_EQ("-1", std::string(buf));
269 EXPECT_EQ(4, Format(buf, "%d", (int8_t)-128));
270 EXPECT_EQ("-128", std::string(buf));
271
272 // Half-words
273 EXPECT_EQ(1, Format(buf, "%d", (uint16_t)1));
274 EXPECT_EQ("1", std::string(buf));
275 EXPECT_EQ(5, Format(buf, "%d", (uint16_t)-1));
276 EXPECT_EQ("65535", std::string(buf));
277 EXPECT_EQ(1, Format(buf, "%d", (int16_t)1));
278 EXPECT_EQ("1", std::string(buf));
279 EXPECT_EQ(2, Format(buf, "%d", (int16_t)-1));
280 EXPECT_EQ("-1", std::string(buf));
281 EXPECT_EQ(6, Format(buf, "%d", (int16_t)-32768));
282 EXPECT_EQ("-32768", std::string(buf));
283
284 // Words
285 EXPECT_EQ(1, Format(buf, "%d", (uint32_t)1));
286 EXPECT_EQ("1", std::string(buf));
287 EXPECT_EQ(10, Format(buf, "%d", (uint32_t)-1));
288 EXPECT_EQ("4294967295", std::string(buf));
289 EXPECT_EQ(1, Format(buf, "%d", (int32_t)1));
290 EXPECT_EQ("1", std::string(buf));
291 EXPECT_EQ(2, Format(buf, "%d", (int32_t)-1));
292 EXPECT_EQ("-1", std::string(buf));
293 // Work-around for an limitation of C90
294 EXPECT_EQ(11, Format(buf, "%d", (int32_t)-2147483647-1));
295 EXPECT_EQ("-2147483648", std::string(buf));
296
297 // Quads
298 EXPECT_EQ(1, Format(buf, "%d", (uint64_t)1));
299 EXPECT_EQ("1", std::string(buf));
300 EXPECT_EQ(20, Format(buf, "%d", (uint64_t)-1));
301 EXPECT_EQ("18446744073709551615", std::string(buf));
302 EXPECT_EQ(1, Format(buf, "%d", (int64_t)1));
303 EXPECT_EQ("1", std::string(buf));
304 EXPECT_EQ(2, Format(buf, "%d", (int64_t)-1));
305 EXPECT_EQ("-1", std::string(buf));
306 // Work-around for an limitation of C90
307 EXPECT_EQ(20, Format(buf, "%d", (int64_t)-9223372036854775807LL-1));
308 EXPECT_EQ("-9223372036854775808", std::string(buf));
309
310 // Strings (both const and mutable).
311 EXPECT_EQ(4, Format(buf, "test"));
312 EXPECT_EQ("test", std::string(buf));
313 EXPECT_EQ(4, Format(buf, buf));
314 EXPECT_EQ("test", std::string(buf));
315
316 // Pointer
317 char addr[20];
318 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)buf);
319 Format(buf, "%p", buf);
320 EXPECT_EQ(std::string(addr), std::string(buf));
321 Format(buf, "%p", (const char *)buf);
322 EXPECT_EQ(std::string(addr), std::string(buf));
323 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)sprintf);
324 Format(buf, "%p", sprintf);
325 EXPECT_EQ(std::string(addr), std::string(buf));
326
327 // Padding for pointers is a little more complicated because of the "0x"
328 // prefix. Padding with '0' zeros is relatively straight-forward, but
329 // padding with ' ' spaces requires more effort.
330 sprintf(addr, "0x%017llX", (unsigned long long)(uintptr_t)buf);
331 Format(buf, "%019p", buf);
332 EXPECT_EQ(std::string(addr), std::string(buf));
333 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)buf);
334 memset(addr, ' ',
335 (char*)memmove(addr + sizeof(addr) - strlen(addr) - 1,
336 addr, strlen(addr)+1) - addr);
337 Format(buf, "%19p", buf);
338 EXPECT_EQ(std::string(addr), std::string(buf));
339 }
340
341 namespace {
342 void PrintLongString(char* buf, size_t sz) {
343 // Output a reasonably complex expression into a limited-size buffer.
344 // At least one byte is available for writing the NUL character.
345 CHECK_GT(sz, static_cast<size_t>(0));
346
347 // Allocate slightly more space, so that we can verify that Format()
348 // never writes past the end of the buffer.
349 scoped_ptr<char[]> tmp(new char[sz+2]);
350 memset(tmp.get(), 'X', sz+2);
351
352 // Use Format() to output a complex list of arguments:
353 // - test padding and truncating %c single characters.
354 // - test truncating %s simple strings.
355 // - test mismatching arguments and truncating (for %d != %s).
356 // - test zero-padding and truncating %x hexadecimal numbers.
357 // - test outputting and truncating %d MININT.
358 // - test outputting and truncating %p arbitrary pointer values.
359 // - test outputting, padding and truncating NULL-pointer %s strings.
360 char* out = tmp.get();
361 size_t out_sz = sz;
362 size_t len;
363 for (scoped_ptr<char[]> perfect_buf;;) {
364 size_t needed = FormatN(out, out_sz,
365 #if defined(NDEBUG)
366 "A%2cong %s: %d %010X %d %p%7s", 'l', "string", "",
367 #else
368 "A%2cong %s: %%d %010X %d %p%7s", 'l', "string",
369 #endif
370 0xDEADBEEF, std::numeric_limits<intptr_t>::min(),
371 PrintLongString, static_cast<char*>(NULL)) + 1;
372
373 // Various sanity checks:
374 // The numbered of characters needed to print the full string should always
375 // be bigger or equal to the bytes that have actually been output.
376 len = strlen(tmp.get());
377 CHECK_GE(needed, len+1);
378
379 // The number of characters output should always fit into the buffer that
380 // was passed into Format().
381 CHECK_LT(len, out_sz);
382
383 // The output is always terminated with a NUL byte (actually, this test is
384 // always going to pass, as strlen() already verified this)
385 EXPECT_FALSE(tmp[len]);
386
387 // ASAN can check that we are not overwriting buffers, iff we make sure the
388 // buffer is exactly the size that we are expecting to be written. After
389 // running FormatN() the first time, it is possible to compute the correct
390 // buffer size for this test. So, allocate a second buffer and run the
391 // exact same FormatN() command again.
392 if (!perfect_buf.get()) {
393 out_sz = std::min(needed, sz);
394 out = new char[out_sz];
395 perfect_buf.reset(out);
396 } else {
397 break;
398 }
399 }
400
401 // All trailing bytes are unchanged.
402 for (size_t i = len+1; i < sz+2; ++i)
403 EXPECT_EQ('X', tmp[i]);
404
405 // The text that was generated by Format() should always match the
406 // equivalent text generated by sprintf(). Please note that the format
407 // string for sprintf() is nor complicated, as it does not have the
408 // benefit of getting type information from the C++ compiler.
409 //
410 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately,
411 // Visual Studio doesn't support this function, and the work-arounds
412 // are all really awkward.
413 char ref[256];
414 CHECK_LE(sz, sizeof(ref));
415 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>",
416 static_cast<long long>(std::numeric_limits<intptr_t>::min()),
417 (long long)PrintLongString);
418 ref[sz-1] = '\000';
419
420 #if defined(NDEBUG)
421 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max();
422 #else
423 const size_t kSSizeMax = internal::GetFormatSSizeMax();
424 #endif
425
426 // Compare the output from Format() to the one from sprintf().
427 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get()));
428
429 // We allocated a slightly larger buffer, so that we could perform some
430 // extra sanity checks. Now that the tests have all passed, we copy the
431 // data to the output buffer that the caller provided.
432 memcpy(buf, tmp.get(), len+1);
433 }
434
435 #if !defined(NDEBUG)
436 class ScopedFormatSSizeMaxSetter {
437 public:
438 ScopedFormatSSizeMaxSetter(size_t sz) {
439 old_ssize_max_ = internal::GetFormatSSizeMax();
440 internal::SetFormatSSizeMax(sz);
441 }
442
443 ~ScopedFormatSSizeMaxSetter() {
444 internal::SetFormatSSizeMax(old_ssize_max_);
445 }
446
447 private:
448 size_t old_ssize_max_;
449
450 DISALLOW_COPY_AND_ASSIGN(ScopedFormatSSizeMaxSetter);
451 };
452 #endif
453
454 } // anonymous namespace
455
456 TEST(FormatTest, Truncation) {
457 // We use PrintLongString() to print a complex long string and then
458 // truncate to all possible lengths. This ends up exercising a lot of
459 // different code paths in Format() and IToASCII(), as truncation can
460 // happen in a lot of different states.
461 char ref[256];
462 PrintLongString(ref, sizeof(ref));
463 for (size_t i = strlen(ref)+1; i; --i) {
464 char buf[sizeof(ref)];
465 PrintLongString(buf, i);
466 EXPECT_EQ(std::string(ref, i - 1), std::string(buf));
467 }
468
469 // When compiling in debug mode, we have the ability to fake a small
470 // upper limit for the maximum value that can be stored in an ssize_t.
471 // Format() uses this upper limit to determine how many bytes it will
472 // write to the buffer, even if the caller claimed a bigger buffer size.
473 // Repeat the truncation test and verify that this other code path in
474 // Format() works correctly, too.
475 #if !defined(NDEBUG)
476 for (size_t i = strlen(ref)+1; i > 1; --i) {
477 ScopedFormatSSizeMaxSetter ssize_max_setter(i);
478 char buf[sizeof(ref)];
479 PrintLongString(buf, sizeof(buf));
480 EXPECT_EQ(std::string(ref, i - 1), std::string(buf));
481 }
482
483 // kSSizeMax is also used to constrain the maximum amount of padding, before
484 // Format() detects an error in the format string.
485 ScopedFormatSSizeMaxSetter ssize_max_setter(100);
486 char buf[256];
487 EXPECT_EQ(99, Format(buf, "%99c", ' '));
488 EXPECT_EQ(std::string(99, ' '), std::string(buf));
489 *buf = '\000';
490 #if defined(ALLOW_DEATH_TEST)
491 EXPECT_DEATH(Format(buf, "%100c", ' '), "padding <= max_padding");
492 #endif
493 EXPECT_EQ(0, *buf);
494 #endif
495 }
496
497 TEST(FormatTest, Padding) {
498 char buf[40], fmt[40];
499
500 // Chars %c
501 EXPECT_EQ(1, Format(buf, "%c", 'A'));
502 EXPECT_EQ("A", std::string(buf));
503 EXPECT_EQ(2, Format(buf, "%2c", 'A'));
504 EXPECT_EQ(" A", std::string(buf));
505 EXPECT_EQ(2, Format(buf, "%02c", 'A'));
506 EXPECT_EQ(" A", std::string(buf));
507 EXPECT_EQ(4, Format(buf, "%-2c", 'A'));
508 EXPECT_EQ("%-2c", std::string(buf));
509 Format(fmt, "%%%dc", std::numeric_limits<ssize_t>::max() - 1);
510 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, Format(buf, fmt, 'A'));
511 Format(fmt, "%%%dc",
512 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
513 #if defined(NDEBUG)
514 EXPECT_EQ(2, Format(buf, fmt, 'A'));
515 EXPECT_EQ("%c", std::string(buf));
516 #elif defined(ALLOW_DEATH_TEST)
517 EXPECT_DEATH(Format(buf, fmt, 'A'), "padding <= max_padding");
518 #endif
519
520 // Octal %o
521 EXPECT_EQ(1, Format(buf, "%o", 1));
522 EXPECT_EQ("1", std::string(buf));
523 EXPECT_EQ(2, Format(buf, "%2o", 1));
524 EXPECT_EQ(" 1", std::string(buf));
525 EXPECT_EQ(2, Format(buf, "%02o", 1));
526 EXPECT_EQ("01", std::string(buf));
527 EXPECT_EQ(12, Format(buf, "%12o", -1));
528 EXPECT_EQ(" 37777777777", std::string(buf));
529 EXPECT_EQ(12, Format(buf, "%012o", -1));
530 EXPECT_EQ("037777777777", std::string(buf));
531 EXPECT_EQ(23, Format(buf, "%23o", -1LL));
532 EXPECT_EQ(" 1777777777777777777777", std::string(buf));
533 EXPECT_EQ(23, Format(buf, "%023o", -1LL));
534 EXPECT_EQ("01777777777777777777777", std::string(buf));
535 EXPECT_EQ(3, Format(buf, "%2o", 0111));
536 EXPECT_EQ("111", std::string(buf));
537 EXPECT_EQ(4, Format(buf, "%-2o", 1));
538 EXPECT_EQ("%-2o", std::string(buf));
539 Format(fmt, "%%%do", std::numeric_limits<ssize_t>::max()-1);
540 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
541 EXPECT_EQ(" ", std::string(buf));
542 Format(fmt, "%%0%do", std::numeric_limits<ssize_t>::max()-1);
543 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
544 EXPECT_EQ("000", std::string(buf));
545 Format(fmt, "%%%do",
546 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
547 #if defined(NDEBUG)
548 EXPECT_EQ(2, Format(buf, fmt, 1));
549 EXPECT_EQ("%o", std::string(buf));
550 #elif defined(ALLOW_DEATH_TEST)
551 EXPECT_DEATH(Format(buf, fmt, 1), "padding <= max_padding");
552 #endif
553
554 // Decimals %d
555 EXPECT_EQ(1, Format(buf, "%d", 1));
556 EXPECT_EQ("1", std::string(buf));
557 EXPECT_EQ(2, Format(buf, "%2d", 1));
558 EXPECT_EQ(" 1", std::string(buf));
559 EXPECT_EQ(2, Format(buf, "%02d", 1));
560 EXPECT_EQ("01", std::string(buf));
561 EXPECT_EQ(3, Format(buf, "%3d", -1));
562 EXPECT_EQ(" -1", std::string(buf));
563 EXPECT_EQ(3, Format(buf, "%03d", -1));
564 EXPECT_EQ("-01", std::string(buf));
565 EXPECT_EQ(3, Format(buf, "%2d", 111));
566 EXPECT_EQ("111", std::string(buf));
567 EXPECT_EQ(4, Format(buf, "%2d", -111));
568 EXPECT_EQ("-111", std::string(buf));
569 EXPECT_EQ(4, Format(buf, "%-2d", 1));
570 EXPECT_EQ("%-2d", std::string(buf));
571 Format(fmt, "%%%dd", std::numeric_limits<ssize_t>::max()-1);
572 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
573 EXPECT_EQ(" ", std::string(buf));
574 Format(fmt, "%%0%dd", std::numeric_limits<ssize_t>::max()-1);
575 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
576 EXPECT_EQ("000", std::string(buf));
577 Format(fmt, "%%%dd",
578 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
579 #if defined(NDEBUG)
580 EXPECT_EQ(2, Format(buf, fmt, 1));
581 EXPECT_EQ("%d", std::string(buf));
582 #elif defined(ALLOW_DEATH_TEST)
583 EXPECT_DEATH(Format(buf, fmt, 1), "padding <= max_padding");
584 #endif
585
586 // Hex %X
587 EXPECT_EQ(1, Format(buf, "%X", 1));
588 EXPECT_EQ("1", std::string(buf));
589 EXPECT_EQ(2, Format(buf, "%2X", 1));
590 EXPECT_EQ(" 1", std::string(buf));
591 EXPECT_EQ(2, Format(buf, "%02X", 1));
592 EXPECT_EQ("01", std::string(buf));
593 EXPECT_EQ(9, Format(buf, "%9X", -1));
594 EXPECT_EQ(" FFFFFFFF", std::string(buf));
595 EXPECT_EQ(9, Format(buf, "%09X", -1));
596 EXPECT_EQ("0FFFFFFFF", std::string(buf));
597 EXPECT_EQ(17, Format(buf, "%17X", -1LL));
598 EXPECT_EQ(" FFFFFFFFFFFFFFFF", std::string(buf));
599 EXPECT_EQ(17, Format(buf, "%017X", -1LL));
600 EXPECT_EQ("0FFFFFFFFFFFFFFFF", std::string(buf));
601 EXPECT_EQ(3, Format(buf, "%2X", 0x111));
602 EXPECT_EQ("111", std::string(buf));
603 EXPECT_EQ(4, Format(buf, "%-2X", 1));
604 EXPECT_EQ("%-2X", std::string(buf));
605 Format(fmt, "%%%dX", std::numeric_limits<ssize_t>::max()-1);
606 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
607 EXPECT_EQ(" ", std::string(buf));
608 Format(fmt, "%%0%dX", std::numeric_limits<ssize_t>::max()-1);
609 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
610 EXPECT_EQ("000", std::string(buf));
611 Format(fmt, "%%%dX",
612 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
613 #if defined(NDEBUG)
614 EXPECT_EQ(2, Format(buf, fmt, 1));
615 EXPECT_EQ("%X", std::string(buf));
616 #elif defined(ALLOW_DEATH_TEST)
617 EXPECT_DEATH(Format(buf, fmt, 1), "padding <= max_padding");
618 #endif
619
620 // Pointer %p
621 EXPECT_EQ(3, Format(buf, "%p", (void*)1));
622 EXPECT_EQ("0x1", std::string(buf));
623 EXPECT_EQ(4, Format(buf, "%4p", (void*)1));
624 EXPECT_EQ(" 0x1", std::string(buf));
625 EXPECT_EQ(4, Format(buf, "%04p", (void*)1));
626 EXPECT_EQ("0x01", std::string(buf));
627 EXPECT_EQ(5, Format(buf, "%4p", (void*)0x111));
628 EXPECT_EQ("0x111", std::string(buf));
629 EXPECT_EQ(4, Format(buf, "%-2p", (void*)1));
630 EXPECT_EQ("%-2p", std::string(buf));
631 Format(fmt, "%%%dp", std::numeric_limits<ssize_t>::max()-1);
632 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1,
633 FormatN(buf, 4, fmt, (void*)1));
634 EXPECT_EQ(" ", std::string(buf));
635 Format(fmt, "%%0%dp", std::numeric_limits<ssize_t>::max()-1);
636 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1,
637 FormatN(buf, 4, fmt, (void*)1));
638 EXPECT_EQ("0x0", std::string(buf));
639 Format(fmt, "%%%dp",
640 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
641 #if defined(NDEBUG)
642 EXPECT_EQ(2, Format(buf, fmt, 1));
643 EXPECT_EQ("%p", std::string(buf));
644 #elif defined(ALLOW_DEATH_TEST)
645 EXPECT_DEATH(Format(buf, fmt, 1), "padding <= max_padding");
646 #endif
647
648 // String
649 EXPECT_EQ(1, Format(buf, "%s", "A"));
650 EXPECT_EQ("A", std::string(buf));
651 EXPECT_EQ(2, Format(buf, "%2s", "A"));
652 EXPECT_EQ(" A", std::string(buf));
653 EXPECT_EQ(2, Format(buf, "%02s", "A"));
654 EXPECT_EQ(" A", std::string(buf));
655 EXPECT_EQ(3, Format(buf, "%2s", "AAA"));
656 EXPECT_EQ("AAA", std::string(buf));
657 EXPECT_EQ(4, Format(buf, "%-2s", "A"));
658 EXPECT_EQ("%-2s", std::string(buf));
659 Format(fmt, "%%%ds", std::numeric_limits<ssize_t>::max()-1);
660 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, "A"));
661 EXPECT_EQ(" ", std::string(buf));
662 Format(fmt, "%%0%ds", std::numeric_limits<ssize_t>::max()-1);
663 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, "A"));
664 EXPECT_EQ(" ", std::string(buf));
665 Format(fmt, "%%%ds",
666 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
667 #if defined(NDEBUG)
668 EXPECT_EQ(2, Format(buf, fmt, "A"));
669 EXPECT_EQ("%s", std::string(buf));
670 #elif defined(ALLOW_DEATH_TEST)
671 EXPECT_DEATH(Format(buf, fmt, "A"), "padding <= max_padding");
672 #endif
673 }
674
675 TEST(FormatTest, EmbeddedNul) {
676 char buf[] = { 'X', 'X', 'X', 'X' };
677 EXPECT_EQ(2, Format(buf, "%3c", 0));
678 EXPECT_EQ(' ', buf[0]);
679 EXPECT_EQ(' ', buf[1]);
680 EXPECT_EQ(0, buf[2]);
681 EXPECT_EQ('X', buf[3]);
682
683 // Check handling of a NUL format character. N.B. this takes two different
684 // code paths depending on whether we are actually passing arguments. If
685 // we don't have any arguments, we are running in the fast-path code, that
686 // looks (almost) like a strncpy().
687 #if defined(NDEBUG)
688 EXPECT_EQ(2, Format(buf, "%%%"));
689 EXPECT_EQ("%%", std::string(buf));
690 EXPECT_EQ(2, Format(buf, "%%%", 0));
691 EXPECT_EQ("%%", std::string(buf));
692 #elif defined(ALLOW_DEATH_TEST)
693 EXPECT_DEATH(Format(buf, "%%%"), "src.1. == '%'");
694 EXPECT_DEATH(Format(buf, "%%%", 0), "ch");
695 #endif
696 }
697
698 TEST(FormatTest, EmitNULL) {
699 char buf[40];
700 #if defined(__GNUC__)
701 #pragma GCC diagnostic push
702 #pragma GCC diagnostic ignored "-Wconversion-null"
703 #endif
704 EXPECT_EQ(1, Format(buf, "%d", NULL));
705 EXPECT_EQ("0", std::string(buf));
706 EXPECT_EQ(3, Format(buf, "%p", NULL));
707 EXPECT_EQ("0x0", std::string(buf));
708 EXPECT_EQ(6, Format(buf, "%s", NULL));
709 EXPECT_EQ("<NULL>", std::string(buf));
710 #if defined(__GCC__)
711 #pragma GCC diagnostic pop
712 #endif
713 }
714
715 TEST(FormatTest, PointerSize) {
716 // The internal data representation is a 64bit value, independent of the
717 // native word size. We want to perform sign-extension for signed integers,
718 // but we want to avoid doing so for pointer types. This could be a
719 // problem on systems, where pointers are only 32bit. This tests verifies
720 // that there is no such problem.
721 char *str = reinterpret_cast<char *>(0x80000000u);
722 void *ptr = str;
723 char buf[40];
724 EXPECT_EQ(10, Format(buf, "%p", str));
725 EXPECT_EQ("0x80000000", std::string(buf));
726 EXPECT_EQ(10, Format(buf, "%p", ptr));
727 EXPECT_EQ("0x80000000", std::string(buf));
728 }
jln (very slow on Chromium) 2013/08/14 04:18:06 Do you mind adding a dead simple test that would c
729
730 } // namespace debug
731 } // namespace base
OLDNEW
« base/debug/format.cc ('K') | « base/debug/format.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698