| 1 |
// |
| 2 |
// |
| 3 |
// Copyright 2021 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 "src/core/ext/xds/xds_channel_stack_modifier.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/grpc.h> |
| 29 |
#include <grpcpp/opencensus.h> |
| 30 |
|
| 31 |
#include "src/core/lib/channel/channel_args.h" |
| 32 |
#include "src/core/lib/channel/channel_stack.h" |
| 33 |
#include "src/core/lib/channel/channel_stack_builder_impl.h" |
| 34 |
#include "src/core/lib/config/core_configuration.h" |
| 35 |
#include "src/core/lib/surface/channel_init.h" |
| 36 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 37 |
#include "src/core/lib/transport/transport_fwd.h" |
| 38 |
#include "src/core/lib/transport/transport_impl.h" |
| 39 |
#include "test/core/util/test_config.h" |
| 40 |
|
| 41 |
namespace grpc_core { |
| 42 |
namespace testing { |
| 43 |
namespace { |
| 44 |
|
| 45 |
// Test that XdsChannelStackModifier can be safely copied to channel args |
| 46 |
// and destroyed |
| 47 |
TEST(XdsChannelStackModifierTest, CopyChannelArgs) { |
| 48 |
grpc_init(); |
| 49 |
auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>( |
| 50 |
std::vector<const grpc_channel_filter*>{}); |
| 51 |
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); |
| 52 |
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); |
| 53 |
EXPECT_EQ(channel_stack_modifier, |
| 54 |
XdsChannelStackModifier::GetFromChannelArgs(*args)); |
| 55 |
grpc_channel_args_destroy(args); |
| 56 |
grpc_shutdown(); |
| 57 |
} |
| 58 |
|
| 59 |
// Test compare on channel args with the same XdsChannelStackModifier |
| 60 |
TEST(XdsChannelStackModifierTest, ChannelArgsCompare) { |
| 61 |
grpc_init(); |
| 62 |
auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>( |
| 63 |
std::vector<const grpc_channel_filter*>{}); |
| 64 |
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); |
| 65 |
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); |
| 66 |
grpc_channel_args* new_args = grpc_channel_args_copy(args); |
| 67 |
EXPECT_EQ(XdsChannelStackModifier::GetFromChannelArgs(*new_args), |
| 68 |
XdsChannelStackModifier::GetFromChannelArgs(*args)); |
| 69 |
grpc_channel_args_destroy(args); |
| 70 |
grpc_channel_args_destroy(new_args); |
| 71 |
grpc_shutdown(); |
| 72 |
} |
| 73 |
|
| 74 |
constexpr char kTestFilter1[] = "test_filter_1"; |
| 75 |
constexpr char kTestFilter2[] = "test_filter_2"; |
| 76 |
|
| 77 |
// Test filters insertion |
| 78 |
TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertion) { |
| 79 |
CoreConfiguration::Reset(); |
| 80 |
grpc_init(); |
| 81 |
// Add 2 test filters to XdsChannelStackModifier |
| 82 |
const grpc_channel_filter test_filter_1 = { |
| 83 |
nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, |
| 84 |
0, nullptr, nullptr, nullptr, nullptr, kTestFilter1}; |
| 85 |
const grpc_channel_filter test_filter_2 = { |
| 86 |
nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, |
| 87 |
0, nullptr, nullptr, nullptr, nullptr, kTestFilter2}; |
| 88 |
auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>( |
| 89 |
std::vector<const grpc_channel_filter*>{&test_filter_1, &test_filter_2}); |
| 90 |
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); |
| 91 |
// Create a phony ChannelStackBuilder object |
| 92 |
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); |
| 93 |
ChannelStackBuilderImpl builder("test", GRPC_SERVER_CHANNEL, |
| 94 |
ChannelArgs::FromC(args)); |
| 95 |
grpc_channel_args_destroy(args); |
| 96 |
grpc_transport_vtable fake_transport_vtable; |
| 97 |
memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable)); |
| 98 |
fake_transport_vtable.name = "fake"; |
| 99 |
grpc_transport fake_transport = {&fake_transport_vtable}; |
| 100 |
builder.SetTransport(&fake_transport); |
| 101 |
// Construct channel stack and verify that the test filters were successfully |
| 102 |
// added |
| 103 |
ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack(&builder)); |
| 104 |
std::vector<std::string> filters; |
| 105 |
for (const auto& entry : *builder.mutable_stack()) { |
| 106 |
filters.push_back(entry->name); |
| 107 |
} |
| 108 |
filters.resize(3); |
| 109 |
EXPECT_EQ(filters, |
| 110 |
std::vector<std::string>({"server", kTestFilter1, kTestFilter2})); |
| 111 |
grpc_shutdown(); |
| 112 |
} |
| 113 |
|
| 114 |
// Test filters insertion with OpenCensus plugin registered |
| 115 |
TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertionAfterCensus) { |
| 116 |
CoreConfiguration::Reset(); |
| 117 |
grpc::RegisterOpenCensusPlugin(); |
| 118 |
grpc_init(); |
| 119 |
// Add 2 test filters to XdsChannelStackModifier |
| 120 |
const grpc_channel_filter test_filter_1 = { |
| 121 |
nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, |
| 122 |
0, nullptr, nullptr, nullptr, nullptr, kTestFilter1}; |
| 123 |
const grpc_channel_filter test_filter_2 = { |
| 124 |
nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, |
| 125 |
0, nullptr, nullptr, nullptr, nullptr, kTestFilter2}; |
| 126 |
auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>( |
| 127 |
std::vector<const grpc_channel_filter*>{&test_filter_1, &test_filter_2}); |
| 128 |
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); |
| 129 |
// Create a phony ChannelStackBuilder object |
| 130 |
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); |
| 131 |
ChannelStackBuilderImpl builder("test", GRPC_SERVER_CHANNEL, |
| 132 |
ChannelArgs::FromC(args)); |
| 133 |
grpc_channel_args_destroy(args); |
| 134 |
grpc_transport_vtable fake_transport_vtable; |
| 135 |
memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable)); |
| 136 |
fake_transport_vtable.name = "fake"; |
| 137 |
grpc_transport fake_transport = {&fake_transport_vtable}; |
| 138 |
builder.SetTransport(&fake_transport); |
| 139 |
// Construct channel stack and verify that the test filters were successfully |
| 140 |
// added after the census filter |
| 141 |
ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack(&builder)); |
| 142 |
std::vector<std::string> filters; |
| 143 |
for (const auto& entry : *builder.mutable_stack()) { |
| 144 |
filters.push_back(entry->name); |
| 145 |
} |
| 146 |
filters.resize(4); |
| 147 |
EXPECT_EQ(filters, std::vector<std::string>({"server", "opencensus_server", |
| 148 |
kTestFilter1, kTestFilter2})); |
| 149 |
grpc_shutdown(); |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace |
| 153 |
} // namespace testing |
| 154 |
} // namespace grpc_core |
| 155 |
|
| 156 |
int main(int argc, char** argv) { |
| 157 |
::testing::InitGoogleTest(&argc, argv); |
| 158 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 159 |
int ret = RUN_ALL_TESTS(); |
| 160 |
return ret; |
| 161 |
} |