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