| 1 | /* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <cstdio> /* std::remove */ |
| 25 | #include <fstream> |
| 26 | #include <string> |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include "components/keyrings/common/data_file/reader.h" |
| 31 | #include "components/keyrings/common/data_file/writer.h" |
| 32 | |
| 33 | namespace file_unittest { |
| 34 | |
| 35 | class File_test : public ::testing::Test {}; |
| 36 | |
| 37 | using keyring_common::data_file::File_reader; |
| 38 | using keyring_common::data_file::File_writer; |
| 39 | |
| 40 | TEST_F(File_test, FileWriteReadTest) { |
| 41 | std::string data("Quick Brown Fox jumped over the lazy dog."); |
| 42 | File_writer file_writer("file_writer_test", data); |
| 43 | ASSERT_TRUE(file_writer.valid()); |
| 44 | std::string read_data; |
| 45 | File_reader file_reader("file_writer_test", false, read_data); |
| 46 | ASSERT_TRUE(file_reader.valid()); |
| 47 | ASSERT_TRUE(data == read_data); |
| 48 | ASSERT_TRUE(std::remove("file_writer_test") == 0); |
| 49 | } |
| 50 | |
| 51 | TEST_F(File_test, FileBackupReadTest) { |
| 52 | std::string data("Quick Brown Fox jumped over the lazy dog."); |
| 53 | std::ofstream writer("file_reader_test.backup"); |
| 54 | writer.write(data.c_str(), data.length()); |
| 55 | writer.close(); |
| 56 | |
| 57 | std::string read_data; |
| 58 | File_reader file_reader("file_reader_test", false, read_data); |
| 59 | ASSERT_TRUE(file_reader.valid()); |
| 60 | ASSERT_TRUE(data == read_data); |
| 61 | ASSERT_TRUE(std::remove("file_reader_test") == 0); |
| 62 | } |
| 63 | |
| 64 | } // namespace file_unittest |