OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/test/scoped_temp_dir.h" | |
16 | |
17 #include <errno.h> | |
18 #include <fcntl.h> | |
19 #include <string.h> | |
20 #include <sys/stat.h> | |
21 #include <unistd.h> | |
22 | |
23 #include "base/posix/eintr_wrapper.h" | |
24 #include "gtest/gtest.h" | |
25 #include "util/test/errors.h" | |
26 | |
27 namespace crashpad { | |
28 namespace test { | |
29 namespace { | |
30 | |
31 bool FileExists(const base::FilePath& path) { | |
32 #if defined(OS_POSIX) | |
33 struct stat st; | |
34 int rv = lstat(path.value().c_str(), &st); | |
35 if (rv < 0) { | |
36 EXPECT_EQ(ENOENT, errno) << ErrnoMessage("stat") << " " << path.value(); | |
Mark Mentovai
2015/01/02 17:49:55
lstat
Robert Sesek
2015/01/02 18:51:13
Done.
| |
37 return false; | |
38 } | |
39 return true; | |
40 #else | |
41 #error "Not implemented" | |
42 #endif | |
43 } | |
44 | |
45 void CreateFile(const base::FilePath& path) { | |
46 #if defined(OS_POSIX) | |
47 int fd = HANDLE_EINTR(creat(path.value().c_str(), 0644)); | |
48 ASSERT_GE(fd, 0) << ErrnoMessage("creat") << " " << path.value(); | |
49 ASSERT_EQ(0, IGNORE_EINTR(close(fd))) | |
50 << ErrnoMessage("close") << " " << path.value(); | |
51 #else | |
52 #error "Not implemented" | |
53 #endif | |
54 EXPECT_TRUE(FileExists(path)); | |
55 } | |
56 | |
57 void CreateDirectory(const base::FilePath& path) { | |
58 #if defined(OS_POSIX) | |
59 ASSERT_EQ(0, mkdir(path.value().c_str(), 0755)) | |
60 << ErrnoMessage("mkdir") << " " << path.value(); | |
61 #else | |
62 #error "Not implemented" | |
63 #endif | |
64 ASSERT_TRUE(FileExists(path)); | |
65 } | |
66 | |
67 TEST(ScopedTempDir, Empty) { | |
68 base::FilePath path; | |
69 { | |
70 ScopedTempDir dir; | |
71 path = dir.path(); | |
72 EXPECT_TRUE(FileExists(path)); | |
73 } | |
74 EXPECT_FALSE(FileExists(path)); | |
75 } | |
76 | |
77 TEST(ScopedTempDir, WithTwoFiles) { | |
78 base::FilePath parent, file1, file2; | |
79 | |
80 { | |
81 ScopedTempDir dir; | |
82 parent = dir.path(); | |
83 ASSERT_TRUE(FileExists(parent)); | |
84 | |
85 file1 = parent.Append("test1"); | |
86 CreateFile(file1); | |
87 | |
88 file2 = parent.Append("test 2"); | |
89 CreateFile(file2); | |
90 } | |
91 | |
92 EXPECT_FALSE(FileExists(file1)); | |
93 EXPECT_FALSE(FileExists(file2)); | |
94 EXPECT_FALSE(FileExists(parent)); | |
95 } | |
96 | |
97 TEST(ScopedTempDir, WithRecursiveDirectory) { | |
98 base::FilePath parent, file1, child_dir, file2; | |
99 | |
100 { | |
101 ScopedTempDir dir; | |
102 parent = dir.path(); | |
103 ASSERT_TRUE(FileExists(parent)); | |
104 | |
105 file1 = parent.Append(".first-level file"); | |
106 CreateFile(file1); | |
107 | |
108 child_dir = parent.Append("subdir"); | |
109 CreateDirectory(child_dir); | |
110 | |
111 file2 = child_dir.Append("second level file"); | |
112 CreateFile(file2); | |
113 } | |
114 | |
115 EXPECT_FALSE(FileExists(file1)); | |
116 EXPECT_FALSE(FileExists(file2)); | |
117 EXPECT_FALSE(FileExists(child_dir)); | |
118 EXPECT_FALSE(FileExists(parent)); | |
119 } | |
120 | |
121 } // namespace | |
122 } // namespace test | |
123 } // namespace crashpad | |
OLD | NEW |