-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcase2.c
98 lines (83 loc) · 1.71 KB
/
testcase2.c
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "filesys.h"
#define NUM_FILES 8
int fdarr[NUM_FILES];
static int corrupt_file (char *filename, int offset)
{
int fd1;
int ret;
char val;
fd1 = open (filename, O_WRONLY, 0);
if (fd1 == -1) {
printf ("Unable to open file descriptor1\n");
return 0;
}
lseek (fd1, offset, SEEK_SET);
val = 2;
ret = write (fd1, &val, 1);
if (ret != 1) {
printf ("Unable to write to file\n");
return 0;
}
close (fd1);
return 1;
}
static int open_file (char *filename, int idx)
{
fdarr[idx] = s_open (filename, O_WRONLY, 0);
if (fdarr[idx] == -1) {
return 0;
}
return 1;
}
static int write_file (int fd1, int offset)
{
char val;
int ret;
s_lseek (fd1, offset, SEEK_SET);
val = 1;
ret = s_write (fd1, &val, 1);
if (ret == -1) {
return 0;
}
return 1;
}
int main ()
{
char filename[32];
int i, ret;
int corrupt_idx = rand() % NUM_FILES;
int offset = rand () % 1000;
if (filesys_init() == 1) {
printf ("Unable to init filesys\n");
return 0;
}
for (i = 0; i < NUM_FILES; i++) {
snprintf (filename, 32, "foo_%d.txt", i);
ret = open_file (filename, i);
if (ret == 0) {
printf ("open failed\n");
return 0;
}
}
snprintf (filename, 32, "foo_%d.txt", corrupt_idx);
ret = corrupt_file (filename, offset);
if (ret == 0) {
return 0;
}
for (i = 0; i < NUM_FILES; i++) {
ret = write_file (fdarr[i], offset);
if ((ret == 0 && i != corrupt_idx) || (ret == 1 && i == corrupt_idx)) {
printf ("write test failed for i=: %d~~~ ret is:= %d corrupt_idx is:= %d\n",i,ret,corrupt_idx);
return 0;
}
}
printf ("write test passed\n");
return 0;
}