1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc++/support/channel_arguments.h>
#include <grpc++/grpc++.h>
#include <grpc/grpc.h>
#include <grpc/support/useful.h>
#include <gtest/gtest.h>
#include "src/core/lib/iomgr/socket_mutator.h"
namespace grpc {
namespace testing {
namespace {
// A simple grpc_socket_mutator to be used to test SetSocketMutator
class TestSocketMutator : public grpc_socket_mutator {
public:
TestSocketMutator();
bool MutateFd(int fd) {
// Do nothing on the fd
return true;
}
};
//
// C API for TestSocketMutator
//
bool test_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
TestSocketMutator* tsm = (TestSocketMutator*)mutator;
return tsm->MutateFd(fd);
}
int test_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
return GPR_ICMP(a, b);
}
void test_mutator_destroy(grpc_socket_mutator* mutator) {
TestSocketMutator* tsm = (TestSocketMutator*)mutator;
delete tsm;
}
grpc_socket_mutator_vtable test_mutator_vtable = {
test_mutator_mutate_fd, test_mutator_compare, test_mutator_destroy};
//
// TestSocketMutator implementation
//
TestSocketMutator::TestSocketMutator() {
grpc_socket_mutator_init(this, &test_mutator_vtable);
}
}
class ChannelArgumentsTest : public ::testing::Test {
protected:
ChannelArgumentsTest()
: pointer_vtable_({&ChannelArguments::PointerVtableMembers::Copy,
&ChannelArguments::PointerVtableMembers::Destroy,
&ChannelArguments::PointerVtableMembers::Compare}) {}
void SetChannelArgs(const ChannelArguments& channel_args,
grpc_channel_args* args) {
channel_args.SetChannelArgs(args);
}
grpc::string GetDefaultUserAgentPrefix() {
std::ostringstream user_agent_prefix;
user_agent_prefix << "grpc-c++/" << Version();
return user_agent_prefix.str();
}
void VerifyDefaultChannelArgs() {
grpc_channel_args args;
SetChannelArgs(channel_args_, &args);
EXPECT_EQ(static_cast<size_t>(1), args.num_args);
EXPECT_STREQ(GRPC_ARG_PRIMARY_USER_AGENT_STRING, args.args[0].key);
EXPECT_EQ(GetDefaultUserAgentPrefix(),
grpc::string(args.args[0].value.string));
}
bool HasArg(grpc_arg expected_arg) {
grpc_channel_args args;
SetChannelArgs(channel_args_, &args);
for (size_t i = 0; i < args.num_args; i++) {
const grpc_arg& arg = args.args[i];
if (arg.type == expected_arg.type &&
grpc::string(arg.key) == expected_arg.key) {
if (arg.type == GRPC_ARG_INTEGER) {
return arg.value.integer == expected_arg.value.integer;
} else if (arg.type == GRPC_ARG_STRING) {
return grpc::string(arg.value.string) == expected_arg.value.string;
} else if (arg.type == GRPC_ARG_POINTER) {
return arg.value.pointer.p == expected_arg.value.pointer.p &&
arg.value.pointer.vtable->copy ==
expected_arg.value.pointer.vtable->copy &&
arg.value.pointer.vtable->destroy ==
expected_arg.value.pointer.vtable->destroy;
}
}
}
return false;
}
grpc_arg_pointer_vtable pointer_vtable_;
ChannelArguments channel_args_;
};
TEST_F(ChannelArgumentsTest, SetInt) {
VerifyDefaultChannelArgs();
grpc::string key0("key0");
grpc_arg arg0;
arg0.type = GRPC_ARG_INTEGER;
arg0.key = const_cast<char*>(key0.c_str());
arg0.value.integer = 0;
grpc::string key1("key1");
grpc_arg arg1;
arg1.type = GRPC_ARG_INTEGER;
arg1.key = const_cast<char*>(key1.c_str());
arg1.value.integer = 1;
grpc::string arg_key0(key0);
channel_args_.SetInt(arg_key0, arg0.value.integer);
// Clear key early to make sure channel_args takes a copy
arg_key0.clear();
EXPECT_TRUE(HasArg(arg0));
grpc::string arg_key1(key1);
channel_args_.SetInt(arg_key1, arg1.value.integer);
arg_key1.clear();
EXPECT_TRUE(HasArg(arg0));
EXPECT_TRUE(HasArg(arg1));
}
TEST_F(ChannelArgumentsTest, SetString) {
VerifyDefaultChannelArgs();
grpc::string key0("key0");
grpc::string val0("val0");
grpc_arg arg0;
arg0.type = GRPC_ARG_STRING;
arg0.key = const_cast<char*>(key0.c_str());
arg0.value.string = const_cast<char*>(val0.c_str());
grpc::string key1("key1");
grpc::string val1("val1");
grpc_arg arg1;
arg1.type = GRPC_ARG_STRING;
arg1.key = const_cast<char*>(key1.c_str());
arg1.value.string = const_cast<char*>(val1.c_str());
grpc::string key(key0);
grpc::string val(val0);
channel_args_.SetString(key, val);
// Clear key/val early to make sure channel_args takes a copy
key = "";
val = "";
EXPECT_TRUE(HasArg(arg0));
key = key1;
val = val1;
channel_args_.SetString(key, val);
// Clear key/val early to make sure channel_args takes a copy
key = "";
val = "";
EXPECT_TRUE(HasArg(arg0));
EXPECT_TRUE(HasArg(arg1));
}
TEST_F(ChannelArgumentsTest, SetPointer) {
VerifyDefaultChannelArgs();
grpc::string key0("key0");
grpc_arg arg0;
arg0.type = GRPC_ARG_POINTER;
arg0.key = const_cast<char*>(key0.c_str());
arg0.value.pointer.p = &key0;
arg0.value.pointer.vtable = &pointer_vtable_;
grpc::string key(key0);
channel_args_.SetPointer(key, arg0.value.pointer.p);
EXPECT_TRUE(HasArg(arg0));
}
TEST_F(ChannelArgumentsTest, SetSocketMutator) {
VerifyDefaultChannelArgs();
grpc_arg arg0, arg1;
TestSocketMutator* mutator0 = new TestSocketMutator();
TestSocketMutator* mutator1 = new TestSocketMutator();
arg0 = grpc_socket_mutator_to_arg(mutator0);
arg1 = grpc_socket_mutator_to_arg(mutator1);
channel_args_.SetSocketMutator(mutator0);
EXPECT_TRUE(HasArg(arg0));
channel_args_.SetSocketMutator(mutator1);
EXPECT_TRUE(HasArg(arg1));
// arg0 is replaced by arg1
EXPECT_FALSE(HasArg(arg0));
// arg0 is destroyed by grpc_socket_mutator_to_arg(mutator1)
arg1.value.pointer.vtable->destroy(arg1.value.pointer.p);
}
TEST_F(ChannelArgumentsTest, SetUserAgentPrefix) {
VerifyDefaultChannelArgs();
grpc::string prefix("prefix");
grpc::string whole_prefix = prefix + " " + GetDefaultUserAgentPrefix();
grpc_arg arg0;
arg0.type = GRPC_ARG_STRING;
arg0.key = const_cast<char*>(GRPC_ARG_PRIMARY_USER_AGENT_STRING);
arg0.value.string = const_cast<char*>(whole_prefix.c_str());
channel_args_.SetUserAgentPrefix(prefix);
EXPECT_TRUE(HasArg(arg0));
}
} // namespace testing
} // namespace grpc
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|