From b18132713165210d45d66d54e7ae6086a021a47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Wed, 28 Jan 2026 15:19:45 +0100 Subject: [PATCH] test: Remove uses of `O_TMPFILE` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `O_TMPFILE` requires file system support, which may not be available in certain environments, usually containerized ones. So do not use it. A new function is added for tests to be able to create unnamed temporary files using `libcamera::MemFd` as the implementation. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart Reviewed-by: Paul Elder --- test/ipc/unixsocket.cpp | 33 +++++++++++++-------------------- test/libtest/test.cpp | 11 +++++++++++ test/libtest/test.h | 8 ++++++++ test/log/log_api.cpp | 13 +++++-------- test/shared-fd.cpp | 2 +- test/unique-fd.cpp | 2 +- 6 files changed, 39 insertions(+), 30 deletions(-) diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp index f39bd986..cd2d254b 100644 --- a/test/ipc/unixsocket.cpp +++ b/test/ipc/unixsocket.cpp @@ -137,11 +137,11 @@ private: } case CMD_JOIN: { - int outfd = open("/tmp", O_TMPFILE | O_RDWR, - S_IRUSR | S_IWUSR); - if (outfd < 0) { + UniqueFD outfd = test::createTemporaryFile(); + if (!outfd.isValid()) { + ret = errno; cerr << "Create out file failed" << endl; - stop(outfd); + stop(ret); return; } @@ -152,15 +152,13 @@ private: if (num < 0) { cerr << "Read failed" << endl; - close(outfd); stop(-EIO); return; } else if (!num) break; - if (write(outfd, buf, num) < 0) { + if (write(outfd.get(), buf, num) < 0) { cerr << "Write failed" << endl; - close(outfd); stop(-EIO); return; } @@ -169,9 +167,9 @@ private: close(fd); } - lseek(outfd, 0, 0); + lseek(outfd.get(), 0, SEEK_SET); response.data.push_back(CMD_JOIN); - response.fds.push_back(outfd); + response.fds.push_back(outfd.get()); ret = ipc_.send(response); if (ret < 0) { @@ -179,8 +177,6 @@ private: stop(ret); } - close(outfd); - break; } @@ -315,22 +311,21 @@ protected: "Foo", "Bar", }; - int fds[2]; + std::array fds; for (unsigned int i = 0; i < std::size(strings); i++) { unsigned int len = strlen(strings[i]); - fds[i] = open("/tmp", O_TMPFILE | O_RDWR, - S_IRUSR | S_IWUSR); - if (fds[i] < 0) + fds[i] = test::createTemporaryFile(); + if (!fds[i].isValid()) return TestFail; - ret = write(fds[i], strings[i], len); + ret = write(fds[i].get(), strings[i], len); if (ret < 0) return TestFail; - lseek(fds[i], 0, 0); - message.fds.push_back(fds[i]); + lseek(fds[i].get(), 0, SEEK_SET); + message.fds.push_back(fds[i].get()); } message.data.push_back(CMD_JOIN); @@ -343,8 +338,6 @@ protected: unsigned int len = strlen(strings[i]); std::vector buf(len); - close(fds[i]); - if (read(response.fds[0], buf.data(), len) <= 0) return TestFail; diff --git a/test/libtest/test.cpp b/test/libtest/test.cpp index 4e03def9..c8364fb9 100644 --- a/test/libtest/test.cpp +++ b/test/libtest/test.cpp @@ -9,6 +9,8 @@ #include "test.h" +#include + Test::Test() { } @@ -36,3 +38,12 @@ int Test::execute() return ret; } + +namespace test { + +libcamera::UniqueFD createTemporaryFile() +{ + return libcamera::MemFd::create("libcamera-test-temporary-file", 0); +} + +} /* namespace test */ diff --git a/test/libtest/test.h b/test/libtest/test.h index 3a90885d..63ba66b4 100644 --- a/test/libtest/test.h +++ b/test/libtest/test.h @@ -10,6 +10,8 @@ #include #include +#include + enum TestStatus { TestPass = 0, TestFail = -1, @@ -43,3 +45,9 @@ int main(int argc, char *argv[]) \ klass.setArgs(argc, argv); \ return klass.execute(); \ } + +namespace test { + +[[nodiscard]] libcamera::UniqueFD createTemporaryFile(); + +} diff --git a/test/log/log_api.cpp b/test/log/log_api.cpp index 8d19cf0c..043de81e 100644 --- a/test/log/log_api.cpp +++ b/test/log/log_api.cpp @@ -109,18 +109,17 @@ protected: int testFile() { - int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); - if (fd < 0) { + UniqueFD fd = test::createTemporaryFile(); + if (!fd.isValid()) { cerr << "Failed to open tmp log file" << endl; return TestFail; } char path[32]; - snprintf(path, sizeof(path), "/proc/self/fd/%u", fd); + snprintf(path, sizeof(path), "/proc/self/fd/%u", fd.get()); if (logSetFile(path) < 0) { cerr << "Failed to set log file" << endl; - close(fd); return TestFail; } @@ -128,13 +127,11 @@ protected: char buf[1000]; memset(buf, 0, sizeof(buf)); - lseek(fd, 0, SEEK_SET); - if (read(fd, buf, sizeof(buf)) < 0) { + lseek(fd.get(), 0, SEEK_SET); + if (read(fd.get(), buf, sizeof(buf)) < 0) { cerr << "Failed to read tmp log file" << endl; - close(fd); return TestFail; } - close(fd); istringstream iss(buf); return verifyOutput(iss); diff --git a/test/shared-fd.cpp b/test/shared-fd.cpp index 57199dfe..56754d9b 100644 --- a/test/shared-fd.cpp +++ b/test/shared-fd.cpp @@ -27,7 +27,7 @@ protected: desc1_ = nullptr; desc2_ = nullptr; - fd_ = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); + fd_ = test::createTemporaryFile().release(); if (fd_ < 0) return TestFail; diff --git a/test/unique-fd.cpp b/test/unique-fd.cpp index e556439e..20330995 100644 --- a/test/unique-fd.cpp +++ b/test/unique-fd.cpp @@ -189,7 +189,7 @@ protected: private: int createFd() { - fd_ = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); + fd_ = test::createTemporaryFile().release(); if (fd_ < 0) return TestFail;