[FFmpeg-devel] [PATCH v2 2/2] avformat/tests: Add a test for avio_check with the pipe protocol

Neil Roberts bpeeluk at yahoo.co.uk
Tue Aug 30 15:09:54 EEST 2022


Creates a UNIX pipe and then verifies that avio_check returns the right
access flags for the two ends of the pipe.

v2: Add support for the Windows version of _pipe

Signed-off-by: Neil Roberts <bpeeluk at yahoo.co.uk>
---
 libavformat/Makefile         |   1 +
 libavformat/tests/.gitignore |   1 +
 libavformat/tests/pipe.c     | 108 +++++++++++++++++++++++++++++++++++
 tests/fate/libavformat.mak   |   5 ++
 4 files changed, 115 insertions(+)
 create mode 100644 libavformat/tests/pipe.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index f67a99f839..9c681c58c5 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -732,6 +732,7 @@ TESTPROGS-$(CONFIG_MOV_MUXER)            += movenc
 TESTPROGS-$(CONFIG_NETWORK)              += noproxy
 TESTPROGS-$(CONFIG_SRTP)                 += srtp
 TESTPROGS-$(CONFIG_IMF_DEMUXER)          += imf
+TESTPROGS-$(CONFIG_PIPE_PROTOCOL)        += pipe
 
 TOOLS     = aviocat                                                     \
             ismindex                                                    \
diff --git a/libavformat/tests/.gitignore b/libavformat/tests/.gitignore
index cdd0cce061..567d6f9e40 100644
--- a/libavformat/tests/.gitignore
+++ b/libavformat/tests/.gitignore
@@ -7,3 +7,4 @@
 /srtp
 /url
 /seek_utils
+/pipe
diff --git a/libavformat/tests/pipe.c b/libavformat/tests/pipe.c
new file mode 100644
index 0000000000..18a8551fd5
--- /dev/null
+++ b/libavformat/tests/pipe.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2022 Neil Roberts
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "libavformat/avio.h"
+#include "libavutil/error.h"
+
+static int check_pipe(const char *url, int mask, int expected)
+{
+    int flags = avio_check(url, mask);
+
+    if (flags < 0) {
+        fprintf(stderr,
+                "avio_check for %s with mask 0x%x failed: %s\n",
+                url,
+                mask,
+                av_err2str(flags));
+        return 0;
+    }
+
+    if (flags != expected) {
+        fprintf(stderr,
+                "Wrong result returned from avio_check for %s with mask 0x%x. "
+                "Expected 0x%x but received 0x%x\n",
+                url,
+                mask,
+                expected,
+                flags);
+        return 0;
+    }
+
+    return 1;
+}
+
+int main(int argc, char **argv)
+{
+    int ret = 0;
+    int pipe_fds[2];
+    char read_url[20], write_url[20];
+    int check_invalid_ret;
+    int pipe_ret;
+
+#ifdef _WIN32
+    pipe_ret = _pipe(pipe_fds, 1024 /* psize */, 0 /* textmode */);
+#else
+    pipe_ret = pipe(pipe_fds);
+#endif
+
+    if (pipe_ret == -1) {
+        fprintf(stderr, "error creating pipe: %s\n", strerror(errno));
+        return 1;
+    }
+
+    snprintf(read_url, sizeof(read_url), "pipe:%d", pipe_fds[0]);
+    snprintf(write_url, sizeof(write_url), "pipe:%d", pipe_fds[1]);
+
+    if (!check_pipe(read_url,
+                    AVIO_FLAG_READ | AVIO_FLAG_WRITE,
+                    AVIO_FLAG_READ))
+        ret = 1;
+
+    if (!check_pipe(write_url,
+                    AVIO_FLAG_READ | AVIO_FLAG_WRITE,
+                    AVIO_FLAG_WRITE))
+        ret = 1;
+
+    /* Ensure that we don't get flags that we didn't ask for */
+    if (!check_pipe(read_url, AVIO_FLAG_WRITE, 0))
+        ret = 1;
+
+    close(pipe_fds[0]);
+    close(pipe_fds[1]);
+
+    /* An invalid fd should return EBADF */
+    check_invalid_ret = avio_check(read_url, AVIO_FLAG_READ);
+
+    if (check_invalid_ret != AVERROR(EBADF)) {
+        fprintf(stderr,
+                "avio_check on invalid FD expected to return %i "
+                "but %i was received\n",
+                AVERROR(EBADF),
+                check_invalid_ret);
+        ret = 1;
+    }
+
+    return ret;
+}
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index d2acb4c9e0..7a22f54c04 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -26,6 +26,11 @@ FATE_LIBAVFORMAT-$(CONFIG_IMF_DEMUXER) += fate-imf
 fate-imf: libavformat/tests/imf$(EXESUF)
 fate-imf: CMD = run libavformat/tests/imf$(EXESUF)
 
+FATE_LIBAVFORMAT-$(CONFIG_PIPE_PROTOCOL) += fate-pipe
+fate-pipe: libavformat/tests/pipe$(EXESUF)
+fate-pipe: CMD = run libavformat/tests/pipe$(EXESUF)
+fate-pipe: CMP = null
+
 FATE_LIBAVFORMAT += fate-seek_utils
 fate-seek_utils: libavformat/tests/seek_utils$(EXESUF)
 fate-seek_utils: CMD = run libavformat/tests/seek_utils$(EXESUF)
-- 
2.37.2



More information about the ffmpeg-devel mailing list