xref: /aosp_15_r20/external/grpc-grpc/test/core/surface/secure_channel_create_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 //
17 
18 #include <memory>
19 
20 #include "gtest/gtest.h"
21 
22 #include <grpc/grpc.h>
23 
24 #include "src/core/lib/channel/channel_fwd.h"
25 #include "src/core/lib/channel/channel_stack.h"
26 #include "src/core/lib/config/core_configuration.h"
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "src/core/lib/security/credentials/credentials.h"
29 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
30 #include "src/core/lib/security/security_connector/security_connector.h"
31 #include "src/core/lib/surface/channel.h"
32 #include "test/core/util/test_config.h"
33 
test_unknown_scheme_target(void)34 void test_unknown_scheme_target(void) {
35   grpc_channel_credentials* creds =
36       grpc_fake_transport_security_credentials_create();
37   grpc_channel* chan = grpc_channel_create("blah://blah", creds, nullptr);
38   grpc_channel_element* elem = grpc_channel_stack_element(
39       grpc_core::Channel::FromC(chan)->channel_stack(), 0);
40   ASSERT_STREQ(elem->filter->name, "lame-client");
41   grpc_core::ExecCtx exec_ctx;
42   grpc_core::Channel::FromC(chan)->Unref();
43   creds->Unref();
44 }
45 
test_security_connector_already_in_arg(void)46 void test_security_connector_already_in_arg(void) {
47   grpc_arg arg = grpc_security_connector_to_arg(nullptr);
48   grpc_channel_args args;
49   args.num_args = 1;
50   args.args = &arg;
51   grpc_channel* chan = grpc_channel_create(nullptr, nullptr, &args);
52   grpc_channel_element* elem = grpc_channel_stack_element(
53       grpc_core::Channel::FromC(chan)->channel_stack(), 0);
54   ASSERT_STREQ(elem->filter->name, "lame-client");
55   grpc_core::ExecCtx exec_ctx;
56   grpc_core::Channel::FromC(chan)->Unref();
57 }
58 
test_null_creds(void)59 void test_null_creds(void) {
60   grpc_channel* chan = grpc_channel_create(nullptr, nullptr, nullptr);
61   grpc_channel_element* elem = grpc_channel_stack_element(
62       grpc_core::Channel::FromC(chan)->channel_stack(), 0);
63   ASSERT_STREQ(elem->filter->name, "lame-client");
64   grpc_core::ExecCtx exec_ctx;
65   grpc_core::Channel::FromC(chan)->Unref();
66 }
67 
TEST(SecureChannelCreateTest,MainTest)68 TEST(SecureChannelCreateTest, MainTest) {
69   grpc_init();
70   test_security_connector_already_in_arg();
71   test_null_creds();
72   grpc_core::CoreConfiguration::RunWithSpecialConfiguration(
73       [](grpc_core::CoreConfiguration::Builder* builder) {
74         BuildCoreConfiguration(builder);
75         // Avoid default prefix
76         builder->resolver_registry()->Reset();
77       },
78       []() { test_unknown_scheme_target(); });
79   grpc_shutdown();
80 }
81 
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83   grpc::testing::TestEnvironment env(&argc, argv);
84   ::testing::InitGoogleTest(&argc, argv);
85   grpc::testing::TestGrpcScope grpc_scope;
86   return RUN_ALL_TESTS();
87 }
88