1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/ext/transport/chttp2/alpn/alpn.h"
20
21 #include <grpc/support/log.h>
22 #include "test/core/util/test_config.h"
23
test_alpn_success(void)24 static void test_alpn_success(void) {
25 GPR_ASSERT(grpc_chttp2_is_alpn_version_supported("h2", 2));
26 GPR_ASSERT(grpc_chttp2_is_alpn_version_supported("grpc-exp", 8));
27 }
28
test_alpn_failure(void)29 static void test_alpn_failure(void) {
30 GPR_ASSERT(!grpc_chttp2_is_alpn_version_supported("h2-155", 6));
31 GPR_ASSERT(!grpc_chttp2_is_alpn_version_supported("h1-15", 5));
32 }
33
34 // First index in ALPN supported version list of a given protocol. Returns a
35 // value one beyond the last valid element index if not found.
alpn_version_index(const char * version,size_t size)36 static size_t alpn_version_index(const char* version, size_t size) {
37 size_t i;
38 for (i = 0; i < grpc_chttp2_num_alpn_versions(); ++i) {
39 if (!strncmp(version, grpc_chttp2_get_alpn_version_index(i), size)) {
40 return i;
41 }
42 }
43 return i;
44 }
45
test_alpn_grpc_before_h2(void)46 static void test_alpn_grpc_before_h2(void) {
47 // grpc-exp is preferred over h2.
48 GPR_ASSERT(alpn_version_index("grpc-exp", 8) < alpn_version_index("h2", 2));
49 }
50
main(int argc,char ** argv)51 int main(int argc, char** argv) {
52 grpc_test_init(argc, argv);
53 test_alpn_success();
54 test_alpn_failure();
55 test_alpn_grpc_before_h2();
56 return 0;
57 }
58