[vlc-commits] https: HTTP 1 connection handling unit test
Rémi Denis-Courmont
git at videolan.org
Sat Dec 19 11:51:41 CET 2015
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Dec 19 12:50:58 2015 +0200| [4c06a2be4bdc11c442e0f0b0e6e9bb933ede8017] | committer: Rémi Denis-Courmont
https: HTTP 1 connection handling unit test
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4c06a2be4bdc11c442e0f0b0e6e9bb933ede8017
---
modules/access/http/Makefile.am | 6 +-
modules/access/http/h1conn_test.c | 227 +++++++++++++++++++++++++++++++++++++
2 files changed, 231 insertions(+), 2 deletions(-)
diff --git a/modules/access/http/Makefile.am b/modules/access/http/Makefile.am
index d4a03fb..6f75794 100644
--- a/modules/access/http/Makefile.am
+++ b/modules/access/http/Makefile.am
@@ -32,6 +32,8 @@ 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)
+h1conn_test_SOURCES = access/http/h1conn_test.c
+h1conn_test_LDADD = libvlc_http.la
h1chunked_test_SOURCES = access/http/chunked_test.c
h1chunked_test_LDADD = libvlc_http.la
http_msg_test_SOURCES = access/http/message_test.c \
@@ -40,8 +42,8 @@ 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 h1chunked_test \
+ h2frame_test h2output_test h2conn_test h1conn_test h1chunked_test \
http_msg_test http_file_test
TESTS += hpack_test hpackenc_test \
- h2frame_test h2output_test h2conn_test h1chunked_test \
+ h2frame_test h2output_test h2conn_test h1conn_test h1chunked_test \
http_msg_test http_file_test
diff --git a/modules/access/http/h1conn_test.c b/modules/access/http/h1conn_test.c
new file mode 100644
index 0000000..61f164a
--- /dev/null
+++ b/modules/access/http/h1conn_test.c
@@ -0,0 +1,227 @@
+/*****************************************************************************
+ * h1conn_test.c: HTTP/1 connection tests
+ *****************************************************************************
+ * 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 <stddef.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#ifndef SOCK_CLOEXEC
+# define SOCK_CLOEXEC 0
+#endif
+
+#include <vlc_common.h>
+#include <vlc_block.h>
+#include <vlc_tls.h>
+#include "conn.h"
+#include "message.h"
+
+static struct vlc_http_conn *conn;
+static int external_fd;
+
+static void conn_create(void)
+{
+ int fds[2];
+
+ if (socketpair(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0, fds))
+ assert(!"socketpair");
+
+ struct vlc_tls *tls = vlc_tls_DummyCreate(NULL, fds[1]);
+ assert(tls != NULL);
+
+ external_fd = fds[0];
+
+ conn = vlc_h1_conn_create(tls);
+ assert(conn != NULL);
+}
+
+static void conn_send_raw(const void *buf, size_t len)
+{
+ ssize_t val = write(external_fd, buf, len);
+ assert((size_t)val == len);
+}
+
+static void conn_send(const char *str)
+{
+ return conn_send_raw(str, strlen(str));
+}
+
+static void conn_shutdown(int how)
+{
+ shutdown(external_fd, how);
+}
+
+static void conn_destroy(void)
+{
+ conn_shutdown(SHUT_WR);
+ vlc_http_conn_release(conn);
+ close(external_fd);
+}
+
+static struct vlc_http_stream *stream_open(void)
+{
+ struct vlc_http_msg *m = vlc_http_req_create("GET", "https",
+ "www.example.com", "/");
+ assert(m != NULL);
+
+ struct vlc_http_stream *s = vlc_http_stream_open(conn, m);
+ vlc_http_msg_destroy(m);
+ return s;
+}
+
+int main(void)
+{
+ struct vlc_http_stream *s;
+ struct vlc_http_msg *m;
+ struct block_t *b;
+
+ /* Dummy */
+ conn_create();
+ conn_destroy();
+
+ /* Test rejected connection */
+ conn_create();
+ conn_shutdown(SHUT_RD);
+ s = stream_open();
+ assert(s == NULL);
+ conn_destroy();
+
+ /* Test rejected stream */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_shutdown(SHUT_WR);
+
+ m = vlc_http_stream_read_headers(s);
+ assert(m == NULL);
+ b = vlc_http_stream_read(s);
+ assert(b == NULL);
+ m = vlc_http_stream_read_headers(s);
+ assert(m == NULL);
+ b = vlc_http_stream_read(s);
+ assert(b == NULL);
+ vlc_http_stream_close(s, true);
+
+ s = stream_open();
+ assert(s == NULL);
+ conn_destroy();
+
+ /* Test garbage */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_send("Go away!\r\n\r\n");
+ conn_shutdown(SHUT_WR);
+
+ m = vlc_http_stream_read_headers(s);
+ assert(m == NULL);
+ b = vlc_http_stream_read(s);
+ assert(b == NULL);
+ conn_destroy();
+ vlc_http_stream_close(s, false);
+
+ /* Test HTTP/1.0 stream */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_send("HTTP/1.0 200 OK\r\n\r\n");
+ m = vlc_http_stream_read_headers(s);
+ assert(m != NULL);
+
+ conn_send("Hello world!");
+ conn_shutdown(SHUT_WR);
+ b = vlc_http_msg_read(m);
+ assert(b != NULL);
+ assert(b->i_buffer == 12);
+ assert(!memcmp(b->p_buffer, "Hello world!", 12));
+ block_Release(b);
+ b = vlc_http_msg_read(m);
+ assert(b == NULL);
+ vlc_http_msg_destroy(m);
+ conn_destroy();
+
+ /* Test HTTP/1.1 with closed connection */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n");
+ m = vlc_http_stream_read_headers(s);
+ assert(m != NULL);
+
+ conn_send("Hello again!");
+ conn_shutdown(SHUT_WR);
+ b = vlc_http_msg_read(m);
+ assert(b != NULL);
+ assert(b->i_buffer == 12);
+ assert(!memcmp(b->p_buffer, "Hello again!", 12));
+ block_Release(b);
+ b = vlc_http_msg_read(m);
+ assert(b == NULL);
+ vlc_http_msg_destroy(m);
+ conn_destroy();
+
+ /* Test HTTP/1.1 with chunked transfer encoding */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_send("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
+ "Content-Length: 1000000\r\n\r\n"); /* length must be ignored */
+ m = vlc_http_stream_read_headers(s);
+ assert(m != NULL);
+
+ conn_send("C\r\nHello there!\r\n0\r\n\r\n");
+ b = vlc_http_msg_read(m);
+ assert(b != NULL);
+ assert(b->i_buffer == 12);
+ assert(!memcmp(b->p_buffer, "Hello there!", 12));
+ block_Release(b);
+ conn_destroy(); /* test connection release before closing stream */
+ b = vlc_http_msg_read(m);
+ assert(b == NULL);
+ vlc_http_msg_destroy(m);
+
+ /* Test HTTP/1.1 with content length */
+ conn_create();
+ s = stream_open();
+ assert(s != NULL);
+ conn_send("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n");
+ m = vlc_http_stream_read_headers(s);
+ assert(m != NULL);
+
+ conn_send("Bye bye!");
+ b = vlc_http_msg_read(m);
+ assert(b != NULL);
+ assert(b->i_buffer == 8);
+ assert(!memcmp(b->p_buffer, "Bye bye!", 8));
+ block_Release(b);
+ b = vlc_http_msg_read(m);
+ assert(b == NULL);
+ vlc_http_msg_destroy(m);
+ conn_destroy();
+
+ return 0;
+}
More information about the vlc-commits
mailing list