1bf54cbb1SJayanth Othayoth /**
2bf54cbb1SJayanth Othayoth * Copyright © 2021 IBM Corporation
3bf54cbb1SJayanth Othayoth *
4bf54cbb1SJayanth Othayoth * Licensed under the Apache License, Version 2.0 (the "License");
5bf54cbb1SJayanth Othayoth * you may not use this file except in compliance with the License.
6bf54cbb1SJayanth Othayoth * You may obtain a copy of the License at
7bf54cbb1SJayanth Othayoth *
8bf54cbb1SJayanth Othayoth * http://www.apache.org/licenses/LICENSE-2.0
9bf54cbb1SJayanth Othayoth *
10bf54cbb1SJayanth Othayoth * Unless required by applicable law or agreed to in writing, software
11bf54cbb1SJayanth Othayoth * distributed under the License is distributed on an "AS IS" BASIS,
12bf54cbb1SJayanth Othayoth * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf54cbb1SJayanth Othayoth * See the License for the specific language governing permissions and
14bf54cbb1SJayanth Othayoth * limitations under the License.
15bf54cbb1SJayanth Othayoth */
16bf54cbb1SJayanth Othayoth #include "extensions/openpower-pels/temporary_file.hpp"
17bf54cbb1SJayanth Othayoth
18bf54cbb1SJayanth Othayoth #include <filesystem>
19bf54cbb1SJayanth Othayoth #include <fstream>
20bf54cbb1SJayanth Othayoth #include <string>
21bf54cbb1SJayanth Othayoth #include <utility>
22bf54cbb1SJayanth Othayoth
23bf54cbb1SJayanth Othayoth #include <gtest/gtest.h>
24bf54cbb1SJayanth Othayoth
25bf54cbb1SJayanth Othayoth using namespace openpower::pels::util;
26bf54cbb1SJayanth Othayoth namespace fs = std::filesystem;
27bf54cbb1SJayanth Othayoth
28bf54cbb1SJayanth Othayoth /**
29bf54cbb1SJayanth Othayoth * Modify the specified file so that fs::remove() can successfully delete it.
30bf54cbb1SJayanth Othayoth *
31bf54cbb1SJayanth Othayoth * Undo the modifications from an earlier call to makeFileUnRemovable().
32bf54cbb1SJayanth Othayoth *
33bf54cbb1SJayanth Othayoth * @param path path to the file
34bf54cbb1SJayanth Othayoth */
makeFileRemovable(const fs::path & path)35bf54cbb1SJayanth Othayoth inline void makeFileRemovable(const fs::path& path)
36bf54cbb1SJayanth Othayoth {
37bf54cbb1SJayanth Othayoth // makeFileUnRemovable() creates a directory at the file path. Remove the
38bf54cbb1SJayanth Othayoth // directory and all of its contents.
39bf54cbb1SJayanth Othayoth fs::remove_all(path);
40bf54cbb1SJayanth Othayoth
41bf54cbb1SJayanth Othayoth // Rename the file back to the original path to restore its contents
42bf54cbb1SJayanth Othayoth fs::path savePath{path.native() + ".save"};
43bf54cbb1SJayanth Othayoth fs::rename(savePath, path);
44bf54cbb1SJayanth Othayoth }
45bf54cbb1SJayanth Othayoth
46bf54cbb1SJayanth Othayoth /**
47bf54cbb1SJayanth Othayoth * Modify the specified file so that fs::remove() fails with an exception.
48bf54cbb1SJayanth Othayoth *
49bf54cbb1SJayanth Othayoth * The file will be renamed and can be restored by calling makeFileRemovable().
50bf54cbb1SJayanth Othayoth *
51bf54cbb1SJayanth Othayoth * @param path path to the file
52bf54cbb1SJayanth Othayoth */
makeFileUnRemovable(const fs::path & path)53bf54cbb1SJayanth Othayoth inline void makeFileUnRemovable(const fs::path& path)
54bf54cbb1SJayanth Othayoth {
55bf54cbb1SJayanth Othayoth // Rename the file to save its contents
56bf54cbb1SJayanth Othayoth fs::path savePath{path.native() + ".save"};
57bf54cbb1SJayanth Othayoth fs::rename(path, savePath);
58bf54cbb1SJayanth Othayoth
59bf54cbb1SJayanth Othayoth // Create a directory at the original file path
60bf54cbb1SJayanth Othayoth fs::create_directory(path);
61bf54cbb1SJayanth Othayoth
62bf54cbb1SJayanth Othayoth // Create a file within the directory. fs::remove() will throw an exception
63bf54cbb1SJayanth Othayoth // if the path is a non-empty directory.
64bf54cbb1SJayanth Othayoth std::ofstream childFile{path / "childFile"};
65bf54cbb1SJayanth Othayoth }
66bf54cbb1SJayanth Othayoth
67bf54cbb1SJayanth Othayoth class TemporaryFileTests : public ::testing::Test
68bf54cbb1SJayanth Othayoth {
69bf54cbb1SJayanth Othayoth protected:
SetUp()70bf54cbb1SJayanth Othayoth void SetUp() override
71bf54cbb1SJayanth Othayoth {
72bf54cbb1SJayanth Othayoth // Create temporary file with some data
73bf54cbb1SJayanth Othayoth std::string buf{"FFDCDATA"};
74bf54cbb1SJayanth Othayoth uint32_t size = buf.size();
75bf54cbb1SJayanth Othayoth tmpFile = new TemporaryFile(buf.c_str(), size);
76bf54cbb1SJayanth Othayoth
77bf54cbb1SJayanth Othayoth // Create temporary file with no data
78bf54cbb1SJayanth Othayoth std::string noData{""};
79bf54cbb1SJayanth Othayoth tmpFileNoData = new TemporaryFile(noData.c_str(), 0);
80bf54cbb1SJayanth Othayoth }
81bf54cbb1SJayanth Othayoth
TearDown()82bf54cbb1SJayanth Othayoth void TearDown() override
83bf54cbb1SJayanth Othayoth {
84bf54cbb1SJayanth Othayoth std::filesystem::remove_all(tmpFile->getPath());
85bf54cbb1SJayanth Othayoth delete tmpFile;
86bf54cbb1SJayanth Othayoth
87bf54cbb1SJayanth Othayoth std::filesystem::remove_all(tmpFileNoData->getPath());
88bf54cbb1SJayanth Othayoth delete tmpFileNoData;
89bf54cbb1SJayanth Othayoth }
90bf54cbb1SJayanth Othayoth
91bf54cbb1SJayanth Othayoth // temporary file with Data
92bf54cbb1SJayanth Othayoth TemporaryFile* tmpFile;
93bf54cbb1SJayanth Othayoth
94bf54cbb1SJayanth Othayoth // temporary file with no data
95bf54cbb1SJayanth Othayoth TemporaryFile* tmpFileNoData;
96bf54cbb1SJayanth Othayoth };
97bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,DefaultConstructor)98bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, DefaultConstructor)
99bf54cbb1SJayanth Othayoth {
100bf54cbb1SJayanth Othayoth fs::path path = tmpFile->getPath();
101bf54cbb1SJayanth Othayoth EXPECT_FALSE(path.empty());
102bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
103bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::is_regular_file(path));
104bf54cbb1SJayanth Othayoth
105bf54cbb1SJayanth Othayoth fs::path parentDir = path.parent_path();
106bf54cbb1SJayanth Othayoth EXPECT_EQ(parentDir, "/tmp");
107bf54cbb1SJayanth Othayoth
108bf54cbb1SJayanth Othayoth std::string fileName = path.filename();
109bf54cbb1SJayanth Othayoth std::string namePrefix = "phosphor-logging-";
110bf54cbb1SJayanth Othayoth EXPECT_EQ(fileName.compare(0, namePrefix.size(), namePrefix), 0);
111bf54cbb1SJayanth Othayoth }
112bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,DefaultConstructorNoData)113bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, DefaultConstructorNoData)
114bf54cbb1SJayanth Othayoth {
115bf54cbb1SJayanth Othayoth fs::path path = tmpFileNoData->getPath();
116bf54cbb1SJayanth Othayoth EXPECT_FALSE(path.empty());
117bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
118bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::is_regular_file(path));
119bf54cbb1SJayanth Othayoth
120bf54cbb1SJayanth Othayoth fs::path parentDir = path.parent_path();
121bf54cbb1SJayanth Othayoth EXPECT_EQ(parentDir, "/tmp");
122bf54cbb1SJayanth Othayoth
123bf54cbb1SJayanth Othayoth std::string fileName = path.filename();
124bf54cbb1SJayanth Othayoth std::string namePrefix = "phosphor-logging-";
125bf54cbb1SJayanth Othayoth EXPECT_EQ(fileName.compare(0, namePrefix.size(), namePrefix), 0);
126bf54cbb1SJayanth Othayoth }
127bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,MoveConstructor)128bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, MoveConstructor)
129bf54cbb1SJayanth Othayoth {
130bf54cbb1SJayanth Othayoth // verify temporary file exists
131bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
132bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
133bf54cbb1SJayanth Othayoth
134bf54cbb1SJayanth Othayoth // Save path to temporary file
135bf54cbb1SJayanth Othayoth fs::path path = tmpFile->getPath();
136bf54cbb1SJayanth Othayoth
137bf54cbb1SJayanth Othayoth // Create second TemporaryFile object by moving first object
138bf54cbb1SJayanth Othayoth TemporaryFile file{std::move(*tmpFile)};
139bf54cbb1SJayanth Othayoth
140bf54cbb1SJayanth Othayoth // Verify first object now has an empty path
141bf54cbb1SJayanth Othayoth EXPECT_TRUE(tmpFile->getPath().empty());
142bf54cbb1SJayanth Othayoth
143bf54cbb1SJayanth Othayoth // Verify second object now owns same temporary file and file exists
144bf54cbb1SJayanth Othayoth EXPECT_EQ(file.getPath(), path);
145bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(file.getPath()));
146bf54cbb1SJayanth Othayoth
147bf54cbb1SJayanth Othayoth // Delete file
148bf54cbb1SJayanth Othayoth std::filesystem::remove_all(file.getPath());
149bf54cbb1SJayanth Othayoth }
150bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,MoveAssignmentOperatorTest1)151bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, MoveAssignmentOperatorTest1)
152bf54cbb1SJayanth Othayoth {
153bf54cbb1SJayanth Othayoth // Test where works: TemporaryFile object is moved
154bf54cbb1SJayanth Othayoth // verify temporary file exists
155bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
156bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
157bf54cbb1SJayanth Othayoth
158bf54cbb1SJayanth Othayoth // Save path to first temporary file
159bf54cbb1SJayanth Othayoth fs::path path1 = tmpFile->getPath();
160bf54cbb1SJayanth Othayoth
161bf54cbb1SJayanth Othayoth // Verify second temporary file exists
162bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFileNoData->getPath().empty());
163bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFileNoData->getPath()));
164bf54cbb1SJayanth Othayoth
165bf54cbb1SJayanth Othayoth // Save path to second temporary file
166bf54cbb1SJayanth Othayoth fs::path path2 = tmpFileNoData->getPath();
167bf54cbb1SJayanth Othayoth
168bf54cbb1SJayanth Othayoth // Verify temporary files are different
169bf54cbb1SJayanth Othayoth EXPECT_NE(path1, path2);
170bf54cbb1SJayanth Othayoth
171bf54cbb1SJayanth Othayoth // Move first object into the second
172bf54cbb1SJayanth Othayoth *tmpFileNoData = std::move(*tmpFile);
173bf54cbb1SJayanth Othayoth
174bf54cbb1SJayanth Othayoth // Verify first object now has an empty path
175bf54cbb1SJayanth Othayoth EXPECT_TRUE(tmpFile->getPath().empty());
176bf54cbb1SJayanth Othayoth
177bf54cbb1SJayanth Othayoth // Verify second object now owns first temporary file and file exists
178bf54cbb1SJayanth Othayoth EXPECT_EQ(tmpFileNoData->getPath(), path1);
179bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path1));
180bf54cbb1SJayanth Othayoth
181bf54cbb1SJayanth Othayoth // Verify second temporary file was deleted
182bf54cbb1SJayanth Othayoth EXPECT_FALSE(fs::exists(path2));
183bf54cbb1SJayanth Othayoth }
184bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,MoveAssignmentOperatorTest2)185bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, MoveAssignmentOperatorTest2)
186bf54cbb1SJayanth Othayoth {
187bf54cbb1SJayanth Othayoth // Test where does nothing: TemporaryFile object is moved into itself
188bf54cbb1SJayanth Othayoth // Verify temporary file exists
189bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
190bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
191bf54cbb1SJayanth Othayoth
192bf54cbb1SJayanth Othayoth // Save path to temporary file
193bf54cbb1SJayanth Othayoth fs::path path = tmpFile->getPath();
194bf54cbb1SJayanth Othayoth
195bf54cbb1SJayanth Othayoth // Try to move object into itself; should do nothing
196*81bc5611SMatt Spinler *tmpFile = static_cast<TemporaryFile&&>(*tmpFile);
197bf54cbb1SJayanth Othayoth
198bf54cbb1SJayanth Othayoth // Verify object still owns same temporary file and file exists
199bf54cbb1SJayanth Othayoth EXPECT_EQ(tmpFile->getPath(), path);
200bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
201bf54cbb1SJayanth Othayoth }
202bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,MoveAssignmentOperatorTest3)203bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, MoveAssignmentOperatorTest3)
204bf54cbb1SJayanth Othayoth {
205bf54cbb1SJayanth Othayoth // Test where fails: Cannot delete temporary file
206bf54cbb1SJayanth Othayoth // Verify temporary file exists
207bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
208bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
209bf54cbb1SJayanth Othayoth
210bf54cbb1SJayanth Othayoth // Save path to first temporary file
211bf54cbb1SJayanth Othayoth fs::path path1 = tmpFile->getPath();
212bf54cbb1SJayanth Othayoth
213bf54cbb1SJayanth Othayoth // Verify temporary file exists
214bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFileNoData->getPath().empty());
215bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
216bf54cbb1SJayanth Othayoth
217bf54cbb1SJayanth Othayoth // Save path to second temporary file
218bf54cbb1SJayanth Othayoth fs::path path2 = tmpFileNoData->getPath();
219bf54cbb1SJayanth Othayoth
220bf54cbb1SJayanth Othayoth // Verify temporary files are different
221bf54cbb1SJayanth Othayoth EXPECT_NE(path1, path2);
222bf54cbb1SJayanth Othayoth
223bf54cbb1SJayanth Othayoth // Make second temporary file unremoveable
224bf54cbb1SJayanth Othayoth makeFileUnRemovable(path2);
225bf54cbb1SJayanth Othayoth
226bf54cbb1SJayanth Othayoth try
227bf54cbb1SJayanth Othayoth {
228bf54cbb1SJayanth Othayoth // Try to move first object into the second; should throw exception
229bf54cbb1SJayanth Othayoth *tmpFileNoData = std::move(*tmpFile);
230bf54cbb1SJayanth Othayoth ADD_FAILURE() << "Should not have reached this line.";
231bf54cbb1SJayanth Othayoth }
232bf54cbb1SJayanth Othayoth catch (const std::exception& e)
233bf54cbb1SJayanth Othayoth {
234bf54cbb1SJayanth Othayoth // This is expected. Exception message will vary.
235bf54cbb1SJayanth Othayoth }
236bf54cbb1SJayanth Othayoth
237bf54cbb1SJayanth Othayoth // Verify first object has not changed and first temporary file exists
238bf54cbb1SJayanth Othayoth EXPECT_EQ(tmpFile->getPath(), path1);
239bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path1));
240bf54cbb1SJayanth Othayoth
241bf54cbb1SJayanth Othayoth // Verify second object has not changed and second temporary file exists
242bf54cbb1SJayanth Othayoth EXPECT_EQ(tmpFileNoData->getPath(), path2);
243bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path2));
244bf54cbb1SJayanth Othayoth
245bf54cbb1SJayanth Othayoth // Make second temporary file removeable so destructor can delete it
246bf54cbb1SJayanth Othayoth makeFileRemovable(path2);
247bf54cbb1SJayanth Othayoth }
248bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,Destructor)249bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, Destructor)
250bf54cbb1SJayanth Othayoth {
251bf54cbb1SJayanth Othayoth // Test where works: Temporary file is not deleted
252bf54cbb1SJayanth Othayoth {
253bf54cbb1SJayanth Othayoth fs::path path{};
254bf54cbb1SJayanth Othayoth {
255bf54cbb1SJayanth Othayoth TemporaryFile file("", 0);
256bf54cbb1SJayanth Othayoth path = file.getPath();
257bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
258bf54cbb1SJayanth Othayoth }
259bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
260bf54cbb1SJayanth Othayoth fs::remove(path);
261bf54cbb1SJayanth Othayoth }
262bf54cbb1SJayanth Othayoth
263bf54cbb1SJayanth Othayoth // Test where works: Temporary file was already deleted
264bf54cbb1SJayanth Othayoth {
265bf54cbb1SJayanth Othayoth fs::path path{};
266bf54cbb1SJayanth Othayoth {
267bf54cbb1SJayanth Othayoth TemporaryFile file("", 0);
268bf54cbb1SJayanth Othayoth path = file.getPath();
269bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
270bf54cbb1SJayanth Othayoth file.remove();
271bf54cbb1SJayanth Othayoth EXPECT_FALSE(fs::exists(path));
272bf54cbb1SJayanth Othayoth }
273bf54cbb1SJayanth Othayoth EXPECT_FALSE(fs::exists(path));
274bf54cbb1SJayanth Othayoth }
275bf54cbb1SJayanth Othayoth
276bf54cbb1SJayanth Othayoth // Test where fails: Cannot delete temporary file: No exception thrown
277bf54cbb1SJayanth Othayoth {
278bf54cbb1SJayanth Othayoth fs::path path{};
279bf54cbb1SJayanth Othayoth try
280bf54cbb1SJayanth Othayoth {
281bf54cbb1SJayanth Othayoth TemporaryFile file("", 0);
282bf54cbb1SJayanth Othayoth path = file.getPath();
283bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
284bf54cbb1SJayanth Othayoth makeFileUnRemovable(path);
285bf54cbb1SJayanth Othayoth }
286bf54cbb1SJayanth Othayoth catch (...)
287bf54cbb1SJayanth Othayoth {
288bf54cbb1SJayanth Othayoth ADD_FAILURE() << "Should not have caught exception.";
289bf54cbb1SJayanth Othayoth }
290bf54cbb1SJayanth Othayoth
291bf54cbb1SJayanth Othayoth // Temporary file should still exist
292bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(path));
293bf54cbb1SJayanth Othayoth
294bf54cbb1SJayanth Othayoth // Make file removable and delete it
295bf54cbb1SJayanth Othayoth makeFileRemovable(path);
296bf54cbb1SJayanth Othayoth fs::remove(path);
297bf54cbb1SJayanth Othayoth }
298bf54cbb1SJayanth Othayoth }
299bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,RemoveTest1)300bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, RemoveTest1)
301bf54cbb1SJayanth Othayoth {
302bf54cbb1SJayanth Othayoth // Test where works
303bf54cbb1SJayanth Othayoth // Vverify temporary file exists
304bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
305bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
306bf54cbb1SJayanth Othayoth
307bf54cbb1SJayanth Othayoth // Save path to temporary file
308bf54cbb1SJayanth Othayoth fs::path path = tmpFile->getPath();
309bf54cbb1SJayanth Othayoth
310bf54cbb1SJayanth Othayoth // Delete temporary file
311bf54cbb1SJayanth Othayoth tmpFile->remove();
312bf54cbb1SJayanth Othayoth
313bf54cbb1SJayanth Othayoth // Verify path is cleared and file does not exist
314bf54cbb1SJayanth Othayoth EXPECT_TRUE(tmpFile->getPath().empty());
315bf54cbb1SJayanth Othayoth EXPECT_FALSE(fs::exists(path));
316bf54cbb1SJayanth Othayoth
317bf54cbb1SJayanth Othayoth // Delete temporary file again; should do nothing
318bf54cbb1SJayanth Othayoth tmpFile->remove();
319bf54cbb1SJayanth Othayoth EXPECT_TRUE(tmpFile->getPath().empty());
320bf54cbb1SJayanth Othayoth EXPECT_FALSE(fs::exists(path));
321bf54cbb1SJayanth Othayoth }
322bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,RemoveTest2)323bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, RemoveTest2)
324bf54cbb1SJayanth Othayoth {
325bf54cbb1SJayanth Othayoth // Test where fails
326bf54cbb1SJayanth Othayoth // Create TemporaryFile object and verify temporary file exists
327bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
328bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
329bf54cbb1SJayanth Othayoth
330bf54cbb1SJayanth Othayoth // Make file unremovable
331bf54cbb1SJayanth Othayoth makeFileUnRemovable(tmpFile->getPath());
332bf54cbb1SJayanth Othayoth
333bf54cbb1SJayanth Othayoth try
334bf54cbb1SJayanth Othayoth {
335bf54cbb1SJayanth Othayoth // Try to delete temporary file; should fail with exception
336bf54cbb1SJayanth Othayoth tmpFile->remove();
337bf54cbb1SJayanth Othayoth ADD_FAILURE() << "Should not have reached this line.";
338bf54cbb1SJayanth Othayoth }
339bf54cbb1SJayanth Othayoth catch (const std::exception& e)
340bf54cbb1SJayanth Othayoth {
341bf54cbb1SJayanth Othayoth // This is expected. Exception message will vary.
342bf54cbb1SJayanth Othayoth }
343bf54cbb1SJayanth Othayoth
344bf54cbb1SJayanth Othayoth // Make file removable again so it will be deleted by the destructor
345bf54cbb1SJayanth Othayoth makeFileRemovable(tmpFile->getPath());
346bf54cbb1SJayanth Othayoth }
347bf54cbb1SJayanth Othayoth
TEST_F(TemporaryFileTests,GetPath)348bf54cbb1SJayanth Othayoth TEST_F(TemporaryFileTests, GetPath)
349bf54cbb1SJayanth Othayoth {
350bf54cbb1SJayanth Othayoth EXPECT_FALSE(tmpFile->getPath().empty());
351bf54cbb1SJayanth Othayoth EXPECT_EQ(tmpFile->getPath().parent_path(), "/tmp");
352bf54cbb1SJayanth Othayoth EXPECT_TRUE(fs::exists(tmpFile->getPath()));
353bf54cbb1SJayanth Othayoth }
354