Debian Codegraph @

test/core/iomgr/socket_utils_test.cc

raw
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 "src/core/lib/iomgr/port.h"
20
21 // This test won't work except with posix sockets enabled
22 #ifdef GRPC_POSIX_SOCKET_UTILS_COMMON
23
24 #include <errno.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <string.h>
28
29 #include <gtest/gtest.h>
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/iomgr/socket_mutator.h"
37 #include "src/core/lib/iomgr/socket_utils_posix.h"
38 #include "test/core/util/test_config.h"
39
40 struct test_socket_mutator {
41 grpc_socket_mutator base;
42 int option_value;
43 };
44
45 static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
46 int newval;
47 socklen_t intlen = sizeof(newval);
48 struct test_socket_mutator* m =
49 reinterpret_cast<struct test_socket_mutator*>(mutator);
50
51 if (0 != setsockopt(fd, IPPROTO_IP, IP_TOS, &m->option_value,
52 sizeof(m->option_value))) {
53 return false;
54 }
55 if (0 != getsockopt(fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
56 return false;
57 }
58 if (newval != m->option_value) {
59 return false;
60 }
61 return true;
62 }
63
64 static bool mutate_fd_2(const grpc_mutate_socket_info* info,
65 grpc_socket_mutator* mutator) {
66 int newval;
67 socklen_t intlen = sizeof(newval);
68 struct test_socket_mutator* m =
69 reinterpret_cast<struct test_socket_mutator*>(mutator);
70
71 if (0 != setsockopt(info->fd, IPPROTO_IP, IP_TOS, &m->option_value,
72 sizeof(m->option_value))) {
73 return false;
74 }
75 if (0 != getsockopt(info->fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
76 return false;
77 }
78 if (newval != m->option_value) {
79 return false;
80 }
81 return true;
82 }
83
84 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
85 struct test_socket_mutator* m =
86 reinterpret_cast<struct test_socket_mutator*>(mutator);
87 gpr_free(m);
88 }
89
90 static int compare_test_mutator(grpc_socket_mutator* a,
91 grpc_socket_mutator* b) {
92 struct test_socket_mutator* ma =
93 reinterpret_cast<struct test_socket_mutator*>(a);
94 struct test_socket_mutator* mb =
95 reinterpret_cast<struct test_socket_mutator*>(b);
96 return grpc_core::QsortCompare(ma->option_value, mb->option_value);
97 }
98
99 static const grpc_socket_mutator_vtable mutator_vtable = {
100 mutate_fd, compare_test_mutator, destroy_test_mutator, nullptr};
101
102 static const grpc_socket_mutator_vtable mutator_vtable2 = {
103 nullptr, compare_test_mutator, destroy_test_mutator, mutate_fd_2};
104
105 static void test_with_vtable(const grpc_socket_mutator_vtable* vtable) {
106 int sock = socket(PF_INET, SOCK_STREAM, 0);
107 ASSERT_GT(sock, 0);
108
109 struct test_socket_mutator mutator;
110 grpc_socket_mutator_init(&mutator.base, vtable);
111
112 mutator.option_value = IPTOS_LOWDELAY;
113 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
114 "set_socket_with_mutator",
115 grpc_set_socket_with_mutator(sock, GRPC_FD_CLIENT_CONNECTION_USAGE,
116 (grpc_socket_mutator*)&mutator)));
117
118 mutator.option_value = IPTOS_THROUGHPUT;
119 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
120 "set_socket_with_mutator",
121 grpc_set_socket_with_mutator(sock, GRPC_FD_CLIENT_CONNECTION_USAGE,
122 (grpc_socket_mutator*)&mutator)));
123
124 mutator.option_value = IPTOS_RELIABILITY;
125 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
126 "set_socket_with_mutator",
127 grpc_set_socket_with_mutator(sock, GRPC_FD_CLIENT_CONNECTION_USAGE,
128 (grpc_socket_mutator*)&mutator)));
129
130 mutator.option_value = -1;
131 auto err = grpc_set_socket_with_mutator(
132 sock, GRPC_FD_CLIENT_CONNECTION_USAGE,
133 reinterpret_cast<grpc_socket_mutator*>(&mutator));
134 ASSERT_FALSE(err.ok());
135 }
136
137 TEST(SocketUtilsTest, MainTest) {
138 int sock;
139
140 sock = socket(PF_INET, SOCK_STREAM, 0);
141 ASSERT_GT(sock, 0);
142
143 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
144 grpc_set_socket_nonblocking(sock, 1)));
145 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
146 grpc_set_socket_nonblocking(sock, 0)));
147 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_cloexec",
148 grpc_set_socket_cloexec(sock, 1)));
149 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_cloexec",
150 grpc_set_socket_cloexec(sock, 0)));
151 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
152 grpc_set_socket_reuse_addr(sock, 1)));
153 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
154 grpc_set_socket_reuse_addr(sock, 0)));
155 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_low_latency",
156 grpc_set_socket_low_latency(sock, 1)));
157 ASSERT_TRUE(GRPC_LOG_IF_ERROR("set_socket_low_latency",
158 grpc_set_socket_low_latency(sock, 0)));
159
160 test_with_vtable(&mutator_vtable);
161 test_with_vtable(&mutator_vtable2);
162
163 close(sock);
164 }
165
166 int main(int argc, char** argv) {
167 grpc::testing::TestEnvironment env(&argc, argv);
168 ::testing::InitGoogleTest(&argc, argv);
169 return RUN_ALL_TESTS();
170 }
171
172 #else /* GRPC_POSIX_SOCKET_UTILS_COMMON */
173
174 int main(int argc, char** argv) { return 1; }
175
176 #endif /* GRPC_POSIX_SOCKET_UTILS_COMMON */