Debian Codegraph @

test/core/bad_ssl/servers/alpn.cc

raw
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 <string.h>
22
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpc/slice.h>
26 #include <grpc/support/log.h>
27
28 #include "src/core/lib/gpr/useful.h"
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/iomgr/load_file.h"
31 #include "test/core/bad_ssl/server_common.h"
32
33 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
34 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
35 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
36
37 /* This test starts a server that is configured to advertise (via alpn and npn)
38 * a protocol that the connecting client does not support. It does this by
39 * overriding the functions declared in alpn.c from the core library. */
40
41 static const char* const fake_versions[] = {"not-h2"};
42
43 int grpc_chttp2_is_alpn_version_supported(const char* version, size_t size) {
44 size_t i;
45 for (i = 0; i < GPR_ARRAY_SIZE(fake_versions); i++) {
46 if (!strncmp(version, fake_versions[i], size)) return 1;
47 }
48 return 0;
49 }
50
51 size_t grpc_chttp2_num_alpn_versions(void) {
52 return GPR_ARRAY_SIZE(fake_versions);
53 }
54
55 const char* grpc_chttp2_get_alpn_version_index(size_t i) {
56 GPR_ASSERT(i < GPR_ARRAY_SIZE(fake_versions));
57 return fake_versions[i];
58 }
59
60 int main(int argc, char** argv) {
61 const char* addr = bad_ssl_addr(argc, argv);
62 grpc_slice cert_slice, key_slice;
63 GPR_ASSERT(GRPC_LOG_IF_ERROR(
64 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
65 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
66 grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
67 const char* server_cert =
68 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
69 const char* server_key =
70 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
71 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
72 grpc_server_credentials* ssl_creds;
73 grpc_server* server;
74
75 grpc_init();
76 ssl_creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
77 0, nullptr);
78 server = grpc_server_create(nullptr, nullptr);
79 GPR_ASSERT(grpc_server_add_http2_port(server, addr, ssl_creds));
80 grpc_server_credentials_release(ssl_creds);
81
82 bad_ssl_run(server);
83 grpc_slice_unref(cert_slice);
84 grpc_slice_unref(key_slice);
85 grpc_shutdown();
86
87 return 0;
88 }