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