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 <algorithm>
22 #include <string>
23
24 #include "gtest/gtest.h"
25
26 #include <grpc/grpc.h>
27
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/channel/channel_stack.h"
30 #include "src/core/lib/channel/channel_stack_builder_impl.h"
31 #include "src/core/lib/config/core_configuration.h"
32 #include "src/core/lib/iomgr/endpoint.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/iomgr/iomgr_fwd.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.h"
38 #include "test/core/util/test_config.h"
39
40 namespace grpc_core {
41 namespace testing {
42 namespace {
43
44 // Test that XdsChannelStackModifier can be safely copied to channel args
45 // and destroyed
TEST(XdsChannelStackModifierTest,CopyChannelArgs)46 TEST(XdsChannelStackModifierTest, CopyChannelArgs) {
47 grpc_init();
48 auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>(
49 std::vector<const grpc_channel_filter*>{});
50 grpc_arg arg = channel_stack_modifier->MakeChannelArg();
51 grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
52 EXPECT_EQ(channel_stack_modifier,
53 XdsChannelStackModifier::GetFromChannelArgs(*args));
54 grpc_channel_args_destroy(args);
55 grpc_shutdown();
56 }
57
58 // Test compare on channel args with the same XdsChannelStackModifier
TEST(XdsChannelStackModifierTest,ChannelArgsCompare)59 TEST(XdsChannelStackModifierTest, ChannelArgsCompare) {
60 grpc_init();
61 auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>(
62 std::vector<const grpc_channel_filter*>{});
63 grpc_arg arg = channel_stack_modifier->MakeChannelArg();
64 grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
65 grpc_channel_args* new_args = grpc_channel_args_copy(args);
66 EXPECT_EQ(XdsChannelStackModifier::GetFromChannelArgs(*new_args),
67 XdsChannelStackModifier::GetFromChannelArgs(*args));
68 grpc_channel_args_destroy(args);
69 grpc_channel_args_destroy(new_args);
70 grpc_shutdown();
71 }
72
73 constexpr char kTestFilter1[] = "test_filter_1";
74 constexpr char kTestFilter2[] = "test_filter_2";
75
76 namespace {
77 class FakeTransport final : public Transport {
78 public:
filter_stack_transport()79 FilterStackTransport* filter_stack_transport() override { return nullptr; }
client_transport()80 ClientTransport* client_transport() override { return nullptr; }
server_transport()81 ServerTransport* server_transport() override { return nullptr; }
82
GetTransportName() const83 absl::string_view GetTransportName() const override { return "fake"; }
SetPollset(grpc_stream *,grpc_pollset *)84 void SetPollset(grpc_stream*, grpc_pollset*) override {}
SetPollsetSet(grpc_stream *,grpc_pollset_set *)85 void SetPollsetSet(grpc_stream*, grpc_pollset_set*) override {}
PerformOp(grpc_transport_op *)86 void PerformOp(grpc_transport_op*) override {}
GetEndpoint()87 grpc_endpoint* GetEndpoint() override { return nullptr; }
Orphan()88 void Orphan() override {}
89 };
90 } // namespace
91
92 // Test filters insertion
TEST(XdsChannelStackModifierTest,XdsHttpFiltersInsertion)93 TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertion) {
94 CoreConfiguration::Reset();
95 grpc_init();
96 // Add 2 test filters to XdsChannelStackModifier
97 const grpc_channel_filter test_filter_1 = {
98 nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr,
99 nullptr, 0, nullptr, nullptr, nullptr, nullptr, kTestFilter1};
100 const grpc_channel_filter test_filter_2 = {
101 nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr,
102 nullptr, 0, nullptr, nullptr, nullptr, nullptr, kTestFilter2};
103 auto channel_stack_modifier = MakeRefCounted<XdsChannelStackModifier>(
104 std::vector<const grpc_channel_filter*>{&test_filter_1, &test_filter_2});
105 grpc_arg arg = channel_stack_modifier->MakeChannelArg();
106 FakeTransport fake_transport;
107 // Create a phony ChannelStackBuilder object
108 grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
109 ChannelStackBuilderImpl builder(
110 "test", GRPC_SERVER_CHANNEL,
111 ChannelArgs::FromC(args).SetObject<Transport>(&fake_transport));
112 grpc_channel_args_destroy(args);
113 // Construct channel stack and verify that the test filters were successfully
114 // added
115 {
116 ExecCtx exec_ctx;
117 ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack(&builder));
118 }
119 std::vector<std::string> filters;
120 for (const auto& entry : *builder.mutable_stack()) {
121 filters.push_back(entry->name);
122 }
123 filters.resize(3);
124 EXPECT_EQ(filters,
125 std::vector<std::string>({"server", kTestFilter1, kTestFilter2}));
126 grpc_shutdown();
127 }
128
129 } // namespace
130 } // namespace testing
131 } // namespace grpc_core
132
main(int argc,char ** argv)133 int main(int argc, char** argv) {
134 ::testing::InitGoogleTest(&argc, argv);
135 grpc::testing::TestEnvironment env(&argc, argv);
136 int ret = RUN_ALL_TESTS();
137 return ret;
138 }
139