0ef88538c6
Summary: * Consolidate use of std::regex for testing to testharness.cc, to minimize Facebook linters constantly flagging uses in non-production code. * Improve syntax and error messages for asserting some string matches a regex in tests. * Add a public Regex wrapper class to encapsulate existing usage in ObjectRegistry. * Remove unnecessary include <regex> * Put warnings that use of Regex in production code could cause bad performance or stack overflow. Intended follow-up work: * Replace std::regex with another underlying implementation like RE2 * Improve ObjectRegistry interface in terms of possibly confusing literal string matching vs. regex and in terms of reporting invalid regex. Pull Request resolved: https://github.com/facebook/rocksdb/pull/8740 Test Plan: tests updated, basic unit test for public Regex, and some manual testing of temporary changes to see example error messages: utilities/backupable/backupable_db_test.cc:917: Failure 000010_1162373755_138626.blob (child.name) does not match regex [0-9]+_[0-9]+_[0-9]+[.]blobHAHAHA (pattern) db/db_basic_test.cc:74: Failure R3SHSBA8C4U0CIMV2ZB0 (sid3) does not match regex [0-9A-Z]{20}HAHAHA Reviewed By: mrambacher Differential Revision: D30706246 Pulled By: pdillinger fbshipit-source-id: ba845e8f563ccad39bdb58f44f04e9da8f78c3fd
216 lines
5.6 KiB
C++
216 lines
5.6 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "port/port.h"
|
|
#include "port/stack_trace.h"
|
|
#include "rocksdb/data_structure.h"
|
|
#include "rocksdb/types.h"
|
|
#include "rocksdb/utilities/regex.h"
|
|
#include "test_util/testharness.h"
|
|
#include "test_util/testutil.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
// Use this to keep track of the cleanups that were actually performed
|
|
void Multiplier(void* arg1, void* arg2) {
|
|
int* res = reinterpret_cast<int*>(arg1);
|
|
int* num = reinterpret_cast<int*>(arg2);
|
|
*res *= *num;
|
|
}
|
|
|
|
class PinnableSliceTest : public testing::Test {
|
|
public:
|
|
void AssertSameData(const std::string& expected,
|
|
const PinnableSlice& slice) {
|
|
std::string got;
|
|
got.assign(slice.data(), slice.size());
|
|
ASSERT_EQ(expected, got);
|
|
}
|
|
};
|
|
|
|
// Test that the external buffer is moved instead of being copied.
|
|
TEST_F(PinnableSliceTest, MoveExternalBuffer) {
|
|
Slice s("123");
|
|
std::string buf;
|
|
PinnableSlice v1(&buf);
|
|
v1.PinSelf(s);
|
|
|
|
PinnableSlice v2(std::move(v1));
|
|
ASSERT_EQ(buf.data(), v2.data());
|
|
ASSERT_EQ(&buf, v2.GetSelf());
|
|
|
|
PinnableSlice v3;
|
|
v3 = std::move(v2);
|
|
ASSERT_EQ(buf.data(), v3.data());
|
|
ASSERT_EQ(&buf, v3.GetSelf());
|
|
}
|
|
|
|
TEST_F(PinnableSliceTest, Move) {
|
|
int n2 = 2;
|
|
int res = 1;
|
|
const std::string const_str1 = "123";
|
|
const std::string const_str2 = "ABC";
|
|
Slice slice1(const_str1);
|
|
Slice slice2(const_str2);
|
|
|
|
{
|
|
// Test move constructor on a pinned slice.
|
|
res = 1;
|
|
PinnableSlice v1;
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
PinnableSlice v2(std::move(v1));
|
|
|
|
// Since v1's Cleanable has been moved to v2,
|
|
// no cleanup should happen in Reset.
|
|
v1.Reset();
|
|
ASSERT_EQ(1, res);
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
// v2 is cleaned up.
|
|
ASSERT_EQ(2, res);
|
|
|
|
{
|
|
// Test move constructor on an unpinned slice.
|
|
PinnableSlice v1;
|
|
v1.PinSelf(slice1);
|
|
PinnableSlice v2(std::move(v1));
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
|
|
{
|
|
// Test move assignment from a pinned slice to
|
|
// another pinned slice.
|
|
res = 1;
|
|
PinnableSlice v1;
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
PinnableSlice v2;
|
|
v2.PinSlice(slice2, Multiplier, &res, &n2);
|
|
v2 = std::move(v1);
|
|
|
|
// v2's Cleanable will be Reset before moving
|
|
// anything from v1.
|
|
ASSERT_EQ(2, res);
|
|
// Since v1's Cleanable has been moved to v2,
|
|
// no cleanup should happen in Reset.
|
|
v1.Reset();
|
|
ASSERT_EQ(2, res);
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
// The Cleanable moved from v1 to v2 will be Reset.
|
|
ASSERT_EQ(4, res);
|
|
|
|
{
|
|
// Test move assignment from a pinned slice to
|
|
// an unpinned slice.
|
|
res = 1;
|
|
PinnableSlice v1;
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
PinnableSlice v2;
|
|
v2.PinSelf(slice2);
|
|
v2 = std::move(v1);
|
|
|
|
// Since v1's Cleanable has been moved to v2,
|
|
// no cleanup should happen in Reset.
|
|
v1.Reset();
|
|
ASSERT_EQ(1, res);
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
// The Cleanable moved from v1 to v2 will be Reset.
|
|
ASSERT_EQ(2, res);
|
|
|
|
{
|
|
// Test move assignment from an upinned slice to
|
|
// another unpinned slice.
|
|
PinnableSlice v1;
|
|
v1.PinSelf(slice1);
|
|
PinnableSlice v2;
|
|
v2.PinSelf(slice2);
|
|
v2 = std::move(v1);
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
|
|
{
|
|
// Test move assignment from an upinned slice to
|
|
// a pinned slice.
|
|
res = 1;
|
|
PinnableSlice v1;
|
|
v1.PinSelf(slice1);
|
|
PinnableSlice v2;
|
|
v2.PinSlice(slice2, Multiplier, &res, &n2);
|
|
v2 = std::move(v1);
|
|
|
|
// v2's Cleanable will be Reset before moving
|
|
// anything from v1.
|
|
ASSERT_EQ(2, res);
|
|
|
|
AssertSameData(const_str1, v2);
|
|
}
|
|
// No Cleanable is moved from v1 to v2, so no more cleanup.
|
|
ASSERT_EQ(2, res);
|
|
}
|
|
|
|
// ***************************************************************** //
|
|
// Unit test for SmallEnumSet
|
|
class SmallEnumSetTest : public testing::Test {
|
|
public:
|
|
SmallEnumSetTest() {}
|
|
~SmallEnumSetTest() {}
|
|
};
|
|
|
|
TEST_F(SmallEnumSetTest, SmallSetTest) {
|
|
FileTypeSet fs;
|
|
ASSERT_TRUE(fs.Add(FileType::kIdentityFile));
|
|
ASSERT_FALSE(fs.Add(FileType::kIdentityFile));
|
|
ASSERT_TRUE(fs.Add(FileType::kInfoLogFile));
|
|
ASSERT_TRUE(fs.Contains(FileType::kIdentityFile));
|
|
ASSERT_FALSE(fs.Contains(FileType::kDBLockFile));
|
|
}
|
|
|
|
// ***************************************************************** //
|
|
// Unit test for Regex
|
|
#ifndef ROCKSDB_LITE
|
|
TEST(RegexTest, ParseEtc) {
|
|
Regex r;
|
|
ASSERT_OK(Regex::Parse("[abc]{5}", &r));
|
|
ASSERT_TRUE(r.Matches("abcba"));
|
|
ASSERT_FALSE(r.Matches("abcb")); // too short
|
|
ASSERT_FALSE(r.Matches("abcbaa")); // too long
|
|
|
|
ASSERT_OK(Regex::Parse(".*foo.*", &r));
|
|
ASSERT_TRUE(r.Matches("123forfoodie456"));
|
|
ASSERT_FALSE(r.Matches("123forfodie456"));
|
|
// Ensure copy operator
|
|
Regex r2;
|
|
r2 = r;
|
|
ASSERT_TRUE(r2.Matches("123forfoodie456"));
|
|
ASSERT_FALSE(r2.Matches("123forfodie456"));
|
|
// Ensure copy constructor
|
|
Regex r3{r};
|
|
ASSERT_TRUE(r3.Matches("123forfoodie456"));
|
|
ASSERT_FALSE(r3.Matches("123forfodie456"));
|
|
|
|
ASSERT_TRUE(Regex::Parse("*foo.*", &r).IsInvalidArgument());
|
|
ASSERT_TRUE(Regex::Parse("[abc", &r).IsInvalidArgument());
|
|
ASSERT_TRUE(Regex::Parse("[abc]{1", &r).IsInvalidArgument());
|
|
}
|
|
#endif // ROCKSDB_LITE
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
int main(int argc, char** argv) {
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|