| 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 |
#include <grpcpp/grpcpp.h> |
| 23 |
#include <grpcpp/support/channel_arguments.h> |
| 24 |
|
| 25 |
#include "src/core/lib/gpr/useful.h" |
| 26 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 27 |
#include "src/core/lib/iomgr/socket_mutator.h" |
| 28 |
#include "test/core/util/test_config.h" |
| 29 |
|
| 30 |
namespace grpc { |
| 31 |
namespace testing { |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
// A simple grpc_socket_mutator to be used to test SetSocketMutator |
| 36 |
class TestSocketMutator : public grpc_socket_mutator { |
| 37 |
public: |
| 38 |
TestSocketMutator(); |
| 39 |
|
| 40 |
bool MutateFd(int /*fd*/) { |
| 41 |
// Do nothing on the fd |
| 42 |
return true; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
// |
| 47 |
// C API for TestSocketMutator |
| 48 |
// |
| 49 |
|
| 50 |
bool test_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) { |
| 51 |
TestSocketMutator* tsm = reinterpret_cast<TestSocketMutator*>(mutator); |
| 52 |
return tsm->MutateFd(fd); |
| 53 |
} |
| 54 |
|
| 55 |
int test_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) { |
| 56 |
return grpc_core::QsortCompare(a, b); |
| 57 |
} |
| 58 |
|
| 59 |
void test_mutator_destroy(grpc_socket_mutator* mutator) { |
| 60 |
TestSocketMutator* tsm = reinterpret_cast<TestSocketMutator*>(mutator); |
| 61 |
delete tsm; |
| 62 |
} |
| 63 |
|
| 64 |
grpc_socket_mutator_vtable test_mutator_vtable = { |
| 65 |
test_mutator_mutate_fd, test_mutator_compare, test_mutator_destroy, |
| 66 |
nullptr}; |
| 67 |
|
| 68 |
// |
| 69 |
// TestSocketMutator implementation |
| 70 |
// |
| 71 |
|
| 72 |
TestSocketMutator::TestSocketMutator() { |
| 73 |
grpc_socket_mutator_init(this, &test_mutator_vtable); |
| 74 |
} |
| 75 |
} // namespace |
| 76 |
|
| 77 |
class ChannelArgumentsTest : public ::testing::Test { |
| 78 |
protected: |
| 79 |
ChannelArgumentsTest() |
| 80 |
: pointer_vtable_({&ChannelArguments::PointerVtableMembers::Copy, |
| 81 |
&ChannelArguments::PointerVtableMembers::Destroy, |
| 82 |
&ChannelArguments::PointerVtableMembers::Compare}) {} |
| 83 |
|
| 84 |
void SetChannelArgs(const ChannelArguments& channel_args, |
| 85 |
grpc_channel_args* args) { |
| 86 |
channel_args.SetChannelArgs(args); |
| 87 |
} |
| 88 |
|
| 89 |
static void SetUpTestCase() { grpc_init(); } |
| 90 |
|
| 91 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 92 |
|
| 93 |
std::string GetDefaultUserAgentPrefix() { |
| 94 |
std::ostringstream user_agent_prefix; |
| 95 |
user_agent_prefix << "grpc-c++/" << Version(); |
| 96 |
return user_agent_prefix.str(); |
| 97 |
} |
| 98 |
|
| 99 |
void VerifyDefaultChannelArgs() { |
| 100 |
grpc_channel_args args; |
| 101 |
SetChannelArgs(channel_args_, &args); |
| 102 |
EXPECT_EQ(static_cast<size_t>(1), args.num_args); |
| 103 |
EXPECT_STREQ(GRPC_ARG_PRIMARY_USER_AGENT_STRING, args.args[0].key); |
| 104 |
EXPECT_EQ(GetDefaultUserAgentPrefix(), |
| 105 |
std::string(args.args[0].value.string)); |
| 106 |
} |
| 107 |
|
| 108 |
bool HasArg(grpc_arg expected_arg) { |
| 109 |
grpc_channel_args args; |
| 110 |
SetChannelArgs(channel_args_, &args); |
| 111 |
for (size_t i = 0; i < args.num_args; i++) { |
| 112 |
const grpc_arg& arg = args.args[i]; |
| 113 |
if (arg.type == expected_arg.type && |
| 114 |
std::string(arg.key) == expected_arg.key) { |
| 115 |
if (arg.type == GRPC_ARG_INTEGER) { |
| 116 |
return arg.value.integer == expected_arg.value.integer; |
| 117 |
} else if (arg.type == GRPC_ARG_STRING) { |
| 118 |
return std::string(arg.value.string) == expected_arg.value.string; |
| 119 |
} else if (arg.type == GRPC_ARG_POINTER) { |
| 120 |
return arg.value.pointer.p == expected_arg.value.pointer.p && |
| 121 |
arg.value.pointer.vtable->copy == |
| 122 |
expected_arg.value.pointer.vtable->copy && |
| 123 |
arg.value.pointer.vtable->destroy == |
| 124 |
expected_arg.value.pointer.vtable->destroy; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
grpc_arg_pointer_vtable pointer_vtable_; |
| 131 |
ChannelArguments channel_args_; |
| 132 |
}; |
| 133 |
|
| 134 |
TEST_F(ChannelArgumentsTest, SetInt) { |
| 135 |
VerifyDefaultChannelArgs(); |
| 136 |
std::string key0("key0"); |
| 137 |
grpc_arg arg0; |
| 138 |
arg0.type = GRPC_ARG_INTEGER; |
| 139 |
arg0.key = const_cast<char*>(key0.c_str()); |
| 140 |
arg0.value.integer = 0; |
| 141 |
std::string key1("key1"); |
| 142 |
grpc_arg arg1; |
| 143 |
arg1.type = GRPC_ARG_INTEGER; |
| 144 |
arg1.key = const_cast<char*>(key1.c_str()); |
| 145 |
arg1.value.integer = 1; |
| 146 |
|
| 147 |
std::string arg_key0(key0); |
| 148 |
channel_args_.SetInt(arg_key0, arg0.value.integer); |
| 149 |
// Clear key early to make sure channel_args takes a copy |
| 150 |
arg_key0.clear(); |
| 151 |
EXPECT_TRUE(HasArg(arg0)); |
| 152 |
|
| 153 |
std::string arg_key1(key1); |
| 154 |
channel_args_.SetInt(arg_key1, arg1.value.integer); |
| 155 |
arg_key1.clear(); |
| 156 |
EXPECT_TRUE(HasArg(arg0)); |
| 157 |
EXPECT_TRUE(HasArg(arg1)); |
| 158 |
} |
| 159 |
|
| 160 |
TEST_F(ChannelArgumentsTest, SetString) { |
| 161 |
VerifyDefaultChannelArgs(); |
| 162 |
std::string key0("key0"); |
| 163 |
std::string val0("val0"); |
| 164 |
grpc_arg arg0; |
| 165 |
arg0.type = GRPC_ARG_STRING; |
| 166 |
arg0.key = const_cast<char*>(key0.c_str()); |
| 167 |
arg0.value.string = const_cast<char*>(val0.c_str()); |
| 168 |
std::string key1("key1"); |
| 169 |
std::string val1("val1"); |
| 170 |
grpc_arg arg1; |
| 171 |
arg1.type = GRPC_ARG_STRING; |
| 172 |
arg1.key = const_cast<char*>(key1.c_str()); |
| 173 |
arg1.value.string = const_cast<char*>(val1.c_str()); |
| 174 |
|
| 175 |
std::string key(key0); |
| 176 |
std::string val(val0); |
| 177 |
channel_args_.SetString(key, val); |
| 178 |
// Clear key/val early to make sure channel_args takes a copy |
| 179 |
key = ""; |
| 180 |
val = ""; |
| 181 |
EXPECT_TRUE(HasArg(arg0)); |
| 182 |
|
| 183 |
key = key1; |
| 184 |
val = val1; |
| 185 |
channel_args_.SetString(key, val); |
| 186 |
// Clear key/val early to make sure channel_args takes a copy |
| 187 |
key = ""; |
| 188 |
val = ""; |
| 189 |
EXPECT_TRUE(HasArg(arg0)); |
| 190 |
EXPECT_TRUE(HasArg(arg1)); |
| 191 |
} |
| 192 |
|
| 193 |
TEST_F(ChannelArgumentsTest, SetPointer) { |
| 194 |
VerifyDefaultChannelArgs(); |
| 195 |
std::string key0("key0"); |
| 196 |
grpc_arg arg0; |
| 197 |
arg0.type = GRPC_ARG_POINTER; |
| 198 |
arg0.key = const_cast<char*>(key0.c_str()); |
| 199 |
arg0.value.pointer.p = &key0; |
| 200 |
arg0.value.pointer.vtable = &pointer_vtable_; |
| 201 |
|
| 202 |
std::string key(key0); |
| 203 |
channel_args_.SetPointer(key, arg0.value.pointer.p); |
| 204 |
EXPECT_TRUE(HasArg(arg0)); |
| 205 |
} |
| 206 |
|
| 207 |
TEST_F(ChannelArgumentsTest, SetSocketMutator) { |
| 208 |
VerifyDefaultChannelArgs(); |
| 209 |
grpc_arg arg0, arg1; |
| 210 |
TestSocketMutator* mutator0 = new TestSocketMutator(); |
| 211 |
TestSocketMutator* mutator1 = new TestSocketMutator(); |
| 212 |
arg0 = grpc_socket_mutator_to_arg(mutator0); |
| 213 |
arg1 = grpc_socket_mutator_to_arg(mutator1); |
| 214 |
|
| 215 |
channel_args_.SetSocketMutator(mutator0); |
| 216 |
EXPECT_TRUE(HasArg(arg0)); |
| 217 |
|
| 218 |
// Exercise the copy constructor because we ran some sanity checks in it. |
| 219 |
grpc::ChannelArguments new_args{channel_args_}; |
| 220 |
|
| 221 |
channel_args_.SetSocketMutator(mutator1); |
| 222 |
EXPECT_TRUE(HasArg(arg1)); |
| 223 |
// arg0 is replaced by arg1 |
| 224 |
EXPECT_FALSE(HasArg(arg0)); |
| 225 |
} |
| 226 |
|
| 227 |
TEST_F(ChannelArgumentsTest, SetUserAgentPrefix) { |
| 228 |
VerifyDefaultChannelArgs(); |
| 229 |
std::string prefix("prefix"); |
| 230 |
std::string whole_prefix = prefix + " " + GetDefaultUserAgentPrefix(); |
| 231 |
grpc_arg arg0; |
| 232 |
arg0.type = GRPC_ARG_STRING; |
| 233 |
arg0.key = const_cast<char*>(GRPC_ARG_PRIMARY_USER_AGENT_STRING); |
| 234 |
arg0.value.string = const_cast<char*>(whole_prefix.c_str()); |
| 235 |
|
| 236 |
channel_args_.SetUserAgentPrefix(prefix); |
| 237 |
EXPECT_TRUE(HasArg(arg0)); |
| 238 |
|
| 239 |
// Test if the user agent string is copied correctly |
| 240 |
ChannelArguments new_channel_args(channel_args_); |
| 241 |
grpc_channel_args args; |
| 242 |
SetChannelArgs(new_channel_args, &args); |
| 243 |
bool found = false; |
| 244 |
for (size_t i = 0; i < args.num_args; i++) { |
| 245 |
const grpc_arg& arg = args.args[i]; |
| 246 |
if (arg.type == GRPC_ARG_STRING && |
| 247 |
std::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) { |
| 248 |
EXPECT_FALSE(found); |
| 249 |
EXPECT_EQ(0, strcmp(arg.value.string, arg0.value.string)); |
| 250 |
found = true; |
| 251 |
} |
| 252 |
} |
| 253 |
EXPECT_TRUE(found); |
| 254 |
} |
| 255 |
|
| 256 |
} // namespace testing |
| 257 |
} // namespace grpc |
| 258 |
|
| 259 |
int main(int argc, char** argv) { |
| 260 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 261 |
::testing::InitGoogleTest(&argc, argv); |
| 262 |
int ret = RUN_ALL_TESTS(); |
| 263 |
return ret; |
| 264 |
} |