t_aio_rw.c revision 1.1 1 /* $NetBSD: t_aio_rw.c,v 1.1 2025/10/10 15:53:55 christos Exp $ */
2
3 /*
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <atf-c.h>
31
32 #include <aio.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 static int mktemp_file(char *, size_t);
46 static void fill_pattern(uint8_t *, size_t, uint8_t);
47 static void wait_all(const struct aiocb * const [], size_t);
48
49 static int
50 mktemp_file(char *path, size_t pathlen)
51 {
52 int fd, n;
53
54 n = snprintf(path, pathlen, "t_aio_rw.XXXXXX");
55 ATF_REQUIRE(n > 0 && (size_t)n < pathlen);
56
57 fd = mkstemp(path);
58 ATF_REQUIRE(fd >= 0);
59
60 return fd;
61 }
62
63 static void
64 fill_pattern(uint8_t *buf, size_t len, uint8_t seed)
65 {
66 size_t i;
67
68 for (i = 0; i < len; i++) {
69 buf[i] = (uint8_t)(seed + (i & 0xff));
70 }
71 }
72
73 static void
74 wait_all(const struct aiocb * const list[], size_t nent)
75 {
76 size_t i;
77 int pending, rv, error;
78
79 for (;;) {
80 pending = 0;
81
82 for (i = 0; i < nent; i++) {
83 if (list[i] == NULL) {
84 continue;
85 }
86
87 error = aio_error(list[i]);
88 if (error == EINPROGRESS) {
89 pending = 1;
90 }
91 }
92
93 if (!pending) {
94 break;
95 }
96
97 rv = aio_suspend(list, (int)nent, NULL);
98 ATF_REQUIRE_EQ_MSG(0, rv, "aio_suspend failed: %s",
99 strerror(errno));
100 }
101 }
102
103 /*
104 * write_then_read_back
105 * Write a block then read it back asynchronously and compare.
106 */
107 ATF_TC_WITHOUT_HEAD(write_then_read_back);
108 ATF_TC_BODY(write_then_read_back, tc)
109 {
110 char path[64];
111 int fd, rv;
112 const size_t blksz = 0x2000;
113 uint8_t *wbuf, *rbuf;
114 struct aiocb wcb, rcb;
115 const struct aiocb *wlist[1], *rlist[1];
116
117 fd = mktemp_file(path, sizeof(path));
118
119 wbuf = malloc(blksz);
120 rbuf = calloc(1, blksz);
121 ATF_REQUIRE(wbuf != NULL && rbuf != NULL);
122
123 fill_pattern(wbuf, blksz, 0xA0);
124
125 memset(&wcb, 0, sizeof(wcb));
126 wcb.aio_fildes = fd;
127 wcb.aio_buf = wbuf;
128 wcb.aio_nbytes = blksz;
129 wcb.aio_offset = 0;
130
131 rv = aio_write(&wcb);
132 ATF_REQUIRE_EQ(0, rv);
133 wlist[0] = &wcb;
134 wait_all(wlist, 1);
135
136 ATF_REQUIRE_EQ(0, aio_error(&wcb));
137 ATF_REQUIRE_EQ((ssize_t)blksz, aio_return(&wcb));
138
139 memset(&rcb, 0, sizeof(rcb));
140 rcb.aio_fildes = fd;
141 rcb.aio_buf = rbuf;
142 rcb.aio_nbytes = blksz;
143 rcb.aio_offset = 0;
144
145 rv = aio_read(&rcb);
146 ATF_REQUIRE_EQ(0, rv);
147 rlist[0] = &rcb;
148 wait_all(rlist, 1);
149
150 ATF_REQUIRE_EQ(0, aio_error(&rcb));
151 ATF_REQUIRE_EQ((ssize_t)blksz, aio_return(&rcb));
152 ATF_REQUIRE_EQ(0, memcmp(wbuf, rbuf, blksz));
153
154 rv = close(fd);
155 ATF_REQUIRE_EQ(0, rv);
156 rv = unlink(path);
157 ATF_REQUIRE_EQ(0, rv);
158
159 free(wbuf);
160 free(rbuf);
161 }
162
163 ATF_TP_ADD_TCS(tp)
164 {
165 ATF_TP_ADD_TC(tp, write_then_read_back);
166 return atf_no_error();
167 }
168