1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Protocol Buffers - Google's data interchange format
// Copyright 2012 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// from google3/base/stringprintf_unittest.cc
#include <stubs/stringprintf.h>
#include <testing/googletest.h>
#include <gtest/gtest.h>
#include <array>
#include <cerrno>
#include <string>
namespace google {
namespace protobuf {
namespace {
TEST(StringPrintfTest, Empty) {
#if 0
// gcc 2.95.3, gcc 4.1.0, and gcc 4.2.2 all warn about this:
// warning: zero-length printf format string.
// so we do not allow them in google3.
EXPECT_EQ("", StringPrintf(""));
#endif
EXPECT_EQ("", StringPrintf("%s", std::string().c_str()));
EXPECT_EQ("", StringPrintf("%s", ""));
}
TEST(StringPrintfTest, Misc) {
// MSVC and mingw does not support $ format specifier.
#if !defined(_MSC_VER) && !defined(__MINGW32__)
EXPECT_EQ("123hello w", StringPrintf("%3$d%2$s %1$c", 'w', "hello", 123));
#endif // !_MSC_VER
}
TEST(StringAppendFTest, Empty) {
std::string value("Hello");
const char* empty = "";
StringAppendF(&value, "%s", empty);
EXPECT_EQ("Hello", value);
}
TEST(StringAppendFTest, EmptyString) {
std::string value("Hello");
StringAppendF(&value, "%s", "");
EXPECT_EQ("Hello", value);
}
TEST(StringAppendFTest, String) {
std::string value("Hello");
StringAppendF(&value, " %s", "World");
EXPECT_EQ("Hello World", value);
}
TEST(StringAppendFTest, Int) {
std::string value("Hello");
StringAppendF(&value, " %d", 123);
EXPECT_EQ("Hello 123", value);
}
TEST(StringPrintfTest, Multibyte) {
// If we are in multibyte mode and feed invalid multibyte sequence,
// StringPrintf should return an empty string instead of running
// out of memory while trying to determine destination buffer size.
// see b/4194543.
char* old_locale_c = setlocale(LC_CTYPE, nullptr);
ASSERT_TRUE(old_locale_c != nullptr);
std::string old_locale = old_locale_c;
// Push locale with multibyte mode
setlocale(LC_CTYPE, "en_US.utf8");
const char kInvalidCodePoint[] = "\375\067s";
std::string value = StringPrintf("%.*s", 3, kInvalidCodePoint);
// In some versions of glibc (e.g. eglibc-2.11.1, aka GRTEv2), snprintf
// returns error given an invalid codepoint. Other versions
// (e.g. eglibc-2.15, aka pre-GRTEv3) emit the codepoint verbatim.
// We test that the output is one of the above.
EXPECT_TRUE(value.empty() || value == kInvalidCodePoint);
// Repeat with longer string, to make sure that the dynamically
// allocated path in StringAppendV is handled correctly.
const size_t n = 2048;
std::array<char, n + 1> buf;
memset(&buf[0], ' ', n - 3);
memcpy(&buf[0] + n - 3, kInvalidCodePoint, 4);
value = StringPrintf("%.*s", n, &buf[0]);
// See GRTEv2 vs. GRTEv3 comment above.
EXPECT_TRUE(value.empty() || value == &buf[0]);
setlocale(LC_CTYPE, old_locale.c_str());
}
TEST(StringPrintfTest, NoMultibyte) {
// No multibyte handling, but the string contains funny chars.
char* old_locale_c = setlocale(LC_CTYPE, nullptr);
ASSERT_TRUE(old_locale_c != nullptr);
std::string old_locale = old_locale_c;
setlocale(LC_CTYPE, "POSIX");
std::string value = StringPrintf("%.*s", 3, "\375\067s");
setlocale(LC_CTYPE, old_locale.c_str());
EXPECT_EQ("\375\067s", value);
}
TEST(StringPrintfTest, DontOverwriteErrno) {
// Check that errno isn't overwritten unless we're printing
// something significantly larger than what people are normally
// printing in their badly written PLOG() statements.
errno = ECHILD;
std::string value = StringPrintf("Hello, %s!", "World");
EXPECT_EQ(ECHILD, errno);
}
TEST(StringPrintfTest, LargeBuf) {
// Check that the large buffer is handled correctly.
int n = 2048;
char* buf = new char[n+1];
memset(buf, ' ', n);
buf[n] = 0;
std::string value = StringPrintf("%s", buf);
EXPECT_EQ(buf, value);
delete[] buf;
}
} // anonymous namespace
} // namespace protobuf
} // namespace google
|