Skip to content

Commit b9fe11c

Browse files
cfriedtcarlescufi
authored andcommitted
tests: net: socketpair: use do not alias read and write
* do not define `read`, `write`, as different symbols * use `recv`, and `send` as defined by `CONFIG_NET_SOCKETS_POSIX_NAMES` * do not specify `(2)` as manual-page sections are somewhat irrelevant Note: we use `CONFIG_NET_SOCKETS_POSIX_NAMES` here, and `CONFIG_POSIX_API` in `samples/` to show that the socketpair API is available in both configurations. Signed-off-by: Chris Friedt <[email protected]>
1 parent c73b8ed commit b9fe11c

File tree

6 files changed

+78
-115
lines changed

6 files changed

+78
-115
lines changed

tests/net/socket/socketpair/src/_main.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
#include <zephyr/posix/unistd.h>
2323
#endif
2424

25-
#undef read
26-
#define read(fd, buf, len) zsock_recv(fd, buf, len, 0)
27-
28-
#undef write
29-
#define write(fd, buf, len) zsock_send(fd, buf, len, 0)
30-
3125
LOG_MODULE_DECLARE(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
3226

3327
extern struct k_work_q test_socketpair_work_q;

tests/net/socket/socketpair/src/block.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ static void work_handler(struct k_work *work)
4343
LOG_DBG("%sing 1 byte %s fd %d", ctx.write ? "read" : "writ",
4444
ctx.write ? "from" : "to", ctx.fd);
4545
if (ctx.write) {
46-
res = read(ctx.fd, &c, 1);
46+
res = recv(ctx.fd, &c, 1, 0);
4747
} else {
48-
res = write(ctx.fd, "x", 1);
48+
res = send(ctx.fd, "x", 1, 0);
4949
}
5050
if (-1 == res || 1 != res) {
51-
LOG_DBG("%s(2) failed: %d", ctx.write ? "read" : "write",
52-
errno);
51+
LOG_DBG("%s() failed: %d", ctx.write ? "recv" : "send", errno);
5352
} else {
5453
LOG_DBG("%s 1 byte", ctx.write ? "read" : "wrote");
5554
}
@@ -62,7 +61,7 @@ ZTEST(net_socketpair, write_block)
6261

6362
LOG_DBG("creating socketpair..");
6463
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
65-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
64+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
6665

6766
for (size_t i = 0; i < 2; ++i) {
6867

@@ -81,9 +80,8 @@ ZTEST(net_socketpair, write_block)
8180
for (ctx.m = 0; atomic_get(&ctx.m)
8281
< CONFIG_NET_SOCKETPAIR_BUFFER_SIZE;) {
8382

84-
res = write(sv[i], "x", 1);
85-
zassert_not_equal(res, -1, "write(2) failed: %d",
86-
errno);
83+
res = send(sv[i], "x", 1, 0);
84+
zassert_not_equal(res, -1, "send() failed: %d", errno);
8785
zassert_equal(res, 1, "wrote %d bytes instead of 1",
8886
res);
8987

@@ -93,8 +91,8 @@ ZTEST(net_socketpair, write_block)
9391

9492
/* try to write one more byte */
9593
LOG_DBG("writing to fd %d", sv[i]);
96-
res = write(sv[i], "x", 1);
97-
zassert_not_equal(res, -1, "write(2) failed: %d", errno);
94+
res = send(sv[i], "x", 1, 0);
95+
zassert_not_equal(res, -1, "send() failed: %d", errno);
9896
zassert_equal(res, 1, "wrote %d bytes instead of 1", res);
9997

10098
LOG_DBG("success!");
@@ -112,7 +110,7 @@ ZTEST(net_socketpair, read_block)
112110

113111
LOG_DBG("creating socketpair..");
114112
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
115-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
113+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
116114

117115
for (size_t i = 0; i < 2; ++i) {
118116

@@ -130,8 +128,8 @@ ZTEST(net_socketpair, read_block)
130128
/* try to read one byte */
131129
LOG_DBG("reading from fd %d", sv[i]);
132130
x = '\0';
133-
res = read(sv[i], &x, 1);
134-
zassert_not_equal(res, -1, "read(2) failed: %d", errno);
131+
res = recv(sv[i], &x, 1, 0);
132+
zassert_not_equal(res, -1, "recv() failed: %d", errno);
135133
zassert_equal(res, 1, "read %d bytes instead of 1", res);
136134

137135
LOG_DBG("success!");

tests/net/socket/socketpair/src/closed_ends.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ ZTEST_USER(net_socketpair, close_one_end_and_write_to_the_other)
1212

1313
for (size_t i = 0; i < 2; ++i) {
1414
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
15-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
15+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
1616

1717
res = close(sv[i]);
1818
zassert_equal(res, 0, "close(sv[%u]) failed: %d", i, errno);
1919

20-
res = write(sv[(!i) & 1], "x", 1);
21-
zassert_equal(res, -1, "expected write(2) to fail");
20+
res = send(sv[(!i) & 1], "x", 1, 0);
21+
zassert_equal(res, -1, "expected send() to fail");
2222
zassert_equal(res, -1, "errno: expected: EPIPE actual: %d",
2323
errno);
2424

@@ -35,29 +35,28 @@ ZTEST_USER(net_socketpair, close_one_end_and_read_from_the_other)
3535

3636
for (size_t i = 0; i < 2; ++i) {
3737
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
38-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
38+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
3939

4040
/* We want to write some bytes to the closing end of the
4141
* socket before it gets shut down, so that we can prove that
4242
* reading is possible from the other end still and that data
4343
* is not lost.
4444
*/
45-
res = write(sv[i], "xx", 2);
46-
zassert_not_equal(res, -1, "write(2) failed: %d", errno);
47-
zassert_equal(res, 2, "write(2) failed to write 2 bytes");
45+
res = send(sv[i], "xx", 2, 0);
46+
zassert_not_equal(res, -1, "send() failed: %d", errno);
47+
zassert_equal(res, 2, "write() failed to write 2 bytes");
4848

4949
res = close(sv[i]);
5050
zassert_equal(res, 0, "close(sv[%u]) failed: %d", i, errno);
5151

5252
memset(xx, 0, sizeof(xx));
53-
res = read(sv[(!i) & 1], xx, sizeof(xx));
54-
zassert_not_equal(res, -1, "read(2) failed: %d", errno);
53+
res = recv(sv[(!i) & 1], xx, sizeof(xx), 0);
54+
zassert_not_equal(res, -1, "read() failed: %d", errno);
5555
zassert_equal(res, 2, "expected to read 2 bytes but read %d",
5656
res);
5757

58-
res = read(sv[(!i) & 1], xx, sizeof(xx));
59-
zassert_equal(res, 0,
60-
"expected read(2) to succeed but read 0 bytes");
58+
res = recv(sv[(!i) & 1], xx, sizeof(xx), 0);
59+
zassert_equal(res, 0, "expected read() to succeed but read 0 bytes");
6160

6261
res = close(sv[(!i) & 1]);
6362
zassert_equal(res, 0, "close(sv[%u]) failed: %d", i, errno);

tests/net/socket/socketpair/src/happy_path.c

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,12 @@ static void happy_path(
3535
for (int i = 0; i < 2; ++i) {
3636

3737
/*
38-
* Test with write(2) / read(2)
39-
*/
40-
41-
LOG_DBG("calling write(%d, '%s', %u)", sv[i], expected_msg,
42-
expected_msg_len);
43-
res = write(sv[i], expected_msg, expected_msg_len);
44-
45-
zassert_not_equal(res, -1, "write(2) failed: %d", errno);
46-
actual_msg_len = res;
47-
zassert_equal(actual_msg_len, expected_msg_len,
48-
"did not write entire message");
49-
50-
memset(actual_msg, 0, sizeof(actual_msg));
51-
52-
LOG_DBG("calling read(%d, %p, %u)", sv[i], actual_msg,
53-
(unsigned int)sizeof(actual_msg));
54-
res = read(sv[(!i) & 1], actual_msg, sizeof(actual_msg));
55-
56-
zassert_not_equal(res, -1, "read(2) failed: %d", errno);
57-
actual_msg_len = res;
58-
zassert_equal(actual_msg_len, expected_msg_len,
59-
"wrong return value");
60-
61-
zassert_true(strncmp(expected_msg, actual_msg,
62-
actual_msg_len) == 0,
63-
"the wrong message was passed through the socketpair");
64-
65-
/*
66-
* Test with send(2) / recv(2)
38+
* Test with send() / recv()
6739
*/
6840

6941
res = send(sv[i], expected_msg, expected_msg_len, 0);
7042

71-
zassert_not_equal(res, -1, "send(2) failed: %d", errno);
43+
zassert_not_equal(res, -1, "send() failed: %d", errno);
7244
actual_msg_len = res;
7345
zassert_equal(actual_msg_len, expected_msg_len,
7446
"did not send entire message");
@@ -77,7 +49,7 @@ static void happy_path(
7749

7850
res = recv(sv[(!i) & 1], actual_msg, sizeof(actual_msg), 0);
7951

80-
zassert_not_equal(res, -1, "recv(2) failed: %d", errno);
52+
zassert_not_equal(res, -1, "recv() failed: %d", errno);
8153
actual_msg_len = res;
8254
zassert_equal(actual_msg_len, expected_msg_len,
8355
"wrong return value");
@@ -92,7 +64,7 @@ static void happy_path(
9264

9365
res = sendto(sv[i], expected_msg, expected_msg_len, 0, NULL, 0);
9466

95-
zassert_not_equal(res, -1, "sendto(2) failed: %d", errno);
67+
zassert_not_equal(res, -1, "sendto() failed: %d", errno);
9668
actual_msg_len = res;
9769
zassert_equal(actual_msg_len, expected_msg_len,
9870
"did not sendto entire message");
@@ -102,7 +74,7 @@ static void happy_path(
10274
len = 0;
10375
res = recvfrom(sv[(!i) & 1], actual_msg, sizeof(actual_msg), 0,
10476
NULL, &len);
105-
zassert_true(res >= 0, "recvfrom(2) failed: %d", errno);
77+
zassert_true(res >= 0, "recvfrom() failed: %d", errno);
10678
actual_msg_len = res;
10779
zassert_equal(actual_msg_len, expected_msg_len,
10880
"wrong return value");
@@ -123,14 +95,14 @@ static void happy_path(
12395

12496
res = sendmsg(sv[i], &msghdr, 0);
12597

126-
zassert_not_equal(res, -1, "sendmsg(2) failed: %d", errno);
98+
zassert_not_equal(res, -1, "sendmsg() failed: %d", errno);
12799
actual_msg_len = res;
128100
zassert_equal(actual_msg_len, expected_msg_len,
129101
"did not sendmsg entire message");
130102

131-
res = read(sv[(!i) & 1], actual_msg, sizeof(actual_msg));
103+
res = recv(sv[(!i) & 1], actual_msg, sizeof(actual_msg), 0);
132104

133-
zassert_not_equal(res, -1, "read(2) failed: %d", errno);
105+
zassert_not_equal(res, -1, "recv() failed: %d", errno);
134106
actual_msg_len = res;
135107
zassert_equal(actual_msg_len, expected_msg_len,
136108
"wrong return value");

tests/net/socket/socketpair/src/nonblock.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ ZTEST_USER(net_socketpair, write_nonblock)
1212
int sv[2] = {-1, -1};
1313

1414
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
15-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
15+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
1616

1717
for (size_t i = 0; i < 2; ++i) {
1818
/* first, fill up the buffer */
1919
for (size_t k = 0; k < CONFIG_NET_SOCKETPAIR_BUFFER_SIZE;
2020
++k) {
21-
res = write(sv[i], "x", 1);
22-
zassert_equal(res, 1, "write(2) failed: %d", errno);
21+
res = send(sv[i], "x", 1, 0);
22+
zassert_equal(res, 1, "send() failed: %d", errno);
2323
}
2424

2525
/* then set the O_NONBLOCK flag */
@@ -30,8 +30,8 @@ ZTEST_USER(net_socketpair, write_nonblock)
3030
zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
3131

3232
/* then, try to write one more byte */
33-
res = write(sv[i], "x", 1);
34-
zassert_equal(res, -1, "expected write to fail");
33+
res = send(sv[i], "x", 1, 0);
34+
zassert_equal(res, -1, "expected send to fail");
3535
zassert_equal(errno, EAGAIN, "errno: expected: EAGAIN "
3636
"actual: %d", errno);
3737
}
@@ -47,7 +47,7 @@ ZTEST_USER(net_socketpair, read_nonblock)
4747
char c;
4848

4949
res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
50-
zassert_equal(res, 0, "socketpair(2) failed: %d", errno);
50+
zassert_equal(res, 0, "socketpair() failed: %d", errno);
5151

5252
for (size_t i = 0; i < 2; ++i) {
5353
/* set the O_NONBLOCK flag */
@@ -58,8 +58,8 @@ ZTEST_USER(net_socketpair, read_nonblock)
5858
zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
5959

6060
/* then, try to read one byte */
61-
res = read(sv[i], &c, 1);
62-
zassert_equal(res, -1, "expected read to fail");
61+
res = recv(sv[i], &c, 1, 0);
62+
zassert_equal(res, -1, "expected recv() to fail");
6363
zassert_equal(errno, EAGAIN, "errno: expected: EAGAIN "
6464
"actual: %d", errno);
6565
}

0 commit comments

Comments
 (0)