summaryrefslogtreecommitdiff
path: root/libutils/String16_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2021-06-10 17:06:26 -0700
committerElliott Hughes <enh@google.com>2021-06-10 17:06:26 -0700
commita6be6f0acb955fc1917496820521ec41cf1e5557 (patch)
treeeef8289300e11f3808c0d78d3d4f2ea7b14c6d1e /libutils/String16_test.cpp
parent8f654d8a99738d096e2a7bf87324a515ec0c33bc (diff)
Check for overflow in String16::append and String16::insert.
Bug: http://b/178802681 Bug: http://b/178821065 Test: new tests Change-Id: I2352ea4c65e3f29e44e2ad6cad20ad610ceace1f
Diffstat (limited to 'libutils/String16_test.cpp')
-rw-r--r--libutils/String16_test.cpp36
1 files changed, 34 insertions, 2 deletions
diff --git a/libutils/String16_test.cpp b/libutils/String16_test.cpp
index 54662ac41..c4783211f 100644
--- a/libutils/String16_test.cpp
+++ b/libutils/String16_test.cpp
@@ -19,7 +19,7 @@
#include <gtest/gtest.h>
-namespace android {
+using namespace android;
::testing::AssertionResult Char16_tStringEquals(const char16_t* a, const char16_t* b) {
if (strcmp16(a, b) != 0) {
@@ -197,4 +197,36 @@ TEST(String16Test, ValidUtf8Conversion) {
EXPECT_STR16EQ(another, u"abcdef");
}
-} // namespace android
+TEST(String16Test, append) {
+ String16 s;
+ EXPECT_EQ(OK, s.append(String16(u"foo")));
+ EXPECT_STR16EQ(u"foo", s);
+ EXPECT_EQ(OK, s.append(String16(u"bar")));
+ EXPECT_STR16EQ(u"foobar", s);
+ EXPECT_EQ(OK, s.append(u"baz", 0));
+ EXPECT_STR16EQ(u"foobar", s);
+ EXPECT_EQ(NO_MEMORY, s.append(u"baz", SIZE_MAX));
+ EXPECT_STR16EQ(u"foobar", s);
+}
+
+TEST(String16Test, insert) {
+ String16 s;
+
+ // Inserting into the empty string inserts at the start.
+ EXPECT_EQ(OK, s.insert(123, u"foo"));
+ EXPECT_STR16EQ(u"foo", s);
+
+ // Inserting zero characters at any position is okay, but won't expand the string.
+ EXPECT_EQ(OK, s.insert(123, u"foo", 0));
+ EXPECT_STR16EQ(u"foo", s);
+
+ // Inserting past the end of a non-empty string appends.
+ EXPECT_EQ(OK, s.insert(123, u"bar"));
+ EXPECT_STR16EQ(u"foobar", s);
+
+ EXPECT_EQ(OK, s.insert(3, u"!"));
+ EXPECT_STR16EQ(u"foo!bar", s);
+
+ EXPECT_EQ(NO_MEMORY, s.insert(3, u"", SIZE_MAX));
+ EXPECT_STR16EQ(u"foo!bar", s);
+}