{"id":2434,"url":"https://patchwork.libcamera.org/api/patches/2434/?format=json","web_url":"https://patchwork.libcamera.org/patch/2434/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20191217015106.1175515-4-niklas.soderlund@ragnatech.se>","date":"2019-12-17T01:51:06","name":"[libcamera-devel,v2,3/3] test: file_descriptor: Add test","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"33c552586f17bb4a44f08aaabb35789880e41e6e","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2434/mbox/","series":[{"id":590,"url":"https://patchwork.libcamera.org/api/series/590/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=590","date":"2019-12-17T01:51:03","name":"libcamera: Add FileDescriptor to help pass numerical fds around","version":2,"mbox":"https://patchwork.libcamera.org/series/590/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2434/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2434/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 98B4C6047E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Dec 2019 02:51:18 +0100 (CET)","from bismarck.berto.se (p54ac5865.dip0.t-ipconnect.de\n\t[84.172.88.101]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA\n\tid b72fedea-206f-11ea-a00b-005056917a89;\n\tTue, 17 Dec 2019 02:51:17 +0100 (CET)"],"X-Halon-ID":"b72fedea-206f-11ea-a00b-005056917a89","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 17 Dec 2019 02:51:06 +0100","Message-Id":"<20191217015106.1175515-4-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.24.0","In-Reply-To":"<20191217015106.1175515-1-niklas.soderlund@ragnatech.se>","References":"<20191217015106.1175515-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 3/3] test: file_descriptor: Add test","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Tue, 17 Dec 2019 01:51:19 -0000"},"content":"Add a test which exercises the whole FileDescriptor interface.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n test/file_descriptor.cpp | 165 +++++++++++++++++++++++++++++++++++++++\n test/meson.build         |   1 +\n 2 files changed, 166 insertions(+)\n create mode 100644 test/file_descriptor.cpp","diff":"diff --git a/test/file_descriptor.cpp b/test/file_descriptor.cpp\nnew file mode 100644\nindex 0000000000000000..177d8bcf16591185\n--- /dev/null\n+++ b/test/file_descriptor.cpp\n@@ -0,0 +1,165 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * file_descriptor.cpp - FileDescriptor test\n+ */\n+\n+#include <fcntl.h>\n+#include <sys/stat.h>\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+#include <libcamera/file_descriptor.h>\n+\n+#include \"test.h\"\n+\n+using namespace libcamera;\n+using namespace std;\n+\n+class FileDescriptorTest : public Test\n+{\n+protected:\n+\tint init()\n+\t{\n+\t\tdesc1_ = nullptr;\n+\t\tdesc2_ = nullptr;\n+\n+\t\tfd_ = open(\"/tmp\", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);\n+\t\tif (fd_ < 0)\n+\t\t\treturn TestFail;\n+\n+\t\t/* Cache inode number of temp file. */\n+\t\tstruct stat s;\n+\t\tif (fstat(fd_, &s))\n+\t\t\treturn TestFail;\n+\n+\t\tinodeNr_ = s.st_ino;\n+\n+\t\treturn 0;\n+\t}\n+\n+\tint run()\n+\t{\n+\t\t/* Test creating FileDescriptor from numerical file descriptor. */\n+\t\tdesc1_ = new FileDescriptor(fd_);\n+\t\tif (desc1_->fd() == fd_)\n+\t\t\treturn TestFail;\n+\n+\t\tif (!isValidFd(fd_) || !isValidFd(desc1_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tint fd = desc1_->fd();\n+\n+\t\tdelete desc1_;\n+\t\tdesc1_ = nullptr;\n+\n+\t\tif (!isValidFd(fd_) || isValidFd(fd))\n+\t\t\treturn TestFail;\n+\n+\t\t/* Test creating FileDescriptor from other FileDescriptor. */\n+\t\tdesc1_ = new FileDescriptor(fd_);\n+\t\tdesc2_ = new FileDescriptor(*desc1_);\n+\n+\t\tif (desc1_->fd() == fd_ || desc2_->fd() == fd_ || desc1_->fd() == desc2_->fd())\n+\t\t\treturn TestFail;\n+\n+\t\tif (!isValidFd(desc1_->fd()) || !isValidFd(desc2_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tdelete desc1_;\n+\t\tdesc1_ = nullptr;\n+\n+\t\tif (!isValidFd(desc2_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tdelete desc2_;\n+\t\tdesc2_ = nullptr;\n+\n+\t\t/* Test creating FileDescriptor by taking over other FileDescriptor. */\n+\t\tdesc1_ = new FileDescriptor(fd_);\n+\t\tfd = desc1_->fd();\n+\t\tdesc2_ = new FileDescriptor(std::move(*desc1_));\n+\n+\t\tif (desc1_->fd() != -1 || desc2_->fd() != fd)\n+\t\t\treturn TestFail;\n+\n+\t\tif (!isValidFd(desc2_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tdelete desc1_;\n+\t\tdesc1_ = nullptr;\n+\t\tdelete desc2_;\n+\t\tdesc2_ = nullptr;\n+\n+\t\t/* Test creating FileDescriptor by copy assignment. */\n+\t\tdesc1_ = new FileDescriptor();\n+\t\tdesc2_ = new FileDescriptor(fd_);\n+\n+\t\tif (desc1_->fd() != -1)\n+\t\t\treturn TestFail;\n+\n+\t\tfd = desc2_->fd();\n+\t\t*desc1_ = *desc2_;\n+\n+\t\tif (desc1_->fd() == fd || desc2_->fd() != fd)\n+\t\t\treturn TestFail;\n+\n+\t\tif (!isValidFd(desc1_->fd()) || !isValidFd(desc2_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tdelete desc1_;\n+\t\tdesc1_ = nullptr;\n+\t\tdelete desc2_;\n+\t\tdesc2_ = nullptr;\n+\n+\t\tif (!isValidFd(fd_))\n+\t\t\treturn TestFail;\n+\n+\t\t/* Test creating FileDescriptor by move assignment. */\n+\t\tdesc1_ = new FileDescriptor();\n+\t\tdesc2_ = new FileDescriptor(fd_);\n+\n+\t\tfd = desc2_->fd();\n+\t\t*desc1_ = std::move(*desc2_);\n+\n+\t\tif (desc1_->fd() != fd || desc2_->fd() != -1)\n+\t\t\treturn TestFail;\n+\n+\t\tif (!isValidFd(desc1_->fd()))\n+\t\t\treturn TestFail;\n+\n+\t\tdelete desc1_;\n+\t\tdesc1_ = nullptr;\n+\t\tdelete desc2_;\n+\t\tdesc2_ = nullptr;\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\tvoid cleanup()\n+\t{\n+\t\tdelete desc2_;\n+\t\tdelete desc1_;\n+\n+\t\tif (fd_ > 0)\n+\t\t\tclose(fd_);\n+\t}\n+\n+private:\n+\tbool isValidFd(int fd)\n+\t{\n+\t\tstruct stat s;\n+\t\tif (fstat(fd, &s))\n+\t\t\treturn false;\n+\n+\t\t/* Check that inode number matches cached temp file. */\n+\t\treturn s.st_ino == inodeNr_;\n+\t}\n+\n+\tint fd_;\n+\tino_t inodeNr_;\n+\tFileDescriptor *desc1_, *desc2_;\n+};\n+\n+TEST_REGISTER(FileDescriptorTest)\ndiff --git a/test/meson.build b/test/meson.build\nindex 1bb2161dc05a7b1d..9dc392df30efd956 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -25,6 +25,7 @@ internal_tests = [\n     ['event',                           'event.cpp'],\n     ['event-dispatcher',                'event-dispatcher.cpp'],\n     ['event-thread',                    'event-thread.cpp'],\n+    ['file_descriptor',                 'file_descriptor.cpp'],\n     ['message',                         'message.cpp'],\n     ['object',                          'object.cpp'],\n     ['object-invoke',                   'object-invoke.cpp'],\n","prefixes":["libcamera-devel","v2","3/3"]}