Skip to content

Commit

Permalink
sk-tcp: Add test cases for TCP_CORK and TCP_NODELAY socket options
Browse files Browse the repository at this point in the history
Currently there are no socket option test cases for TCP_CORK and
TCP_NODELAY, this commit adds related test cases.

The socket option test cases for TCP_KEEPCNT, TCP_KEEPIDLE, and
TCP_KEEPINTVL already exist in socket-tcp_keepalive.c, so they are
not included in this test case.

Signed-off-by: Juntong Deng <[email protected]>
  • Loading branch information
juntongdeng authored and avagin committed May 21, 2024
1 parent 277878b commit 516b369
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ TST_NOFILE := \
sock_opts02 \
sock_ip_opts00 \
sock_ip_opts01 \
sock_tcp_opts00 \
sock_tcp_opts01 \
sk-unix-unconn \
sk-unix-unconn-seqpacket \
ipc_namespace \
Expand Down Expand Up @@ -609,6 +611,7 @@ socket-tcp6-closed: CFLAGS += -D ZDTM_IPV4V6
socket-tcp-closed-last-ack: CFLAGS += -D ZDTM_TCP_LAST_ACK
socket-tcp-skip-in-flight: CFLAGS += -D ZDTM_IPV4V6
sock_ip_opts01: CFLAGS += -DZDTM_VAL_ZERO
sock_tcp_opts01: CFLAGS += -DZDTM_VAL_ZERO
tun_ns: CFLAGS += -DTUN_NS
mnt_ext_manual: CFLAGS += -D ZDTM_EXTMAP_MANUAL
mntns_pivot_root_ro: CFLAGS += -DMNTNS_PIVOT_ROOT_RO
Expand Down
96 changes: 96 additions & 0 deletions test/zdtm/static/sock_tcp_opts00.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#include "zdtmtst.h"

const char *test_doc = "Check that different tcp socket options are restored";
const char *test_author = "Juntong Deng <[email protected]>";

#ifdef ZDTM_VAL_ZERO
#define TCP_OPT_VAL 0
#else
#define TCP_OPT_VAL 1
#endif

#ifndef SOL_TCP
#define SOL_TCP 6
#endif

struct sk_opt {
int level;
int opt;
int val;
};

struct sk_opt tcp_sk_opts[] = {
{ SOL_TCP, TCP_CORK, TCP_OPT_VAL },
{ SOL_TCP, TCP_NODELAY, TCP_OPT_VAL },
};

struct sk_conf {
int domain;
int type;
int protocol;
int sk;
} sk_confs[] = {
{ AF_INET, SOCK_STREAM, IPPROTO_TCP },
{ AF_INET6, SOCK_STREAM, IPPROTO_TCP },
};

int main(int argc, char **argv)
{
struct sk_opt *opts = tcp_sk_opts;
int n_opts = ARRAY_SIZE(tcp_sk_opts);
int exit_code = 1;
int i, j, val;
socklen_t len;

test_init(argc, argv);

for (i = 0; i < ARRAY_SIZE(sk_confs); i++) {
sk_confs[i].sk = socket(sk_confs[i].domain, sk_confs[i].type, sk_confs[i].protocol);
if (sk_confs[i].sk == -1) {
pr_perror("socket(%d,%d,%d) failed", sk_confs[i].domain, sk_confs[i].type,
sk_confs[i].protocol);
goto close;
}
}

for (i = 0; i < ARRAY_SIZE(sk_confs); i++) {
for (j = 0; j < n_opts; j++) {
val = opts[j].val;
if (setsockopt(sk_confs[i].sk, opts[j].level, opts[j].opt, &val, sizeof(int)) == -1) {
pr_perror("setsockopt(%d, %d) failed", opts[j].level, opts[j].opt);
goto close;
}
}
}

test_daemon();
test_waitsig();

for (i = 0; i < ARRAY_SIZE(sk_confs); i++) {
for (j = 0; j < n_opts; j++) {
len = sizeof(int);
if (getsockopt(sk_confs[i].sk, opts[j].level, opts[j].opt, &val, &len) == -1) {
pr_perror("getsockopt(%d, %d) failed", opts[j].level, opts[j].opt);
goto close;
}

if (val != opts[j].val) {
fail("Unexpected value socket(%d,%d,%d) opts(%d,%d)", sk_confs[i].domain,
sk_confs[i].type, sk_confs[i].protocol, opts[j].level, opts[j].opt);
goto close;
}
}
}

pass();
exit_code = 0;
close:
for (i = 0; i < ARRAY_SIZE(sk_confs); i++)
close(sk_confs[i].sk);
return exit_code;
}
1 change: 1 addition & 0 deletions test/zdtm/static/sock_tcp_opts00.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'flags': 'suid'}
1 change: 1 addition & 0 deletions test/zdtm/static/sock_tcp_opts01.c
1 change: 1 addition & 0 deletions test/zdtm/static/sock_tcp_opts01.desc

0 comments on commit 516b369

Please sign in to comment.