1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <arpa/inet.h>
#include <linux/capability.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <cstdio>
#include <cstring>
#include <map>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "test/syscalls/linux/ip_socket_test_util.h"
#include "test/syscalls/linux/socket_bind_to_device_util.h"
#include "test/syscalls/linux/socket_test_util.h"
#include "test/util/capability_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
namespace gvisor {
namespace testing {
using std::string;
using std::vector;
// Test fixture for SO_BINDTODEVICE tests the results of sequences of socket
// binding.
class BindToDeviceSequenceTest : public ::testing::TestWithParam<SocketKind> {
protected:
void SetUp() override {
printf("Testing case: %s\n", GetParam().description.c_str());
ASSERT_TRUE(ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW)))
<< "CAP_NET_RAW is required to use SO_BINDTODEVICE";
socket_factory_ = GetParam();
interface_names_ = GetInterfaceNames();
}
PosixErrorOr<std::unique_ptr<FileDescriptor>> NewSocket() const {
return socket_factory_.Create();
}
// Gets a device by device_id. If the device_id has been seen before, returns
// the previously returned device. If not, finds or creates a new device.
// Returns an empty string on failure.
void GetDevice(int device_id, string *device_name) {
auto device = devices_.find(device_id);
if (device != devices_.end()) {
*device_name = device->second;
return;
}
// Need to pick a new device. Try ethernet first.
*device_name = absl::StrCat("eth", next_unused_eth_);
if (interface_names_.find(*device_name) != interface_names_.end()) {
devices_[device_id] = *device_name;
next_unused_eth_++;
return;
}
// Need to make a new tunnel device. gVisor tests should have enough
// ethernet devices to never reach here.
ASSERT_FALSE(IsRunningOnGvisor());
// Need a tunnel.
tunnels_.push_back(ASSERT_NO_ERRNO_AND_VALUE(Tunnel::New()));
devices_[device_id] = tunnels_.back()->GetName();
*device_name = devices_[device_id];
}
// Release the socket
void ReleaseSocket(int socket_id) {
// Close the socket that was made in a previous action. The socket_id
// indicates which socket to close based on index into the list of actions.
sockets_to_close_.erase(socket_id);
}
// SetDevice changes the bind_to_device option. It does not bind or re-bind.
void SetDevice(int socket_id, int device_id) {
auto socket_fd = sockets_to_close_[socket_id]->get();
string device_name;
ASSERT_NO_FATAL_FAILURE(GetDevice(device_id, &device_name));
EXPECT_THAT(setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE,
device_name.c_str(), device_name.size() + 1),
SyscallSucceedsWithValue(0));
}
// Bind a socket with the reuse options and bind_to_device options. Checks
// that all steps succeed and that the bind command's error matches want.
// Sets the socket_id to uniquely identify the socket bound if it is not
// nullptr.
void BindSocket(bool reuse_port, bool reuse_addr, int device_id = 0,
int want = 0, int *socket_id = nullptr) {
next_socket_id_++;
sockets_to_close_[next_socket_id_] = ASSERT_NO_ERRNO_AND_VALUE(NewSocket());
auto socket_fd = sockets_to_close_[next_socket_id_]->get();
if (socket_id != nullptr) {
*socket_id = next_socket_id_;
}
// If reuse_port is indicated, do that.
if (reuse_port) {
EXPECT_THAT(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEPORT, &kSockOptOn,
sizeof(kSockOptOn)),
SyscallSucceedsWithValue(0));
}
// If reuse_addr is indicated, do that.
if (reuse_addr) {
EXPECT_THAT(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &kSockOptOn,
sizeof(kSockOptOn)),
SyscallSucceedsWithValue(0));
}
// If the device is non-zero, bind to that device.
if (device_id != 0) {
string device_name;
ASSERT_NO_FATAL_FAILURE(GetDevice(device_id, &device_name));
EXPECT_THAT(setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE,
device_name.c_str(), device_name.size() + 1),
SyscallSucceedsWithValue(0));
char get_device[100];
socklen_t get_device_size = 100;
EXPECT_THAT(getsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, get_device,
&get_device_size),
SyscallSucceedsWithValue(0));
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = port_;
if (want == 0) {
ASSERT_THAT(
bind(socket_fd, reinterpret_cast<const struct sockaddr *>(&addr),
sizeof(addr)),
SyscallSucceeds());
} else {
ASSERT_THAT(
bind(socket_fd, reinterpret_cast<const struct sockaddr *>(&addr),
sizeof(addr)),
SyscallFailsWithErrno(want));
}
if (port_ == 0) {
// We don't yet know what port we'll be using so we need to fetch it and
// remember it for future commands.
socklen_t addr_size = sizeof(addr);
ASSERT_THAT(
getsockname(socket_fd, reinterpret_cast<struct sockaddr *>(&addr),
&addr_size),
SyscallSucceeds());
port_ = addr.sin_port;
}
}
private:
SocketKind socket_factory_;
// devices maps from the device id in the test case to the name of the device.
std::unordered_map<int, string> devices_;
// These are the tunnels that were created for the test and will be destroyed
// by the destructor.
vector<std::unique_ptr<Tunnel>> tunnels_;
// A list of all interface names before the test started.
std::unordered_set<string> interface_names_;
// The next ethernet device to use when requested a device.
int next_unused_eth_ = 1;
// The port for all tests. Originally 0 (any) and later set to the port that
// all further commands will use.
in_port_t port_ = 0;
// sockets_to_close_ is a map from action index to the socket that was
// created.
std::unordered_map<int,
std::unique_ptr<gvisor::testing::FileDescriptor>>
sockets_to_close_;
int next_socket_id_ = 0;
};
TEST_P(BindToDeviceSequenceTest, BindTwiceWithDeviceFails) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ false, /* bind_to_device */ 3));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 3, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindToDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ false, /* bind_to_device */ 1));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ false, /* bind_to_device */ 2));
}
TEST_P(BindToDeviceSequenceTest, BindToDeviceAndThenWithoutDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindWithoutDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindWithDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 456, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 789, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindWithReuse) {
ASSERT_NO_FATAL_FAILURE(
BindSocket(/* reusePort */ true, /* reuse_addr */ false));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false,
/* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 0));
}
TEST_P(BindToDeviceSequenceTest, BindingWithReuseAndDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 456));
ASSERT_NO_FATAL_FAILURE(
BindSocket(/* reuse_port */ true, /* reuse_addr */ false));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 789));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 999, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, MixingReuseAndNotReuseByBindingToDevice) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 123, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 456, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 789, 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 999, 0));
}
TEST_P(BindToDeviceSequenceTest, CannotBindTo0AfterMixingReuseAndNotReuse) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 456));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindAndRelease) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 123));
int to_release;
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, 0, &to_release));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 345, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 789));
// Release the bind to device 0 and try again.
ASSERT_NO_FATAL_FAILURE(ReleaseSocket(to_release));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 345));
}
TEST_P(BindToDeviceSequenceTest, BindTwiceWithReuseOnce) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindWithReuseAddr) {
// FIXME(b/129164367): Support SO_REUSEADDR on UDP sockets.
SKIP_IF(IsRunningOnGvisor());
ASSERT_NO_FATAL_FAILURE(
BindSocket(/* reusePort */ false, /* reuse_addr */ true));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 123, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ true, /* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ true, /* bind_to_device */ 0));
}
TEST_P(BindToDeviceSequenceTest,
CannotBindTo0AfterMixingReuseAddrAndNotReuseAddr) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 123));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 456));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindReuseAddrReusePortThenReusePort) {
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindReuseAddrReusePortThenReuseAddr) {
// FIXME(b/129164367): Support SO_REUSEADDR on UDP sockets.
SKIP_IF(IsRunningOnGvisor());
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindDoubleReuseAddrReusePortThenReusePort) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ true, /* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindDoubleReuseAddrReusePortThenReuseAddr) {
// FIXME(b/129164367): Support SO_REUSEADDR on UDP sockets.
SKIP_IF(IsRunningOnGvisor());
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ true, /* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindReusePortThenReuseAddrReusePort) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ true, /* reuse_addr */ false, /* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ true,
/* bind_to_device */ 0, EADDRINUSE));
}
TEST_P(BindToDeviceSequenceTest, BindReuseAddrThenReuseAddr) {
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ true, /* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0, EADDRINUSE));
}
// This behavior seems like a bug?
TEST_P(BindToDeviceSequenceTest,
BindReuseAddrThenReuseAddrReusePortThenReuseAddr) {
// FIXME(b/129164367): Support SO_REUSEADDR on UDP sockets.
SKIP_IF(IsRunningOnGvisor());
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ true, /* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ true,
/* bind_to_device */ 0));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ true,
/* reuse_addr */ false,
/* bind_to_device */ 0));
}
// Repro test for gvisor.dev/issue/1217. Not replicated in ports_test.go as this
// test is different from the others and wouldn't fit well there.
TEST_P(BindToDeviceSequenceTest, BindAndReleaseDifferentDevice) {
int to_release;
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 3, 0, &to_release));
ASSERT_NO_FATAL_FAILURE(BindSocket(/* reuse_port */ false,
/* reuse_addr */ false,
/* bind_to_device */ 3, EADDRINUSE));
// Change the device. Since the socket was already bound, this should have no
// effect.
SetDevice(to_release, 2);
// Release the bind to device 3 and try again.
ASSERT_NO_FATAL_FAILURE(ReleaseSocket(to_release));
ASSERT_NO_FATAL_FAILURE(BindSocket(
/* reuse_port */ false, /* reuse_addr */ false, /* bind_to_device */ 3));
}
INSTANTIATE_TEST_SUITE_P(BindToDeviceTest, BindToDeviceSequenceTest,
::testing::Values(IPv4UDPUnboundSocket(0),
IPv4TCPUnboundSocket(0)));
} // namespace testing
} // namespace gvisor
|