1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Twist Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "nghttp2_alpn_test.h"
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "munit.h"
31
32 #include <nghttp2/nghttp2.h>
33
34 static const MunitTest tests[] = {
35 munit_void_test(test_nghttp2_alpn),
36 munit_test_end(),
37 };
38
39 const MunitSuite alpn_suite = {
40 "/alpn", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
41 };
42
http2(void)43 static void http2(void) {
44 const unsigned char p[] = {8, 'h', 't', 't', 'p', '/', '1', '.', '1', 2,
45 'h', '2', 6, 's', 'p', 'd', 'y', '/', '3'};
46 unsigned char outlen;
47 const unsigned char *out;
48 assert_int(1, ==,
49 nghttp2_select_next_protocol((unsigned char **)&out, &outlen, p,
50 sizeof(p)));
51 assert_uchar(NGHTTP2_PROTO_VERSION_ID_LEN, ==, outlen);
52 assert_memory_equal(outlen, NGHTTP2_PROTO_VERSION_ID, out);
53
54 outlen = 0;
55 out = NULL;
56
57 assert_int(1, ==, nghttp2_select_alpn(&out, &outlen, p, sizeof(p)));
58 assert_uchar(NGHTTP2_PROTO_VERSION_ID_LEN, ==, outlen);
59 assert_memory_equal(outlen, NGHTTP2_PROTO_VERSION_ID, out);
60 }
61
http11(void)62 static void http11(void) {
63 const unsigned char spdy[] = {
64 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/',
65 '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
66 };
67 unsigned char outlen;
68 const unsigned char *out;
69 assert_int(0, ==,
70 nghttp2_select_next_protocol((unsigned char **)&out, &outlen, spdy,
71 sizeof(spdy)));
72 assert_uchar(8, ==, outlen);
73 assert_memory_equal(outlen, "http/1.1", out);
74
75 outlen = 0;
76 out = NULL;
77
78 assert_int(0, ==, nghttp2_select_alpn(&out, &outlen, spdy, sizeof(spdy)));
79 assert_uchar(8, ==, outlen);
80 assert_memory_equal(outlen, "http/1.1", out);
81 }
82
no_overlap(void)83 static void no_overlap(void) {
84 const unsigned char spdy[] = {
85 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/',
86 '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '0',
87 };
88 unsigned char outlen = 0;
89 const unsigned char *out = NULL;
90 assert_int(-1, ==,
91 nghttp2_select_next_protocol((unsigned char **)&out, &outlen, spdy,
92 sizeof(spdy)));
93 assert_uchar(0, ==, outlen);
94 assert_null(out);
95
96 outlen = 0;
97 out = NULL;
98
99 assert_int(-1, ==, nghttp2_select_alpn(&out, &outlen, spdy, sizeof(spdy)));
100 assert_uchar(0, ==, outlen);
101 assert_null(out);
102 }
103
test_nghttp2_alpn(void)104 void test_nghttp2_alpn(void) {
105 http2();
106 http11();
107 no_overlap();
108 }
109