aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmin Hassani <ahassani@google.com>2018-02-21 16:40:29 -0800
committerAmin Hassani <ahassani@google.com>2018-02-21 17:37:08 -0800
commitbbcceef51dae94bc08ff93f7c18c3feb8161461c (patch)
tree7fc875b15da5d0b186fec83266314fbccd1e29e4
parenta4c5ba6acd3a7935a17d586d88ebf32a7c0663ce (diff)
downloadplatform_external_puffin-bbcceef51dae94bc08ff93f7c18c3feb8161461c.tar.gz
platform_external_puffin-bbcceef51dae94bc08ff93f7c18c3feb8161461c.tar.bz2
platform_external_puffin-bbcceef51dae94bc08ff93f7c18c3feb8161461c.zip
Add MakeTempFile function
We need a function to (semi-)properly create a temporary file. Bug: 73487244 Test: unittests Change-Id: I486935b4bd9e85aad2eff6fc2fd9f88d0013cf17
-rw-r--r--Android.bp1
-rw-r--r--puffin.gyp1
-rw-r--r--src/patching_unittest.cc3
-rw-r--r--src/unittest_common.cc26
-rw-r--r--src/unittest_common.h5
5 files changed, 35 insertions, 1 deletions
diff --git a/Android.bp b/Android.bp
index d95bc49..5d99dcb 100644
--- a/Android.bp
+++ b/Android.bp
@@ -98,6 +98,7 @@ cc_test {
"src/sample_generator.cc",
"src/stream_unittest.cc",
"src/testrunner.cc",
+ "src/unittest_common.cc",
"src/utils_unittest.cc",
],
shared_libs: [
diff --git a/puffin.gyp b/puffin.gyp
index 33f8688..4903e00 100644
--- a/puffin.gyp
+++ b/puffin.gyp
@@ -171,6 +171,7 @@
'src/puff_io_unittest.cc',
'src/puffin_unittest.cc',
'src/stream_unittest.cc',
+ 'src/unittest_common.cc',
'src/utils_unittest.cc',
],
},
diff --git a/src/patching_unittest.cc b/src/patching_unittest.cc
index d478262..d6327cf 100644
--- a/src/patching_unittest.cc
+++ b/src/patching_unittest.cc
@@ -30,7 +30,8 @@ void TestPatching(const Buffer& src_buf,
const vector<BitExtent>& dst_deflates,
const Buffer patch) {
Buffer patch_out;
- string patch_path = "/tmp/patch.tmp";
+ string patch_path;
+ ASSERT_TRUE(MakeTempFile(&patch_path, nullptr));
ScopedPathUnlinker scoped_unlinker(patch_path);
ASSERT_TRUE(PuffDiff(src_buf, dst_buf, src_deflates, dst_deflates, patch_path,
&patch_out));
diff --git a/src/unittest_common.cc b/src/unittest_common.cc
new file mode 100644
index 0000000..b542740
--- /dev/null
+++ b/src/unittest_common.cc
@@ -0,0 +1,26 @@
+// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "puffin/src/unittest_common.h"
+
+namespace puffin {
+
+using std::string;
+
+bool MakeTempFile(string* filename, int* fd) {
+ char tmp_template[] = "/tmp/puffin-XXXXXX";
+ int mkstemp_fd = mkstemp(tmp_template);
+ TEST_AND_RETURN_FALSE(mkstemp_fd >= 0);
+ if (filename) {
+ *filename = tmp_template;
+ }
+ if (fd) {
+ *fd = mkstemp_fd;
+ } else {
+ close(mkstemp_fd);
+ }
+ return true;
+}
+
+} // namespace puffin
diff --git a/src/unittest_common.h b/src/unittest_common.h
index ac1e71c..d0bbb02 100644
--- a/src/unittest_common.h
+++ b/src/unittest_common.h
@@ -29,6 +29,11 @@ class ScopedPathUnlinker {
DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker);
};
+// Makes a temporary file as /tmp/puffin-XXXXXX. Both |filename| and |fd| are
+// optional, but if given, they will be populated with the new temporary file's
+// values.
+bool MakeTempFile(std::string* filename, int* fd);
+
// clang-format off
// Uncompressed deflate block.
const Buffer kRaw1 = {0x01, 0x02, 0x03, 0x04, 0x05};