diff mbox

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

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

Commit Message

Micah Galizia May 3, 2017, 12:29 a.m. UTC
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

Comments

Michael Niedermayer May 3, 2017, 1:01 p.m. UTC | #1
On Tue, May 02, 2017 at 08:29:25PM -0400, Micah Galizia wrote:
> 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

seems to crash here
vagrind shows these:

...

'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'

==1192== Conditional jump or move depends on uninitialised value(s)
==1192==    at 0x5FECFD: av_log (log.c:372)
==1192==    by 0x4076C6: get_cookies (http.c:990)
==1192==    by 0x40A0FA: test_get_cookies (http.c:93)
==1192==    by 0x40A575: main (http.c:177)
==1192==
==1192== Conditional jump or move depends on uninitialised value(s)
==1192==    at 0x5FE5D4: format_line (log.c:258)
==1192==    by 0x5FE9B2: av_log_default_callback (log.c:324)
==1192==    by 0x5FEDA8: av_vlog (log.c:383)
==1192==    by 0x5FED68: av_log (log.c:375)
==1192==    by 0x4076C6: get_cookies (http.c:990)
==1192==    by 0x40A0FA: test_get_cookies (http.c:93)
==1192==    by 0x40A575: main (http.c:177)
==1192==
...
==1194== 46 (16 direct, 30 indirect) bytes in 1 blocks are definitely lost in loss record 158 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4068D1: parse_cookie (http.c:748)
==1194==    by 0x40A4B3: test_parse_cookie (http.c:159)
==1194==    by 0x40A585: main (http.c:181)
==1194==
==1194== 83 (16 direct, 67 indirect) bytes in 1 blocks are definitely lost in loss record 165 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x406BF6: parse_cookie (http.c:797)
==1194==    by 0x40A4B3: test_parse_cookie (http.c:159)
==1194==    by 0x40A585: main (http.c:181)
==1194==
==1194== 97 (16 direct, 81 indirect) bytes in 1 blocks are definitely lost in loss record 168 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4068D1: parse_cookie (http.c:748)
==1194==    by 0x40A39C: test_parse_cookie (http.c:145)
==1194==    by 0x40A585: main (http.c:181)
==1194==
==1194== 97 (16 direct, 81 indirect) bytes in 1 blocks are definitely lost in loss record 169 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4068D1: parse_cookie (http.c:748)
==1194==    by 0x40A50C: test_parse_cookie (http.c:163)
==1194==    by 0x40A585: main (http.c:181)
==1194==
==1194== 101 (16 direct, 85 indirect) bytes in 1 blocks are definitely lost in loss record 170 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4068D1: parse_cookie (http.c:748)
==1194==    by 0x40A44E: test_parse_cookie (http.c:154)
==1194==    by 0x40A585: main (http.c:181)
==1194==
==1194== 122 (16 direct, 106 indirect) bytes in 1 blocks are definitely lost in loss record 173 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x406BF6: parse_cookie (http.c:797)
==1194==    by 0x40769D: get_cookies (http.c:989)
==1194==    by 0x40A0FA: test_get_cookies (http.c:93)
==1194==    by 0x40A575: main (http.c:177)
==1194==
==1194== 1,330 (144 direct, 1,186 indirect) bytes in 9 blocks are definitely lost in loss record 219 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4076DC: get_cookies (http.c:993)
==1194==    by 0x40A0FA: test_get_cookies (http.c:93)
==1194==    by 0x40A575: main (http.c:177)
==1194==
==1194== 1,495 (160 direct, 1,335 indirect) bytes in 10 blocks are definitely lost in loss record 222 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x5F2600: av_dict_set (dict.c:89)
==1194==    by 0x40683E: parse_set_cookie (http.c:730)
==1194==    by 0x4068D1: parse_cookie (http.c:748)
==1194==    by 0x40769D: get_cookies (http.c:989)
==1194==    by 0x40A0FA: test_get_cookies (http.c:93)
==1194==    by 0x40A575: main (http.c:177)
==1194==
==1194== 6,398 (104 direct, 6,294 indirect) bytes in 1 blocks are definitely lost in loss record 236 of 238
==1194==    at 0x4C2A6C5: memalign (vg_replace_malloc.c:727)
==1194==    by 0x4C2A760: posix_memalign (vg_replace_malloc.c:876)
==1194==    by 0x600F59: av_malloc (mem.c:87)
==1194==    by 0x6011EF: av_mallocz (mem.c:224)
==1194==    by 0x40A704: url_alloc_for_protocol (avio.c:94)
==1194==    by 0x40B2BA: ffurl_alloc (avio.c:299)
==1194==    by 0x40A04E: test_get_cookies (http.c:84)
==1194==    by 0x40A575: main (http.c:177)
==1194==
==1194== LEAK SUMMARY:
==1194==    definitely lost: 504 bytes in 26 blocks
==1194==    indirectly lost: 9,265 bytes in 190 blocks
==1194==      possibly lost: 0 bytes in 0 blocks
==1194==    still reachable: 83,768 bytes in 2,707 blocks
==1194==         suppressed: 0 bytes in 0 blocks

[...9
diff mbox

Patch

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 6bdfbe6789..640a348c2f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -598,6 +598,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 0000000000..76a70ae814
--- /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 cf1ba189dd..3d211bd53e 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 0000000000..c6d01fd7c5
--- /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'