Commit 2cf7a2f34f for ffmpeg
commit 2cf7a2f34fca559b453eacf9c44e0551e2450ebb
Author: Michael Niedermayer <michael@niedermayer.cc>
Date: Sat Nov 7 00:26:39 2026 +0100
avformat/tests: add a test for the cookie parsing
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 869664a713..0df1f0843a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -791,7 +791,7 @@ TESTPROGS-$(CONFIG_RTPDEC) += rtpdec
FIFO-MUXER-TESTPROGS-$(CONFIG_NETWORK) += fifo_muxer
TESTPROGS-$(CONFIG_FIFO_MUXER) += $(FIFO-MUXER-TESTPROGS-yes)
TESTPROGS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpdh
-TESTPROGS-$(CONFIG_HTTP_PROTOCOL) += http
+TESTPROGS-$(CONFIG_HTTP_PROTOCOL) += http httpcookie
# http_nobody covers both the http and httpproxy read paths.
HTTP_NOBODY_TESTPROG-$(CONFIG_HTTP_PROTOCOL) = http_nobody
HTTP_NOBODY_TESTPROG-$(CONFIG_HTTPPROXY_PROTOCOL) = http_nobody
diff --git a/libavformat/tests/.gitignore b/libavformat/tests/.gitignore
index c3afb57677..fc795689cc 100644
--- a/libavformat/tests/.gitignore
+++ b/libavformat/tests/.gitignore
@@ -15,4 +15,5 @@
/url
/seek_utils
/http
+/httpcookie
/ogg_pages
diff --git a/libavformat/tests/httpcookie.c b/libavformat/tests/httpcookie.c
new file mode 100644
index 0000000000..069c6846ef
--- /dev/null
+++ b/libavformat/tests/httpcookie.c
@@ -0,0 +1,105 @@
+/*
+ * 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 <inttypes.h>
+#include <stdio.h>
+
+#include "libavutil/macros.h"
+#include "libavutil/mem.h"
+#include "libavutil/parseutils.h"
+#include "libavformat/http.h"
+
+static const char *const cookies[] = {
+ "sid=secret",
+ "sid= secret ; Path= /a ; Domain= .example.com",
+ "domain=localhost",
+ "path=secret; Domain=example.com; Path=/",
+ "sid=secret; =domain=example.com",
+ "sid=secret; Path=abc",
+ "sid=secret; Path=/a; Path=\"/\"",
+ "sid=secret; Secure; HttpOnly",
+ "sid=secret; Max-Age=0; Expires=Wed, 09 Jun 2100 10:18:14 GMT",
+ "sid=secret; Max-Age=-1",
+ "sid=secret; Max-Age=0; Max-Age=bad",
+ "sid=secret; Expires=Sunday, 06-Nov-1994 08:49:37 GMT; Expires=bad",
+ "sid=secret; Domain=example.com; Domain=",
+ "sid=secret; Max-Age=-; Expires=Wed, 09 Jun 2100 10:18:14 GMT",
+ "sid=secret; Max-Age=1x; Expires=Sunday, 06-Nov-1994 08:49:37 GMT",
+ "sid=sec\tret",
+ "sid=sec\rret",
+ "si\x7f" "d=secret",
+ "sid",
+ "=secret",
+ "; sid=secret",
+ "",
+};
+
+static const char *const dates[] = {
+ "Wed, 21 Oct 2015 07:28:00 GMT",
+ "Wednesday, 21-Oct-15 07:28:00 GMT",
+ "Wed Oct 21 07:28:00 2015",
+ "Thu, 01 Jan 1970 00:00:00 GMT",
+ "Thu, 29 Feb 2024 12:00:00 GMT",
+ "Thu, 29 Feb 2023 12:00:00 GMT",
+ "Wed, 21 Oct 1600 07:28:00 GMT",
+ "Wed, 21 Oct 2015 24:00:00 GMT",
+ "120",
+};
+
+int main(void)
+{
+ char *bound = av_strdup("sid=secret\ndomain=localhost\nsid=secret; Domain=example.com\n"
+ "sid=secret; @hostonly=other\r\n");
+
+ for (int i = 0; i < FF_ARRAY_ELEMS(cookies); i++) {
+ AVDictionary *pair = NULL, *attributes = NULL;
+ const AVDictionaryEntry *e = NULL;
+ const char *domain;
+ int ret = ff_http_parse_set_cookie(cookies[i], &pair, &attributes);
+
+ printf("%d:", i);
+ if (ret < 0) {
+ printf(" %s\n", ret == AVERROR_INVALIDDATA ? "no cookie" : "error");
+ } else {
+ e = av_dict_iterate(pair, NULL);
+ printf(" [%s]=[%s]", e->key, e->value);
+ for (e = NULL; (e = av_dict_iterate(attributes, e)); )
+ printf(" [%s]=[%s]", e->key, e->value);
+ domain = ff_http_cookie_domain(attributes);
+ printf(" domain=%s expiry=%"PRId64"\n", domain ? domain : "-",
+ FFMIN(ff_http_cookie_expiry(attributes), INT32_MAX));
+ }
+ av_dict_free(&pair);
+ av_dict_free(&attributes);
+ }
+
+ for (int i = 0; i < FF_ARRAY_ELEMS(dates); i++) {
+ struct tm tm;
+
+ if (ff_http_parse_date(dates[i], &tm) < 0)
+ printf("%s: invalid\n", dates[i]);
+ else
+ printf("%s: %"PRId64"\n", dates[i], (int64_t)av_timegm(&tm));
+ }
+
+ if (!bound || ff_http_bind_cookies_to_host(&bound, "example.com") < 0)
+ return 1;
+ printf("%s", bound);
+ av_free(bound);
+ return 0;
+}
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index 4beb17dd1e..36d812c24d 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -56,6 +56,10 @@ FATE_LIBAVFORMAT-$(CONFIG_HTTP_PROTOCOL) += fate-http-status-line
fate-http-status-line: libavformat/tests/http$(EXESUF)
fate-http-status-line: CMD = run libavformat/tests/http$(EXESUF) $(FATE_HTTP_STATUS_LINES)
+FATE_LIBAVFORMAT-$(CONFIG_HTTP_PROTOCOL) += fate-httpcookie
+fate-httpcookie: libavformat/tests/httpcookie$(EXESUF)
+fate-httpcookie: CMD = run libavformat/tests/httpcookie$(EXESUF)
+
FATE_LIBAVFORMAT-$(CONFIG_NETWORK) += fate-noproxy
fate-noproxy: libavformat/tests/noproxy$(EXESUF)
fate-noproxy: CMD = run libavformat/tests/noproxy$(EXESUF)
diff --git a/tests/ref/fate/httpcookie b/tests/ref/fate/httpcookie
new file mode 100644
index 0000000000..5d07b68ff3
--- /dev/null
+++ b/tests/ref/fate/httpcookie
@@ -0,0 +1,35 @@
+0: [sid]=[secret] domain=- expiry=0
+1: [sid]=[secret] [Path]=[/a] [Domain]=[.example.com] domain=example.com expiry=0
+2: [domain]=[localhost] domain=- expiry=0
+3: [path]=[secret] [Domain]=[example.com] [Path]=[/] domain=example.com expiry=0
+4: [sid]=[secret] domain=- expiry=0
+5: [sid]=[secret] [Path]=[abc] domain=- expiry=0
+6: [sid]=[secret] [Path]=["/"] domain=- expiry=0
+7: [sid]=[secret] [Secure]=[] [HttpOnly]=[] domain=- expiry=0
+8: [sid]=[secret] [Max-Age]=[0] [Expires]=[Wed, 09 Jun 2100 10:18:14 GMT] domain=- expiry=1
+9: [sid]=[secret] [Max-Age]=[-1] domain=- expiry=1
+10: [sid]=[secret] [Max-Age]=[0] domain=- expiry=1
+11: [sid]=[secret] [Expires]=[Sunday, 06-Nov-1994 08:49:37 GMT] domain=- expiry=784111777
+12: [sid]=[secret] [Domain]=[example.com] domain=example.com expiry=0
+13: [sid]=[secret] [Expires]=[Wed, 09 Jun 2100 10:18:14 GMT] domain=- expiry=2147483647
+14: [sid]=[secret] [Expires]=[Sunday, 06-Nov-1994 08:49:37 GMT] domain=- expiry=784111777
+15: [sid]=[sec ret] domain=- expiry=0
+16: no cookie
+17: no cookie
+18: no cookie
+19: no cookie
+20: no cookie
+21: no cookie
+Wed, 21 Oct 2015 07:28:00 GMT: 1445412480
+Wednesday, 21-Oct-15 07:28:00 GMT: 1445412480
+Wed Oct 21 07:28:00 2015: 1445412480
+Thu, 01 Jan 1970 00:00:00 GMT: 0
+Thu, 29 Feb 2024 12:00:00 GMT: 1709208000
+Thu, 29 Feb 2023 12:00:00 GMT: invalid
+Wed, 21 Oct 1600 07:28:00 GMT: invalid
+Wed, 21 Oct 2015 24:00:00 GMT: invalid
+120: invalid
+sid=secret; @hostonly=example.com
+domain=localhost; @hostonly=example.com
+sid=secret; Domain=example.com
+sid=secret; @hostonly=other