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 | |
22 #include "base/posix/eintr_wrapper.h" | |
23 #include "build/build_config.h" | |
24 #include "gtest/gtest.h" | |
25 #include "util/test/errors.h" | |
26 | |
27 #if defined(OS_POSIX) | |
28 #include <unistd.h> | |
29 #elif defined(OS_WIN) | |
30 #include <direct.h> | |
31 #include <io.h> | |
32 #endif // OS_POSIX | |
33 | |
34 namespace crashpad { | |
35 namespace test { | |
36 namespace { | |
37 | |
38 bool FileExists(const base::FilePath& path) { | |
39 #if defined(OS_POSIX) | |
40 struct stat st; | |
41 int rv = lstat(path.value().c_str(), &st); | |
42 const char stat_function[] = "lstat"; | |
43 #elif defined(OS_WIN) | |
44 struct _stat st; | |
45 int rv = _wstat(path.value().c_str(), &st); | |
46 const char stat_function[] = "_wstat"; | |
47 #else | |
48 #error "Not implemented" | |
49 #endif | |
50 if (rv < 0) { | |
51 EXPECT_EQ(ENOENT, errno) << ErrnoMessage(stat_function) << " " | |
52 << path.value(); | |
53 return false; | |
54 } | |
55 return true; | |
56 } | |
57 | |
58 void CreateFile(const base::FilePath& path) { | |
59 #if defined(OS_POSIX) | |
60 int fd = HANDLE_EINTR(creat(path.value().c_str(), 0644)); | |
61 ASSERT_GE(fd, 0) << ErrnoMessage("creat") << " " << path.value(); | |
62 ASSERT_EQ(0, IGNORE_EINTR(close(fd))) | |
63 << ErrnoMessage("close") << " " << path.value(); | |
64 #elif defined(OS_WIN) | |
65 int fd = _wcreat(path.value().c_str(), _S_IREAD | _S_IWRITE); | |
66 ASSERT_GE(fd, 0) << ErrnoMessage("_wcreat") << " " << path.value(); | |
67 ASSERT_EQ(0, _close(fd)) << ErrnoMessage("_close") << " " << path.value(); | |
68 #else | |
69 #error "Not implemented" | |
70 #endif | |
71 EXPECT_TRUE(FileExists(path)); | |
72 } | |
73 | |
74 void CreateDirectory(const base::FilePath& path) { | |
75 #if defined(OS_POSIX) | |
76 ASSERT_EQ(0, mkdir(path.value().c_str(), 0755)) | |
77 << ErrnoMessage("mkdir") << " " << path.value(); | |
78 #elif defined(OS_WIN) | |
79 ASSERT_EQ(0, _wmkdir(path.value().c_str())) | |
80 << ErrnoMessage("_wmkdir") << " " << path.value(); | |
81 #else | |
82 #error "Not implemented" | |
83 #endif | |
84 ASSERT_TRUE(FileExists(path)); | |
85 } | |
86 | |
87 TEST(ScopedTempDir, Empty) { | |
88 base::FilePath path; | |
89 { | |
90 ScopedTempDir dir; | |
91 path = dir.path(); | |
92 EXPECT_TRUE(FileExists(path)); | |
93 } | |
94 EXPECT_FALSE(FileExists(path)); | |
95 } | |
96 | |
97 TEST(ScopedTempDir, WithTwoFiles) { | |
98 base::FilePath parent, file1, file2; | |
99 | |
100 { | |
101 ScopedTempDir dir; | |
102 parent = dir.path(); | |
103 ASSERT_TRUE(FileExists(parent)); | |
104 | |
105 file1 = parent.Append(FILE_PATH_LITERAL("test1")); | |
106 CreateFile(file1); | |
107 | |
108 file2 = parent.Append(FILE_PATH_LITERAL("test 2")); | |
109 CreateFile(file2); | |
110 } | |
111 | |
112 EXPECT_FALSE(FileExists(file1)); | |
113 EXPECT_FALSE(FileExists(file2)); | |
114 EXPECT_FALSE(FileExists(parent)); | |
115 } | |
116 | |
117 TEST(ScopedTempDir, WithRecursiveDirectory) { | |
118 base::FilePath parent, file1, child_dir, file2; | |
119 | |
120 { | |
121 ScopedTempDir dir; | |
122 parent = dir.path(); | |
123 ASSERT_TRUE(FileExists(parent)); | |
124 | |
125 file1 = parent.Append(FILE_PATH_LITERAL(".first-level file")); | |
126 CreateFile(file1); | |
127 | |
128 child_dir = parent.Append(FILE_PATH_LITERAL("subdir")); | |
129 CreateDirectory(child_dir); | |
130 | |
131 file2 = child_dir.Append(FILE_PATH_LITERAL("second level file")); | |
132 CreateFile(file2); | |
133 } | |
134 | |
135 EXPECT_FALSE(FileExists(file1)); | |
136 EXPECT_FALSE(FileExists(file2)); | |
137 EXPECT_FALSE(FileExists(child_dir)); | |
138 EXPECT_FALSE(FileExists(parent)); | |
139 } | |
140 | |
141 } // namespace | |
142 } // namespace test | |
143 } // namespace crashpad | |
OLD | NEW |