17a338472SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26089ae0bSPaolo Bonzini /*
36089ae0bSPaolo Bonzini * tools/testing/selftests/kvm/lib/io.c
46089ae0bSPaolo Bonzini *
56089ae0bSPaolo Bonzini * Copyright (C) 2018, Google LLC.
66089ae0bSPaolo Bonzini */
76089ae0bSPaolo Bonzini
86089ae0bSPaolo Bonzini #include "test_util.h"
96089ae0bSPaolo Bonzini
106089ae0bSPaolo Bonzini /* Test Write
116089ae0bSPaolo Bonzini *
126089ae0bSPaolo Bonzini * A wrapper for write(2), that automatically handles the following
136089ae0bSPaolo Bonzini * special conditions:
146089ae0bSPaolo Bonzini *
156089ae0bSPaolo Bonzini * + Interrupted system call (EINTR)
166089ae0bSPaolo Bonzini * + Write of less than requested amount
176089ae0bSPaolo Bonzini * + Non-block return (EAGAIN)
186089ae0bSPaolo Bonzini *
196089ae0bSPaolo Bonzini * For each of the above, an additional write is performed to automatically
206089ae0bSPaolo Bonzini * continue writing the requested data.
216089ae0bSPaolo Bonzini * There are also many cases where write(2) can return an unexpected
226089ae0bSPaolo Bonzini * error (e.g. EIO). Such errors cause a TEST_ASSERT failure.
236089ae0bSPaolo Bonzini *
246089ae0bSPaolo Bonzini * Note, for function signature compatibility with write(2), this function
256089ae0bSPaolo Bonzini * returns the number of bytes written, but that value will always be equal
266089ae0bSPaolo Bonzini * to the number of requested bytes. All other conditions in this and
276089ae0bSPaolo Bonzini * future enhancements to this function either automatically issue another
286089ae0bSPaolo Bonzini * write(2) or cause a TEST_ASSERT failure.
296089ae0bSPaolo Bonzini *
306089ae0bSPaolo Bonzini * Args:
316089ae0bSPaolo Bonzini * fd - Opened file descriptor to file to be written.
326089ae0bSPaolo Bonzini * count - Number of bytes to write.
336089ae0bSPaolo Bonzini *
346089ae0bSPaolo Bonzini * Output:
356089ae0bSPaolo Bonzini * buf - Starting address of data to be written.
366089ae0bSPaolo Bonzini *
376089ae0bSPaolo Bonzini * Return:
386089ae0bSPaolo Bonzini * On success, number of bytes written.
396089ae0bSPaolo Bonzini * On failure, a TEST_ASSERT failure is caused.
406089ae0bSPaolo Bonzini */
test_write(int fd,const void * buf,size_t count)416089ae0bSPaolo Bonzini ssize_t test_write(int fd, const void *buf, size_t count)
426089ae0bSPaolo Bonzini {
436089ae0bSPaolo Bonzini ssize_t rc;
446089ae0bSPaolo Bonzini ssize_t num_written = 0;
456089ae0bSPaolo Bonzini size_t num_left = count;
466089ae0bSPaolo Bonzini const char *ptr = buf;
476089ae0bSPaolo Bonzini
486089ae0bSPaolo Bonzini /* Note: Count of zero is allowed (see "RETURN VALUE" portion of
496089ae0bSPaolo Bonzini * write(2) manpage for details.
506089ae0bSPaolo Bonzini */
516089ae0bSPaolo Bonzini TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
526089ae0bSPaolo Bonzini
536089ae0bSPaolo Bonzini do {
546089ae0bSPaolo Bonzini rc = write(fd, ptr, num_left);
556089ae0bSPaolo Bonzini
566089ae0bSPaolo Bonzini switch (rc) {
576089ae0bSPaolo Bonzini case -1:
586089ae0bSPaolo Bonzini TEST_ASSERT(errno == EAGAIN || errno == EINTR,
596089ae0bSPaolo Bonzini "Unexpected write failure,\n"
606089ae0bSPaolo Bonzini " rc: %zi errno: %i", rc, errno);
616089ae0bSPaolo Bonzini continue;
626089ae0bSPaolo Bonzini
636089ae0bSPaolo Bonzini case 0:
64*352be2c5SWainer dos Santos Moschetta TEST_FAIL("Unexpected EOF,\n"
656089ae0bSPaolo Bonzini " rc: %zi num_written: %zi num_left: %zu",
666089ae0bSPaolo Bonzini rc, num_written, num_left);
676089ae0bSPaolo Bonzini break;
686089ae0bSPaolo Bonzini
696089ae0bSPaolo Bonzini default:
706089ae0bSPaolo Bonzini TEST_ASSERT(rc >= 0, "Unexpected ret from write,\n"
716089ae0bSPaolo Bonzini " rc: %zi errno: %i", rc, errno);
726089ae0bSPaolo Bonzini num_written += rc;
736089ae0bSPaolo Bonzini num_left -= rc;
746089ae0bSPaolo Bonzini ptr += rc;
756089ae0bSPaolo Bonzini break;
766089ae0bSPaolo Bonzini }
776089ae0bSPaolo Bonzini } while (num_written < count);
786089ae0bSPaolo Bonzini
796089ae0bSPaolo Bonzini return num_written;
806089ae0bSPaolo Bonzini }
816089ae0bSPaolo Bonzini
826089ae0bSPaolo Bonzini /* Test Read
836089ae0bSPaolo Bonzini *
846089ae0bSPaolo Bonzini * A wrapper for read(2), that automatically handles the following
856089ae0bSPaolo Bonzini * special conditions:
866089ae0bSPaolo Bonzini *
876089ae0bSPaolo Bonzini * + Interrupted system call (EINTR)
886089ae0bSPaolo Bonzini * + Read of less than requested amount
896089ae0bSPaolo Bonzini * + Non-block return (EAGAIN)
906089ae0bSPaolo Bonzini *
916089ae0bSPaolo Bonzini * For each of the above, an additional read is performed to automatically
926089ae0bSPaolo Bonzini * continue reading the requested data.
936089ae0bSPaolo Bonzini * There are also many cases where read(2) can return an unexpected
946089ae0bSPaolo Bonzini * error (e.g. EIO). Such errors cause a TEST_ASSERT failure. Note,
956089ae0bSPaolo Bonzini * it is expected that the file opened by fd at the current file position
966089ae0bSPaolo Bonzini * contains at least the number of requested bytes to be read. A TEST_ASSERT
976089ae0bSPaolo Bonzini * failure is produced if an End-Of-File condition occurs, before all the
986089ae0bSPaolo Bonzini * data is read. It is the callers responsibility to assure that sufficient
996089ae0bSPaolo Bonzini * data exists.
1006089ae0bSPaolo Bonzini *
1016089ae0bSPaolo Bonzini * Note, for function signature compatibility with read(2), this function
1026089ae0bSPaolo Bonzini * returns the number of bytes read, but that value will always be equal
1036089ae0bSPaolo Bonzini * to the number of requested bytes. All other conditions in this and
1046089ae0bSPaolo Bonzini * future enhancements to this function either automatically issue another
1056089ae0bSPaolo Bonzini * read(2) or cause a TEST_ASSERT failure.
1066089ae0bSPaolo Bonzini *
1076089ae0bSPaolo Bonzini * Args:
1086089ae0bSPaolo Bonzini * fd - Opened file descriptor to file to be read.
1096089ae0bSPaolo Bonzini * count - Number of bytes to read.
1106089ae0bSPaolo Bonzini *
1116089ae0bSPaolo Bonzini * Output:
1126089ae0bSPaolo Bonzini * buf - Starting address of where to write the bytes read.
1136089ae0bSPaolo Bonzini *
1146089ae0bSPaolo Bonzini * Return:
1156089ae0bSPaolo Bonzini * On success, number of bytes read.
1166089ae0bSPaolo Bonzini * On failure, a TEST_ASSERT failure is caused.
1176089ae0bSPaolo Bonzini */
test_read(int fd,void * buf,size_t count)1186089ae0bSPaolo Bonzini ssize_t test_read(int fd, void *buf, size_t count)
1196089ae0bSPaolo Bonzini {
1206089ae0bSPaolo Bonzini ssize_t rc;
1216089ae0bSPaolo Bonzini ssize_t num_read = 0;
1226089ae0bSPaolo Bonzini size_t num_left = count;
1236089ae0bSPaolo Bonzini char *ptr = buf;
1246089ae0bSPaolo Bonzini
1256089ae0bSPaolo Bonzini /* Note: Count of zero is allowed (see "If count is zero" portion of
1266089ae0bSPaolo Bonzini * read(2) manpage for details.
1276089ae0bSPaolo Bonzini */
1286089ae0bSPaolo Bonzini TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
1296089ae0bSPaolo Bonzini
1306089ae0bSPaolo Bonzini do {
1316089ae0bSPaolo Bonzini rc = read(fd, ptr, num_left);
1326089ae0bSPaolo Bonzini
1336089ae0bSPaolo Bonzini switch (rc) {
1346089ae0bSPaolo Bonzini case -1:
1356089ae0bSPaolo Bonzini TEST_ASSERT(errno == EAGAIN || errno == EINTR,
1366089ae0bSPaolo Bonzini "Unexpected read failure,\n"
1376089ae0bSPaolo Bonzini " rc: %zi errno: %i", rc, errno);
1386089ae0bSPaolo Bonzini break;
1396089ae0bSPaolo Bonzini
1406089ae0bSPaolo Bonzini case 0:
141*352be2c5SWainer dos Santos Moschetta TEST_FAIL("Unexpected EOF,\n"
1426089ae0bSPaolo Bonzini " rc: %zi num_read: %zi num_left: %zu",
1436089ae0bSPaolo Bonzini rc, num_read, num_left);
1446089ae0bSPaolo Bonzini break;
1456089ae0bSPaolo Bonzini
1466089ae0bSPaolo Bonzini default:
1476089ae0bSPaolo Bonzini TEST_ASSERT(rc > 0, "Unexpected ret from read,\n"
1486089ae0bSPaolo Bonzini " rc: %zi errno: %i", rc, errno);
1496089ae0bSPaolo Bonzini num_read += rc;
1506089ae0bSPaolo Bonzini num_left -= rc;
1516089ae0bSPaolo Bonzini ptr += rc;
1526089ae0bSPaolo Bonzini break;
1536089ae0bSPaolo Bonzini }
1546089ae0bSPaolo Bonzini } while (num_read < count);
1556089ae0bSPaolo Bonzini
1566089ae0bSPaolo Bonzini return num_read;
1576089ae0bSPaolo Bonzini }
158