3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/ext/transport/chttp2/alpn/alpn.h"
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 #include <grpc/support/log.h>
26 #include "src/core/lib/gpr/useful.h"
27 #include "src/core/lib/iomgr/load_file.h"
28 #include "test/core/bad_ssl/server_common.h"
30 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
31 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
32 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
34 /* This test starts a server that is configured to advertise (via alpn and npn)
35 * a protocol that the connecting client does not support. It does this by
36 * overriding the functions declared in alpn.c from the core library. */
38 static const char* const fake_versions[] = {"not-h2"};
40 int grpc_chttp2_is_alpn_version_supported(const char* version, size_t size) {
42 for (i = 0; i < GPR_ARRAY_SIZE(fake_versions); i++) {
43 if (!strncmp(version, fake_versions[i], size)) return 1;
48 size_t grpc_chttp2_num_alpn_versions(void) {
49 return GPR_ARRAY_SIZE(fake_versions);
52 const char* grpc_chttp2_get_alpn_version_index(size_t i) {
53 GPR_ASSERT(i < GPR_ARRAY_SIZE(fake_versions));
54 return fake_versions[i];
57 int main(int argc, char** argv) {
58 const char* addr = bad_ssl_addr(argc, argv);
59 grpc_slice cert_slice, key_slice;
60 GPR_ASSERT(GRPC_LOG_IF_ERROR(
61 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
62 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
63 grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
64 const char* server_cert =
65 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
66 const char* server_key =
67 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
68 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
69 grpc_server_credentials* ssl_creds;
73 ssl_creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
75 server = grpc_server_create(nullptr, nullptr);
76 GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
77 grpc_server_credentials_release(ssl_creds);
80 grpc_slice_unref(cert_slice);
81 grpc_slice_unref(key_slice);