Add unit tests for untested SkString APIs.

Unit tests for SkString::appendVAList would have preemptively caught the
associated fuzzer failure.

Change-Id: I19a414e5e937f9e3fbe0f75e062b4befa6e2877b
Bug: chromium:1092743
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/297473
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
John Stiles 2020-06-18 15:49:38 -04:00 committed by Skia Commit-Bot
parent 889563cbeb
commit 75c9c5bd26

View File

@ -162,33 +162,35 @@ DEF_TEST(String, reporter) {
}
REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
}
static void assert_2000_spaces(skiatest::Reporter* reporter, const SkString& str) {
REPORTER_ASSERT(reporter, str.size() == 2000);
for (size_t i = 0; i < str.size(); ++i) {
REPORTER_ASSERT(reporter, str[i] == ' ');
}
}
DEF_TEST(String_overflow, reporter) {
// 2000 is larger than the static buffer size inside SkString.cpp
a = SkStringPrintf("%2000s", " ");
REPORTER_ASSERT(reporter, a.size() == 2000);
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != ' ') {
ERRORF(reporter, "SkStringPrintf fail: a[%zu] = '%c'", i, a[i]);
break;
}
}
a.reset();
SkString a = SkStringPrintf("%2000s", " ");
assert_2000_spaces(reporter, a);
a = "X";
a.printf("%2000s", " ");
REPORTER_ASSERT(reporter, a.size() == 2000);
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != ' ') {
ERRORF(reporter, "SkString::printf fail: a[%zu] = '%c'", i, a[i]);
break;
}
}
a.appendf("%2000s", " ");
REPORTER_ASSERT(reporter, a.size() == 4000);
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != ' ') {
ERRORF(reporter, "SkString::appendf fail: a[%zu] = '%c'", i, a[i]);
break;
}
}
assert_2000_spaces(reporter, a);
a = "X";
a.appendf("%1999s", " ");
REPORTER_ASSERT(reporter, a[0] == 'X');
a[0] = ' ';
assert_2000_spaces(reporter, a);
a = "X";
a.prependf("%1999s", " ");
REPORTER_ASSERT(reporter, a[1999] == 'X');
a[1999] = ' ';
assert_2000_spaces(reporter, a);
}
DEF_TEST(String_SkStrSplit, r) {
@ -320,3 +322,84 @@ DEF_TEST(String_fromUTF16, r) {
REPORTER_ASSERT(r, SkStringFromUTF16(test3, SK_ARRAY_COUNT(test3)).equals("αβγδε ζηθικ"));
}
static void test_va_list_print(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("123");
str.printVAList(format, args);
REPORTER_ASSERT(r, str.equals("hello world"));
va_end(args);
}
static void test_va_list_append(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("123");
str.appendVAList(format, args);
REPORTER_ASSERT(r, str.equals("123hello world"));
va_end(args);
}
static void test_va_list_prepend(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("123");
str.prependVAList(format, args);
REPORTER_ASSERT(r, str.equals("hello world123"));
va_end(args);
}
DEF_TEST(String_VAList, r) {
test_va_list_print(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd');
test_va_list_append(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd');
test_va_list_prepend(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd');
}
static void test_va_list_overflow_print(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("X");
str.printVAList(format, args);
assert_2000_spaces(r, str);
va_end(args);
}
static void test_va_list_overflow_append(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("X");
str.appendVAList(format, args);
REPORTER_ASSERT(r, str[0] == 'X');
str[0] = ' ';
assert_2000_spaces(r, str);
va_end(args);
}
static void test_va_list_overflow_prepend(skiatest::Reporter* r, const char format[], ...) {
va_list args;
va_start(args, format);
SkString str("X");
str.prependVAList(format, args);
REPORTER_ASSERT(r, str[1999] == 'X');
str[1999] = ' ';
assert_2000_spaces(r, str);
va_end(args);
}
DEF_TEST(String_VAList_overflow, r) {
test_va_list_overflow_print(r, "%2000s", " ");
test_va_list_overflow_append(r, "%1999s", " ");
test_va_list_overflow_prepend(r, "%1999s", " ");
}