Debian Codegraph @

unittest/gunit/keyring/file_io-t.cc

raw symbols SCIP
1 /* Copyright (c) 2016, 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 <fcntl.h>
25 #include <gtest/gtest.h>
26
27 #include "my_inttypes.h"
28 #include "my_io.h"
29 #include "plugin/keyring/file_io.h"
30 #include "sql/auth/auth_acls.h"
31 #include "sql/current_thd.h"
32 #include "sql/sql_class.h"
33 #include "unittest/gunit/keyring/mock_logger.h"
34 #include "unittest/gunit/test_utils.h"
35
36 #if defined(HAVE_PSI_INTERFACE)
37 namespace keyring {
38 extern PSI_file_key keyring_file_data_key;
39 extern PSI_file_key keyring_backup_file_data_key;
40 } // namespace keyring
41 #endif
42
43 namespace keyring__file_io_unittest {
44 using keyring::Mock_logger;
45 using my_testing::Server_initializer;
46 using ::testing::StartsWith;
47 using ::testing::StrEq;
48
49 class File_io_test : public ::testing::Test {
50 protected:
51 void SetUp() override {
52 keyring::keyring_file_data_key = PSI_NOT_INSTRUMENTED;
53 keyring::keyring_backup_file_data_key = PSI_NOT_INSTRUMENTED;
54 logger = new Mock_logger();
55 initializer.SetUp();
56
57 // Set user as super
58 Security_context *sec_ctx = current_thd->security_context();
59 sec_ctx->set_master_access(sec_ctx->master_access() | SUPER_ACL);
60 }
61
62 void TearDown() override {
63 delete logger;
64 initializer.TearDown();
65 }
66
67 protected:
68 Server_initializer initializer; // needed to initialize current_thd
69 Mock_logger *logger;
70 };
71
72 // Those tests are to check if methods inside File_io do not call my_error but
73 // instead generate warnings for filesystem operations errors. If one of those
74 // functions would call my_error then error would be set and we would have to
75 // call initializer.set_expected_error. As we do not call this in any functions
76 // this proves that my_error is not called. Also these tests make sure that
77 // correct messages are generated
78
79 TEST_F(File_io_test, OpenNotExistingFile) {
80 keyring::File_io file_io(logger);
81 remove("./some_funny_name"); // just to be sure some_funny_name does not
82 // exist
83
84 EXPECT_CALL(*logger, log(ERROR_LEVEL,
85 StrEq("File './some_funny_name' not found "
86 "(OS errno 2 - No such file or directory)")));
87 File file = file_io.open(keyring::keyring_file_data_key, "./some_funny_name",
88 O_RDONLY, MYF(MY_WME));
89 ASSERT_TRUE(file < 0); // could not open the file
90 }
91
92 TEST_F(File_io_test, CloseTwiceTheSame) {
93 keyring::File_io file_io(logger);
94
95 File file = file_io.open(keyring::keyring_file_data_key, "./some_funny_name",
96 O_RDONLY | O_CREAT, MYF(MY_WME));
97 ASSERT_TRUE(file >= 0); // successfully created
98 file_io.close(file, MYF(MY_WME));
99
100 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Error on close of")));
101 ASSERT_TRUE(file_io.close(file, MYF(MY_WME)) != 0);
102
103 remove("./some_funny_name");
104 }
105
106 TEST_F(File_io_test, ReadFromInvalidFileDescriptor) {
107 keyring::File_io file_io(logger);
108 File file = 2050;
109 uchar buff[2];
110
111 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Error reading file")));
112 ASSERT_TRUE(file_io.read(file, buff, 10, MYF(MY_WME)) != 10);
113 }
114
115 TEST_F(File_io_test, WriteToInvalidFileDescriptor) {
116 keyring::File_io file_io(logger);
117 File file = 2050;
118
119 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Error writing file")));
120 ASSERT_TRUE(file_io.write(file, reinterpret_cast<const uchar *>("123"), 10,
121 MYF(MY_WME)) != 10);
122 }
123
124 TEST_F(File_io_test, SeekOnInvalidFileDescriptor) {
125 keyring::File_io file_io(logger);
126 File file = 2050;
127
128 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Cannot seek in file")));
129 ASSERT_TRUE(file_io.seek(file, 0, MY_SEEK_END, MYF(MY_WME)) ==
130 MY_FILEPOS_ERROR);
131 }
132
133 TEST_F(File_io_test, TellOnInvalidFileDescriptor) {
134 keyring::File_io file_io(logger);
135 File file = 2050;
136
137 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Cannot seek in file")));
138 ASSERT_TRUE(file_io.tell(file, MYF(MY_WME)) == ((my_off_t)-1));
139 }
140
141 TEST_F(File_io_test, SyncOnInvalidFileDescriptor) {
142 keyring::File_io file_io(logger);
143 File file = 2050;
144
145 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Can't sync file")));
146 ASSERT_TRUE(file_io.sync(file, MYF(MY_WME)) != 0);
147 }
148
149 TEST_F(File_io_test, FStatOnInvalidFileDescriptor) {
150 keyring::File_io file_io(logger);
151 File file = 2050;
152
153 EXPECT_CALL(*logger,
154 log(ERROR_LEVEL, StartsWith("Error while reading stat for")));
155 MY_STAT keyring_file_stat;
156 ASSERT_TRUE(file_io.fstat(file, &keyring_file_stat, MYF(MY_WME)) != 0);
157 }
158
159 TEST_F(File_io_test, RemoveNotExistingFile) {
160 keyring::File_io file_io(logger);
161 remove("./some_funny_name"); // just to be sure some_funny_name does not
162 // exist
163
164 EXPECT_CALL(
165 *logger,
166 log(ERROR_LEVEL, StrEq("Could not remove file ./some_funny_name OS "
167 "retuned this error: No such file or directory")));
168 ASSERT_TRUE(file_io.remove("./some_funny_name", MYF(MY_WME)) != 0);
169 }
170
171 TEST_F(File_io_test, TruncateOnNotExistingFile) {
172 keyring::File_io file_io(logger);
173 File file = 2050;
174
175 EXPECT_CALL(*logger, log(ERROR_LEVEL, StartsWith("Could not truncate file")));
176 ASSERT_TRUE(file_io.truncate(file, MYF(MY_WME)) != 0);
177 }
178
179 } // namespace keyring__file_io_unittest