yaffs Working on timothy_tests and have updated yaffs_importer.py
[yaffs2.git] / direct / tests / fuzzer.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * Fuzzer to fuzz a file
14  */
15
16 #include <string.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <pthread.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24                      
25 int main(int argc, char *argv[])
26 {
27         int prob = 10000;
28         int h;
29         int flen = 0;
30         int changesPerBuffer = 0;
31         int b;
32         char c;
33         unsigned char buffer[1000000];
34         int bufsize;
35         int x;
36         int i;
37         int nbuffers;
38
39         while((c = getopt(argc,argv,"p:")) != -1){
40                 switch(c){
41                         case 'p':
42                                 prob = atoi(optarg);
43                                 break;
44                 }
45         }
46         if(prob < 100){
47                 printf("-p value less than 100 is invalid\n");
48                 return 1;
49         }
50         
51         if(optind >= argc){
52                 printf(" Needs a file name to fuzz\n");
53                 return 1;
54         }
55         
56         h = open(argv[optind], O_RDWR);
57         flen = lseek(h,0,SEEK_END);
58         lseek(h,0,SEEK_SET);
59         if(flen < 1){
60                 printf(" File is too short\n");
61                 return 1;
62         }
63         
64         nbuffers = (flen + sizeof(buffer) - 1) / sizeof(buffer);
65
66         changesPerBuffer = 1+ (sizeof(buffer) * 8) / prob;
67
68         printf("Fuzzing file %s. Size %d, probablity 1/%d, changing %d bits in each of %d buffers\n",
69                 argv[optind],flen,prob,changesPerBuffer,nbuffers);
70
71         srand(time(0));
72
73         for(b = 0; b < nbuffers; b++){
74                 /* printf("buffer %d\n",b); */
75                 lseek(h,b * sizeof(buffer),SEEK_SET);
76                 bufsize = read(h,buffer,sizeof(buffer));
77                 for(i = 0; i < changesPerBuffer; i++){
78                         x = rand() % (sizeof(buffer) * 8);
79                         buffer[x >> 3] ^= (1 << (x & 7));
80                 }
81                 lseek(h,b * sizeof(buffer),SEEK_SET);
82                 write(h,buffer,bufsize);
83         }
84         close(h);
85         return 0;
86 }