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 *****************************************************************************/
32 #include <vlc_common.h>
39 static unsigned char counter
= 0;
40 static bool send_failure
= false;
41 static bool expect_hello
= true;
44 static int fd_callback(vlc_tls_t
*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
,
54 assert(tls
->writev
== send_callback
);
56 const uint8_t *p
= iov
->iov_base
;
57 size_t len
= iov
->iov_len
;
62 assert(!memcmp(p
, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", 24));
68 return send_failure
? -1 : (ssize_t
)len
;
75 assert(p
[9] == counter
);
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);
99 static struct vlc_h2_frame
*frame_list(struct vlc_h2_frame
*first
, ...)
101 struct vlc_h2_frame
**pp
= &first
;
105 for (struct vlc_h2_frame
*f
= first
;
107 f
= va_arg(ap
, struct vlc_h2_frame
*))
118 struct vlc_h2_output
*out
;
121 out
= vlc_h2_output_create(&fake_tls
, false);
123 vlc_h2_output_destroy(out
);
125 vlc_sem_init(&rx
, 0);
126 out
= vlc_h2_output_create(&fake_tls
, expect_hello
= true);
128 vlc_h2_output_destroy(out
);
129 vlc_sem_destroy(&rx
);
132 vlc_sem_init(&rx
, 0);
133 out
= vlc_h2_output_create(&fake_tls
, false);
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
++)
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
);
156 vlc_sem_init(&rx
, 0);
158 out
= vlc_h2_output_create(&fake_tls
, false);
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);
172 out
= vlc_h2_output_create(&fake_tls
, expect_hello
= true);
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
);