Loading...
   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
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*
 * Copyright (c) 2025 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

/*
 * sockopt_tcp.c
 *
 * Tests for IPPROTO_TCP level socket options.
 * Tests TCP-specific options like TCP_NODELAY, TCP_KEEPALIVE, TCP_MAXSEG,
 * TCP_CONNECTIONTIMEOUT, TCP_NOOPT, TCP_NOPUSH, etc.
 */

#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/tcp_private.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>

#include <darwintest.h>

T_GLOBAL_META(
	T_META_NAMESPACE("xnu.net.sockopt"),
	T_META_CHECK_LEAKS(false),
	T_META_RUN_CONCURRENTLY(true)
	);

/*
 * Helper to test basic integer TCP option round-trip
 */
static void
test_tcp_int_option_roundtrip(int fd, int optname, const char *optname_str,
    int setval, int expected_getval)
{
	int getval = 0;
	socklen_t optlen = sizeof(getval);

	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, optname, &setval, sizeof(setval)),
		"setsockopt %s = %d", optname_str, setval);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, optname, &getval, &optlen),
		"getsockopt %s", optname_str);

	T_EXPECT_EQ(getval, expected_getval,
	    "%s value matches (expected %d, got %d)",
	    optname_str, expected_getval, getval);
}

/*
 * Helper to test boolean TCP options
 * For boolean options: 0 means disabled, any non-zero means enabled
 */
static void
test_tcp_bool_option(int fd, int optname, const char *optname_str,
    int setval, bool expect_enabled)
{
	int getval = 0;
	socklen_t optlen = sizeof(getval);

	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, optname, &setval, sizeof(setval)),
		"setsockopt %s = %d", optname_str, setval);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, optname, &getval, &optlen),
		"getsockopt %s", optname_str);

	if (expect_enabled) {
		T_EXPECT_NE(getval, 0,
		    "%s is enabled (set %d, got non-zero %d)",
		    optname_str, setval, getval);
	} else {
		T_EXPECT_EQ(getval, 0,
		    "%s is disabled (set %d, got %d)",
		    optname_str, setval, getval);
	}
}

/*
 * Test TCP_NODELAY - Disable Nagle's algorithm
 */
T_DECL(tcp_nodelay_inet, "TCP_NODELAY on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NODELAY, "TCP_NODELAY", 1, true);
	test_tcp_bool_option(fd, TCP_NODELAY, "TCP_NODELAY", 0, false);

	close(fd);
}

T_DECL(tcp_nodelay_inet6, "TCP_NODELAY on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NODELAY, "TCP_NODELAY", 1, true);
	test_tcp_bool_option(fd, TCP_NODELAY, "TCP_NODELAY", 0, false);

	close(fd);
}

/*
 * Test TCP_MAXSEG - Maximum segment size (read-only after connection)
 */
T_DECL(tcp_maxseg_readonly, "TCP_MAXSEG is readable")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	int mss = 0;
	socklen_t optlen = sizeof(mss);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &mss, &optlen),
		"getsockopt TCP_MAXSEG");

	T_EXPECT_GT(mss, 0, "TCP_MAXSEG > 0 (got %d)", mss);
	T_LOG("TCP_MAXSEG: %d", mss);

	close(fd);
}

/*
 * Test TCP_NOOPT - Disable TCP options
 */
T_DECL(tcp_noopt_inet, "TCP_NOOPT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOOPT, "TCP_NOOPT", 1, true);
	test_tcp_bool_option(fd, TCP_NOOPT, "TCP_NOOPT", 0, false);

	close(fd);
}

/*
 * Test TCP_NOPUSH - Don't push last partial segment
 */
T_DECL(tcp_nopush_inet, "TCP_NOPUSH on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOPUSH, "TCP_NOPUSH", 1, true);
	test_tcp_bool_option(fd, TCP_NOPUSH, "TCP_NOPUSH", 0, false);

	close(fd);
}

/*
 * Test TCP_KEEPALIVE - Keepalive idle time in seconds
 */
T_DECL(tcp_keepalive_inet, "TCP_KEEPALIVE on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Enable SO_KEEPALIVE first */
	int enable = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)),
		"setsockopt SO_KEEPALIVE");

	/* Set TCP_KEEPALIVE to 60 seconds */
	test_tcp_int_option_roundtrip(fd, TCP_KEEPALIVE, "TCP_KEEPALIVE", 60, 60);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPALIVE, "TCP_KEEPALIVE", 120, 120);

	close(fd);
}

T_DECL(tcp_keepalive_inet6, "TCP_KEEPALIVE on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	int enable = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)),
		"setsockopt SO_KEEPALIVE");

	test_tcp_int_option_roundtrip(fd, TCP_KEEPALIVE, "TCP_KEEPALIVE", 30, 30);

	close(fd);
}

/*
 * Test TCP_KEEPINTVL - Keepalive probe interval
 */
T_DECL(tcp_keepintvl_inet, "TCP_KEEPINTVL on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_KEEPINTVL, "TCP_KEEPINTVL", 10, 10);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPINTVL, "TCP_KEEPINTVL", 30, 30);

	close(fd);
}

/*
 * Test TCP_KEEPCNT - Keepalive probe count
 */
T_DECL(tcp_keepcnt_inet, "TCP_KEEPCNT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_KEEPCNT, "TCP_KEEPCNT", 5, 5);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPCNT, "TCP_KEEPCNT", 10, 10);

	close(fd);
}

/*
 * Test TCP_CONNECTIONTIMEOUT - Connection timeout
 */
T_DECL(tcp_connectiontimeout_inet, "TCP_CONNECTIONTIMEOUT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set timeout to 30 seconds */
	test_tcp_int_option_roundtrip(fd, TCP_CONNECTIONTIMEOUT,
	    "TCP_CONNECTIONTIMEOUT", 30, 30);

	close(fd);
}

T_DECL(tcp_connectiontimeout_inet6, "TCP_CONNECTIONTIMEOUT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_CONNECTIONTIMEOUT,
	    "TCP_CONNECTIONTIMEOUT", 60, 60);

	close(fd);
}

/*
 * Test TCP_RXT_CONNDROPTIME - Retransmit timeout for connection drop
 */
T_DECL(tcp_rxt_conndroptime_inet, "TCP_RXT_CONNDROPTIME on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set to 60 seconds */
	test_tcp_int_option_roundtrip(fd, TCP_RXT_CONNDROPTIME,
	    "TCP_RXT_CONNDROPTIME", 60, 60);

	close(fd);
}

/*
 * Test TCP_RXT_FINDROP - Enable/disable FIN drop behavior
 */
T_DECL(tcp_rxt_findrop_inet, "TCP_RXT_FINDROP on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_RXT_FINDROP, "TCP_RXT_FINDROP", 1, true);
	test_tcp_bool_option(fd, TCP_RXT_FINDROP, "TCP_RXT_FINDROP", 0, false);

	close(fd);
}

/*
 * Test TCP_SENDMOREACKS - Send more ACKs
 */
T_DECL(tcp_sendmoreacks_inet, "TCP_SENDMOREACKS on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_SENDMOREACKS, "TCP_SENDMOREACKS", 1, true);
	test_tcp_bool_option(fd, TCP_SENDMOREACKS, "TCP_SENDMOREACKS", 0, false);

	close(fd);
}

/*
 * Test TCP_ENABLE_ECN - Enable Explicit Congestion Notification
 */
T_DECL(tcp_enable_ecn_inet, "TCP_ENABLE_ECN on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_ENABLE_ECN, "TCP_ENABLE_ECN", 1, true);
	test_tcp_bool_option(fd, TCP_ENABLE_ECN, "TCP_ENABLE_ECN", 0, false);

	close(fd);
}

/*
 * Test TCP_FASTOPEN - Enable TCP Fast Open
 * Note: TCP_FASTOPEN requires socket to be in LISTEN state
 */
T_DECL(tcp_fastopen_inet, "TCP_FASTOPEN on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Bind and listen to make socket eligible for TCP_FASTOPEN */
	struct sockaddr_in addr = {
		.sin_family = AF_INET,
		.sin_port = 0, /* Let kernel choose port */
		.sin_addr.s_addr = htonl(INADDR_LOOPBACK)
	};

	T_ASSERT_POSIX_SUCCESS(
		bind(fd, (struct sockaddr *)&addr, sizeof(addr)),
		"bind to localhost");

	T_ASSERT_POSIX_SUCCESS(
		listen(fd, 5),
		"listen");

	/* Now TCP_FASTOPEN should work (if system supports it) */
	int enable = 1;
	int ret = setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &enable, sizeof(enable));
	if (ret == -1) {
		if (errno == ENOTSUP) {
			T_LOG("TCP_FASTOPEN not supported on this system (server mode disabled)");
		} else {
			T_ASSERT_POSIX_SUCCESS(ret, "setsockopt TCP_FASTOPEN = 1");
		}
	} else {
		T_LOG("TCP_FASTOPEN enabled successfully");

		/* Verify it's enabled */
		int getval = 0;
		socklen_t optlen = sizeof(getval);
		T_ASSERT_POSIX_SUCCESS(
			getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &getval, &optlen),
			"getsockopt TCP_FASTOPEN");
		T_EXPECT_NE(getval, 0, "TCP_FASTOPEN is enabled (got %d)", getval);

		/* Disable it */
		int disable = 0;
		T_ASSERT_POSIX_SUCCESS(
			setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &disable, sizeof(disable)),
			"setsockopt TCP_FASTOPEN = 0");

		T_ASSERT_POSIX_SUCCESS(
			getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &getval, &optlen),
			"getsockopt TCP_FASTOPEN");
		T_EXPECT_EQ(getval, 0, "TCP_FASTOPEN is disabled");
	}

	close(fd);
}

T_DECL(tcp_fastopen_not_listening, "TCP_FASTOPEN fails on non-listening socket")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Try to enable TCP_FASTOPEN without listen() */
	int enable = 1;
	int ret = setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &enable, sizeof(enable));

	T_ASSERT_EQ(ret, -1, "TCP_FASTOPEN should fail on non-listening socket");
	T_EXPECT_TRUE(errno == EINVAL || errno == ENOTSUP,
	    "errno should be EINVAL or ENOTSUP (got %d: %s)",
	    errno, strerror(errno));

	close(fd);
}

/*
 * Test TCP_NOTSENT_LOWAT - Unsent data low water mark
 */
T_DECL(tcp_notsent_lowat_inet, "TCP_NOTSENT_LOWAT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_NOTSENT_LOWAT,
	    "TCP_NOTSENT_LOWAT", 4096, 4096);
	test_tcp_int_option_roundtrip(fd, TCP_NOTSENT_LOWAT,
	    "TCP_NOTSENT_LOWAT", 8192, 8192);

	close(fd);
}

/*
 * Test TCP_CONNECTION_INFO - Get connection information (read-only)
 */
T_DECL(tcp_connection_info_readonly, "TCP_CONNECTION_INFO is read-only")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	struct tcp_connection_info conninfo = {0};
	socklen_t optlen = sizeof(conninfo);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO, &conninfo, &optlen),
		"getsockopt TCP_CONNECTION_INFO");

	T_LOG("TCP connection state: %u", conninfo.tcpi_state);
	T_LOG("TCP options: 0x%x", conninfo.tcpi_options);

	/* Try to set TCP_CONNECTION_INFO (should fail) */
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO,
		&conninfo, sizeof(conninfo)),
		ENOPROTOOPT,
		"setsockopt TCP_CONNECTION_INFO should fail");

	close(fd);
}

/*
 * Test multiple TCP options can be set on the same socket
 */
T_DECL(tcp_multiple_options, "Multiple TCP options on same socket")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set multiple options */
	int nodelay = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)),
		"setsockopt TCP_NODELAY");

	int nopush = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush)),
		"setsockopt TCP_NOPUSH");

	int keepalive = 60;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive, sizeof(keepalive)),
		"setsockopt TCP_KEEPALIVE");

	/* Verify all options are set correctly */
	int getval = 0;
	socklen_t optlen = sizeof(getval);

	/* TCP_NODELAY and TCP_NOPUSH are boolean - check for non-zero */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &getval, &optlen),
		"getsockopt TCP_NODELAY");
	T_EXPECT_NE(getval, 0, "TCP_NODELAY still enabled (got %d)", getval);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_NOPUSH, &getval, &optlen),
		"getsockopt TCP_NOPUSH");
	T_EXPECT_NE(getval, 0, "TCP_NOPUSH still enabled (got %d)", getval);

	/* TCP_KEEPALIVE is integer value - check exact value */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &getval, &optlen),
		"getsockopt TCP_KEEPALIVE");
	T_EXPECT_EQ(getval, 60, "TCP_KEEPALIVE still 60");

	close(fd);
}

/*
 * Test TCP options on unconnected socket
 */
T_DECL(tcp_options_unconnected, "TCP options work on unconnected socket")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* These should all work on unconnected socket */
	test_tcp_bool_option(fd, TCP_NODELAY, "TCP_NODELAY", 1, true);
	test_tcp_bool_option(fd, TCP_NOOPT, "TCP_NOOPT", 1, true);
	test_tcp_bool_option(fd, TCP_NOPUSH, "TCP_NOPUSH", 1, true);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPALIVE, "TCP_KEEPALIVE", 30, 30);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPINTVL, "TCP_KEEPINTVL", 15, 15);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPCNT, "TCP_KEEPCNT", 8, 8);

	close(fd);
}

/*
 * Test invalid TCP option values
 */
T_DECL(tcp_keepalive_negative, "TCP_KEEPALIVE rejects negative values")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	int negative = -1;
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &negative, sizeof(negative)),
		EINVAL,
		"TCP_KEEPALIVE should reject negative value");

	close(fd);
}

/*
 * Test TCP option invalid lengths
 */
T_DECL(tcp_nodelay_invalid_optlen, "TCP_NODELAY with invalid optlen")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Too small */
	char smallval = 1;
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &smallval, sizeof(smallval)),
		EINVAL,
		"setsockopt TCP_NODELAY with too small optlen should fail");

	/* NULL pointer with non-zero length */
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, NULL, sizeof(int)),
		EFAULT,
		"setsockopt TCP_NODELAY with NULL optval should fail");

	close(fd);
}

/*
 * Private TCP Options Tests
 * Testing options from netinet/tcp_private.h
 */

/*
 * Test TCP_INFO - Read-only option that returns tcp_info structure
 */
T_DECL(tcp_info_readonly, "TCP_INFO is read-only")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	struct tcp_info info = {0};
	socklen_t optlen = sizeof(info);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &optlen),
		"getsockopt TCP_INFO");

	T_LOG("TCP_INFO state: %u, options: 0x%x, snd_mss: %u, rcv_mss: %u",
	    info.tcpi_state, info.tcpi_options, info.tcpi_snd_mss, info.tcpi_rcv_mss);

	/* Try to set TCP_INFO (should fail) */
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, sizeof(info)),
		ENOPROTOOPT,
		"setsockopt TCP_INFO should fail (read-only option)");

	close(fd);
}

T_DECL(tcp_info_inet6, "TCP_INFO on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	struct tcp_info info = {0};
	socklen_t optlen = sizeof(info);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &optlen),
		"getsockopt TCP_INFO");

	T_EXPECT_GE(optlen, (socklen_t)sizeof(struct tcp_info), "optlen >= sizeof(tcp_info)");

	close(fd);
}

/*
 * Test TCP_MEASURE_SND_BW - Measure sender's bandwidth
 */
T_DECL(tcp_measure_snd_bw_inet, "TCP_MEASURE_SND_BW on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_MEASURE_SND_BW, "TCP_MEASURE_SND_BW", 1, true);
	test_tcp_bool_option(fd, TCP_MEASURE_SND_BW, "TCP_MEASURE_SND_BW", 0, false);

	close(fd);
}

/*
 * Test TCP_MEASURE_BW_BURST - Burst size for bandwidth measurement
 */
T_DECL(tcp_measure_bw_burst_inet, "TCP_MEASURE_BW_BURST on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	struct tcp_measure_bw_burst burst = {
		.min_burst_size = 4,
		.max_burst_size = 16
	};

	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_MEASURE_BW_BURST,
		&burst, sizeof(burst)),
		"setsockopt TCP_MEASURE_BW_BURST");

	struct tcp_measure_bw_burst get_burst = {0};
	socklen_t optlen = sizeof(get_burst);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_MEASURE_BW_BURST,
		&get_burst, &optlen),
		"getsockopt TCP_MEASURE_BW_BURST");

	T_EXPECT_EQ(get_burst.min_burst_size, burst.min_burst_size,
	    "min_burst_size matches");
	T_EXPECT_EQ(get_burst.max_burst_size, burst.max_burst_size,
	    "max_burst_size matches");

	close(fd);
}

/*
 * Test TCP_PEER_PID - Lookup pid of connected process (read-only)
 */
T_DECL(tcp_peer_pid_unconnected, "TCP_PEER_PID on unconnected socket")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	pid_t peer_pid = 0;
	socklen_t optlen = sizeof(peer_pid);

	/* On unconnected socket, should return 0 or fail */
	int ret = getsockopt(fd, IPPROTO_TCP, TCP_PEER_PID, &peer_pid, &optlen);
	if (ret == 0) {
		T_LOG("TCP_PEER_PID on unconnected socket: %d", peer_pid);
		T_EXPECT_EQ(peer_pid, 0, "peer_pid should be 0 on unconnected socket");
	} else {
		T_LOG("getsockopt TCP_PEER_PID failed (expected on unconnected): %d (%s)",
		    errno, strerror(errno));
	}

	/* Try to set TCP_PEER_PID (should fail - read-only) */
	peer_pid = 1234;
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_PEER_PID, &peer_pid, sizeof(peer_pid)),
		ENOPROTOOPT,
		"setsockopt TCP_PEER_PID should fail (read-only option)");

	close(fd);
}

/*
 * Test TCP_ADAPTIVE_READ_TIMEOUT - Read timeout as multiple of RTT
 */
T_DECL(tcp_adaptive_read_timeout_inet, "TCP_ADAPTIVE_READ_TIMEOUT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set timeout to 5 RTTs */
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_READ_TIMEOUT,
	    "TCP_ADAPTIVE_READ_TIMEOUT", 5, 5);

	/* Set to 0 to disable */
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_READ_TIMEOUT,
	    "TCP_ADAPTIVE_READ_TIMEOUT", 0, 0);

	close(fd);
}

/*
 * Test TCP_ADAPTIVE_WRITE_TIMEOUT - Write timeout as multiple of RTT
 */
T_DECL(tcp_adaptive_write_timeout_inet, "TCP_ADAPTIVE_WRITE_TIMEOUT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set timeout to 10 RTTs */
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_WRITE_TIMEOUT,
	    "TCP_ADAPTIVE_WRITE_TIMEOUT", 10, 10);

	/* Set to 0 to disable */
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_WRITE_TIMEOUT,
	    "TCP_ADAPTIVE_WRITE_TIMEOUT", 0, 0);

	close(fd);
}

/*
 * Test TCP_NOTIMEWAIT - Avoid TIME_WAIT state
 */
T_DECL(tcp_notimewait_inet, "TCP_NOTIMEWAIT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOTIMEWAIT, "TCP_NOTIMEWAIT", 1, true);
	test_tcp_bool_option(fd, TCP_NOTIMEWAIT, "TCP_NOTIMEWAIT", 0, false);

	close(fd);
}

T_DECL(tcp_notimewait_inet6, "TCP_NOTIMEWAIT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOTIMEWAIT, "TCP_NOTIMEWAIT", 1, true);
	test_tcp_bool_option(fd, TCP_NOTIMEWAIT, "TCP_NOTIMEWAIT", 0, false);

	close(fd);
}

/*
 * Test TCP_DISABLE_BLACKHOLE_DETECTION - Disable PMTU blackhole detection
 */
T_DECL(tcp_disable_blackhole_detection_inet, "TCP_DISABLE_BLACKHOLE_DETECTION on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_DISABLE_BLACKHOLE_DETECTION,
	    "TCP_DISABLE_BLACKHOLE_DETECTION", 1, true);
	test_tcp_bool_option(fd, TCP_DISABLE_BLACKHOLE_DETECTION,
	    "TCP_DISABLE_BLACKHOLE_DETECTION", 0, false);

	close(fd);
}

/*
 * Test TCP_ECN_MODE - Fine grain ECN control
 */
T_DECL(tcp_ecn_mode_inet, "TCP_ECN_MODE on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Test different ECN modes */
	test_tcp_int_option_roundtrip(fd, TCP_ECN_MODE, "TCP_ECN_MODE",
	    ECN_MODE_DEFAULT, ECN_MODE_DEFAULT);
	test_tcp_int_option_roundtrip(fd, TCP_ECN_MODE, "TCP_ECN_MODE",
	    ECN_MODE_ENABLE, ECN_MODE_ENABLE);
	test_tcp_int_option_roundtrip(fd, TCP_ECN_MODE, "TCP_ECN_MODE",
	    ECN_MODE_DISABLE, ECN_MODE_DISABLE);

	close(fd);
}

T_DECL(tcp_ecn_mode_inet6, "TCP_ECN_MODE on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_ECN_MODE, "TCP_ECN_MODE",
	    ECN_MODE_ENABLE, ECN_MODE_ENABLE);

	close(fd);
}

/*
 * Test TCP_KEEPALIVE_OFFLOAD - Offload keepalive to firmware
 * Note: Requires com.apple.developer.networking.tcp_ka_offload entitlement (iOS only)
 */
#if TARGET_OS_OSX
T_DECL(tcp_keepalive_offload_inet, "TCP_KEEPALIVE_OFFLOAD on AF_INET (macOS)",
    T_META_ENABLED(TARGET_OS_OSX))
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	T_MAYFAIL;
	test_tcp_bool_option(fd, TCP_KEEPALIVE_OFFLOAD, "TCP_KEEPALIVE_OFFLOAD", 1, true);

	T_MAYFAIL;
	test_tcp_bool_option(fd, TCP_KEEPALIVE_OFFLOAD, "TCP_KEEPALIVE_OFFLOAD", 0, false);

	close(fd);
}
#else
T_DECL(tcp_keepalive_offload_inet, "TCP_KEEPALIVE_OFFLOAD on AF_INET (iOS)",
    T_META_ENABLED(!TARGET_OS_OSX))
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* On macOS, PRIV_NETINET_TCP_KA_OFFLOAD is not available, so setting should fail with EPERM */
	int enable = 1;
	int ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_OFFLOAD, &enable, sizeof(enable));
	T_ASSERT_EQ(ret, -1, "TCP_KEEPALIVE_OFFLOAD should fail on macOS");
	T_EXPECT_EQ(errno, EPERM, "errno should be EPERM (got %d: %s)", errno, strerror(errno));

	/* getsockopt should still work and return disabled state */
	int getval = 0;
	socklen_t optlen = sizeof(getval);
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_OFFLOAD, &getval, &optlen),
		"getsockopt TCP_KEEPALIVE_OFFLOAD");
	T_EXPECT_EQ(getval, 0, "TCP_KEEPALIVE_OFFLOAD is disabled (got %d)", getval);

	close(fd);
}
#endif

/*
 * Test TCP_NOTIFY_ACKNOWLEDGEMENT - Notify when data is acknowledged
 * Note: Requires socket to have data in send buffer (ENOBUFS otherwise)
 */
T_DECL(tcp_notify_acknowledgement_no_data, "TCP_NOTIFY_ACKNOWLEDGEMENT requires send data")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Try to set a marker ID without any send data - should fail with ENOBUFS */
	tcp_notify_ack_id_t marker_id = 12345;
	int ret = setsockopt(fd, IPPROTO_TCP, TCP_NOTIFY_ACKNOWLEDGEMENT,
	    &marker_id, sizeof(marker_id));

	T_ASSERT_EQ(ret, -1, "TCP_NOTIFY_ACKNOWLEDGEMENT should fail without send data");
	T_EXPECT_EQ(errno, ENOBUFS, "errno should be ENOBUFS (got %d: %s)",
	    errno, strerror(errno));

	close(fd);
}

T_DECL(tcp_notify_acknowledgement_get, "TCP_NOTIFY_ACKNOWLEDGEMENT can be queried")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Query for completed notifications on unconnected socket */
	struct tcp_notify_ack_complete complete = {0};
	socklen_t optlen = sizeof(complete);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_NOTIFY_ACKNOWLEDGEMENT,
		&complete, &optlen),
		"getsockopt TCP_NOTIFY_ACKNOWLEDGEMENT");

	T_LOG("notify_pending: %u, notify_complete_count: %u",
	    complete.notify_pending, complete.notify_complete_count);

	/* Should have no pending or completed notifications */
	T_EXPECT_EQ(complete.notify_pending, 0U, "No pending notifications");
	T_EXPECT_EQ(complete.notify_complete_count, 0U, "No completed notifications");

	close(fd);
}

/*
 * Test TCP_RXT_MINIMUM_TIMEOUT - Minimum retransmit timeout
 */
T_DECL(tcp_rxt_minimum_timeout_inet, "TCP_RXT_MINIMUM_TIMEOUT on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set timeout to 30 seconds */
	test_tcp_int_option_roundtrip(fd, TCP_RXT_MINIMUM_TIMEOUT,
	    "TCP_RXT_MINIMUM_TIMEOUT", 30, 30);

	/* Test maximum allowed value (5 minutes) */
	test_tcp_int_option_roundtrip(fd, TCP_RXT_MINIMUM_TIMEOUT,
	    "TCP_RXT_MINIMUM_TIMEOUT", TCP_RXT_MINIMUM_TIMEOUT_LIMIT,
	    TCP_RXT_MINIMUM_TIMEOUT_LIMIT);

	close(fd);
}

T_DECL(tcp_rxt_minimum_timeout_invalid, "TCP_RXT_MINIMUM_TIMEOUT value handling")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Test value exceeding limit - should be clamped to limit */
	int toolarge = TCP_RXT_MINIMUM_TIMEOUT_LIMIT + 100;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_RXT_MINIMUM_TIMEOUT,
		&toolarge, sizeof(toolarge)),
		"setsockopt TCP_RXT_MINIMUM_TIMEOUT with value > limit");

	/* Verify it was clamped to the limit */
	int getval = 0;
	socklen_t optlen = sizeof(getval);
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_RXT_MINIMUM_TIMEOUT, &getval, &optlen),
		"getsockopt TCP_RXT_MINIMUM_TIMEOUT");
	T_EXPECT_EQ(getval, TCP_RXT_MINIMUM_TIMEOUT_LIMIT,
	    "Value clamped to limit (set %d, got %d, limit %d)",
	    toolarge, getval, TCP_RXT_MINIMUM_TIMEOUT_LIMIT);

	/* Test negative value - should fail */
	int negative = -1;
	T_ASSERT_POSIX_FAILURE(
		setsockopt(fd, IPPROTO_TCP, TCP_RXT_MINIMUM_TIMEOUT,
		&negative, sizeof(negative)),
		EINVAL,
		"TCP_RXT_MINIMUM_TIMEOUT should reject negative value");

	close(fd);
}

/*
 * Test TCP_FASTOPEN_FORCE_ENABLE - Force enable TCP Fast Open
 */
T_DECL(tcp_fastopen_force_enable_inet, "TCP_FASTOPEN_FORCE_ENABLE on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_FASTOPEN_FORCE_ENABLE,
	    "TCP_FASTOPEN_FORCE_ENABLE", 1, true);
	test_tcp_bool_option(fd, TCP_FASTOPEN_FORCE_ENABLE,
	    "TCP_FASTOPEN_FORCE_ENABLE", 0, false);

	close(fd);
}

/*
 * Test TCP_ENABLE_L4S - Enable L4S (Low Latency, Low Loss, Scalable throughput)
 */
T_DECL(tcp_enable_l4s_inet, "TCP_ENABLE_L4S on AF_INET")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_ENABLE_L4S, "TCP_ENABLE_L4S", 1, true);
	test_tcp_bool_option(fd, TCP_ENABLE_L4S, "TCP_ENABLE_L4S", 0, false);

	close(fd);
}

T_DECL(tcp_enable_l4s_inet6, "TCP_ENABLE_L4S on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_ENABLE_L4S, "TCP_ENABLE_L4S", 1, true);
	test_tcp_bool_option(fd, TCP_ENABLE_L4S, "TCP_ENABLE_L4S", 0, false);

	close(fd);
}

/*
 * Test multiple private TCP options on same socket
 */
T_DECL(tcp_private_multiple_options, "Multiple private TCP options on same socket")
{
	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET, SOCK_STREAM)");

	/* Set multiple options */
	int notimewait = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_NOTIMEWAIT, &notimewait, sizeof(notimewait)),
		"setsockopt TCP_NOTIMEWAIT");

	int ecn_mode = ECN_MODE_ENABLE;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_ECN_MODE, &ecn_mode, sizeof(ecn_mode)),
		"setsockopt TCP_ECN_MODE");

	int adaptive_read_timeout = 5;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_ADAPTIVE_READ_TIMEOUT,
		&adaptive_read_timeout, sizeof(adaptive_read_timeout)),
		"setsockopt TCP_ADAPTIVE_READ_TIMEOUT");

	int measure_bw = 1;
	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_MEASURE_SND_BW, &measure_bw, sizeof(measure_bw)),
		"setsockopt TCP_MEASURE_SND_BW");

	/* Verify all options are still set */
	int getval = 0;
	socklen_t optlen = sizeof(getval);

	/* TCP_NOTIMEWAIT is boolean - check for non-zero */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_NOTIMEWAIT, &getval, &optlen),
		"getsockopt TCP_NOTIMEWAIT");
	T_EXPECT_NE(getval, 0, "TCP_NOTIMEWAIT still enabled (got %d)", getval);

	/* TCP_ECN_MODE is integer value */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_ECN_MODE, &getval, &optlen),
		"getsockopt TCP_ECN_MODE");
	T_EXPECT_EQ(getval, ECN_MODE_ENABLE, "TCP_ECN_MODE still set");

	/* TCP_ADAPTIVE_READ_TIMEOUT is integer value */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_ADAPTIVE_READ_TIMEOUT, &getval, &optlen),
		"getsockopt TCP_ADAPTIVE_READ_TIMEOUT");
	T_EXPECT_EQ(getval, 5, "TCP_ADAPTIVE_READ_TIMEOUT still set");

	/* TCP_MEASURE_SND_BW is boolean - check for non-zero */
	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_MEASURE_SND_BW, &getval, &optlen),
		"getsockopt TCP_MEASURE_SND_BW");
	T_EXPECT_NE(getval, 0, "TCP_MEASURE_SND_BW still enabled (got %d)", getval);

	close(fd);
}

/*
 * Additional AF_INET6 coverage for TCP private options
 * Testing options that were previously only tested on AF_INET
 */

T_DECL(tcp_measure_snd_bw_inet6, "TCP_MEASURE_SND_BW on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_MEASURE_SND_BW, "TCP_MEASURE_SND_BW", 1, true);
	test_tcp_bool_option(fd, TCP_MEASURE_SND_BW, "TCP_MEASURE_SND_BW", 0, false);

	close(fd);
}

T_DECL(tcp_measure_bw_burst_inet6, "TCP_MEASURE_BW_BURST on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	struct tcp_measure_bw_burst burst = {
		.min_burst_size = 4,
		.max_burst_size = 16
	};

	T_ASSERT_POSIX_SUCCESS(
		setsockopt(fd, IPPROTO_TCP, TCP_MEASURE_BW_BURST,
		&burst, sizeof(burst)),
		"setsockopt TCP_MEASURE_BW_BURST");

	struct tcp_measure_bw_burst get_burst = {0};
	socklen_t optlen = sizeof(get_burst);

	T_ASSERT_POSIX_SUCCESS(
		getsockopt(fd, IPPROTO_TCP, TCP_MEASURE_BW_BURST,
		&get_burst, &optlen),
		"getsockopt TCP_MEASURE_BW_BURST");

	T_EXPECT_EQ(get_burst.min_burst_size, burst.min_burst_size,
	    "min_burst_size matches");
	T_EXPECT_EQ(get_burst.max_burst_size, burst.max_burst_size,
	    "max_burst_size matches");

	close(fd);
}

T_DECL(tcp_adaptive_read_timeout_inet6, "TCP_ADAPTIVE_READ_TIMEOUT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_READ_TIMEOUT,
	    "TCP_ADAPTIVE_READ_TIMEOUT", 5, 5);
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_READ_TIMEOUT,
	    "TCP_ADAPTIVE_READ_TIMEOUT", 0, 0);

	close(fd);
}

T_DECL(tcp_adaptive_write_timeout_inet6, "TCP_ADAPTIVE_WRITE_TIMEOUT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_WRITE_TIMEOUT,
	    "TCP_ADAPTIVE_WRITE_TIMEOUT", 10, 10);
	test_tcp_int_option_roundtrip(fd, TCP_ADAPTIVE_WRITE_TIMEOUT,
	    "TCP_ADAPTIVE_WRITE_TIMEOUT", 0, 0);

	close(fd);
}

T_DECL(tcp_disable_blackhole_detection_inet6, "TCP_DISABLE_BLACKHOLE_DETECTION on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_DISABLE_BLACKHOLE_DETECTION,
	    "TCP_DISABLE_BLACKHOLE_DETECTION", 1, true);
	test_tcp_bool_option(fd, TCP_DISABLE_BLACKHOLE_DETECTION,
	    "TCP_DISABLE_BLACKHOLE_DETECTION", 0, false);

	close(fd);
}

T_DECL(tcp_fastopen_force_enable_inet6, "TCP_FASTOPEN_FORCE_ENABLE on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_FASTOPEN_FORCE_ENABLE,
	    "TCP_FASTOPEN_FORCE_ENABLE", 1, true);
	test_tcp_bool_option(fd, TCP_FASTOPEN_FORCE_ENABLE,
	    "TCP_FASTOPEN_FORCE_ENABLE", 0, false);

	close(fd);
}

T_DECL(tcp_rxt_minimum_timeout_inet6, "TCP_RXT_MINIMUM_TIMEOUT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_RXT_MINIMUM_TIMEOUT,
	    "TCP_RXT_MINIMUM_TIMEOUT", 30, 30);
	test_tcp_int_option_roundtrip(fd, TCP_RXT_MINIMUM_TIMEOUT,
	    "TCP_RXT_MINIMUM_TIMEOUT", TCP_RXT_MINIMUM_TIMEOUT_LIMIT,
	    TCP_RXT_MINIMUM_TIMEOUT_LIMIT);

	close(fd);
}

T_DECL(tcp_noopt_inet6, "TCP_NOOPT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOOPT, "TCP_NOOPT", 1, true);
	test_tcp_bool_option(fd, TCP_NOOPT, "TCP_NOOPT", 0, false);

	close(fd);
}

T_DECL(tcp_nopush_inet6, "TCP_NOPUSH on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_NOPUSH, "TCP_NOPUSH", 1, true);
	test_tcp_bool_option(fd, TCP_NOPUSH, "TCP_NOPUSH", 0, false);

	close(fd);
}

T_DECL(tcp_keepintvl_inet6, "TCP_KEEPINTVL on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_KEEPINTVL, "TCP_KEEPINTVL", 10, 10);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPINTVL, "TCP_KEEPINTVL", 30, 30);

	close(fd);
}

T_DECL(tcp_keepcnt_inet6, "TCP_KEEPCNT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_KEEPCNT, "TCP_KEEPCNT", 5, 5);
	test_tcp_int_option_roundtrip(fd, TCP_KEEPCNT, "TCP_KEEPCNT", 10, 10);

	close(fd);
}

T_DECL(tcp_rxt_conndroptime_inet6, "TCP_RXT_CONNDROPTIME on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_RXT_CONNDROPTIME,
	    "TCP_RXT_CONNDROPTIME", 60, 60);

	close(fd);
}

T_DECL(tcp_rxt_findrop_inet6, "TCP_RXT_FINDROP on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_RXT_FINDROP, "TCP_RXT_FINDROP", 1, true);
	test_tcp_bool_option(fd, TCP_RXT_FINDROP, "TCP_RXT_FINDROP", 0, false);

	close(fd);
}

T_DECL(tcp_sendmoreacks_inet6, "TCP_SENDMOREACKS on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_SENDMOREACKS, "TCP_SENDMOREACKS", 1, true);
	test_tcp_bool_option(fd, TCP_SENDMOREACKS, "TCP_SENDMOREACKS", 0, false);

	close(fd);
}

T_DECL(tcp_enable_ecn_inet6, "TCP_ENABLE_ECN on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_bool_option(fd, TCP_ENABLE_ECN, "TCP_ENABLE_ECN", 1, true);
	test_tcp_bool_option(fd, TCP_ENABLE_ECN, "TCP_ENABLE_ECN", 0, false);

	close(fd);
}

T_DECL(tcp_notsent_lowat_inet6, "TCP_NOTSENT_LOWAT on AF_INET6")
{
	int fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	T_ASSERT_POSIX_SUCCESS(fd, "socket(AF_INET6, SOCK_STREAM)");

	test_tcp_int_option_roundtrip(fd, TCP_NOTSENT_LOWAT,
	    "TCP_NOTSENT_LOWAT", 4096, 4096);
	test_tcp_int_option_roundtrip(fd, TCP_NOTSENT_LOWAT,
	    "TCP_NOTSENT_LOWAT", 8192, 8192);

	close(fd);
}