| 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 |
|
| 19 |
#include "gtest/gtest.h" |
| 20 |
|
| 21 |
#include <grpc/grpc.h> |
| 22 |
|
| 23 |
#include "src/core/lib/channel/channel_fwd.h" |
| 24 |
#include "src/core/lib/channel/channel_stack.h" |
| 25 |
#include "src/core/lib/config/core_configuration.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/util/test_config.h" |
| 32 |
|
| 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 = |
| 38 |
grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0); |
| 39 |
ASSERT_STREQ(elem->filter->name, "lame-client"); |
| 40 |
grpc_core::ExecCtx exec_ctx; |
| 41 |
grpc_core::Channel::FromC(chan)->Unref(); |
| 42 |
creds->Unref(); |
| 43 |
} |
| 44 |
|
| 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 = |
| 52 |
grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0); |
| 53 |
ASSERT_STREQ(elem->filter->name, "lame-client"); |
| 54 |
grpc_core::ExecCtx exec_ctx; |
| 55 |
grpc_core::Channel::FromC(chan)->Unref(); |
| 56 |
} |
| 57 |
|
| 58 |
void test_null_creds(void) { |
| 59 |
grpc_channel* chan = grpc_channel_create(nullptr, nullptr, nullptr); |
| 60 |
grpc_channel_element* elem = |
| 61 |
grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0); |
| 62 |
ASSERT_STREQ(elem->filter->name, "lame-client"); |
| 63 |
grpc_core::ExecCtx exec_ctx; |
| 64 |
grpc_core::Channel::FromC(chan)->Unref(); |
| 65 |
} |
| 66 |
|
| 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 |
|
| 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 |
} |