|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Friedt Professional Engineering Services, Inc |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <fcntl.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#include <logging/log.h> |
| 11 | +LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL); |
| 12 | + |
| 13 | +#include <net/socket.h> |
| 14 | +#include <sys/util.h> |
| 15 | +#include <posix/unistd.h> |
| 16 | + |
| 17 | +#include <ztest_assert.h> |
| 18 | + |
| 19 | +#undef read |
| 20 | +#define read(fd, buf, len) zsock_recv(fd, buf, len, 0) |
| 21 | + |
| 22 | +#undef write |
| 23 | +#define write(fd, buf, len) zsock_send(fd, buf, len, 0) |
| 24 | + |
| 25 | +#define TIMEOUT K_FOREVER |
| 26 | + |
| 27 | +struct ctx { |
| 28 | + /* true if test is write_block(), false if test is read_block() */ |
| 29 | + bool write; |
| 30 | + /* the secondary-side socket of the socketpair */ |
| 31 | + int fd; |
| 32 | + /* the count of the main thread */ |
| 33 | + atomic_t m; |
| 34 | + /* the count of the secondary thread */ |
| 35 | + size_t n; |
| 36 | + /* true when secondary thread is done */ |
| 37 | + bool done; |
| 38 | + /* true if both main and secondary thread should immediately quit */ |
| 39 | + bool fail; |
| 40 | + /* thread id of the secondary thread */ |
| 41 | + k_tid_t tid; |
| 42 | +}; |
| 43 | +ZTEST_BMEM struct ctx ctx; |
| 44 | +#define STACK_SIZE 512 |
| 45 | +/* thread structure for secondary thread */ |
| 46 | +ZTEST_BMEM struct k_thread th; |
| 47 | +/* stack for the secondary thread */ |
| 48 | +static K_THREAD_STACK_DEFINE(th_stack, STACK_SIZE); |
| 49 | + |
| 50 | +static void th_fun(void *arg0, void *arg1, void *arg2) |
| 51 | +{ |
| 52 | + (void) arg0; |
| 53 | + (void) arg1; |
| 54 | + (void) arg2; |
| 55 | + |
| 56 | + int res; |
| 57 | + char c = '\0'; |
| 58 | + |
| 59 | + LOG_DBG("secondary thread running"); |
| 60 | + |
| 61 | + while (true) { |
| 62 | + if (ctx.write) { |
| 63 | + LOG_DBG("ctx.m: %u", ctx.m); |
| 64 | + if (atomic_get(&ctx.m) |
| 65 | + < CONFIG_NET_SOCKETPAIR_BUFFER_SIZE) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + LOG_DBG("ready to read!"); |
| 69 | + } else { |
| 70 | + LOG_DBG("sleeping for 100ms.."); |
| 71 | + k_sleep(K_MSEC(100)); |
| 72 | + LOG_DBG("ready to write!"); |
| 73 | + } |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + LOG_DBG("%sing 1 byte %s fd %d", ctx.write ? "read" : "write", |
| 78 | + ctx.write ? "from" : "to", ctx.fd); |
| 79 | + if (ctx.write) { |
| 80 | + res = read(ctx.fd, &c, 1); |
| 81 | + } else { |
| 82 | + res = write(ctx.fd, "x", 1); |
| 83 | + } |
| 84 | + if (-1 == res || 1 != res) { |
| 85 | + LOG_DBG("%s(2) failed: %d", ctx.write ? "read" : "write", |
| 86 | + errno); |
| 87 | + goto out; |
| 88 | + } |
| 89 | + LOG_DBG("%s 1 byte", ctx.write ? "read" : "wrote"); |
| 90 | + ctx.n = 1; |
| 91 | + |
| 92 | +out: |
| 93 | + ctx.done = true; |
| 94 | + |
| 95 | + LOG_DBG("terminating.."); |
| 96 | +} |
| 97 | + |
| 98 | +void test_socketpair_write_block(void) |
| 99 | +{ |
| 100 | + int res; |
| 101 | + int sv[2] = {-1, -1}; |
| 102 | + |
| 103 | + LOG_DBG("creating socketpair.."); |
| 104 | + res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| 105 | + zassert_equal(res, 0, "socketpair(2) failed: %d", errno); |
| 106 | + |
| 107 | + for (size_t i = 0; i < 2; ++i) { |
| 108 | + |
| 109 | + LOG_DBG("data direction %d -> %d", sv[i], sv[(!i) & 1]); |
| 110 | + |
| 111 | + LOG_DBG("setting up context"); |
| 112 | + memset(&ctx, 0, sizeof(ctx)); |
| 113 | + ctx.write = true; |
| 114 | + ctx.fd = sv[(!i) & 1]; |
| 115 | + |
| 116 | + LOG_DBG("creating secondary thread"); |
| 117 | + ctx.tid = k_thread_create(&th, th_stack, |
| 118 | + STACK_SIZE, th_fun, |
| 119 | + NULL, NULL, NULL, |
| 120 | + CONFIG_MAIN_THREAD_PRIORITY, |
| 121 | + K_INHERIT_PERMS, K_NO_WAIT); |
| 122 | + LOG_DBG("created secondary thread %p", ctx.tid); |
| 123 | + |
| 124 | + /* fill up the buffer */ |
| 125 | + for (ctx.m = 0; atomic_get(&ctx.m) |
| 126 | + < CONFIG_NET_SOCKETPAIR_BUFFER_SIZE;) { |
| 127 | + |
| 128 | + res = write(sv[i], "x", 1); |
| 129 | + zassert_not_equal(res, -1, "write(2) failed: %d", |
| 130 | + errno); |
| 131 | + zassert_equal(res, 1, "wrote %d bytes instead of 1", |
| 132 | + res); |
| 133 | + |
| 134 | + atomic_inc(&ctx.m); |
| 135 | + LOG_DBG("have written %u bytes", ctx.m); |
| 136 | + } |
| 137 | + |
| 138 | + /* try to write one more byte */ |
| 139 | + LOG_DBG("writing to fd %d", sv[i]); |
| 140 | + res = write(sv[i], "x", 1); |
| 141 | + zassert_not_equal(res, -1, "write(2) failed: %d", errno); |
| 142 | + zassert_equal(res, 1, "wrote %d bytes instead of 1", res); |
| 143 | + |
| 144 | + LOG_DBG("success!"); |
| 145 | + |
| 146 | + k_thread_join(&th, K_MSEC(1000)); |
| 147 | + } |
| 148 | + |
| 149 | + close(sv[0]); |
| 150 | + close(sv[1]); |
| 151 | +} |
| 152 | + |
| 153 | +void test_socketpair_read_block(void) |
| 154 | +{ |
| 155 | + int res; |
| 156 | + int sv[2] = {-1, -1}; |
| 157 | + char x; |
| 158 | + |
| 159 | + LOG_DBG("creating socketpair.."); |
| 160 | + res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| 161 | + zassert_equal(res, 0, "socketpair(2) failed: %d", errno); |
| 162 | + |
| 163 | + for (size_t i = 0; i < 2; ++i) { |
| 164 | + |
| 165 | + LOG_DBG("data direction %d -> %d", sv[i], sv[(!i) & 1]); |
| 166 | + |
| 167 | + LOG_DBG("setting up context"); |
| 168 | + memset(&ctx, 0, sizeof(ctx)); |
| 169 | + ctx.write = false; |
| 170 | + ctx.fd = sv[(!i) & 1]; |
| 171 | + |
| 172 | + LOG_DBG("creating secondary thread"); |
| 173 | + ctx.tid = k_thread_create(&th, th_stack, |
| 174 | + STACK_SIZE, th_fun, |
| 175 | + NULL, NULL, NULL, |
| 176 | + CONFIG_MAIN_THREAD_PRIORITY, |
| 177 | + K_INHERIT_PERMS, K_NO_WAIT); |
| 178 | + LOG_DBG("created secondary thread %p", ctx.tid); |
| 179 | + |
| 180 | + /* try to read one byte */ |
| 181 | + LOG_DBG("reading from fd %d", sv[i]); |
| 182 | + x = '\0'; |
| 183 | + res = read(sv[i], &x, 1); |
| 184 | + zassert_not_equal(res, -1, "read(2) failed: %d", errno); |
| 185 | + zassert_equal(res, 1, "read %d bytes instead of 1", res); |
| 186 | + |
| 187 | + LOG_DBG("success!"); |
| 188 | + |
| 189 | + k_thread_join(&th, K_MSEC(1000)); |
| 190 | + } |
| 191 | + |
| 192 | + close(sv[0]); |
| 193 | + close(sv[1]); |
| 194 | +} |
0 commit comments