-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcase1.c
77 lines (65 loc) · 1.28 KB
/
testcase1.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
#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
static int corrupt_file (char *filename)
{
int fd1;
int ret;
char val;
int offset;
fd1 = open (filename, O_WRONLY, 0);
if (fd1 == -1) {
printf ("Unable to open file descriptor1 in testcase1\n");
return 0;
}
offset = rand () % 1000;
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 fd2;
fd2 = s_open (filename, O_RDONLY, 0);
if (fd2 == -1) {
return 0;
}
return 1;
}
int main ()
{
char filename[32];
int i, ret;
int corrupt_idx = rand() % NUM_FILES;
if (filesys_init() == 1) {
printf ("Unable to init filesys\n");
return 0;
}
snprintf (filename, 32, "foo_%d.txt", corrupt_idx);
ret = corrupt_file (filename);
if (ret == 0) {
return 0;
}
for (i = 0; i < NUM_FILES; i++) {
snprintf (filename, 32, "foo_%d.txt", i);
ret = open_file (filename);
if ((ret == 0 && i != corrupt_idx) || (ret == 1 && i == corrupt_idx)) {
printf ("open test failed\n");
return 0;
}
}
printf ("open test passed\n");
return 0;
}