|
1 // Copyright (c) 2011, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 // Utility class for creating a temporary directory for unit tests |
|
31 // that is deleted in the destructor. |
|
32 #ifndef GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |
|
33 #define GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |
|
34 |
|
35 #include <dirent.h> |
|
36 #include <sys/types.h> |
|
37 |
|
38 #include <string> |
|
39 |
|
40 #include "breakpad_googletest_includes.h" |
|
41 #include "common/using_std_string.h" |
|
42 |
|
43 #if !defined(__ANDROID__) |
|
44 #define TEMPDIR "/tmp" |
|
45 #else |
|
46 #define TEMPDIR "/data/local/tmp" |
|
47 #include "common/android/testing/mkdtemp.h" |
|
48 #endif |
|
49 |
|
50 namespace google_breakpad { |
|
51 |
|
52 class AutoTempDir { |
|
53 public: |
|
54 AutoTempDir() { |
|
55 char temp_dir[] = TEMPDIR "/breakpad.XXXXXX"; |
|
56 EXPECT_TRUE(mkdtemp(temp_dir) != NULL); |
|
57 path_.assign(temp_dir); |
|
58 } |
|
59 |
|
60 ~AutoTempDir() { |
|
61 DeleteRecursively(path_); |
|
62 } |
|
63 |
|
64 const string& path() const { |
|
65 return path_; |
|
66 } |
|
67 |
|
68 private: |
|
69 void DeleteRecursively(const string& path) { |
|
70 // First remove any files in the dir |
|
71 DIR* dir = opendir(path.c_str()); |
|
72 if (!dir) |
|
73 return; |
|
74 |
|
75 dirent* entry; |
|
76 while ((entry = readdir(dir)) != NULL) { |
|
77 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) |
|
78 continue; |
|
79 string entry_path = path + "/" + entry->d_name; |
|
80 struct stat stats; |
|
81 EXPECT_TRUE(lstat(entry_path.c_str(), &stats) == 0); |
|
82 if (S_ISDIR(stats.st_mode)) |
|
83 DeleteRecursively(entry_path); |
|
84 else |
|
85 EXPECT_TRUE(unlink(entry_path.c_str()) == 0); |
|
86 } |
|
87 EXPECT_TRUE(closedir(dir) == 0); |
|
88 EXPECT_TRUE(rmdir(path.c_str()) == 0); |
|
89 } |
|
90 |
|
91 // prevent copy construction and assignment |
|
92 AutoTempDir(const AutoTempDir&); |
|
93 AutoTempDir& operator=(const AutoTempDir&); |
|
94 |
|
95 string path_; |
|
96 }; |
|
97 |
|
98 } // namespace google_breakpad |
|
99 |
|
100 #endif // GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |