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
23
24 #include "src/core/lib/iomgr/socket_utils_posix.h"
25
26 #include <errno.h>
27 #include <netinet/ip.h>
28 #include <string.h>
29
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
33
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"
37
38 struct test_socket_mutator {
39 grpc_socket_mutator base;
40 int option_value;
41 };
42
mutate_fd(int fd,grpc_socket_mutator * mutator)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
destroy_test_mutator(grpc_socket_mutator * mutator)62 static void destroy_test_mutator(grpc_socket_mutator* mutator) {
63 struct test_socket_mutator* m =
64 reinterpret_cast<struct test_socket_mutator*>(mutator);
65 gpr_free(m);
66 }
67
compare_test_mutator(grpc_socket_mutator * a,grpc_socket_mutator * b)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);
75 }
76
77 static const grpc_socket_mutator_vtable mutator_vtable = {
78 mutate_fd, compare_test_mutator, destroy_test_mutator};
79
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81 int sock;
82 grpc_error* err;
83 grpc_test_init(argc, argv);
84
85 sock = socket(PF_INET, SOCK_STREAM, 0);
86 GPR_ASSERT(sock > 0);
87
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)));
104
105 struct test_socket_mutator mutator;
106 grpc_socket_mutator_init(&mutator.base, &mutator_vtable);
107
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)));
112
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)));
117
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)));
122
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);
128
129 close(sock);
130
131 return 0;
132 }
133
134 #else /* GRPC_POSIX_SOCKET */
135
main(int argc,char ** argv)136 int main(int argc, char** argv) { return 1; }
137
138 #endif /* GRPC_POSIX_SOCKET */
139