blob: 7fe0a4deda7cd355d90d825ecb0ae7d496eafd09 [file] [log] [blame]
Shawn McCarneyb6f07c92020-09-03 21:49:21 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "temporary_file.hpp"
Shawn McCarneyf3633f62020-09-02 10:34:52 -050017#include "test_utils.hpp"
Shawn McCarneyb6f07c92020-09-03 21:49:21 -050018
19#include <filesystem>
Shawn McCarneyb6f07c92020-09-03 21:49:21 -050020#include <string>
21#include <utility>
22
23#include <gtest/gtest.h>
24
25using namespace phosphor::power::regulators;
Shawn McCarneyf3633f62020-09-02 10:34:52 -050026using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyb6f07c92020-09-03 21:49:21 -050027namespace fs = std::filesystem;
28
Shawn McCarneyb6f07c92020-09-03 21:49:21 -050029TEST(TemporaryFileTests, DefaultConstructor)
30{
31 TemporaryFile file{};
32
33 fs::path path = file.getPath();
34 EXPECT_FALSE(path.empty());
35 EXPECT_TRUE(fs::exists(path));
36 EXPECT_TRUE(fs::is_regular_file(path));
37
38 fs::path parentDir = path.parent_path();
39 EXPECT_EQ(parentDir, "/tmp");
40
41 std::string fileName = path.filename();
42 std::string namePrefix = "phosphor-regulators-";
43 EXPECT_EQ(fileName.compare(0, namePrefix.size(), namePrefix), 0);
44}
45
46TEST(TemporaryFileTests, MoveConstructor)
47{
48 // Create first TemporaryFile object and verify temporary file exists
49 TemporaryFile file1{};
50 EXPECT_FALSE(file1.getPath().empty());
51 EXPECT_TRUE(fs::exists(file1.getPath()));
52
53 // Save path to temporary file
54 fs::path path = file1.getPath();
55
56 // Create second TemporaryFile object by moving first object
57 TemporaryFile file2{std::move(file1)};
58
59 // Verify first object now has an empty path
60 EXPECT_TRUE(file1.getPath().empty());
61
62 // Verify second object now owns same temporary file and file exists
63 EXPECT_EQ(file2.getPath(), path);
64 EXPECT_TRUE(fs::exists(file2.getPath()));
65}
66
67TEST(TemporaryFileTests, MoveAssignmentOperator)
68{
69 // Test where works: TemporaryFile object is moved
70 {
71 // Create first TemporaryFile object and verify temporary file exists
72 TemporaryFile file1{};
73 EXPECT_FALSE(file1.getPath().empty());
74 EXPECT_TRUE(fs::exists(file1.getPath()));
75
76 // Save path to first temporary file
77 fs::path path1 = file1.getPath();
78
79 // Create second TemporaryFile object and verify temporary file exists
80 TemporaryFile file2{};
81 EXPECT_FALSE(file2.getPath().empty());
82 EXPECT_TRUE(fs::exists(file2.getPath()));
83
84 // Save path to second temporary file
85 fs::path path2 = file2.getPath();
86
87 // Verify temporary files are different
88 EXPECT_NE(path1, path2);
89
90 // Move first object into the second
91 file2 = std::move(file1);
92
93 // Verify first object now has an empty path
94 EXPECT_TRUE(file1.getPath().empty());
95
96 // Verify second object now owns first temporary file and file exists
97 EXPECT_EQ(file2.getPath(), path1);
98 EXPECT_TRUE(fs::exists(path1));
99
100 // Verify second temporary file was deleted
101 EXPECT_FALSE(fs::exists(path2));
102 }
103
104 // Test where does nothing: TemporaryFile object is moved into itself
105 {
106 // Create TemporaryFile object and verify temporary file exists
107 TemporaryFile file{};
108 EXPECT_FALSE(file.getPath().empty());
109 EXPECT_TRUE(fs::exists(file.getPath()));
110
111 // Save path to temporary file
112 fs::path path = file.getPath();
113
114 // Try to move object into itself; should do nothing
115 file = std::move(file);
116
117 // Verify object still owns same temporary file and file exists
118 EXPECT_EQ(file.getPath(), path);
119 EXPECT_TRUE(fs::exists(path));
120 }
121
122 // Test where fails: Cannot delete temporary file
123 {
124 // Create first TemporaryFile object and verify temporary file exists
125 TemporaryFile file1{};
126 EXPECT_FALSE(file1.getPath().empty());
127 EXPECT_TRUE(fs::exists(file1.getPath()));
128
129 // Save path to first temporary file
130 fs::path path1 = file1.getPath();
131
132 // Create second TemporaryFile object and verify temporary file exists
133 TemporaryFile file2{};
134 EXPECT_FALSE(file2.getPath().empty());
135 EXPECT_TRUE(fs::exists(file2.getPath()));
136
137 // Save path to second temporary file
138 fs::path path2 = file2.getPath();
139
140 // Verify temporary files are different
141 EXPECT_NE(path1, path2);
142
143 // Make second temporary file unremoveable
144 makeFileUnRemovable(path2);
145
146 try
147 {
148 // Try to move first object into the second; should throw exception
149 file2 = std::move(file1);
150 ADD_FAILURE() << "Should not have reached this line.";
151 }
152 catch (const std::exception& e)
153 {
154 // This is expected. Exception message will vary.
155 }
156
157 // Verify first object has not changed and first temporary file exists
158 EXPECT_EQ(file1.getPath(), path1);
159 EXPECT_TRUE(fs::exists(path1));
160
161 // Verify second object has not changed and second temporary file exists
162 EXPECT_EQ(file2.getPath(), path2);
163 EXPECT_TRUE(fs::exists(path2));
164
165 // Make second temporary file removeable so destructor can delete it
166 makeFileRemovable(path2);
167 }
168}
169
170TEST(TemporaryFileTests, Destructor)
171{
172 // Test where works: Temporary file is deleted
173 {
174 fs::path path{};
175 {
176 TemporaryFile file{};
177 path = file.getPath();
178 EXPECT_TRUE(fs::exists(path));
179 }
180 EXPECT_FALSE(fs::exists(path));
181 }
182
183 // Test where works: Temporary file was already deleted
184 {
185 fs::path path{};
186 {
187 TemporaryFile file{};
188 path = file.getPath();
189 EXPECT_TRUE(fs::exists(path));
190 file.remove();
191 EXPECT_FALSE(fs::exists(path));
192 }
193 EXPECT_FALSE(fs::exists(path));
194 }
195
196 // Test where fails: Cannot delete temporary file: No exception thrown
197 {
198 fs::path path{};
199 try
200 {
201 TemporaryFile file{};
202 path = file.getPath();
203 EXPECT_TRUE(fs::exists(path));
204 makeFileUnRemovable(path);
205 }
206 catch (...)
207 {
208 ADD_FAILURE() << "Should not have caught exception.";
209 }
210
211 // Temporary file should still exist
212 EXPECT_TRUE(fs::exists(path));
213
214 // Make file removable and delete it
215 makeFileRemovable(path);
216 fs::remove(path);
217 }
218}
219
220TEST(TemporaryFileTests, Remove)
221{
222 // Test where works
223 {
224 // Create TemporaryFile object and verify temporary file exists
225 TemporaryFile file{};
226 EXPECT_FALSE(file.getPath().empty());
227 EXPECT_TRUE(fs::exists(file.getPath()));
228
229 // Save path to temporary file
230 fs::path path = file.getPath();
231
232 // Delete temporary file
233 file.remove();
234
235 // Verify path is cleared and file does not exist
236 EXPECT_TRUE(file.getPath().empty());
237 EXPECT_FALSE(fs::exists(path));
238
239 // Delete temporary file again; should do nothing
240 file.remove();
241 EXPECT_TRUE(file.getPath().empty());
242 EXPECT_FALSE(fs::exists(path));
243 }
244
245 // Test where fails
246 {
247 // Create TemporaryFile object and verify temporary file exists
248 TemporaryFile file{};
249 EXPECT_FALSE(file.getPath().empty());
250 EXPECT_TRUE(fs::exists(file.getPath()));
251
252 // Make file unremovable
253 makeFileUnRemovable(file.getPath());
254
255 try
256 {
257 // Try to delete temporary file; should fail with exception
258 file.remove();
259 ADD_FAILURE() << "Should not have reached this line.";
260 }
261 catch (const std::exception& e)
262 {
263 // This is expected. Exception message will vary.
264 }
265
266 // Make file removable again so it will be deleted by the destructor
267 makeFileRemovable(file.getPath());
268 }
269}
270
271TEST(TemporaryFileTests, GetPath)
272{
273 TemporaryFile file{};
274 EXPECT_FALSE(file.getPath().empty());
275 EXPECT_EQ(file.getPath().parent_path(), "/tmp");
276 EXPECT_TRUE(fs::exists(file.getPath()));
277}