bionic tests: switch to using android-base/file.h for TemporaryFile
A matching definition of TemporaryFile exists in libbase now.
Test: compile
Bug: 119313545
Change-Id: I6f84dbf3af9a9c4b270a2532a36c9cb4c0f6bb8f
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 14848ae..00850f6 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -14,13 +14,6 @@
* limitations under the License.
*/
-#include <gtest/gtest.h>
-
-#include "BionicDeathTest.h"
-#include "math_data_test.h"
-#include "TemporaryFile.h"
-#include "utils.h"
-
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
@@ -31,10 +24,16 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <unistd.h>
#include <limits>
#include <string>
+#include <android-base/macros.h>
+#include <gtest/gtest.h>
+
+#include "BionicDeathTest.h"
+#include "math_data_test.h"
#include "utils.h"
#if defined(__BIONIC__)
@@ -45,6 +44,41 @@
#endif
#endif
+template <typename T = int (*)(char*)>
+class GenericTemporaryFile {
+ public:
+ explicit GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn_(mk_fn) {
+ // Since we might be running on the host or the target, and if we're
+ // running on the host we might be running under bionic or glibc,
+ // let's just try both possible temporary directories and take the
+ // first one that works.
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
+ }
+ }
+
+ ~GenericTemporaryFile() {
+ close(fd);
+ unlink(path);
+ }
+
+ int fd;
+ char path[1024];
+
+ private:
+ T mk_fn_;
+
+ void init(const char* tmp_dir) {
+ snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir);
+ fd = mk_fn_(path);
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
+};
+
+typedef GenericTemporaryFile<> MyTemporaryFile;
+
// The random number generator tests all set the seed, get four values, reset the seed and check
// that they get the first two values repeated, and then reset the seed and check two more values
// to rule out the possibility that we're just going round a cycle of four values.
@@ -386,24 +420,24 @@
}
TEST(stdlib, mkostemp64) {
- TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
+ MyTemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
AssertCloseOnExec(tf.fd, true);
}
TEST(stdlib, mkostemp) {
- TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
+ MyTemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
AssertCloseOnExec(tf.fd, true);
}
TEST(stdlib, mkstemp64) {
- TemporaryFile tf(mkstemp64);
+ MyTemporaryFile tf(mkstemp64);
struct stat64 sb;
ASSERT_EQ(0, fstat64(tf.fd, &sb));
ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
}
TEST(stdlib, mkstemp) {
- TemporaryFile tf;
+ MyTemporaryFile tf(mkstemp);
struct stat sb;
ASSERT_EQ(0, fstat(tf.fd, &sb));
}