summaryrefslogtreecommitdiff
path: root/src/mongo/util/str_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/util/str_test.cpp')
-rw-r--r--src/mongo/util/str_test.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/mongo/util/str_test.cpp b/src/mongo/util/str_test.cpp
index 22b7d81a0e2..74223b76540 100644
--- a/src/mongo/util/str_test.cpp
+++ b/src/mongo/util/str_test.cpp
@@ -325,4 +325,46 @@ TEST(StringUtilsTest, GetCodePointLength) {
}
}
+TEST(StringUtilsTest, UassertNoEmbeddedNulBytes) {
+ // These shouldn't throw.
+ uassertNoEmbeddedNulBytes({nullptr, 0});
+ uassertNoEmbeddedNulBytes(""_sd);
+ uassertNoEmbeddedNulBytes("hello"_sd);
+ uassertNoEmbeddedNulBytes("hello\0"_sd.substr(0, 5));
+
+ // These should throw.
+ ASSERT_THROWS_CODE(uassertNoEmbeddedNulBytes("\0"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(uassertNoEmbeddedNulBytes("\0hello"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(uassertNoEmbeddedNulBytes("hello\0"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(uassertNoEmbeddedNulBytes("hello\0world"_sd), DBException, 9527900);
+}
+
+TEST(StringUtilsTest, CopyAsCString) {
+ char dest[100]; // big enough for anything we would reasonably add here.
+
+ // Print address not contents on failures.
+ auto ptr = [](const char* p) { return static_cast<const void*>(p); };
+ auto testValid = [&](StringData noNul, int line) {
+ // Make sure we write a nul byte. Without this, the test could pass if dest happened to have
+ // uninitialized zero bytes.
+ std::fill_n(dest, sizeof(dest), 0xff);
+
+ ASSERT_EQ(ptr(copyAsCString(dest, noNul)), ptr(dest + noNul.size() + 1)) << "line:" << line;
+ ASSERT_EQ(dest[noNul.size()], '\0') << "line:" << line;
+ ASSERT_EQ(StringData(dest, noNul.size()), noNul) << "line:" << line;
+ };
+
+ // These shouldn't throw.
+ testValid({nullptr, 0}, __LINE__);
+ testValid(""_sd, __LINE__);
+ testValid("hello"_sd, __LINE__);
+ testValid("hello world"_sd.substr(0, 5), __LINE__);
+
+ // These should throw.
+ ASSERT_THROWS_CODE(copyAsCString(dest, "\0"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(copyAsCString(dest, "\0hello"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(copyAsCString(dest, "hello\0"_sd), DBException, 9527900);
+ ASSERT_THROWS_CODE(copyAsCString(dest, "hello\0world"_sd), DBException, 9527900);
+}
+
} // namespace mongo::str