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/ip.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/iomgr/socket_mutator.h"
36 #include "test/core/util/test_config.h"
38 struct test_socket_mutator {
39 grpc_socket_mutator base;
43 static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
45 socklen_t intlen = sizeof(newval);
46 struct test_socket_mutator* m =
47 reinterpret_cast<struct test_socket_mutator*>(mutator);
49 if (0 != setsockopt(fd, IPPROTO_IP, IP_TOS, &m->option_value,
50 sizeof(m->option_value))) {
53 if (0 != getsockopt(fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
56 if (newval != m->option_value) {
62 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
63 struct test_socket_mutator* m =
64 reinterpret_cast<struct test_socket_mutator*>(mutator);
68 static int compare_test_mutator(grpc_socket_mutator* a,
69 grpc_socket_mutator* b) {
70 struct test_socket_mutator* ma =
71 reinterpret_cast<struct test_socket_mutator*>(a);
72 struct test_socket_mutator* mb =
73 reinterpret_cast<struct test_socket_mutator*>(b);
74 return GPR_ICMP(ma->option_value, mb->option_value);
77 static const grpc_socket_mutator_vtable mutator_vtable = {
78 mutate_fd, compare_test_mutator, destroy_test_mutator};
80 int main(int argc, char** argv) {
83 grpc::testing::TestEnvironment env(argc, argv);
85 sock = socket(PF_INET, SOCK_STREAM, 0);
88 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
89 grpc_set_socket_nonblocking(sock, 1)));
90 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_nonblocking",
91 grpc_set_socket_nonblocking(sock, 0)));
92 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
93 grpc_set_socket_cloexec(sock, 1)));
94 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_cloexec",
95 grpc_set_socket_cloexec(sock, 0)));
96 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
97 grpc_set_socket_reuse_addr(sock, 1)));
98 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_reuse_addr",
99 grpc_set_socket_reuse_addr(sock, 0)));
100 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_low_latency",
101 grpc_set_socket_low_latency(sock, 1)));
102 GPR_ASSERT(GRPC_LOG_IF_ERROR("set_socket_low_latency",
103 grpc_set_socket_low_latency(sock, 0)));
105 struct test_socket_mutator mutator;
106 grpc_socket_mutator_init(&mutator.base, &mutator_vtable);
108 mutator.option_value = IPTOS_LOWDELAY;
109 GPR_ASSERT(GRPC_LOG_IF_ERROR(
110 "set_socket_with_mutator",
111 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
113 mutator.option_value = IPTOS_THROUGHPUT;
114 GPR_ASSERT(GRPC_LOG_IF_ERROR(
115 "set_socket_with_mutator",
116 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
118 mutator.option_value = IPTOS_RELIABILITY;
119 GPR_ASSERT(GRPC_LOG_IF_ERROR(
120 "set_socket_with_mutator",
121 grpc_set_socket_with_mutator(sock, (grpc_socket_mutator*)&mutator)));
123 mutator.option_value = -1;
124 err = grpc_set_socket_with_mutator(
125 sock, reinterpret_cast<grpc_socket_mutator*>(&mutator));
126 GPR_ASSERT(err != GRPC_ERROR_NONE);
127 GRPC_ERROR_UNREF(err);
134 #else /* GRPC_POSIX_SOCKET */
136 int main(int argc, char** argv) { return 1; }
138 #endif /* GRPC_POSIX_SOCKET */