/Users/deen/code/yugabyte-db/src/yb/encryption/encrypted_env-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <string> |
15 | | |
16 | | #include <glog/logging.h> |
17 | | #include <gtest/gtest.h> |
18 | | |
19 | | #include "yb/encryption/encrypted_file_factory.h" |
20 | | #include "yb/encryption/encryption_test_util.h" |
21 | | #include "yb/encryption/encryption_util.h" |
22 | | #include "yb/encryption/header_manager_mock_impl.h" |
23 | | |
24 | | #include "yb/gutil/casts.h" |
25 | | |
26 | | #include "yb/util/random_util.h" |
27 | | #include "yb/util/status.h" |
28 | | #include "yb/util/test_macros.h" |
29 | | #include "yb/util/test_util.h" |
30 | | |
31 | | namespace yb { |
32 | | namespace encryption { |
33 | | |
34 | | constexpr uint32_t kDataSize = 1000; |
35 | | |
36 | | class TestEncryptedEnv : public YBTest {}; |
37 | | |
38 | 1 | TEST_F(TestEncryptedEnv, FileOps) { |
39 | 1 | auto header_manager = GetMockHeaderManager(); |
40 | 1 | HeaderManager* hm_ptr = header_manager.get(); |
41 | | |
42 | 1 | auto env = NewEncryptedEnv(std::move(header_manager)); |
43 | 1 | auto fname_template = "test-fileXXXXXX"; |
44 | 1 | auto bytes = RandomBytes(kDataSize); |
45 | 1 | Slice data(bytes.data(), kDataSize); |
46 | | |
47 | 2 | for (bool encrypted : {false, true}) { |
48 | 2 | down_cast<HeaderManagerMockImpl*>(hm_ptr)->SetFileEncryption(encrypted); |
49 | | |
50 | 2 | string fname; |
51 | 2 | std::unique_ptr<WritableFile> writable_file; |
52 | 2 | ASSERT_OK(env->NewTempWritableFile( |
53 | 2 | WritableFileOptions(), fname_template, &fname, &writable_file)); |
54 | 2 | TestWrites(writable_file.get(), data); |
55 | | |
56 | 2 | std::unique_ptr<yb::RandomAccessFile> ra_file; |
57 | 2 | ASSERT_OK(env->NewRandomAccessFile(fname, &ra_file)); |
58 | 2 | TestRandomAccessReads<uint8_t>(ra_file.get(), data); |
59 | | |
60 | 2 | ASSERT_OK(env->DeleteFile(fname)); |
61 | 2 | } |
62 | 1 | } |
63 | | |
64 | | } // namespace encryption |
65 | | } // namespace yb |