summaryrefslogtreecommitdiffstats
path: root/tests/encryption/frag.c
blob: 86da037c6074e8f818068d64eaead51df8dde46d (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
  Copyright (c) 2008-2013 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>

#define MAX_NUM_OPS (1 << 20)
#define MAX_FILE_SIZE (1 << 30)

typedef enum {
	READ_OP,
	WRITE_OP,
	TRUNC_OP,
	LAST_OP
} frag_op;

struct frag_ctx {
	int test_fd;
	int good_fd;
	char *test_buf;
	char *good_buf;
	char *content;
	int max_file_size;
};

typedef int (*frag_op_t)(struct frag_ctx *ctx, off_t offset, size_t count);

static int doread(int fd, off_t offset, size_t count,
		  char *buf, int max_file_size)
{
	int ret = 0;
	int was_read = 0;

	if (lseek(fd, offset, SEEK_SET) == -1) {
		perror("lseek failed");
                return -1;
	}
	while (count) {
		ret = read(fd, buf + offset + was_read, count);
		if (ret < 0)
			return -1;
		if (ret == 0)
			break;
		if (ret > count) {
			fprintf(stderr, "READ: read more than asked\n");
			return -1;
		}
		count -= ret;
		was_read += ret;
	}
	return ret;
}

static int dowrite(int fd, off_t offset, size_t count, char *buf)
{
	int ret;

	ret = lseek(fd, offset, SEEK_SET);
	if (ret == -1)
                return ret;
	return write(fd, buf, count);
}

static int dotrunc(int fd, off_t offset)
{
	int ret;

	ret = ftruncate(fd, offset);
	if (ret == -1)
		perror("truncate failed");
	return ret;
}

static int prepare_file(char *filename, int *fd, char **buf, int max_file_size)
{
	int ret;

	*buf = malloc(max_file_size);
	if (*buf == NULL) {
		perror("malloc failed");
	        return -1;
	}
	*fd = open(filename, O_CREAT | O_RDWR, S_IRWXU);
	if (*fd == -1) {
		perror("open failed");
		free(*buf);
		*buf = NULL;
		return -1;
	}
	return 0;
}

/*
 * @offset, @count: random values from [0, max_file_size - 1]
 */
static int frag_write(struct frag_ctx *ctx, off_t offset, size_t count)
{
	int ret;
	struct stat test_stbuf;
	struct stat good_stbuf;

	if (offset + count > ctx->max_file_size)
		offset = offset / 2;
	if (offset + count > ctx->max_file_size)
		count = count / 2;

	if (fstat(ctx->test_fd, &test_stbuf)) {
		fprintf(stderr, "WRITE: fstat of test file failed\n");
		return -1;
	}
	if (offset > test_stbuf.st_size)
		printf("writing hole\n");

	ret = dowrite(ctx->test_fd, offset, count, ctx->content);
	if (ret < 0 || ret != count){
		fprintf(stderr, "WRITE: failed to write test file\n");
		return -1;
	}
	ret = dowrite(ctx->good_fd, offset, count, ctx->content);
	if (ret < 0 || ret != count) {
		fprintf(stderr, "WRITE: failed to write test file\n");
		return -1;
	}	
	if (fstat(ctx->test_fd, &test_stbuf)) {
                fprintf(stderr, "WRITE: fstat of test file failed\n");
		return -1;
	}
	if (fstat(ctx->good_fd, &good_stbuf)) {
                fprintf(stderr, "WRITE: fstat of good file failed\n");
		return -1;
	}
	if (test_stbuf.st_size != good_stbuf.st_size) {
		fprintf(stderr,
			"READ: Bad file size %d (expected %d)\n",
			(int)test_stbuf.st_size,
			(int)good_stbuf.st_size);
		return -1;
	}
	return 0;
}

/*
 * @offset, @count: random values from [0, max_file_size - 1]
 */
static int frag_read(struct frag_ctx *ctx, off_t offset, size_t count)
{
	ssize_t test_ret;
	ssize_t good_ret;

	test_ret = doread(ctx->test_fd,
			  offset, count, ctx->test_buf, ctx->max_file_size);
	if (test_ret < 0) {
		fprintf(stderr, "READ: failed to read test file\n");
		return -1;
	}
	good_ret = doread(ctx->good_fd,
			  offset, count, ctx->good_buf, ctx->max_file_size);
	if (good_ret < 0) {
		fprintf(stderr, "READ: failed to read good file\n");
		return -1;
	}
	if (test_ret != good_ret) {
		fprintf(stderr,
			"READ: Bad return value %d (expected %d\n)",
			test_ret, good_ret);
		return -1;
	}
	if (memcmp(ctx->test_buf + offset, ctx->good_buf + offset, good_ret)) {
		fprintf(stderr, "READ: bad data\n");
		return -1;
	}
	return 0;
}

/*
 * @offset: random value from [0, max_file_size - 1]
 */
static int frag_truncate(struct frag_ctx *ctx,
			 off_t offset, __attribute__((unused))size_t count)
{
	int ret;
	struct stat test_stbuf;
	struct stat good_stbuf;

	if (fstat(ctx->test_fd, &test_stbuf)) {
		fprintf(stderr, "TRUNCATE: fstat of test file failed\n");
		return -1;
	}
	if (offset > test_stbuf.st_size)
		printf("expanding truncate to %d\n", offset);
	else if (offset < test_stbuf.st_size)
		printf("shrinking truncate to %d\n", offset);
	else
		printf("trivial truncate\n");
	
	ret = dotrunc(ctx->test_fd, offset);
	if (ret == -1) {
		fprintf(stderr, "TRUNCATE: failed for test file\n");
		return -1;
	}
	ret = dotrunc(ctx->good_fd, offset);
	if (ret == -1) {
		fprintf(stderr, "TRUNCATE: failed for good file\n");
		return -1;
	}
	if (fstat(ctx->test_fd, &test_stbuf)) {
		fprintf(stderr, "TRUNCATE: fstat of test file failed\n");
		return -1;
	}
	if (fstat(ctx->good_fd, &good_stbuf)) {
		fprintf(stderr, "TRUNCATE: fstat of good file failed\n");
		return -1;
	}
	if (test_stbuf.st_size != good_stbuf.st_size) {
		fprintf(stderr,
			"TRUNCATE: bad test file size %d (expected %d)\n",
			test_stbuf.st_size,
			good_stbuf.st_size);
		return -1;
	}
	return 0;
}

frag_op_t frag_ops[LAST_OP] = {
	[READ_OP]  = frag_read,
	[WRITE_OP] = frag_write,
	[TRUNC_OP] = frag_truncate
};

static void put_ctx(struct frag_ctx *ctx)
{
	if (ctx->test_buf)
		free(ctx->test_buf);
	if (ctx->good_buf)
		free(ctx->good_buf);
	if (ctx->content)
		free(ctx->content);
}

main (int argc, char *argv[])
{
	int i;
	int ret = 0;
	struct frag_ctx ctx;
	char *test_filename = NULL;
	char *good_filename = NULL;
	int num_ops;
	int max_file_size;

	memset(&ctx, 0, sizeof(ctx));
	if (argc != 5) {
                fprintf(stderr,
			"usage: %s <test-file-name> <good-file-name> <max-file-size> <number-of-operations>\n",
			argv[0]);
		ret = -1;
                goto exit;
        }
	test_filename = argv[1];
	good_filename = argv[2];
	max_file_size = atoi(argv[3]);
	if (max_file_size > MAX_FILE_SIZE)
		max_file_size = MAX_FILE_SIZE;
	num_ops = atoi(argv[4]);
	if (num_ops > MAX_NUM_OPS)
		num_ops = MAX_NUM_OPS;

	ret = prepare_file(test_filename,
			   &ctx.test_fd, &ctx.test_buf, max_file_size);
	if (ret)
		goto exit;
	ret = prepare_file(good_filename,
			   &ctx.good_fd, &ctx.good_buf, max_file_size);
	if (ret) {
		if (close(ctx.test_fd) == -1)
			perror("close test_buf failed");
		goto exit;
	}
	ctx.content = malloc(max_file_size);
	if (!ctx.content) {
		perror("malloc failed");
		goto close;
	}
	ctx.max_file_size = max_file_size;
	for (i = 0; i < max_file_size; i++)
                ctx.content[i] = random() % 256;

	for (i = 0; i < num_ops; i++) {
		ret = frag_ops[random() % LAST_OP](&ctx,
					random() % max_file_size, /* offset */
					random() % max_file_size  /* count */);
		if (ret)
			break;
	}
 close:
	if (close(ctx.test_fd) == -1)
		perror("close test_fd failed");
	if (close(ctx.good_fd) == -1)
		perror("close good_fd failed");	
 exit:
	put_ctx(&ctx);
	if (ret)
		exit(1);
	exit(0);
}

/*
  Local variables:
  c-indentation-style: "K&R"
  mode-name: "LC"
  c-basic-offset: 8
  tab-width: 8
  fill-column: 80
  scroll-step: 1
  End:
*/