1 // Copyright 2017 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef SRC_UNITTEST_COMMON_H_ 6 #define SRC_UNITTEST_COMMON_H_ 7 8 #include <unistd.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "puffin/src/include/puffin/common.h" 14 #include "puffin/src/logging.h" 15 16 namespace puffin { 17 18 // Utility class to delete a file when it goes out of scope. 19 class ScopedPathUnlinker { 20 public: ScopedPathUnlinker(const std::string & path)21 explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} ~ScopedPathUnlinker()22 ~ScopedPathUnlinker() { 23 if (unlink(path_.c_str()) < 0) { 24 LOG(ERROR) << "Failed to unlink: " << path_; 25 } 26 } 27 28 private: 29 const std::string path_; 30 31 DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); 32 }; 33 34 // Makes a temporary file as /tmp/puffin-XXXXXX. Both |filename| and |fd| are 35 // optional, but if given, they will be populated with the new temporary file's 36 // values. 37 bool MakeTempFile(std::string* filename, int* fd); 38 39 extern const Buffer kDeflatesSample1; 40 extern const Buffer kPuffsSample1; 41 extern const std::vector<ByteExtent> kDeflateExtentsSample1; 42 extern const std::vector<BitExtent> kSubblockDeflateExtentsSample1; 43 extern const std::vector<ByteExtent> kPuffExtentsSample1; 44 45 extern const Buffer kDeflatesSample2; 46 extern const Buffer kPuffsSample2; 47 extern const std::vector<ByteExtent> kDeflateExtentsSample2; 48 extern const std::vector<BitExtent> kSubblockDeflateExtentsSample2; 49 extern const std::vector<ByteExtent> kPuffExtentsSample2; 50 51 extern const Buffer kProblematicCache; 52 extern const std::vector<BitExtent> kProblematicCacheDeflateExtents; 53 extern const std::vector<BitExtent> kProblematicCachePuffExtents; 54 55 } // namespace puffin 56 57 #endif // SRC_UNITTEST_COMMON_H_ 58