contrib: soxr: enable by default
[vlc.git] / modules / access / http / h2output_test.c
blob18afa3c5bc54e63c80fc4d0c2cf418c107dbbdbd
1 /*****************************************************************************
2 * h2output_test.c: HTTP/2 send queue test
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #undef NDEBUG
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <vlc_common.h>
33 #include <vlc_tls.h>
34 #include "h2frame.h"
35 #include "h2output.h"
37 #undef msleep
39 static unsigned char counter = 0;
40 static bool send_failure = false;
41 static bool expect_hello = true;
42 static vlc_sem_t rx;
44 static int fd_callback(vlc_tls_t *tls)
46 (void) tls;
47 return fileno(stderr); /* should be writable (at least most of the time) */
50 static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov,
51 unsigned count)
53 assert(count == 1);
54 assert(tls->writev == send_callback);
56 const uint8_t *p = iov->iov_base;
57 size_t len = iov->iov_len;
59 if (expect_hello)
61 assert(len >= 24);
62 assert(!memcmp(p, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", 24));
64 expect_hello = false;
65 vlc_sem_post(&rx);
67 if (len == 24)
68 return send_failure ? -1 : (ssize_t)len;
70 p += 24;
71 len -= 24;
74 assert(len == 9 + 1);
75 assert(p[9] == counter);
77 if (send_failure)
78 errno = EIO;
79 else
80 counter++;
81 vlc_sem_post(&rx);
83 return send_failure ? -1 : (ssize_t)len;
86 static vlc_tls_t fake_tls =
88 .get_fd = fd_callback,
89 .writev = send_callback,
92 static struct vlc_h2_frame *frame(unsigned char c)
94 struct vlc_h2_frame *f = vlc_h2_frame_data(1, &c, 1, false);
95 assert(f != NULL);
96 return f;
99 static struct vlc_h2_frame *frame_list(struct vlc_h2_frame *first, ...)
101 struct vlc_h2_frame **pp = &first;
102 va_list ap;
104 va_start(ap, first);
105 for (struct vlc_h2_frame *f = first;
106 f != NULL;
107 f = va_arg(ap, struct vlc_h2_frame *))
109 *pp = f;
110 pp = &f->next;
112 va_end(ap);
113 return first;
116 int main(void)
118 struct vlc_h2_output *out;
120 /* Dummy */
121 out = vlc_h2_output_create(&fake_tls, false);
122 assert(out != NULL);
123 vlc_h2_output_destroy(out);
125 vlc_sem_init(&rx, 0);
126 out = vlc_h2_output_create(&fake_tls, expect_hello = true);
127 assert(out != NULL);
128 vlc_h2_output_destroy(out);
129 vlc_sem_destroy(&rx);
131 /* Success */
132 vlc_sem_init(&rx, 0);
133 out = vlc_h2_output_create(&fake_tls, false);
134 assert(out != NULL);
135 assert(vlc_h2_output_send_prio(out, NULL) == -1);
136 assert(vlc_h2_output_send_prio(out, frame(0)) == 0);
137 assert(vlc_h2_output_send_prio(out, frame(1)) == 0);
138 assert(vlc_h2_output_send(out, NULL) == -1);
139 assert(vlc_h2_output_send(out, frame(2)) == 0);
140 assert(vlc_h2_output_send(out, frame(3)) == 0);
141 assert(vlc_h2_output_send(out, frame_list(frame(4), frame(5),
142 frame(6), NULL)) == 0);
143 assert(vlc_h2_output_send(out, frame(7)) == 0);
144 for (unsigned i = 0; i < 8; i++)
145 vlc_sem_wait(&rx);
147 assert(vlc_h2_output_send_prio(out, frame(8)) == 0);
148 assert(vlc_h2_output_send(out, frame(9)) == 0);
150 vlc_h2_output_destroy(out);
151 vlc_sem_destroy(&rx);
153 /* Failure */
154 send_failure = true;
156 vlc_sem_init(&rx, 0);
157 counter = 10;
158 out = vlc_h2_output_create(&fake_tls, false);
159 assert(out != NULL);
161 assert(vlc_h2_output_send(out, frame(10)) == 0);
162 for (unsigned char i = 11; vlc_h2_output_send(out, frame(i)) == 0; i++)
163 msleep(CLOCK_FREQ/10); /* eventually, it should start failing */
164 assert(vlc_h2_output_send(out, frame(0)) == -1);
165 assert(vlc_h2_output_send_prio(out, frame(0)) == -1);
166 vlc_h2_output_destroy(out);
167 vlc_sem_destroy(&rx);
169 /* Failure during hello */
170 vlc_sem_init(&rx, 0);
171 counter = 0;
172 out = vlc_h2_output_create(&fake_tls, expect_hello = true);
173 assert(out != NULL);
174 vlc_sem_wait(&rx);
176 for (unsigned char i = 1; vlc_h2_output_send_prio(out, frame(i)) == 0; i++)
177 msleep(CLOCK_FREQ/10);
178 assert(vlc_h2_output_send(out, frame(0)) == -1);
179 assert(vlc_h2_output_send_prio(out, frame(0)) == -1);
180 vlc_h2_output_destroy(out);
181 vlc_sem_destroy(&rx);
183 return 0;