3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/iomgr/port.h"
21 // This test won't work except with posix sockets enabled
22 #ifdef GRPC_POSIX_SOCKET
24 #include "src/core/lib/iomgr/socket_utils_posix.h"
27 #include <netinet/in.h>
28 #include <netinet/ip.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/iomgr/socket_mutator.h"
37 #include "test/core/util/test_config.h"
39 struct test_socket_mutator {
40 grpc_socket_mutator base;
44 static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
46 socklen_t intlen = sizeof(newval);
47 struct test_socket_mutator* m =
48 reinterpret_cast<struct test_socket_mutator*>(mutator);
50 if (0 != setsockopt(fd, IPPROTO_IP, IP_TOS, &m->option_value,
51 sizeof(m->option_value))) {
54 if (0 != getsockopt(fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
57 if (newval != m->option_value) {
63 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
64 struct test_socket_mutator* m =
65 reinterpret_cast<struct test_socket_mutator*>(mutator);
69 static int compare_test_mutator(grpc_socket_mutator* a,
70 grpc_socket_mutator* b) {
71 struct test_socket_mutator* ma =
72 reinterpret_cast<struct test_socket_mutator*>(a);
73 struct test_socket_mutator* mb =
74 reinterpret_cast<struct test_socket_mutator*>(b);
75 return GPR_ICMP(ma->option_value, mb->option_value);
78 static const grpc_socket_mutator_vtable mutator_vtable = {
79 mutate_fd, compare_test_mutator, destroy_test_mutator};
81 int main(int argc, char** argv) {
84 grpc::testing::TestEnvironment env(argc, argv);
86 sock = socket(PF_INET, SOCK_STREAM, 0);
89 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
90 grpc_set_socket_nonblocking(sock, 1)));
91 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
92 grpc_set_socket_nonblocking(sock, 0)));
93 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
94 grpc_set_socket_cloexec(sock, 1)));
95 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
96 grpc_set_socket_cloexec(sock, 0)));
97 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
98 grpc_set_socket_reuse_addr(sock, 1)));
99 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
100 grpc_set_socket_reuse_addr(sock, 0)));
101 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_low_latency",
102 grpc_set_socket_low_latency(sock, 1)));
103 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_low_latency",
104 grpc_set_socket_low_latency(sock, 0)));
106 struct test_socket_mutator mutator;
107 grpc_socket_mutator_init(&mutator.base, &mutator_vtable);
109 mutator.option_value = IPTOS_LOWDELAY;
110 GPR_ASSERT(GRPC_LOG_IF_ERROR(
111 "set_socket_with_mutator",
112 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
114 mutator.option_value = IPTOS_THROUGHPUT;
115 GPR_ASSERT(GRPC_LOG_IF_ERROR(
116 "set_socket_with_mutator",
117 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
119 mutator.option_value = IPTOS_RELIABILITY;
120 GPR_ASSERT(GRPC_LOG_IF_ERROR(
121 "set_socket_with_mutator",
122 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
124 mutator.option_value = -1;
125 err = grpc_set_socket_with_mutator(
126 sock, reinterpret_cast<grpc_socket_mutator*>(&mutator));
127 GPR_ASSERT(err != GRPC_ERROR_NONE);
128 GRPC_ERROR_UNREF(err);
135 #else /* GRPC_POSIX_SOCKET */
137 int main(int argc, char** argv) { return 1; }
139 #endif /* GRPC_POSIX_SOCKET */