1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
* Author: Colin King <colin.king@canonical.com>
*
* Copyright (C) 2012 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#define TEST_PASSED (0)
#define TEST_FAILED (1)
#define TEST_ERROR (2)
/*
* Attempt to create a file with lots of holes, a bit like downloading
* an ISO with a torrent client with many multiple random writes
*/
#define BUF_SZ 32
#define DEFAULT_SIZE (660*1024) /* CD-ROM Size */
int test_write(int fd, char *buffer, size_t len, off_t offset)
{
ssize_t rc;
if (lseek(fd, offset, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to position %jd: %s\n",
(intmax_t)offset, strerror(errno));
return TEST_FAILED;
}
rc = write(fd, buffer, len);
if (rc < 0) {
fprintf(stderr, "Failed to write %zu bytes, position %jd: %s\n",
len, (intmax_t)offset, strerror(errno));
return TEST_FAILED;
} else if (((size_t)rc) < len) {
fprintf(stderr, "Failed to write %zu bytes, position %jd: short write of %zd bytes\n",
len, (intmax_t)offset, rc);
return TEST_FAILED;
}
return TEST_PASSED;
}
int test_read(int fd, char *buffer, size_t len, off_t offset)
{
ssize_t rc;
if (lseek(fd, offset, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to position %jd: %s\n",
(intmax_t)offset, strerror(errno));
return TEST_FAILED;
}
rc = read(fd, buffer, len);
if (rc < 0) {
fprintf(stderr, "Failed to read %zu bytes, position %jd: %s\n",
len, (intmax_t)offset, strerror(errno));
return TEST_FAILED;
} else if (((size_t)rc) < len) {
fprintf(stderr, "Failed to read %zu bytes, position %jd: short read of %zd bytes\n",
len, (intmax_t)offset, rc);
return TEST_FAILED;
}
return TEST_PASSED;
}
int test_write_read(int fd, char *buffer1, size_t len, off_t offset)
{
char buffer2[BUF_SZ];
int ret;
memset(buffer2, 0, BUF_SZ);
if ((ret = test_write(fd, buffer1, BUF_SZ, offset)) != 0)
return ret;
if ((ret = test_read(fd, buffer2, BUF_SZ, offset)) != 0)
return ret;
if (memcmp(buffer1, buffer2, BUF_SZ)) {
fprintf(stderr, "Data read is not same as data written, offset = %jd",
(intmax_t)offset);
return TEST_FAILED;
}
return TEST_PASSED;
}
int test_exercise(char *filename, off_t max_offset, char data)
{
int fd;
int i;
int ret = TEST_FAILED;
struct stat statbuf;
char buffer1[BUF_SZ];
memset(buffer1, data, BUF_SZ);
srandom((unsigned int)max_offset);
unlink(filename);
if ((fd = open(filename, O_RDWR | O_CREAT, 0600)) < 0) {
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
return TEST_FAILED;
}
if (test_write_read(fd, buffer1, BUF_SZ, 0))
goto finish;
for (i=0; i<8192; i++) {
off_t offset = (off_t)random() % max_offset;
if (test_write_read(fd, buffer1, BUF_SZ, offset))
goto finish;
}
if (test_write_read(fd, buffer1, BUF_SZ, max_offset))
goto finish;
srandom((unsigned int)max_offset);
for (i=0; i<8192; i++) {
char buffer2[BUF_SZ];
off_t offset = (off_t)random() % max_offset;
if (test_read(fd, buffer2, BUF_SZ, offset))
goto finish;
if (memcmp(buffer1, buffer2, BUF_SZ)) {
fprintf(stderr, "Data read is not same as data written, offset = %jd",
(intmax_t)offset);
goto finish;
}
}
if (fstat(fd, &statbuf) < 0) {
fprintf(stderr, "Failed to fstat file %s: %s\n", filename, strerror(errno));
goto finish;
}
if (statbuf.st_size != max_offset + BUF_SZ) {
fprintf(stderr, "Filesize was %jd and not %jd\n",
(intmax_t)statbuf.st_size,
(intmax_t)(max_offset + BUF_SZ));
goto finish;
}
ret = TEST_PASSED;
finish:
if (close(fd) < 0) {
fprintf(stderr, "Failed to close %s: %s\n", filename, strerror(errno));
return TEST_FAILED;
}
if (unlink(filename) < 0) {
fprintf(stderr, "Failed to unlink %s: %s\n", filename, strerror(errno));
return TEST_FAILED;
}
return ret;
}
void sighandler(int dummy)
{
exit(TEST_ERROR);
}
int main(int argc, char **argv)
{
off_t len = DEFAULT_SIZE;
int i;
int ret;
if (argc < 2) {
fprintf(stderr, "Syntax: filename [size_in_K]\n");
exit(TEST_ERROR);
}
if (argc == 3) {
len = atoll(argv[2]);
if (len < 1) {
fprintf(stderr, "size should be > 0\n");
exit(TEST_ERROR);
}
}
len *= 1024;
signal(SIGINT, sighandler);
for (i=0; i < 2; i++) {
ret = test_exercise(argv[1], len, i + '@');
if (ret != TEST_PASSED)
exit(ret);
}
exit(TEST_PASSED);
}
|