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