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

Side by Side Diff: base/strings/safe_sprintf_unittest.cc

Issue 1852433005: Convert //base to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase after r384946 Created 4 years, 8 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
« no previous file with comments | « base/sequence_checker_unittest.cc ('k') | base/strings/utf_offset_string_conversions.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/strings/safe_sprintf.h" 5 #include "base/strings/safe_sprintf.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <string.h> 10 #include <string.h>
11 11
12 #include <limits> 12 #include <limits>
13 #include <memory>
13 14
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 // Death tests on Android are currently very flaky. No need to add more flaky 20 // Death tests on Android are currently very flaky. No need to add more flaky
21 // tests, as they just make it hard to spot real problems. 21 // tests, as they just make it hard to spot real problems.
22 // TODO(markus): See if the restrictions on Android can eventually be lifted. 22 // TODO(markus): See if the restrictions on Android can eventually be lifted.
23 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) 23 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
24 #define ALLOW_DEATH_TEST 24 #define ALLOW_DEATH_TEST
25 #endif 25 #endif
26 26
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 EXPECT_DEATH(SafeSPrintf(buf, "%c%c", 'A'), "cur_arg < max_args"); 198 EXPECT_DEATH(SafeSPrintf(buf, "%c%c", 'A'), "cur_arg < max_args");
199 #endif 199 #endif
200 } 200 }
201 201
202 TEST(SafeSPrintfTest, ASANFriendlyBufferTest) { 202 TEST(SafeSPrintfTest, ASANFriendlyBufferTest) {
203 // Print into a buffer that is sized exactly to size. ASAN can verify that 203 // Print into a buffer that is sized exactly to size. ASAN can verify that
204 // nobody attempts to write past the end of the buffer. 204 // nobody attempts to write past the end of the buffer.
205 // There is a more complicated test in PrintLongString() that covers a lot 205 // There is a more complicated test in PrintLongString() that covers a lot
206 // more edge case, but it is also harder to debug in case of a failure. 206 // more edge case, but it is also harder to debug in case of a failure.
207 const char kTestString[] = "This is a test"; 207 const char kTestString[] = "This is a test";
208 scoped_ptr<char[]> buf(new char[sizeof(kTestString)]); 208 std::unique_ptr<char[]> buf(new char[sizeof(kTestString)]);
209 EXPECT_EQ(static_cast<ssize_t>(sizeof(kTestString) - 1), 209 EXPECT_EQ(static_cast<ssize_t>(sizeof(kTestString) - 1),
210 SafeSNPrintf(buf.get(), sizeof(kTestString), kTestString)); 210 SafeSNPrintf(buf.get(), sizeof(kTestString), kTestString));
211 EXPECT_EQ(std::string(kTestString), std::string(buf.get())); 211 EXPECT_EQ(std::string(kTestString), std::string(buf.get()));
212 EXPECT_EQ(static_cast<ssize_t>(sizeof(kTestString) - 1), 212 EXPECT_EQ(static_cast<ssize_t>(sizeof(kTestString) - 1),
213 SafeSNPrintf(buf.get(), sizeof(kTestString), "%s", kTestString)); 213 SafeSNPrintf(buf.get(), sizeof(kTestString), "%s", kTestString));
214 EXPECT_EQ(std::string(kTestString), std::string(buf.get())); 214 EXPECT_EQ(std::string(kTestString), std::string(buf.get()));
215 } 215 }
216 216
217 TEST(SafeSPrintfTest, NArgs) { 217 TEST(SafeSPrintfTest, NArgs) {
218 // Pre-C++11 compilers have a different code path, that can only print 218 // Pre-C++11 compilers have a different code path, that can only print
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 } 362 }
363 363
364 namespace { 364 namespace {
365 void PrintLongString(char* buf, size_t sz) { 365 void PrintLongString(char* buf, size_t sz) {
366 // Output a reasonably complex expression into a limited-size buffer. 366 // Output a reasonably complex expression into a limited-size buffer.
367 // At least one byte is available for writing the NUL character. 367 // At least one byte is available for writing the NUL character.
368 CHECK_GT(sz, static_cast<size_t>(0)); 368 CHECK_GT(sz, static_cast<size_t>(0));
369 369
370 // Allocate slightly more space, so that we can verify that SafeSPrintf() 370 // Allocate slightly more space, so that we can verify that SafeSPrintf()
371 // never writes past the end of the buffer. 371 // never writes past the end of the buffer.
372 scoped_ptr<char[]> tmp(new char[sz+2]); 372 std::unique_ptr<char[]> tmp(new char[sz + 2]);
373 memset(tmp.get(), 'X', sz+2); 373 memset(tmp.get(), 'X', sz+2);
374 374
375 // Use SafeSPrintf() to output a complex list of arguments: 375 // Use SafeSPrintf() to output a complex list of arguments:
376 // - test padding and truncating %c single characters. 376 // - test padding and truncating %c single characters.
377 // - test truncating %s simple strings. 377 // - test truncating %s simple strings.
378 // - test mismatching arguments and truncating (for %d != %s). 378 // - test mismatching arguments and truncating (for %d != %s).
379 // - test zero-padding and truncating %x hexadecimal numbers. 379 // - test zero-padding and truncating %x hexadecimal numbers.
380 // - test outputting and truncating %d MININT. 380 // - test outputting and truncating %d MININT.
381 // - test outputting and truncating %p arbitrary pointer values. 381 // - test outputting and truncating %p arbitrary pointer values.
382 // - test outputting, padding and truncating NULL-pointer %s strings. 382 // - test outputting, padding and truncating NULL-pointer %s strings.
383 char* out = tmp.get(); 383 char* out = tmp.get();
384 size_t out_sz = sz; 384 size_t out_sz = sz;
385 size_t len; 385 size_t len;
386 for (scoped_ptr<char[]> perfect_buf;;) { 386 for (std::unique_ptr<char[]> perfect_buf;;) {
387 size_t needed = SafeSNPrintf(out, out_sz, 387 size_t needed = SafeSNPrintf(out, out_sz,
388 #if defined(NDEBUG) 388 #if defined(NDEBUG)
389 "A%2cong %s: %d %010X %d %p%7s", 'l', "string", "", 389 "A%2cong %s: %d %010X %d %p%7s", 'l', "string", "",
390 #else 390 #else
391 "A%2cong %s: %%d %010X %d %p%7s", 'l', "string", 391 "A%2cong %s: %%d %010X %d %p%7s", 'l', "string",
392 #endif 392 #endif
393 0xDEADBEEF, std::numeric_limits<intptr_t>::min(), 393 0xDEADBEEF, std::numeric_limits<intptr_t>::min(),
394 PrintLongString, static_cast<char*>(NULL)) + 1; 394 PrintLongString, static_cast<char*>(NULL)) + 1;
395 395
396 // Various sanity checks: 396 // Various sanity checks:
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 void *ptr = str; 754 void *ptr = str;
755 char buf[40]; 755 char buf[40];
756 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); 756 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str));
757 EXPECT_EQ("0x80000000", std::string(buf)); 757 EXPECT_EQ("0x80000000", std::string(buf));
758 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); 758 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr));
759 EXPECT_EQ("0x80000000", std::string(buf)); 759 EXPECT_EQ("0x80000000", std::string(buf));
760 } 760 }
761 761
762 } // namespace strings 762 } // namespace strings
763 } // namespace base 763 } // namespace base
OLDNEW
« no previous file with comments | « base/sequence_checker_unittest.cc ('k') | base/strings/utf_offset_string_conversions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698