socket_utils_test.cc
Go to the documentation of this file.
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 
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 <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/sync.h>
32 
37 
38 struct test_socket_mutator {
40  int option_value;
41 };
42 
43 static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
44  int newval;
45  socklen_t intlen = sizeof(newval);
46  struct test_socket_mutator* m =
47  reinterpret_cast<struct test_socket_mutator*>(mutator);
48 
49  if (0 != setsockopt(fd, IPPROTO_IP, IP_TOS, &m->option_value,
50  sizeof(m->option_value))) {
51  return false;
52  }
53  if (0 != getsockopt(fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
54  return false;
55  }
56  if (newval != m->option_value) {
57  return false;
58  }
59  return true;
60 }
61 
62 static bool mutate_fd_2(const grpc_mutate_socket_info* info,
63  grpc_socket_mutator* mutator) {
64  int newval;
65  socklen_t intlen = sizeof(newval);
66  struct test_socket_mutator* m =
67  reinterpret_cast<struct test_socket_mutator*>(mutator);
68 
69  if (0 != setsockopt(info->fd, IPPROTO_IP, IP_TOS, &m->option_value,
70  sizeof(m->option_value))) {
71  return false;
72  }
73  if (0 != getsockopt(info->fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
74  return false;
75  }
76  if (newval != m->option_value) {
77  return false;
78  }
79  return true;
80 }
81 
82 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
83  struct test_socket_mutator* m =
84  reinterpret_cast<struct test_socket_mutator*>(mutator);
85  gpr_free(m);
86 }
87 
88 static int compare_test_mutator(grpc_socket_mutator* a,
90  struct test_socket_mutator* ma =
91  reinterpret_cast<struct test_socket_mutator*>(a);
92  struct test_socket_mutator* mb =
93  reinterpret_cast<struct test_socket_mutator*>(b);
94  return grpc_core::QsortCompare(ma->option_value, mb->option_value);
95 }
96 
97 static const grpc_socket_mutator_vtable mutator_vtable = {
98  mutate_fd, compare_test_mutator, destroy_test_mutator, nullptr};
99 
100 static const grpc_socket_mutator_vtable mutator_vtable2 = {
101  nullptr, compare_test_mutator, destroy_test_mutator, mutate_fd_2};
102 
103 static void test_with_vtable(const grpc_socket_mutator_vtable* vtable) {
104  int sock = socket(PF_INET, SOCK_STREAM, 0);
105  GPR_ASSERT(sock > 0);
106 
107  struct test_socket_mutator mutator;
108  grpc_socket_mutator_init(&mutator.base, vtable);
109 
110  mutator.option_value = IPTOS_LOWDELAY;
112  "set_socket_with_mutator",
114  (grpc_socket_mutator*)&mutator)));
115 
116  mutator.option_value = IPTOS_THROUGHPUT;
118  "set_socket_with_mutator",
120  (grpc_socket_mutator*)&mutator)));
121 
122  mutator.option_value = IPTOS_RELIABILITY;
124  "set_socket_with_mutator",
126  (grpc_socket_mutator*)&mutator)));
127 
128  mutator.option_value = -1;
131  reinterpret_cast<grpc_socket_mutator*>(&mutator));
134 }
135 
136 int main(int argc, char** argv) {
137  int sock;
138  grpc::testing::TestEnvironment env(&argc, argv);
139 
140  sock = socket(PF_INET, SOCK_STREAM, 0);
141  GPR_ASSERT(sock > 0);
142 
143  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
144  grpc_set_socket_nonblocking(sock, 1)));
145  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
146  grpc_set_socket_nonblocking(sock, 0)));
147  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
148  grpc_set_socket_cloexec(sock, 1)));
149  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
150  grpc_set_socket_cloexec(sock, 0)));
151  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
152  grpc_set_socket_reuse_addr(sock, 1)));
153  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
154  grpc_set_socket_reuse_addr(sock, 0)));
155  GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_low_latency",
156  grpc_set_socket_low_latency(sock, 1)));
157  GPR_ASSERT(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  return 0;
166 }
167 
168 #else /* GRPC_POSIX_SOCKET_UTILS_COMMON */
169 
170 int main(int argc, char** argv) { return 1; }
171 
172 #endif /* GRPC_POSIX_SOCKET_UTILS_COMMON */
grpc_set_socket_reuse_addr
grpc_error_handle grpc_set_socket_reuse_addr(int fd, int reuse)
vtable
static const grpc_transport_vtable vtable
Definition: binder_transport.cc:680
log.h
generate.env
env
Definition: generate.py:37
grpc_set_socket_low_latency
grpc_error_handle grpc_set_socket_low_latency(int fd, int low_latency)
grpc_socket_mutator_init
void grpc_socket_mutator_init(grpc_socket_mutator *mutator, const grpc_socket_mutator_vtable *vtable)
Definition: socket_mutator.cc:30
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
error_ref_leak.err
err
Definition: error_ref_leak.py:35
main
int main(int argc, char **argv)
Definition: socket_utils_test.cc:170
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
grpc_mutate_socket_info::fd
int fd
Definition: socket_mutator.h:42
grpc_set_socket_cloexec
grpc_error_handle grpc_set_socket_cloexec(int fd, int close_on_exec)
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
close
#define close
Definition: test-fs.c:48
grpc_socket_mutator_vtable
Definition: socket_mutator.h:48
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_socket_mutator
Definition: socket_mutator.h:62
grpc_set_socket_with_mutator
grpc_error_handle grpc_set_socket_with_mutator(int fd, grpc_fd_usage usage, grpc_socket_mutator *mutator)
test_config.h
grpc_mutate_socket_info
Definition: socket_mutator.h:40
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
port.h
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
socket_utils_posix.h
GRPC_FD_CLIENT_CONNECTION_USAGE
@ GRPC_FD_CLIENT_CONNECTION_USAGE
Definition: socket_mutator.h:32
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
test_server.socket
socket
Definition: test_server.py:65
grpc_set_socket_nonblocking
grpc_error_handle grpc_set_socket_nonblocking(int fd, int non_blocking)
regress.m
m
Definition: regress/regress.py:25
socket_mutator.h
sync.h
errno.h
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13