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 "src/core/lib/iomgr/socket_utils_posix.h"
25
26 #include <errno.h>
27 #include <netinet/in.h>
28 #include <netinet/ip.h>
29 #include <string.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 "test/core/util/test_config.h"
38
39 struct test_socket_mutator {
40 grpc_socket_mutator base;
41 int option_value;
42 };
43
mutate_fd(int fd,grpc_socket_mutator * mutator)44 static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
45 int newval;
46 socklen_t intlen = sizeof(newval);
47 struct test_socket_mutator* m =
48 reinterpret_cast<struct test_socket_mutator*>(mutator);
49
50 if (0 != setsockopt(fd, IPPROTO_IP, IP_TOS, &m->option_value,
51 sizeof(m->option_value))) {
52 return false;
53 }
54 if (0 != getsockopt(fd, IPPROTO_IP, IP_TOS, &newval, &intlen)) {
55 return false;
56 }
57 if (newval != m->option_value) {
58 return false;
59 }
60 return true;
61 }
62
destroy_test_mutator(grpc_socket_mutator * mutator)63 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
64 struct test_socket_mutator* m =
65 reinterpret_cast<struct test_socket_mutator*>(mutator);
66 gpr_free(m);
67 }
68
compare_test_mutator(grpc_socket_mutator * a,grpc_socket_mutator * b)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);
76 }
77
78 static const grpc_socket_mutator_vtable mutator_vtable = {
79 mutate_fd, compare_test_mutator, destroy_test_mutator};
80
main(int argc,char ** argv)81 int main(int argc, char** argv) {
82 int sock;
83 grpc_error* err;
84 grpc::testing::TestEnvironment env(argc, argv);
85
86 sock = socket(PF_INET, SOCK_STREAM, 0);
87 GPR_ASSERT(sock > 0);
88
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)));
105
106 struct test_socket_mutator mutator;
107 grpc_socket_mutator_init(&mutator.base, &mutator_vtable);
108
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)));
113
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)));
118
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)));
123
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);
129
130 close(sock);
131
132 return 0;
133 }
134
135 #else /* GRPC_POSIX_SOCKET_UTILS_COMMON */
136
main(int argc,char ** argv)137 int main(int argc, char** argv) { return 1; }
138
139 #endif /* GRPC_POSIX_SOCKET_UTILS_COMMON */
140