secure_channel_create_test.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <string.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/config/core_configuration.h"
  23. #include "src/core/lib/resolver/resolver_registry.h"
  24. #include "src/core/lib/security/credentials/fake/fake_credentials.h"
  25. #include "src/core/lib/security/security_connector/security_connector.h"
  26. #include "src/core/lib/surface/channel.h"
  27. #include "test/core/util/test_config.h"
  28. void test_unknown_scheme_target(void) {
  29. grpc_channel_credentials* creds =
  30. grpc_fake_transport_security_credentials_create();
  31. grpc_channel* chan = grpc_channel_create("blah://blah", creds, nullptr);
  32. grpc_channel_element* elem =
  33. grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0);
  34. GPR_ASSERT(0 == strcmp(elem->filter->name, "lame-client"));
  35. grpc_core::ExecCtx exec_ctx;
  36. GRPC_CHANNEL_INTERNAL_UNREF(chan, "test");
  37. creds->Unref();
  38. }
  39. void test_security_connector_already_in_arg(void) {
  40. grpc_arg arg = grpc_security_connector_to_arg(nullptr);
  41. grpc_channel_args args;
  42. args.num_args = 1;
  43. args.args = &arg;
  44. grpc_channel* chan = grpc_channel_create(nullptr, nullptr, &args);
  45. grpc_channel_element* elem =
  46. grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0);
  47. GPR_ASSERT(0 == strcmp(elem->filter->name, "lame-client"));
  48. grpc_core::ExecCtx exec_ctx;
  49. GRPC_CHANNEL_INTERNAL_UNREF(chan, "test");
  50. }
  51. void test_null_creds(void) {
  52. grpc_channel* chan = grpc_channel_create(nullptr, nullptr, nullptr);
  53. grpc_channel_element* elem =
  54. grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0);
  55. GPR_ASSERT(0 == strcmp(elem->filter->name, "lame-client"));
  56. grpc_core::ExecCtx exec_ctx;
  57. GRPC_CHANNEL_INTERNAL_UNREF(chan, "test");
  58. }
  59. int main(int argc, char** argv) {
  60. grpc::testing::TestEnvironment env(argc, argv);
  61. grpc_init();
  62. test_security_connector_already_in_arg();
  63. test_null_creds();
  64. grpc_core::CoreConfiguration::RunWithSpecialConfiguration(
  65. [](grpc_core::CoreConfiguration::Builder* builder) {
  66. BuildCoreConfiguration(builder);
  67. // Avoid default prefix
  68. builder->resolver_registry()->Reset();
  69. },
  70. []() { test_unknown_scheme_target(); });
  71. grpc_shutdown();
  72. return 0;
  73. }