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_npn_test.h"
26
27 #include <string.h>
28
29 #include <CUnit/CUnit.h>
30 #include <nghttp2/nghttp2.h>
31
http2(void)32 static void http2(void) {
33 const unsigned char p[] = {8, 'h', 't', 't', 'p', '/', '1', '.', '1', 2,
34 'h', '2', 6, 's', 'p', 'd', 'y', '/', '3'};
35 unsigned char outlen;
36 unsigned char *out;
37 CU_ASSERT(1 == nghttp2_select_next_protocol(&out, &outlen, p, sizeof(p)));
38 CU_ASSERT(NGHTTP2_PROTO_VERSION_ID_LEN == outlen);
39 CU_ASSERT(memcmp(NGHTTP2_PROTO_VERSION_ID, out, outlen) == 0);
40 }
41
http11(void)42 static void http11(void) {
43 const unsigned char spdy[] = {
44 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/',
45 '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
46 };
47 unsigned char outlen;
48 unsigned char *out;
49 CU_ASSERT(0 ==
50 nghttp2_select_next_protocol(&out, &outlen, spdy, sizeof(spdy)));
51 CU_ASSERT(8 == outlen);
52 CU_ASSERT(memcmp("http/1.1", out, outlen) == 0);
53 }
54
no_overlap(void)55 static void no_overlap(void) {
56 const unsigned char spdy[] = {
57 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/',
58 '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '0',
59 };
60 unsigned char outlen = 0;
61 unsigned char *out = NULL;
62 CU_ASSERT(-1 ==
63 nghttp2_select_next_protocol(&out, &outlen, spdy, sizeof(spdy)));
64 CU_ASSERT(0 == outlen);
65 CU_ASSERT(NULL == out);
66 }
67
test_nghttp2_npn(void)68 void test_nghttp2_npn(void) {
69 http2();
70 http11();
71 no_overlap();
72 }
73