/Users/deen/code/yugabyte-db/src/yb/rocksdb/tools/ldb_cmd_test.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | #ifndef ROCKSDB_LITE |
21 | | |
22 | | #include "yb/rocksdb/tools/ldb_cmd.h" |
23 | | #include "yb/rocksdb/util/testharness.h" |
24 | | #include "yb/rocksdb/env.h" |
25 | | #include "yb/rocksdb/db/db_test_util.h" |
26 | | |
27 | | #include "yb/util/test_macros.h" |
28 | | |
29 | | DECLARE_bool(TEST_exit_on_finish); |
30 | | |
31 | | const string kDbName = "/lbd_cmd_test"; |
32 | | const string kKeyFileOption = "--key_file=" + rocksdb::DBTestBase::kKeyId + ":" + |
33 | | rocksdb::DBTestBase::kKeyFile; |
34 | | |
35 | | |
36 | | class LdbCmdTest : public rocksdb::DBTestBase, public testing::WithParamInterface<bool> { |
37 | | public: |
38 | 8 | LdbCmdTest() : rocksdb::DBTestBase(kDbName, GetParam()) {} |
39 | | |
40 | 4 | yb::Status WriteRecordsAndFlush() { |
41 | 4 | RETURN_NOT_OK(Put("k1", "v1")); |
42 | 4 | RETURN_NOT_OK(Put("k2", "v2")); |
43 | 4 | return Flush(); |
44 | 4 | } |
45 | | |
46 | 4 | std::vector<std::string> CreateArgs(std::string command_name) { |
47 | 4 | std::vector<std::string> args; |
48 | 4 | args.push_back("./ldb"); |
49 | 4 | args.push_back("--db=" + dbname_); |
50 | 4 | args.push_back(command_name); |
51 | 4 | if (GetParam()) { |
52 | 2 | args.push_back(kKeyFileOption); |
53 | 2 | } |
54 | 4 | return args; |
55 | 4 | } |
56 | | |
57 | 4 | void RunLdb(const std::vector<std::string>& args) { |
58 | 4 | std::vector<char *> args_ptr; |
59 | 4 | args_ptr.resize(args.size()); |
60 | 16 | std::transform(args.begin(), args.end(), args_ptr.begin(), [](const std::string& str) { |
61 | 16 | return const_cast<char*>(str.c_str()); |
62 | 16 | }); |
63 | 4 | rocksdb::LDBTool tool; |
64 | 4 | ASSERT_NO_FATALS(tool.Run(static_cast<int>(args_ptr.size()), args_ptr.data())); |
65 | 4 | } |
66 | | }; |
67 | | |
68 | | INSTANTIATE_TEST_CASE_P(EncryptionEnabled, LdbCmdTest, ::testing::Bool()); |
69 | | |
70 | 2 | TEST_P(LdbCmdTest, HexToString) { |
71 | | // map input to expected outputs. |
72 | 2 | map<string, vector<int>> inputMap = { |
73 | 2 | {"0x7", {7}}, {"0x5050", {80, 80}}, {"0xFF", {-1}}, |
74 | 2 | {"0x1234", {18, 52}}, {"0xaa", {-86}}, {"0x123", {18, 3}}, |
75 | 2 | }; |
76 | | |
77 | 12 | for (const auto& inPair : inputMap) { |
78 | 12 | auto actual = rocksdb::LDBCommand::HexToString(inPair.first); |
79 | 12 | auto expected = inPair.second; |
80 | 30 | for (unsigned int i = 0; i < actual.length(); i++) { |
81 | 18 | ASSERT_EQ(expected[i], static_cast<int>(actual[i])); |
82 | 18 | } |
83 | 12 | } |
84 | 2 | } |
85 | | |
86 | 2 | TEST_P(LdbCmdTest, HexToStringBadInputs) { |
87 | 2 | const vector<string> badInputs = { |
88 | 2 | "0xZZ", "123", "0xx5", "0x11G", "Ox12", "0xT", "0x1Q1", |
89 | 2 | }; |
90 | 14 | for (const auto& badInput : badInputs) { |
91 | 14 | try { |
92 | 14 | rocksdb::LDBCommand::HexToString(badInput); |
93 | 14 | std::cerr << "Should fail on bad hex value: " << badInput << "\n"; |
94 | 14 | FAIL(); |
95 | 14 | } catch (...) { |
96 | 14 | } |
97 | 14 | } |
98 | 2 | } |
99 | | |
100 | 2 | TEST_P(LdbCmdTest, Scan) { |
101 | 2 | FLAGS_TEST_exit_on_finish = false; |
102 | 2 | ASSERT_OK(WriteRecordsAndFlush()); |
103 | 2 | auto args = CreateArgs("scan"); |
104 | 2 | args.push_back("--only_verify_checksums"); |
105 | 2 | ASSERT_NO_FATALS(RunLdb(args)); |
106 | 2 | } |
107 | | |
108 | 2 | TEST_P(LdbCmdTest, CheckConsistency) { |
109 | 2 | FLAGS_TEST_exit_on_finish = false; |
110 | 2 | ASSERT_OK(WriteRecordsAndFlush()); |
111 | 2 | auto args = CreateArgs("checkconsistency"); |
112 | 2 | ASSERT_NO_FATALS(RunLdb(args)); |
113 | 2 | } |
114 | | |
115 | 13.2k | int main(int argc, char** argv) { |
116 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
117 | 13.2k | return RUN_ALL_TESTS(); |
118 | 13.2k | } |
119 | | #else |
120 | | #include <stdio.h> |
121 | | |
122 | | int main(int argc, char** argv) { |
123 | | fprintf(stderr, "SKIPPED as LDBCommand is not supported in ROCKSDB_LITE\n"); |
124 | | return 0; |
125 | | } |
126 | | |
127 | | #endif // ROCKSDB_LITE |