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 <grpc/grpc.h>
19
20 #include <memory>
21
22 #include "gtest/gtest.h"
23 #include "src/core/config/core_configuration.h"
24 #include "src/core/lib/channel/channel_fwd.h"
25 #include "src/core/lib/channel/channel_stack.h"
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "src/core/lib/security/credentials/credentials.h"
28 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
29 #include "src/core/lib/security/security_connector/security_connector.h"
30 #include "src/core/lib/surface/channel.h"
31 #include "test/core/test_util/test_config.h"
32
test_unknown_scheme_target(void)33 void test_unknown_scheme_target(void) {
34 grpc_channel_credentials* creds =
35 grpc_fake_transport_security_credentials_create();
36 grpc_channel* chan = grpc_channel_create("blah://blah", creds, nullptr);
37 grpc_channel_element* elem = grpc_channel_stack_element(
38 grpc_core::Channel::FromC(chan)->channel_stack(), 0);
39 ASSERT_EQ(elem->filter->name.name(), "lame-client");
40 grpc_core::ExecCtx exec_ctx;
41 grpc_core::Channel::FromC(chan)->Unref();
42 creds->Unref();
43 }
44
test_security_connector_already_in_arg(void)45 void test_security_connector_already_in_arg(void) {
46 grpc_arg arg = grpc_security_connector_to_arg(nullptr);
47 grpc_channel_args args;
48 args.num_args = 1;
49 args.args = &arg;
50 grpc_channel* chan = grpc_channel_create(nullptr, nullptr, &args);
51 grpc_channel_element* elem = grpc_channel_stack_element(
52 grpc_core::Channel::FromC(chan)->channel_stack(), 0);
53 ASSERT_EQ(elem->filter->name.name(), "lame-client");
54 grpc_core::ExecCtx exec_ctx;
55 grpc_core::Channel::FromC(chan)->Unref();
56 }
57
test_null_creds(void)58 void test_null_creds(void) {
59 grpc_channel* chan = grpc_channel_create(nullptr, nullptr, nullptr);
60 grpc_channel_element* elem = grpc_channel_stack_element(
61 grpc_core::Channel::FromC(chan)->channel_stack(), 0);
62 ASSERT_EQ(elem->filter->name.name(), "lame-client");
63 grpc_core::ExecCtx exec_ctx;
64 grpc_core::Channel::FromC(chan)->Unref();
65 }
66
TEST(SecureChannelCreateTest,MainTest)67 TEST(SecureChannelCreateTest, MainTest) {
68 grpc_init();
69 test_security_connector_already_in_arg();
70 test_null_creds();
71 grpc_core::CoreConfiguration::RunWithSpecialConfiguration(
72 [](grpc_core::CoreConfiguration::Builder* builder) {
73 BuildCoreConfiguration(builder);
74 // Avoid default prefix
75 builder->resolver_registry()->Reset();
76 },
77 []() { test_unknown_scheme_target(); });
78 grpc_shutdown();
79 }
80
main(int argc,char ** argv)81 int main(int argc, char** argv) {
82 grpc::testing::TestEnvironment env(&argc, argv);
83 ::testing::InitGoogleTest(&argc, argv);
84 grpc::testing::TestGrpcScope grpc_scope;
85 return RUN_ALL_TESTS();
86 }
87