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