@@ -9264,365 +9264,3 @@ pub const SHADOW_STACK = struct {
9264
9264
/// Set up a restore token in the shadow stack.
9265
9265
pub const SET_TOKEN : u64 = 1 << 0 ;
9266
9266
};
9267
-
9268
- // socket functions not tested:
9269
- // - send (these forward to sendto in the kernel, and Zig doesn't provide bindings for them)
9270
- // - recv (these forward to recvfrom in the kernel, and Zig doesn't provide bindings for them)
9271
- // - accept4 (accept forwards to accept4 in the kernel, and Zig's binding for accept forwards directly to accept4)
9272
-
9273
- test "inet sockets" {
9274
- // socket functions tested:
9275
- // - [x] socket
9276
- // - [x] bind
9277
- // - [x] connect
9278
- // - [x] listen
9279
- // - [x] accept (and by extension accept4)
9280
- // - [x] getsockname
9281
- // - [x] getpeername
9282
- // - [ ] socketpair
9283
- // - [ ] send
9284
- // - [ ] recv
9285
- // - [x] sendto
9286
- // - [x] recvfrom
9287
- // - [x] shutdown
9288
- // - [x] setsockopt
9289
- // - [x] getsockopt
9290
- // - [ ] sendmsg
9291
- // - [ ] recvmsg
9292
- // - [ ] accept4
9293
- // - [ ] recvmmsg
9294
- // - [ ] sendmmsg
9295
-
9296
- // create a socket "fd" to be the server
9297
- var rc = socket (AF .INET , SOCK .STREAM , 0 );
9298
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9299
- const fd : fd_t = @intCast (rc );
9300
-
9301
- const test_sndbuf : c_int = 4096 ;
9302
- rc = setsockopt (fd , SOL .SOCKET , SO .SNDBUF , std .mem .asBytes (& test_sndbuf ).ptr , std .mem .asBytes (& test_sndbuf ).len );
9303
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9304
- try std .testing .expectEqual (0 , rc );
9305
-
9306
- var sndbuf : c_int = undefined ;
9307
- var sndbuf_len : socklen_t = std .mem .asBytes (& sndbuf ).len ;
9308
- rc = getsockopt (fd , SOL .SOCKET , SO .SNDBUF , std .mem .asBytes (& sndbuf ).ptr , & sndbuf_len );
9309
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9310
- try std .testing .expectEqual (0 , rc );
9311
- try std .testing .expectEqual (test_sndbuf * 2 , sndbuf );
9312
-
9313
- var addr : sockaddr.storage = undefined ;
9314
- var len : u32 = @sizeOf (@TypeOf (addr ));
9315
- rc = getsockname (fd , @ptrCast (& addr ), & len );
9316
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9317
- try std .testing .expectEqual (0 , rc );
9318
- try std .testing .expectEqual (@sizeOf (sockaddr .in ), len );
9319
- try std .testing .expectEqual (AF .INET , addr .family );
9320
- const addr_in = @as (* sockaddr .in , @ptrCast (& addr ));
9321
- try std .testing .expectEqual (0 , addr_in .addr );
9322
- try std .testing .expectEqual (0 , addr_in .port );
9323
-
9324
- // bind the socket to a random port
9325
- const INADDR_LOOPBACK = std .mem .nativeToBig (u32 , 0x7F_00_00_01 );
9326
- addr = undefined ;
9327
- addr_in .* = .{
9328
- .addr = INADDR_LOOPBACK ,
9329
- .port = 0 ,
9330
- };
9331
- rc = bind (fd , @ptrCast (& addr ), @sizeOf (sockaddr .in ));
9332
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9333
- try std .testing .expectEqual (0 , rc );
9334
-
9335
- // test getsockname
9336
- addr = undefined ;
9337
- len = @sizeOf (@TypeOf (addr ));
9338
- rc = getsockname (fd , @ptrCast (& addr ), & len );
9339
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9340
- try std .testing .expectEqual (0 , rc );
9341
- try std .testing .expectEqual (@sizeOf (sockaddr .in ), len );
9342
- try std .testing .expectEqual (AF .INET , addr .family );
9343
- try std .testing .expectEqual (INADDR_LOOPBACK , addr_in .addr );
9344
- const bind_port = addr_in .port ;
9345
-
9346
- // listen with a backlog of one so we can establish a connection and accept on the same thread
9347
- rc = listen (fd , 1 );
9348
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9349
- try std .testing .expectEqual (0 , rc );
9350
-
9351
- // create another socket "other_fd" to be the client
9352
- rc = socket (AF .INET , SOCK .STREAM , 0 );
9353
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9354
- const other_fd : fd_t = @intCast (rc );
9355
-
9356
- // connect to other_fd from fd
9357
- addr = undefined ;
9358
- addr_in .* = .{
9359
- .addr = INADDR_LOOPBACK ,
9360
- .port = bind_port ,
9361
- };
9362
- rc = connect (other_fd , @ptrCast (& addr ), @sizeOf (sockaddr .in ));
9363
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9364
- try std .testing .expectEqual (0 , rc );
9365
-
9366
- // check connection sock addr
9367
- addr = undefined ;
9368
- len = @sizeOf (@TypeOf (addr ));
9369
- rc = getsockname (other_fd , @ptrCast (& addr ), & len );
9370
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9371
- try std .testing .expectEqual (0 , rc );
9372
- try std .testing .expectEqual (@sizeOf (sockaddr .in ), len );
9373
- try std .testing .expectEqual (AF .INET , addr .family );
9374
- try std .testing .expectEqual (INADDR_LOOPBACK , addr_in .addr );
9375
- const other_port = addr_in .port ;
9376
-
9377
- // check connection peer addr
9378
- addr = undefined ;
9379
- len = @sizeOf (@TypeOf (addr ));
9380
- rc = getpeername (other_fd , @ptrCast (& addr ), & len );
9381
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9382
- try std .testing .expectEqual (0 , rc );
9383
- try std .testing .expectEqual (@sizeOf (sockaddr .in ), len );
9384
- try std .testing .expectEqual (AF .INET , addr .family );
9385
- try std .testing .expectEqual (INADDR_LOOPBACK , addr_in .addr );
9386
- try std .testing .expectEqual (bind_port , addr_in .port );
9387
-
9388
- // accept the connection to fd from other_fd and check peer addr
9389
- addr = undefined ;
9390
- len = @sizeOf (@TypeOf (addr ));
9391
- rc = accept (fd , @ptrCast (& addr ), & len );
9392
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9393
- try std .testing .expectEqual (AF .INET , addr .family );
9394
- try std .testing .expectEqual (INADDR_LOOPBACK , addr_in .addr );
9395
- try std .testing .expectEqual (other_port , addr_in .port );
9396
- const conn_fd : fd_t = @intCast (rc );
9397
-
9398
- const data = "foo" ;
9399
- rc = sendto (conn_fd , data , data .len , 0 , null , 0 );
9400
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9401
- try std .testing .expectEqual (data .len , rc );
9402
-
9403
- rc = shutdown (fd , SHUT .RDWR );
9404
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9405
- try std .testing .expectEqual (0 , rc );
9406
-
9407
- var recv_data : [data .len ]u8 = undefined ;
9408
- rc = recvfrom (other_fd , & recv_data , recv_data .len , 0 , null , null );
9409
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9410
- try std .testing .expectEqual (recv_data .len , rc );
9411
- try std .testing .expectEqualSlices (u8 , data , & recv_data );
9412
-
9413
- rc = close (conn_fd );
9414
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9415
- try std .testing .expectEqual (0 , rc );
9416
-
9417
- rc = close (fd );
9418
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9419
- try std .testing .expectEqual (0 , rc );
9420
-
9421
- rc = close (other_fd );
9422
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9423
- try std .testing .expectEqual (0 , rc );
9424
- }
9425
-
9426
- test "unix sockets sendmsg and recvmsg" {
9427
- // socket functions tested:
9428
- // - [ ] socket
9429
- // - [ ] bind
9430
- // - [ ] connect
9431
- // - [ ] listen
9432
- // - [ ] accept
9433
- // - [ ] getsockname
9434
- // - [ ] getpeername
9435
- // - [x] socketpair
9436
- // - [ ] send
9437
- // - [ ] recv
9438
- // - [ ] sendto
9439
- // - [ ] recvfrom
9440
- // - [ ] shutdown
9441
- // - [ ] setsockopt
9442
- // - [ ] getsockopt
9443
- // - [x] sendmsg
9444
- // - [x] recvmsg
9445
- // - [ ] accept4
9446
- // - [ ] recvmmsg
9447
- // - [ ] sendmmsg
9448
-
9449
- // create a socket "fd" to be the server
9450
- var socks : [2 ]socket_t = undefined ;
9451
- var rc = socketpair (AF .UNIX , SOCK .SEQPACKET , 0 , & socks );
9452
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9453
-
9454
- // test sendmsg
9455
- const data = "foo" ;
9456
- const send_iov : [1 ]iovec_const = .{.{
9457
- .base = data ,
9458
- .len = data .len ,
9459
- }};
9460
- rc = sendmsg (socks [0 ], &.{
9461
- .name = null ,
9462
- .namelen = 0 ,
9463
- .iov = & send_iov ,
9464
- .iovlen = send_iov .len ,
9465
- .control = null ,
9466
- .controllen = 0 ,
9467
- .flags = 0 ,
9468
- }, 0 );
9469
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9470
- try std .testing .expectEqual (data .len , rc );
9471
-
9472
- // test recvmsg
9473
- var recv_data : [data .len ]u8 = undefined ;
9474
- var recv_iov : [1 ]iovec = .{.{
9475
- .base = & recv_data ,
9476
- .len = recv_data .len ,
9477
- }};
9478
- var hdr : msghdr = .{
9479
- .name = null ,
9480
- .namelen = 0 ,
9481
- .iov = & recv_iov ,
9482
- .iovlen = recv_iov .len ,
9483
- .control = null ,
9484
- .controllen = 0 ,
9485
- .flags = 0 ,
9486
- };
9487
- rc = recvmsg (socks [1 ], & hdr , 0 );
9488
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9489
- try std .testing .expectEqual (data .len , rc );
9490
- try std .testing .expectEqual (data .len , recv_iov [0 ].len );
9491
- try std .testing .expectEqualSlices (u8 , data , & recv_data );
9492
-
9493
- rc = close (socks [0 ]);
9494
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9495
- try std .testing .expectEqual (0 , rc );
9496
-
9497
- rc = close (socks [1 ]);
9498
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9499
- try std .testing .expectEqual (0 , rc );
9500
- }
9501
-
9502
- test "unix sockets sendmmsg and recvmmsg" {
9503
- // socket functions tested:
9504
- // - [ ] socket
9505
- // - [ ] bind
9506
- // - [ ] connect
9507
- // - [ ] listen
9508
- // - [ ] accept
9509
- // - [ ] getsockname
9510
- // - [ ] getpeername
9511
- // - [x] socketpair
9512
- // - [ ] send
9513
- // - [ ] recv
9514
- // - [ ] sendto
9515
- // - [ ] recvfrom
9516
- // - [ ] shutdown
9517
- // - [ ] setsockopt
9518
- // - [ ] getsockopt
9519
- // - [ ] sendmsg
9520
- // - [ ] recvmsg
9521
- // - [ ] accept4
9522
- // - [x] recvmmsg
9523
- // - [x] sendmmsg
9524
-
9525
- // create a socket "fd" to be the server
9526
- var socks : [2 ]socket_t = undefined ;
9527
- var rc = socketpair (AF .UNIX , SOCK .SEQPACKET , 0 , & socks );
9528
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9529
-
9530
- // test sendmmsg
9531
- const data : [2 ][]const u8 = .{ "foo" , "bar" };
9532
- const send_iov : [2 ][1 ]iovec_const = .{
9533
- .{.{
9534
- .base = data [0 ].ptr ,
9535
- .len = data [0 ].len ,
9536
- }},
9537
- .{.{
9538
- .base = data [1 ].ptr ,
9539
- .len = data [1 ].len ,
9540
- }},
9541
- };
9542
- var send_hdr : [2 ]mmsghdr_const = .{
9543
- .{
9544
- .hdr = .{
9545
- .name = null ,
9546
- .namelen = 0 ,
9547
- .iov = & send_iov [0 ],
9548
- .iovlen = send_iov [0 ].len ,
9549
- .control = null ,
9550
- .controllen = 0 ,
9551
- .flags = 0 ,
9552
- },
9553
- .len = 0 ,
9554
- },
9555
- .{
9556
- .hdr = .{
9557
- .name = null ,
9558
- .namelen = 0 ,
9559
- .iov = & send_iov [1 ],
9560
- .iovlen = send_iov [1 ].len ,
9561
- .control = null ,
9562
- .controllen = 0 ,
9563
- .flags = 0 ,
9564
- },
9565
- .len = 0 ,
9566
- },
9567
- };
9568
- rc = sendmmsg (socks [0 ], & send_hdr , 2 , 0 );
9569
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9570
- try std .testing .expectEqual (data .len , rc );
9571
- for (data , send_hdr ) | data_el , hdr | {
9572
- try std .testing .expectEqual (data_el .len , hdr .len );
9573
- }
9574
-
9575
- // test recvmmsg
9576
- var recv_data : struct { [data [0 ].len ]u8 , [data [1 ].len ]u8 } = undefined ;
9577
- var recv_iov : [2 ][1 ]iovec = .{
9578
- .{.{
9579
- .base = & recv_data [0 ],
9580
- .len = recv_data [0 ].len ,
9581
- }},
9582
- .{.{
9583
- .base = & recv_data [1 ],
9584
- .len = recv_data [1 ].len ,
9585
- }},
9586
- };
9587
- var recv_hdr : [2 ]mmsghdr = .{
9588
- .{
9589
- .hdr = .{
9590
- .name = null ,
9591
- .namelen = 0 ,
9592
- .iov = & recv_iov [0 ],
9593
- .iovlen = recv_iov [0 ].len ,
9594
- .control = null ,
9595
- .controllen = 0 ,
9596
- .flags = 0 ,
9597
- },
9598
- .len = 0 ,
9599
- },
9600
- .{
9601
- .hdr = .{
9602
- .name = null ,
9603
- .namelen = 0 ,
9604
- .iov = & recv_iov [1 ],
9605
- .iovlen = recv_iov [1 ].len ,
9606
- .control = null ,
9607
- .controllen = 0 ,
9608
- .flags = 0 ,
9609
- },
9610
- .len = 0 ,
9611
- },
9612
- };
9613
- rc = recvmmsg (socks [1 ], & recv_hdr , recv_hdr .len , 0 , null );
9614
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9615
- try std .testing .expectEqual (recv_hdr .len , rc );
9616
- inline for (data , recv_iov , & recv_data ) | data_el , iov , * recv_data_el | {
9617
- try std .testing .expectEqual (data_el .len , iov [0 ].len );
9618
- try std .testing .expectEqualSlices (u8 , data_el , recv_data_el );
9619
- }
9620
-
9621
- rc = close (socks [0 ]);
9622
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9623
- try std .testing .expectEqual (0 , rc );
9624
-
9625
- rc = close (socks [1 ]);
9626
- try std .testing .expectEqual (.SUCCESS , E .init (rc ));
9627
- try std .testing .expectEqual (0 , rc );
9628
- }
0 commit comments