Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: bcefdfb9dcb0933492c6dc7b5a93ac2c9e2523fb [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]e6811ed52010-08-17 03:45:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj51d26a402024-04-25 14:23:565#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7#pragma allow_unsafe_buffers
8#endif
9
[email protected]8ad97ad2013-06-08 06:05:4710#include "base/strings/stringprintf.h"
11
[email protected]34d58a22013-05-02 23:06:2612#include <errno.h>
avi84f37e12015-12-25 09:31:4213#include <stddef.h>
[email protected]34d58a22013-05-02 23:06:2614
avi84f37e12015-12-25 09:31:4215#include "build/build_config.h"
[email protected]e6811ed52010-08-17 03:45:3716#include "testing/gtest/include/gtest/gtest.h"
17
18namespace base {
19
20namespace {
21
22// A helper for the StringAppendV test that follows.
23//
24// Just forwards its args to StringAppendV.
Jan Wilken Dörrie8ef22c72019-09-20 05:48:1925template <class CharT>
26static void StringAppendVTestHelper(std::basic_string<CharT>* out,
27 const CharT* format,
28 ...) {
[email protected]e6811ed52010-08-17 03:45:3729 va_list ap;
30 va_start(ap, format);
31 StringAppendV(out, format, ap);
32 va_end(ap);
33}
34
35} // namespace
36
37TEST(StringPrintfTest, StringPrintfEmpty) {
38 EXPECT_EQ("", StringPrintf("%s", ""));
39}
40
41TEST(StringPrintfTest, StringPrintfMisc) {
42 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
[email protected]e6811ed52010-08-17 03:45:3743}
44
45TEST(StringPrintfTest, StringAppendfEmptyString) {
46 std::string value("Hello");
47 StringAppendF(&value, "%s", "");
48 EXPECT_EQ("Hello", value);
[email protected]e6811ed52010-08-17 03:45:3749}
50
51TEST(StringPrintfTest, StringAppendfString) {
52 std::string value("Hello");
53 StringAppendF(&value, " %s", "World");
54 EXPECT_EQ("Hello World", value);
[email protected]e6811ed52010-08-17 03:45:3755}
56
57TEST(StringPrintfTest, StringAppendfInt) {
58 std::string value("Hello");
59 StringAppendF(&value, " %d", 123);
60 EXPECT_EQ("Hello 123", value);
[email protected]e6811ed52010-08-17 03:45:3761}
62
63// Make sure that lengths exactly around the initial buffer size are handled
64// correctly.
65TEST(StringPrintfTest, StringPrintfBounds) {
66 const int kSrcLen = 1026;
67 char src[kSrcLen];
Jan Wilken Dörrie8ef22c72019-09-20 05:48:1968 std::fill_n(src, kSrcLen, 'A');
[email protected]e6811ed52010-08-17 03:45:3769
70 wchar_t srcw[kSrcLen];
Jan Wilken Dörrie8ef22c72019-09-20 05:48:1971 std::fill_n(srcw, kSrcLen, 'A');
72
73 char16_t src16[kSrcLen];
74 std::fill_n(src16, kSrcLen, 'A');
[email protected]e6811ed52010-08-17 03:45:3775
76 for (int i = 1; i < 3; i++) {
77 src[kSrcLen - i] = 0;
78 std::string out;
Peter Kasting638bd692023-08-15 23:56:5879 EXPECT_EQ(src, StringPrintf("%s", src));
[email protected]e6811ed52010-08-17 03:45:3780 }
81}
82
83// Test very large sprintfs that will cause the buffer to grow.
84TEST(StringPrintfTest, Grow) {
85 char src[1026];
Peter Kasting134ef9af2024-12-28 02:30:0986 for (auto& i : src) {
jdoerrie6c6229352018-10-22 15:55:4387 i = 'A';
Peter Kasting134ef9af2024-12-28 02:30:0988 }
[email protected]e6811ed52010-08-17 03:45:3789 src[1025] = 0;
90
thestig073d514d2014-10-21 03:11:2191 const char fmt[] = "%sB%sB%sB%sB%sB%sB%s";
[email protected]e6811ed52010-08-17 03:45:3792
[email protected]e6811ed52010-08-17 03:45:3793 const int kRefSize = 320000;
94 char* ref = new char[kRefSize];
Xiaohan Wang6700dcf12022-01-15 14:47:0095#if BUILDFLAG(IS_WIN)
[email protected]e6811ed52010-08-17 03:45:3796 sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src);
Xiaohan Wang6700dcf12022-01-15 14:47:0097#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]e6811ed52010-08-17 03:45:3798 snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src);
99#endif
100
Peter Kasting638bd692023-08-15 23:56:58101 EXPECT_EQ(ref, StringPrintf(fmt, src, src, src, src, src, src, src));
[email protected]e6811ed52010-08-17 03:45:37102 delete[] ref;
103}
104
105TEST(StringPrintfTest, StringAppendV) {
106 std::string out;
107 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
108 EXPECT_EQ("1 foo bar", out);
109}
110
111// Test the boundary condition for the size of the string_util's
112// internal buffer.
113TEST(StringPrintfTest, GrowBoundary) {
thestig073d514d2014-10-21 03:11:21114 const int kStringUtilBufLen = 1024;
[email protected]e6811ed52010-08-17 03:45:37115 // Our buffer should be one larger than the size of StringAppendVT's stack
116 // buffer.
thestig073d514d2014-10-21 03:11:21117 // And need extra one for NULL-terminator.
118 const int kBufLen = kStringUtilBufLen + 1 + 1;
119 char src[kBufLen];
Peter Kasting134ef9af2024-12-28 02:30:09120 for (int i = 0; i < kBufLen - 1; ++i) {
[email protected]e6811ed52010-08-17 03:45:37121 src[i] = 'a';
Peter Kasting134ef9af2024-12-28 02:30:09122 }
thestig073d514d2014-10-21 03:11:21123 src[kBufLen - 1] = 0;
[email protected]e6811ed52010-08-17 03:45:37124
Peter Kasting638bd692023-08-15 23:56:58125 EXPECT_EQ(src, StringPrintf("%s", src));
[email protected]e6811ed52010-08-17 03:45:37126}
127
[email protected]34d58a22013-05-02 23:06:26128// Test that StringPrintf and StringAppendV do not change errno.
129TEST(StringPrintfTest, StringPrintfErrno) {
130 errno = 1;
131 EXPECT_EQ("", StringPrintf("%s", ""));
132 EXPECT_EQ(1, errno);
133 std::string out;
134 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
135 EXPECT_EQ(1, errno);
136}
137
[email protected]e6811ed52010-08-17 03:45:37138} // namespace base