vlc | branch: master | Rémi Denis-Courmont <[email protected]> | Fri Dec 18 22:06:23 2015 +0200| [55d00831f2d150f6c724a4ba5be55ae6ccac603d] | committer: Rémi Denis-Courmont
https: add chunked transfer encoding unit test > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=55d00831f2d150f6c724a4ba5be55ae6ccac603d --- modules/access/http/Makefile.am | 6 +- modules/access/http/chunked.c | 1 - modules/access/http/chunked_test.c | 151 ++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 3 deletions(-) diff --git a/modules/access/http/Makefile.am b/modules/access/http/Makefile.am index 2ebe78f..bc24f1b 100644 --- a/modules/access/http/Makefile.am +++ b/modules/access/http/Makefile.am @@ -32,14 +32,16 @@ h2output_test_SOURCES = access/http/h2output_test.c h2output_test_LDADD = libvlc_http.la $(LIBPTHREAD) h2conn_test_SOURCES = access/http/h2conn_test.c h2conn_test_LDADD = libvlc_http.la $(LIBPTHREAD) +h1chunked_test_SOURCES = access/http/chunked_test.c +h1chunked_test_LDADD = libvlc_http.la http_msg_test_SOURCES = access/http/message_test.c \ access/http/message.c access/http/message.h http_file_test_SOURCES = access/http/file_test.c \ access/http/message.c access/http/message.h \ access/http/file.c access/http/file.h check_PROGRAMS += hpack_test hpackenc_test \ - h2frame_test h2output_test h2conn_test \ + h2frame_test h2output_test h2conn_test h1chunked_test \ http_msg_test http_file_test TESTS += hpack_test hpackenc_test \ - h2frame_test h2output_test h2conn_test \ + h2frame_test h2output_test h2conn_test h1chunked_test \ http_msg_test http_file_test diff --git a/modules/access/http/chunked.c b/modules/access/http/chunked.c index a9e2f5d..c0d7940 100644 --- a/modules/access/http/chunked.c +++ b/modules/access/http/chunked.c @@ -50,7 +50,6 @@ static_assert(offsetof(struct vlc_chunked_stream, stream) == 0, "Cast error"); static void *vlc_chunked_fatal(struct vlc_chunked_stream *s) { - assert(!s->error); s->error = true; return NULL; } diff --git a/modules/access/http/chunked_test.c b/modules/access/http/chunked_test.c new file mode 100644 index 0000000..d235aa9 --- /dev/null +++ b/modules/access/http/chunked_test.c @@ -0,0 +1,151 @@ +/***************************************************************************** + * chunked_test.c: HTTP 1.1 chunked encoding decoder test + ***************************************************************************** + * Copyright (C) 2015 Rémi Denis-Courmont + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#undef NDEBUG + +#include <assert.h> +//#include <inttypes.h> +//#include <stdlib.h> +#include <string.h> + +#include <vlc_common.h> +#include <vlc_tls.h> +#include <vlc_block.h> +#include "h1conn.h" +#include "message.h" + +/* I/O callbacks */ +static const char *stream_content; +static size_t stream_length; +static bool stream_bad; + +static ssize_t recv_callback(struct vlc_tls *tls, void *buf, size_t len) +{ + size_t copy = len; + if (copy > stream_length) + copy = stream_length; + if (copy > 0) + { + memcpy(buf, stream_content, copy); + stream_content += copy; + stream_length -= copy; + } + (void) tls; + return copy; +} + +static void close_callback(struct vlc_tls *tls) +{ + (void) tls; +} + +static struct vlc_tls chunked_tls = +{ + .recv = recv_callback, + .close = close_callback, +}; + +static void stream_close_callback(struct vlc_http_stream *stream, bool bad) +{ + (void) stream; + assert(bad == stream_bad); +} + +static const struct vlc_http_stream_cbs chunked_stream_cbs = +{ + .close = stream_close_callback, +}; + +static struct vlc_http_stream chunked_stream = +{ + &chunked_stream_cbs, +}; + +/* Test cases */ + +static void test_good(void) +{ + struct vlc_http_stream *s; + block_t *b; + + /* Simple good payload */ + stream_content = + "A\r\n" "1234567890\r\n" + "1A; ext-foo=1\r\n" "abcdefghijklmnopqrstuvwxyz\r\n" + "0\r\n" "\r\n"; + stream_length = strlen(stream_content); + stream_bad = false; + + s = vlc_chunked_open(&chunked_stream, &chunked_tls); + assert(s != NULL); + assert(vlc_http_stream_read_headers(s) == NULL); + + b = vlc_http_stream_read(s); + assert(b != NULL); + assert(b->i_buffer == 10); + assert(!memcmp(b->p_buffer, "1234567890", 10)); + block_Release(b); + + b = vlc_http_stream_read(s); + assert(b != NULL); + assert(b->i_buffer == 26); + assert(!memcmp(b->p_buffer, "abcdefghijklmnopqrstuvwxyz", 26)); + block_Release(b); + + b = vlc_http_stream_read(s); + assert(b == NULL); + b = vlc_http_stream_read(s); + assert(b == NULL); + + vlc_http_stream_close(s, false); +} + +static void test_bad(const char *payload) +{ + struct vlc_http_stream *s; + block_t *b; + + stream_content = payload; + stream_length = strlen(payload); + stream_bad = true; + + s = vlc_chunked_open(&chunked_stream, &chunked_tls); + assert(s != NULL); + + while ((b = vlc_http_stream_read(s)) != NULL) + block_Release(b); + + vlc_http_stream_close(s, false); +} + +int main(void) +{ + test_good(); + test_bad(""); + test_bad("A\r\n" "123456789"); + test_bad("Z\r\n" "123456789"); + test_bad("0\r\n"); + + return 0; +} _______________________________________________ vlc-commits mailing list [email protected] https://mailman.videolan.org/listinfo/vlc-commits
