xref: /aosp_15_r20/external/cronet/base/strings/stringprintf_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/strings/stringprintf.h"
6 
7 #include <errno.h>
8 #include <stddef.h>
9 
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 
15 namespace {
16 
17 // A helper for the StringAppendV test that follows.
18 //
19 // Just forwards its args to StringAppendV.
20 template <class CharT>
StringAppendVTestHelper(std::basic_string<CharT> * out,const CharT * format,...)21 static void StringAppendVTestHelper(std::basic_string<CharT>* out,
22                                     const CharT* format,
23                                     ...) {
24   va_list ap;
25   va_start(ap, format);
26   StringAppendV(out, format, ap);
27   va_end(ap);
28 }
29 
30 }  // namespace
31 
TEST(StringPrintfTest,StringPrintfEmpty)32 TEST(StringPrintfTest, StringPrintfEmpty) {
33   EXPECT_EQ("", StringPrintf("%s", ""));
34 }
35 
TEST(StringPrintfTest,StringPrintfMisc)36 TEST(StringPrintfTest, StringPrintfMisc) {
37   EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
38 }
39 
TEST(StringPrintfTest,StringAppendfEmptyString)40 TEST(StringPrintfTest, StringAppendfEmptyString) {
41   std::string value("Hello");
42   StringAppendF(&value, "%s", "");
43   EXPECT_EQ("Hello", value);
44 }
45 
TEST(StringPrintfTest,StringAppendfString)46 TEST(StringPrintfTest, StringAppendfString) {
47   std::string value("Hello");
48   StringAppendF(&value, " %s", "World");
49   EXPECT_EQ("Hello World", value);
50 }
51 
TEST(StringPrintfTest,StringAppendfInt)52 TEST(StringPrintfTest, StringAppendfInt) {
53   std::string value("Hello");
54   StringAppendF(&value, " %d", 123);
55   EXPECT_EQ("Hello 123", value);
56 }
57 
58 // Make sure that lengths exactly around the initial buffer size are handled
59 // correctly.
TEST(StringPrintfTest,StringPrintfBounds)60 TEST(StringPrintfTest, StringPrintfBounds) {
61   const int kSrcLen = 1026;
62   char src[kSrcLen];
63   std::fill_n(src, kSrcLen, 'A');
64 
65   wchar_t srcw[kSrcLen];
66   std::fill_n(srcw, kSrcLen, 'A');
67 
68   char16_t src16[kSrcLen];
69   std::fill_n(src16, kSrcLen, 'A');
70 
71   for (int i = 1; i < 3; i++) {
72     src[kSrcLen - i] = 0;
73     std::string out;
74     EXPECT_EQ(src, StringPrintf("%s", src));
75   }
76 }
77 
78 // Test very large sprintfs that will cause the buffer to grow.
TEST(StringPrintfTest,Grow)79 TEST(StringPrintfTest, Grow) {
80   char src[1026];
81   for (auto& i : src)
82     i = 'A';
83   src[1025] = 0;
84 
85   const char fmt[] = "%sB%sB%sB%sB%sB%sB%s";
86 
87   const int kRefSize = 320000;
88   char* ref = new char[kRefSize];
89 #if BUILDFLAG(IS_WIN)
90   sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src);
91 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
92   snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src);
93 #endif
94 
95   EXPECT_EQ(ref, StringPrintf(fmt, src, src, src, src, src, src, src));
96   delete[] ref;
97 }
98 
TEST(StringPrintfTest,StringAppendV)99 TEST(StringPrintfTest, StringAppendV) {
100   std::string out;
101   StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
102   EXPECT_EQ("1 foo bar", out);
103 }
104 
105 // Test the boundary condition for the size of the string_util's
106 // internal buffer.
TEST(StringPrintfTest,GrowBoundary)107 TEST(StringPrintfTest, GrowBoundary) {
108   const int kStringUtilBufLen = 1024;
109   // Our buffer should be one larger than the size of StringAppendVT's stack
110   // buffer.
111   // And need extra one for NULL-terminator.
112   const int kBufLen = kStringUtilBufLen + 1 + 1;
113   char src[kBufLen];
114   for (int i = 0; i < kBufLen - 1; ++i)
115     src[i] = 'a';
116   src[kBufLen - 1] = 0;
117 
118   EXPECT_EQ(src, StringPrintf("%s", src));
119 }
120 
121 // Test that StringPrintf and StringAppendV do not change errno.
TEST(StringPrintfTest,StringPrintfErrno)122 TEST(StringPrintfTest, StringPrintfErrno) {
123   errno = 1;
124   EXPECT_EQ("", StringPrintf("%s", ""));
125   EXPECT_EQ(1, errno);
126   std::string out;
127   StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
128   EXPECT_EQ(1, errno);
129 }
130 
131 }  // namespace base
132