[FFmpeg-devel,2/2] Add http cookie tests cases to fate

Submitted by Micah Galizia on April 6, 2017, 2:50 a.m.

Details

Message ID 20170406025036.4386-2-micahgalizia@gmail.com
State New
Headers show

Commit Message

Micah Galizia April 6, 2017, 2:50 a.m.
Signed-off-by: Micah Galizia <micahgalizia@gmail.com>
---
 libavformat/Makefile       |   1 +
 libavformat/tests/http.c   | 186 +++++++++++++++++++++++++++++++++++++++++++++
 tests/fate/libavformat.mak |   5 ++
 tests/ref/fate/http        |  30 ++++++++
 4 files changed, 222 insertions(+)
 create mode 100644 libavformat/tests/http.c
 create mode 100644 tests/ref/fate/http

Patch hide | download patch | download mbox

diff --git a/libavformat/Makefile b/libavformat/Makefile
index f56ef16..a4abd1b 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -597,6 +597,7 @@  SKIPHEADERS-$(CONFIG_NETWORK)            += network.h rtsp.h
 
 TESTPROGS = seek                                                        \
             url                                                         \
+            http                                                        \
 #           async                                                       \
 
 FIFO-MUXER-TESTPROGS-$(CONFIG_NETWORK)   += fifo_muxer
diff --git a/libavformat/tests/http.c b/libavformat/tests/http.c
new file mode 100644
index 0000000..76a70ae
--- /dev/null
+++ b/libavformat/tests/http.c
@@ -0,0 +1,186 @@ 
+/*
+ * Copyright (c) 2017 Micah Galizia
+ *
+ * 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 "libavformat/http.c"
+#include "libavformat/avio.h"
+
+typedef struct GetCookiesTestCase {
+    const char *set_cookie;
+    const char *cookie_str;
+    const char *path;
+    const char *domain;
+} GetCookiesTestCase;
+
+// Don't go past Tue, 19 Jan 2038 03:14:07 GMT or 32-bit time_t overflows
+GetCookiesTestCase get_cookies_tests[] = {
+    /* Test good and expired cookie. Should be acceptable */
+    {"first=\"good\"; Domain=.test.com; Path=/\nsecond=great; domain=.test.com; path=/; HttpOnly",
+     "first=\"good\"; second=great", "/hello", "cookie.test.com"},
+
+     /* Test good and expired cookie. Should be acceptable */
+    {"expired=\"really_old\"; Domain=.test.com; Expires=Thu, 01 Jan 1970 00:00:10 GMT; Path=/\ngood=not_expired; domain=.test.com; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly",
+     "good=not_expired", "/hello", "cookie.test.com"},
+
+    /* Test a good and expired cookie in the neulion format.
+     * Should be acceptable */
+    {"expired=\"really_old\"; Domain=.test.com; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/\nneulion=not_expired; domain=.test.com; path=/; expires=Tue, 19-Jan-2038 03:14:07 GMT; HttpOnly",
+     "neulion=not_expired", "/hello", "cookie.test.com"},
+
+    /* Test an expiry date without the day of week specified */
+    {"no_day=still_ok; domain=.test.com; path=/; expires=19 Jan 2038 03:14:07 GMT; HttpOnly",
+     "no_day=still_ok", "/hello", "cookie.test.com"},
+
+    /* Test a date that cannot be parsed. Allow the cookie */
+    {"unparsable_date=allow_cookie; domain=.test.com; path=/; expires=19 Jon 2038 03:14:07 GMT; HttpOnly",
+     "unparsable_date=allow_cookie", "/hello", "cookie.test.com"},
+
+    /* Test a cookie that has a different domain. Do not use the cookie */
+    {"different_domain=exclude; domain=.nottest.com; path=/; expires=19 Jan 2038 03:14:07 GMT; HttpOnly",
+     NULL, "/hello", "cookie.test.com"},
+
+    /* Test a set-cookie that has no spaces */
+    {"no_spaces=no_big_deal;domain=.test.com;path=/;expires=Tue,19Jan203803:14:07GMT;HttpOnly",
+     "no_spaces=no_big_deal", "/hello", "cookie.test.com"},
+
+    /* Test a set-cookie that has no spaces and is expired. Excluded */
+    {"no_spaces_expired=not_ok;domain=.test.com;path=/;expires=Thu01Jan1970000010GMT;HttpOnly",
+     NULL, "/hello", "cookie.test.com"},
+
+    /* Test a set-cookie with a date that is too long. */
+    {"long=handled;domain=.test.com;path=/;expires=Tue, 19 Jan 2038 03:14:07GMTGMTGMTGMTGMTGMT;HttpOnly",
+     "long=handled", "/hello", "cookie.test.com"},
+
+    /* Test a set-cookie with a date that starts with too many characters */
+    {"bad_start=ok;domain=.test.com;path=/;expires=BooBooBooTue, 19 Jan 2038 03:14:07;HttpOnly",
+     "bad_start=ok", "/hello", "cookie.test.com"},
+
+    {NULL}
+};
+
+
+static int test_get_cookies(void)
+{
+    URLContext *c = NULL;
+    char *cookies = NULL;
+    HTTPContext http;
+
+    if (ffurl_alloc(&c, "http://test.com", AVIO_FLAG_READ, NULL) < 0) {
+        printf("Unable to allocate HTTP URL protocol\n");
+        return -1;
+    }
+    http.cookie_dict = NULL;
+
+    for (int i = 0; get_cookies_tests[i].set_cookie; i++) {
+        http.cookies = av_strdup(get_cookies_tests[i].set_cookie);
+        av_dict_free(&http.cookie_dict);
+        get_cookies(&http, &cookies, get_cookies_tests[i].path, get_cookies_tests[i].domain);
+        printf("%d) '%s'=>'%s'\n\n", i, http.cookies, cookies);
+
+        if (get_cookies_tests[i].cookie_str == NULL) {
+            if (cookies != NULL)
+                printf("ERROR: test case %d failed NULL check\n", i);
+        } else if (cookies == NULL)
+            printf("ERROR: test case %d failed NULL check\n", i);
+        else if (strcmp(cookies, get_cookies_tests[i].cookie_str))
+            printf("ERROR: test case %d failed\n", i);
+
+        av_freep(&http.cookies);
+        av_free(cookies);
+    }
+
+    return 0;
+}
+
+static int test_parse_set_cookie(void) {
+    AVDictionary *d = NULL;
+    AVDictionaryEntry *e;
+    const char *set_cookie;
+
+    set_cookie = "good=not_expired; domain=.test.com; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly";
+    parse_set_cookie(set_cookie, &d);
+    if (!(e = av_dict_get(d, "expires", NULL, 0))) {
+        printf("Unable to get expiry\n");
+        return -1;
+    }
+    printf("'%s'->'%s'|'%s'\n", set_cookie, e->key, e->value);
+    av_dict_free(&d);
+
+    set_cookie = "good=great";
+    parse_set_cookie(set_cookie, &d);
+    if (!(e = av_dict_get(d, "good", NULL, 0))) {
+        printf("Unable to get expiry\n");
+        return -1;
+    }
+    printf("'%s'->'%s'|'%s'\n", set_cookie, e->key, e->value);
+    av_dict_free(&d);
+
+    return 0;
+}
+
+static int test_parse_cookie(void) {
+    AVDictionaryEntry *e;
+    AVDictionary *dict = NULL;
+    const char *timeless = "name=timeless";
+    const char *new = "name=value; Expires=Tue, 19 Jan 2038 03:14:00 GMT";
+    const char *old = "name=bad_value; Expires=Thu, 01 Jan 1970 00:00:10 GMT";
+    const char *newer = "name=new_value; Expires=Tue, 19 Jan 2038 03:14:07 GMT";
+
+    parse_cookie(NULL, new, &dict);
+    e = av_dict_get(dict, "name", NULL, 0);
+    printf("'%s'->'%s'|'%s'\n", new, e->key, e->value);
+
+
+    parse_cookie(NULL, old, &dict);
+    e = av_dict_get(dict, "name", NULL, 0);
+    printf("'%s'->'%s'|'%s'\n", old, e->key, e->value);
+
+    parse_cookie(NULL, newer, &dict);
+    e = av_dict_get(dict, "name", NULL, 0);
+    printf("'%s'->'%s'|'%s'\n", newer, e->key, e->value);
+
+    av_dict_free(&dict);
+    parse_cookie(NULL, timeless, &dict);
+    e = av_dict_get(dict, "name", NULL, 0);
+    printf("'%s'->'%s'|'%s'\n", timeless, e->key, e->value);
+
+    parse_cookie(NULL, new, &dict);
+    e = av_dict_get(dict, "name", NULL, 0);
+    printf("'%s'->'%s'|'%s'\n", timeless, e->key, e->value);
+
+    return 0;
+}
+
+int main(void) {
+    avformat_network_init();
+
+    if (test_parse_set_cookie()) {
+        return 1;
+    }
+
+    if (test_get_cookies()) {
+        return 1;
+    }
+
+    if (test_parse_cookie()) {
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index cf1ba18..3d211bd 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -14,6 +14,10 @@  FATE_LIBAVFORMAT-$(CONFIG_SRTP) += fate-srtp
 fate-srtp: libavformat/tests/srtp$(EXESUF)
 fate-srtp: CMD = run libavformat/tests/srtp
 
+FATE_LIBAVFORMAT-$(CONFIG_HTTP_PROTOCOL) += fate-http
+fate-http: libavformat/tests/http$(EXESUF)
+fate-http: CMD = run libavformat/tests/http
+
 FATE_LIBAVFORMAT-yes += fate-url
 fate-url: libavformat/tests/url$(EXESUF)
 fate-url: CMD = run libavformat/tests/url
@@ -25,3 +29,4 @@  fate-movenc: CMD = run libavformat/tests/movenc
 FATE_LIBAVFORMAT += $(FATE_LIBAVFORMAT-yes)
 FATE-$(CONFIG_AVFORMAT) += $(FATE_LIBAVFORMAT)
 fate-libavformat: $(FATE_LIBAVFORMAT)
+
diff --git a/tests/ref/fate/http b/tests/ref/fate/http
new file mode 100644
index 0000000..c6d01fd
--- /dev/null
+++ b/tests/ref/fate/http
@@ -0,0 +1,30 @@ 
+'good=not_expired; domain=.test.com; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly'->'expires'|'Tue, 19 Jan 2038 03:14:07 GMT'
+'good=great'->'good'|'great'
+0) 'first="good"; Domain=.test.com; Path=/
+second=great; domain=.test.com; path=/; HttpOnly'=>'first="good"; second=great'
+
+1) 'expired="really_old"; Domain=.test.com; Expires=Thu, 01 Jan 1970 00:00:10 GMT; Path=/
+good=not_expired; domain=.test.com; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly'=>'good=not_expired'
+
+2) 'expired="really_old"; Domain=.test.com; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
+neulion=not_expired; domain=.test.com; path=/; expires=Tue, 19-Jan-2038 03:14:07 GMT; HttpOnly'=>'neulion=not_expired'
+
+3) 'no_day=still_ok; domain=.test.com; path=/; expires=19 Jan 2038 03:14:07 GMT; HttpOnly'=>'no_day=still_ok'
+
+4) 'unparsable_date=allow_cookie; domain=.test.com; path=/; expires=19 Jon 2038 03:14:07 GMT; HttpOnly'=>'unparsable_date=allow_cookie'
+
+5) 'different_domain=exclude; domain=.nottest.com; path=/; expires=19 Jan 2038 03:14:07 GMT; HttpOnly'=>'(null)'
+
+6) 'no_spaces=no_big_deal;domain=.test.com;path=/;expires=Tue,19Jan203803:14:07GMT;HttpOnly'=>'no_spaces=no_big_deal'
+
+7) 'no_spaces_expired=not_ok;domain=.test.com;path=/;expires=Thu01Jan1970000010GMT;HttpOnly'=>'(null)'
+
+8) 'long=handled;domain=.test.com;path=/;expires=Tue, 19 Jan 2038 03:14:07GMTGMTGMTGMTGMTGMT;HttpOnly'=>'long=handled'
+
+9) 'bad_start=ok;domain=.test.com;path=/;expires=BooBooBooTue, 19 Jan 2038 03:14:07;HttpOnly'=>'bad_start=ok'
+
+'name=value; Expires=Tue, 19 Jan 2038 03:14:00 GMT'->'name'|'=value; Expires=Tue, 19 Jan 2038 03:14:00 GMT'
+'name=bad_value; Expires=Thu, 01 Jan 1970 00:00:10 GMT'->'name'|'=value; Expires=Tue, 19 Jan 2038 03:14:00 GMT'
+'name=new_value; Expires=Tue, 19 Jan 2038 03:14:07 GMT'->'name'|'=new_value; Expires=Tue, 19 Jan 2038 03:14:07 GMT'
+'name=timeless'->'name'|'=timeless'
+'name=timeless'->'name'|'=value; Expires=Tue, 19 Jan 2038 03:14:00 GMT'