alpn.cc 2.9 KB

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