OLD | NEW |
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 <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 // Death tests on Android are currently very flaky. No need to add more flaky | 16 // Death tests on Android are currently very flaky. No need to add more flaky |
17 // tests, as they just make it hard to spot real problems. | 17 // tests, as they just make it hard to spot real problems. |
18 // TODO(markus): See if the restrictions on Android can eventually be lifted. | 18 // TODO(markus): See if the restrictions on Android can eventually be lifted. |
19 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) | 19 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) |
20 #define ALLOW_DEATH_TEST | 20 #define ALLOW_DEATH_TEST |
21 #endif | 21 #endif |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 namespace strings { | 24 namespace strings { |
25 | 25 |
26 TEST(SafeSPrintfTest, Empty) { | 26 TEST(SafeSPrintfTest, Empty) { |
27 char buf[2] = { 'X', 'X' }; | 27 char buf[2] = { 'X', 'X' }; |
28 | 28 |
29 // Negative buffer size should always result in an error. | 29 // Negative buffer size should always result in an error. |
30 EXPECT_EQ(-1, SafeSNPrintf(buf, -1, "")); | 30 EXPECT_EQ(-1, SafeSNPrintf(buf, static_cast<size_t>(-1), "")); |
31 EXPECT_EQ('X', buf[0]); | 31 EXPECT_EQ('X', buf[0]); |
32 EXPECT_EQ('X', buf[1]); | 32 EXPECT_EQ('X', buf[1]); |
33 | 33 |
34 // Zero buffer size should always result in an error. | 34 // Zero buffer size should always result in an error. |
35 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, "")); | 35 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, "")); |
36 EXPECT_EQ('X', buf[0]); | 36 EXPECT_EQ('X', buf[0]); |
37 EXPECT_EQ('X', buf[1]); | 37 EXPECT_EQ('X', buf[1]); |
38 | 38 |
39 // A one-byte buffer should always print a single NUL byte. | 39 // A one-byte buffer should always print a single NUL byte. |
40 EXPECT_EQ(0, SafeSNPrintf(buf, 1, "")); | 40 EXPECT_EQ(0, SafeSNPrintf(buf, 1, "")); |
(...skipping 17 matching lines...) Expand all Loading... |
58 TEST(SafeSPrintfTest, NoArguments) { | 58 TEST(SafeSPrintfTest, NoArguments) { |
59 // Output a text message that doesn't require any substitutions. This | 59 // Output a text message that doesn't require any substitutions. This |
60 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does | 60 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does |
61 // always add a trailing NUL; it always deduplicates '%' characters). | 61 // always add a trailing NUL; it always deduplicates '%' characters). |
62 static const char text[] = "hello world"; | 62 static const char text[] = "hello world"; |
63 char ref[20], buf[20]; | 63 char ref[20], buf[20]; |
64 memset(ref, 'X', sizeof(char) * arraysize(buf)); | 64 memset(ref, 'X', sizeof(char) * arraysize(buf)); |
65 memcpy(buf, ref, sizeof(buf)); | 65 memcpy(buf, ref, sizeof(buf)); |
66 | 66 |
67 // A negative buffer size should always result in an error. | 67 // A negative buffer size should always result in an error. |
68 EXPECT_EQ(-1, SafeSNPrintf(buf, -1, text)); | 68 EXPECT_EQ(-1, SafeSNPrintf(buf, static_cast<size_t>(-1), text)); |
69 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); | 69 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); |
70 | 70 |
71 // Zero buffer size should always result in an error. | 71 // Zero buffer size should always result in an error. |
72 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, text)); | 72 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, text)); |
73 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); | 73 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); |
74 | 74 |
75 // A one-byte buffer should always print a single NUL byte. | 75 // A one-byte buffer should always print a single NUL byte. |
76 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, SafeSNPrintf(buf, 1, text)); | 76 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, SafeSNPrintf(buf, 1, text)); |
77 EXPECT_EQ(0, buf[0]); | 77 EXPECT_EQ(0, buf[0]); |
78 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1)); | 78 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 122 |
123 TEST(SafeSPrintfTest, OneArgument) { | 123 TEST(SafeSPrintfTest, OneArgument) { |
124 // Test basic single-argument single-character substitution. | 124 // Test basic single-argument single-character substitution. |
125 const char text[] = "hello world"; | 125 const char text[] = "hello world"; |
126 const char fmt[] = "hello%cworld"; | 126 const char fmt[] = "hello%cworld"; |
127 char ref[20], buf[20]; | 127 char ref[20], buf[20]; |
128 memset(ref, 'X', sizeof(buf)); | 128 memset(ref, 'X', sizeof(buf)); |
129 memcpy(buf, ref, sizeof(buf)); | 129 memcpy(buf, ref, sizeof(buf)); |
130 | 130 |
131 // A negative buffer size should always result in an error. | 131 // A negative buffer size should always result in an error. |
132 EXPECT_EQ(-1, SafeSNPrintf(buf, -1, fmt, ' ')); | 132 EXPECT_EQ(-1, SafeSNPrintf(buf, static_cast<size_t>(-1), fmt, ' ')); |
133 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); | 133 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); |
134 | 134 |
135 // Zero buffer size should always result in an error. | 135 // Zero buffer size should always result in an error. |
136 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, fmt, ' ')); | 136 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, fmt, ' ')); |
137 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); | 137 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); |
138 | 138 |
139 // A one-byte buffer should always print a single NUL byte. | 139 // A one-byte buffer should always print a single NUL byte. |
140 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, | 140 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, |
141 SafeSNPrintf(buf, 1, fmt, ' ')); | 141 SafeSNPrintf(buf, 1, fmt, ' ')); |
142 EXPECT_EQ(0, buf[0]); | 142 EXPECT_EQ(0, buf[0]); |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 void *ptr = str; | 743 void *ptr = str; |
744 char buf[40]; | 744 char buf[40]; |
745 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); | 745 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); |
746 EXPECT_EQ("0x80000000", std::string(buf)); | 746 EXPECT_EQ("0x80000000", std::string(buf)); |
747 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); | 747 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); |
748 EXPECT_EQ("0x80000000", std::string(buf)); | 748 EXPECT_EQ("0x80000000", std::string(buf)); |
749 } | 749 } |
750 | 750 |
751 } // namespace strings | 751 } // namespace strings |
752 } // namespace base | 752 } // namespace base |
OLD | NEW |