-
Notifications
You must be signed in to change notification settings - Fork 0
/
testfs.c
1374 lines (1126 loc) · 38.4 KB
/
testfs.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*-C-*-
*******************************************************************************
*
* File: testfs.c
* RCS: $Id: testfs.c,v 1.3 2009/11/12 09:19:51 jot836 Exp $
* Description: Test of file system capabilities
* Author: David Choffnes
* Northwestern Systems Research Group
* Department of Computer Science
* Northwestern University
* Created: Thu Nov 07, 2002 at 15:10:17
* Modified: Fri Nov 18, 2005 at 16:05:29 drchoffnes@cs.northwestern.edu
* Language: C
* Package: N/A
* Status: Experimental (Do Not Distribute)
*
* (C) Copyright 2005, Northwestern University, all rights reserved.
*
*******************************************************************************
*/
#define COMPETITION 0
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "sdisk.h"
#include "sfs.h"
#include "config.h"
#include <sys/resource.h>
#include <sys/time.h>
//char* program_name;
bool gbIsVerbose = 0; // verbose mode flag
char* gsDiskFName = NULL; // the name of the diskfile
FILE* f_ls;
/* prototypes */
void usage(char* program_name, FILE* stream, int status);
// Tests Core
int runTests();
int initDiskTest();
int initFSTest();
int customTest();
int createSimpleFileTest();
int createSimpleFileWriteReadTest();
int singleBigFileTest();
int multipleFilesTest();
int createFolderTest();
int multipleFoldersTest();
int appendFileTest();
int multipleOpenFilesTest();
int nestedFoldersTest();
int errorTest();
int removeTest();
int perfTest();
// Tests helpers
int initDisk();
int saveAndCloseDisk();
int initAndLoadDisk();
int initFS();
int createSmallFile(char *fileName, char *data, int fsize);
int verifyFile(char *fileName, char *data, int fsize);
int singleBigFile(char *fileName, int fsize);
int multipleFiles(char *fileBaseName, int numFiles, int maxFileSize);
int createFolder(char *dirName);
int createFolders(char *dirBaseName, int numDirs);
int appendFile();
int nestedFolders(char *dirBaseName, int num);
int refreshDisk();
void initBuffer(char *buf, int fsize);
int checkBuffers(char *buf, char *cpy, int fsize, int offset);
int testFile(char* name, int fsize);
/*
* usage: report usage to given stream and exit
*
* Parameters: Where to report usage and our exit status
*
* Returns: -
*
*/
void usage(char *program_name, FILE* stream, int status) {
fprintf(stream, "Usage: %s -h -v -f FILE\n"
"Test driver for a simple virtual disk.\n"
" -v \tverbose output\n"
" -h \tthis help message\n"
" -f FILE \tdisk image file (this is left behind)\n", program_name);
exit(status);
} /* !usage */
int main(int argc, char* argv[]) {
char c;
char* program_name = argv[0];
while ((c = getopt(argc, argv, "vhf:")) != EOF) {
switch (c) {
case 'h':
usage(program_name, stdout, 0);
break;
case 'v':
gbIsVerbose = TRUE;
break;
case 'f':
gsDiskFName = strdup(optarg);
LOG(stdout, "Disk image file: %s\n", gsDiskFName);
break;
default:
usage(program_name, stdout, 1);
break;
}
}
if (gsDiskFName == NULL) {
fprintf(stdout, "You must specify a file name for this test!\n");
usage(program_name, stdout, 1);
}
runTests();
SAFE_FREE(gsDiskFName);
return 0;
} /* !main */
/**
* Runs the test suite.
*/
int runTests() {
int hr = SUCCESS;
#ifndef EXTRA_CREDIT
#ifndef COMPETITION_TEST
FILE *f_ls_base;
#else
FILE *f_ls_compTest;
#endif
#endif
#ifdef EXTRA_CREDIT
FILE *f_ls_extra_credit;
#endif
#if COMPETITION
FILE *f_ls_competition;
f_ls_competition = fopen("competition.ls", "w");
#endif
RUN_TEST(initDiskTest());
#ifndef EXTRA_CREDIT
#ifndef COMPETITION_TEST
f_ls_base = fopen("base.ls", "w");
f_ls = f_ls_base;
RUN_TEST(initFSTest());
RUN_TEST(createSimpleFileTest());
RUN_TEST(createSimpleFileTest());
RUN_TEST(createSimpleFileWriteReadTest());
RUN_TEST(singleBigFileTest());
RUN_TEST(multipleFilesTest());
RUN_TEST(createFolderTest());
RUN_TEST(multipleFoldersTest());
RUN_TEST(appendFileTest());
RUN_TEST(multipleOpenFilesTest());
RUN_TEST(nestedFoldersTest());
RUN_TEST(errorTest());
#else
f_ls_compTest = fopen("compTest.ls", "w");
f_ls = f_ls_compTest;
RUN_TEST(customTest());
#endif
#endif
#ifdef EXTRA_CREDIT
f_ls_extra_credit = fopen("extra_credit.ls", "w");
f_ls = f_ls_extra_credit;
RUN_TEST(removeTest());
#endif
// NOTE: We're not using this competition code this year.
#if COMPETITION
f_ls = f_ls_competition;
RUN_TEST(perfTest());
#endif
Fail:
#ifndef EXTRA_CREDIT
#ifndef COMPETITION_TEST
fclose(f_ls_base);
#else
fclose(f_ls_compTest);
#endif
#endif
#ifdef EXTRA_CREDIT
fclose(f_ls_extra_credit);
#endif
#if COMPETITION
fclose(f_ls_competition);
#endif
return hr;
}
/**
* Testing the initialization code of the fs. Should work without any of the sfs API
* being implemented.
*/
int initDiskTest() {
int hr = SUCCESS;
FAIL_BRK4(initDisk());
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Init Disk Test (This test should not fail. Not worth points.)");
return hr;
}
/**
* Tests the initFS functionality.
*/
int initFSTest() {
int hr = SUCCESS;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Init FS Test");
return hr;
}
/**
* This is a place where you need to put your test. Please insert your test code here and
* change the name of the test to be representative of what you are testing. You can use this test
* during the development period to test your code. When you do the final submission here you have to
* implement a test that tests some interesting functionality of the sfs.
* @return SUCCESS if the test is passed successfully FAIL otherwise.
*/
int customTest() {
int hr = SUCCESS;
int i, j, fd, fd1, fd2, lseek2;
char *randomBuf;//buffer contain junk data, size: SD_SECTORSIZE
randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE);
// For later tests
char* asciidata = (char*)malloc(257 * sizeof(char)); // 2
char* morealphabet = (char*)malloc(7*sizeof(char));
morealphabet[0] = 't';
morealphabet[1] = 'u';
morealphabet[2] = 'v';
morealphabet[3] = 'w';
morealphabet[4] = 'x';
morealphabet[5] = 'y';
morealphabet[6] = 'z';
char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
char* fd1read = (char*)malloc(26*sizeof(char));
// normal test from testfs
// initialize disk
FAIL_BRK4(SD_initDisk());
for (i = 0; i < SD_NUMSECTORS; i++) {
for (j = 0; j < SD_SECTORSIZE; j++) {
randomBuf[j] = (char) rand();
}
while (SD_write(i, (void*) randomBuf));
}
FAIL_BRK4(SD_loadDisk(gsDiskFName));
refreshDisk();
// initialize fs, sfs_mkfs
FAIL_BRK4(initFS());
refreshDisk();
// initialize test sfs_mkfs, when mkfs, nothing should appear again.
FAIL_BRK4(initFS());
FAIL_BRK4(sfs_mkdir("foo"));
refreshDisk();
FAIL_BRK4(sfs_fcd("foo"));
FAIL_BRK4(sfs_mkdir("bar"));
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((sfs_fcd("foo") != -1));
FAIL_BRK4((sfs_fcd("bar") != -1));
FAIL_BRK4(sfs_fcd("/"));
FAIL_BRK4((sfs_fcd("foo") != -1));
FAIL_BRK4((sfs_fcd("bar") != -1));
FAIL_BRK4(sfs_mkdir("foo"));
refreshDisk();
FAIL_BRK4(sfs_fcd("foo"));
FAIL_BRK4((sfs_fcd("bar") != -1));
//normal test . and ..
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((sfs_mkdir(".") != -1));
FAIL_BRK4(sfs_mkdir(".foo."));
FAIL_BRK4(sfs_mkdir(".foo"));
FAIL_BRK4((sfs_mkdir("..") != -1));
FAIL_BRK4(sfs_mkdir("..foo"));
FAIL_BRK4(sfs_mkdir("..foo.."));
FAIL_BRK4((sfs_mkdir("/") != -1));
FAIL_BRK4(sfs_mkdir("..."));
FAIL_BRK4(sfs_mkdir("...."));
FAIL_BRK4((sfs_mkdir("./") != -1));
FAIL_BRK4((sfs_mkdir("/.") != -1));
FAIL_BRK4((sfs_mkdir("./.") != -1));
// ascii code test, make a file containing all ascii code chars to make sure implementation does not use an EOF char for size
FAIL_BRK4(initFS());
refreshDisk();
//257 chars, possible ascii 0 -> 255 + 255 at the end to make sure we test
// create data
for (i = 0; i < 256; i++) {
asciidata[i] = i; // table sequentially
}
// now put 255 in again at the end
asciidata[256] = 255;
// now fwrite into the a file and fread and compare
FAIL_BRK4(createSmallFile("asciitable", asciidata, 257));
refreshDisk();
FAIL_BRK4(verifyFile("asciitable", asciidata, 257));
// This test will open the same file twice, write more onto it using one of the file descriptors to lengthen it, and read both to make sure they still match (should test to make sure size is with inodes and not the table
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4(createSmallFile("alphabet", "abcdefghijklmnopqrst", 20));
// Open twice
FAIL_BRK4((fd1 = sfs_fopen("alphabet")) == -1);
FAIL_BRK4((fd2 = sfs_fopen("alphabet")) == -1);
// lseek to end and write to second one
FAIL_BRK4((lseek2 = sfs_lseek(fd2, 19)) == -1);
FAIL_BRK4((sfs_fwrite(fd2, morealphabet, 7)) == -1);
// Now verify we can read from fd1 and it should be the full alphabet since we wrote to fd2 which is the same file
FAIL_BRK4((sfs_fread(fd1, fd1read, 26)) == -1);
FAIL_BRK4((strncmp(alphabet, fd1read, 26)) != 0); // here is the comparison of strings
FAIL_BRK4((sfs_fclose(fd1)) == -1);
FAIL_BRK4((sfs_fclose(fd2)) == -1);
//test dir takes more that one sector
//test for file
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
FAIL_BRK4(sfs_fclose(fd));
FAIL_BRK4((sfs_fcd("foo.bin") != -1));
//test same name
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4(sfs_mkdir("foo.bin"));
FAIL_BRK4((fd = sfs_fopen("foo.bin")) != -1);
FAIL_BRK4((fd = sfs_fopen("bar.bin")) == -1);
FAIL_BRK4((sfs_mkdir("bar.bin") != -1));
//test initFS will erase the file descriptor
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
FAIL_BRK4(initFS());
FAIL_BRK4((sfs_fclose(fd) != -1));
//test create nothing
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((sfs_mkdir("") != -1));
FAIL_BRK4((fd = sfs_fopen("")) != -1);
//test open . and ..
FAIL_BRK4(initFS());
refreshDisk();
FAIL_BRK4((fd = sfs_fopen("/")) != -1);
FAIL_BRK4((fd = sfs_fopen(".")) != -1);
FAIL_BRK4((fd = sfs_fopen("..")) != -1);
FAIL_BRK4(sfs_mkdir("foo"));
FAIL_BRK4((fd = sfs_fopen("foo")) != -1);
FAIL_BRK4(sfs_fcd("foo"));
FAIL_BRK4((fd = sfs_fopen("/")) != -1);
FAIL_BRK4((fd = sfs_fopen(".")) != -1);
FAIL_BRK4((fd = sfs_fopen("..")) != -1);
FAIL_BRK4(sfs_fcd("/"));
FAIL_BRK4((fd = sfs_fopen("/.")) != -1);
FAIL_BRK4((fd = sfs_fopen("./")) != -1);
FAIL_BRK4((fd = sfs_fopen("./.")) != -1);
FAIL_BRK4((fd = sfs_fopen("/..")) != -1);
FAIL_BRK4((fd = sfs_fopen("../")) != -1);
FAIL_BRK4((fd = sfs_fopen("../..")) != -1);
FAIL_BRK4((fd = sfs_fopen("//")) != -1);
FAIL_BRK4((fd = sfs_fopen(".foo")) == -1);
FAIL_BRK4((fd = sfs_fopen(".foo.")) == -1);
FAIL_BRK4((fd = sfs_fopen("..foo")) == -1);
FAIL_BRK4((fd = sfs_fopen("..foo..")) == -1);
FAIL_BRK4((fd = sfs_fopen("...")) == -1);
FAIL_BRK4((fd = sfs_fopen(".....")) == -1);
// test the malloc not been released
FAIL_BRK4(sfs_mkfs());
refreshDisk();
int thememmax;
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
thememmax=ru.ru_maxrss;
long int k;
for(k = 0; k < 100000; ++k)
{
FAIL_BRK4(sfs_mkfs());
FAIL_BRK4(sfs_mkdir("foo"));
FAIL_BRK4(sfs_fcd("foo"));
FAIL_BRK4(sfs_ls(f_ls));
FAIL_BRK4((fd = sfs_fopen("bar")) == -1);
FAIL_BRK4((sfs_fwrite(fd, randomBuf, SD_SECTORSIZE)) == -1);
FAIL_BRK4((sfs_lseek(fd, 1)) == -1);
FAIL_BRK4((sfs_fread(fd, randomBuf, SD_SECTORSIZE - 1)) == -1);
FAIL_BRK4(sfs_fclose(fd));
}
getrusage(RUSAGE_SELF, &ru);
FAIL_BRK4(100 * thememmax <= ru.ru_maxrss);
/*
FAIL_BRK4(createFolder("bar"));
FAIL_BRK4(sfs_fcd("bar"));
FAIL_BRK4(createFolder("foo"));
FAIL_BRK4(sfs_fcd("foo"));
FAIL_BRK4(sfs_fcd("/bar/foo"));
// FAIL_BRK3((sfs_fcd("//bar/foo") != -1), stdout, "Error: sfs_fcd() failed\n");
FAIL_BRK4(sfs_fcd("/../bar/foo"));
FAIL_BRK4(sfs_fcd("/../bar/foo/"));
FAIL_BRK4(sfs_fcd("/"));
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
FAIL_BRK4(sfs_fcd("/bar"));
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
//int fopentmp;
//FAIL_BRK3((fopentmp = sfs_fopen("test.txt") != -1), stdout, "Error: Allowing read from unopened file\n");
//FAIL_BRK3((sfs_fread(fopentmp, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n");
// test if fcd does not allow going to places that does not exist
FAIL_BRK3((sfs_fcd("bla") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
FAIL_BRK3((sfs_fcd("x") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
*/
Fail:
//clean up code goes here
SAFE_FREE(asciidata);
SAFE_FREE(morealphabet);
SAFE_FREE(fd1read);
SAFE_FREE(randomBuf);
saveAndCloseDisk();
PRINT_RESULTS("customTest!");
return hr;
}
/**
* Tests the creation of a small file without attempting to read it afterwards.
*/
int createSimpleFileTest() {
int hr = SUCCESS;
int fsize = 250;
char fdata[fsize];
initBuffer(fdata, fsize);
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(createSmallFile("foo", fdata, fsize));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Create Simple File Test");
return hr;
}
/**
* Tests the file can also be read back and that contents match
*/
int createSimpleFileWriteReadTest() {
int hr = SUCCESS;
int fsize = 250;
char fdata[fsize];
initBuffer(fdata, fsize);
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(createSmallFile("foo", fdata, fsize));
FAIL_BRK4(saveAndCloseDisk());
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(verifyFile("foo", fdata, fsize));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Create Simple File Write Read Test");
return hr;
}
/**
* Tests a big file write and then read with various techniques.
*/
int singleBigFileTest() {
int hr = SUCCESS;
int fsize = SD_SECTORSIZE * 16;
char *fileName = "foo";
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(singleBigFile(fileName, fsize));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Single Big File Test");
return hr;
}
/**
* Tests a number of files in the root folder.
*/
int multipleFilesTest() {
int hr = SUCCESS;
int numFiles = 50;
int maxFileSize = 10 * SD_SECTORSIZE;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(multipleFiles("file", numFiles, maxFileSize));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Multiple Files Test");
return hr;
}
/**
* Tests the creation of a simple folder.
*/
int createFolderTest() {
int hr = SUCCESS;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(createFolder("bar"));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Create Folder Test");
return hr;
}
/**
* Tests the creation of a number of folders in the root
*/
int multipleFoldersTest() {
int hr = SUCCESS;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(createFolders("folder", 500));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Multiple Folders Test");
return hr;
}
/**
* Tests the ability to add more data to a existing file.
*/
int appendFileTest() {
int hr = SUCCESS;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(appendFile());
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Append File Test");
return hr;
}
/**
* Tests having multiple files openned and written at the same time
*/
int multipleOpenFilesTest() {
int hr = SUCCESS;
int fd, fd2, fd3, fsize = SD_SECTORSIZE;
char *fname1 = "simult1";
char *fname2 = "simult2";
char *fname3 = "simult3";
char *buffer = malloc(fsize);
initBuffer(buffer, fsize);
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
// Openning the files
fd = sfs_fopen(fname1);
FAIL_BRK3((fd == -1), stdout, "Error: fopen for (%s) failed\n", fname1);
fd2 = sfs_fopen(fname2);
FAIL_BRK3((fd2 == -1), stdout, "Error: fopen for (%s) failed\n", fname2);
fd3 = sfs_fopen(fname3);
FAIL_BRK3((fd3 == -1), stdout, "Error: fopen for (%s) failed\n", fname3);
// Writing in a different order
FAIL_BRK3((sfs_fwrite(fd2, buffer, fsize) != fsize), stdout,
"Error: Write failed\n");
FAIL_BRK3((sfs_fwrite(fd, buffer, fsize) != fsize), stdout,
"Error: Write failed\n");
FAIL_BRK3((sfs_fwrite(fd3, buffer, fsize) != fsize), stdout,
"Error: Write failed\n");
// Closing the files in different order
FAIL_BRK3(sfs_fclose(fd3), stdout, "Error: Closing the file failed\n");
FAIL_BRK3(sfs_fclose(fd2), stdout, "Error: Closing the file failed\n");
FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n");
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: ls failed\n");
// reload the disk and verify
FAIL_BRK4(saveAndCloseDisk());
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(verifyFile(fname1, buffer, fsize));
FAIL_BRK4(verifyFile(fname2, buffer, fsize));
FAIL_BRK4(verifyFile(fname3, buffer, fsize));
Fail:
SAFE_FREE(buffer);
saveAndCloseDisk();
PRINT_RESULTS("Multiple Open Files Test");
return hr;
}
/**
* Tests the nesting of folders
*/
int nestedFoldersTest() {
int hr = SUCCESS;
int numNestedFolders = 50;
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK4(nestedFolders("dir", numNestedFolders));
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Nested Folders Test");
return hr;
}
/**
* Tests some error messages that we expect when we pass bogus arguments the sfs API
*/
int errorTest() {
int hr = SUCCESS;
char buf[100];
// test setup
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
// make sure we are not allowed to read if the files are not open
FAIL_BRK3((sfs_fread(0, buf, 50) != -1), stdout,
"Error: Allowing read from unopened file\n");
FAIL_BRK3((sfs_fread(rand(), buf, 50) != -1), stdout,
"Error: Allowing read from unopened file\n");
FAIL_BRK3((sfs_fread(-5, buf, 50) != -1), stdout,
"Error: Allowing read from unopened file\n");
// test if fcd does not allow going to places that does not exist
FAIL_BRK3((sfs_fcd("bla") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
FAIL_BRK3((sfs_fcd("x") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout,
"Error: Allowing cd to folder that does not exist\n");
// testing if lseek does not allow going pass the end of the file
int fsize = 10;
FAIL_BRK3(createSmallFile("foo", "aaaaaaaaaaaaaaaaaaaaaaaa", fsize),
stdout, "Error: Creating file failed\n");
int fd = sfs_fopen("foo");
FAIL_BRK3((fd == -1), stdout, "Error: reopening the file failed\n");
FAIL_BRK3((sfs_lseek(fd, fsize) != -1), stdout,
"Error: Allowing seek pass the end of the file\n");
Fail:
saveAndCloseDisk();
PRINT_RESULTS("Error Test");
return hr;
}
/**
* Tests sfs_rm functionality.
*/
int removeTest() {
int hr = SUCCESS;
char dirName[16], fileName[16];
int i, numDirs = 100, numFiles = 100, fsize = SD_SECTORSIZE * 1.8;
char *buffer = malloc(fsize * sizeof(char));
// test setup
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
// make some files
for (i = 0; i < numFiles; i++) {
sprintf(fileName, "file%04d", i);
initBuffer(buffer, fsize);
FAIL_BRK4(createSmallFile(fileName, buffer, fsize));
}
// make some folders
for (i = 0; i < numDirs; i++) {
sprintf(dirName, "dir%04d", i);
FAIL_BRK4(createFolder(dirName));
}
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
// delete the even folders and files
for (i = 0; i < numFiles; i += 2) {
sprintf(fileName, "file%04d", i);
FAIL_BRK3(sfs_rm(fileName), stdout,
"Error: deleting file (%s) failed\n", fileName);
sprintf(dirName, "dir%04d", i);
FAIL_BRK3(sfs_rm(dirName), stdout,
"Error: deleting folder (%s) failed\n", dirName);
}
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
// delete the rest of the files and folders
for (i = 1; i < numFiles; i += 2) {
sprintf(fileName, "file%04d", i);
FAIL_BRK3(sfs_rm(fileName), stdout,
"Error: deleting file (%s) failed\n", fileName);
sprintf(dirName, "dir%04d", i);
FAIL_BRK3(sfs_rm(dirName), stdout,
"Error: deleting folder (%s) failed\n", dirName);
}
FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
Fail:
SAFE_FREE(buffer);
saveAndCloseDisk();
PRINT_RESULTS("Remove Test");
return hr;
}
/**
* Tests the algorithm for performance
*/
int perfTest() {
int hr = SUCCESS;
int i;
char *fileName = malloc(32);
char *dirName = malloc(32);
printf("###########################\n");
printf("Let the competition begin!\n");
FAIL_BRK4(initAndLoadDisk());
FAIL_BRK4(initFS());
// lots of files
FAIL_BRK4(multipleFiles("file", 900, SD_SECTORSIZE));
// deleting them
for (i = 0; i < 900; i++) {
sprintf(fileName, "file%05d", i);
FAIL_BRK4(sfs_rm(fileName));
}
// lots of files again
FAIL_BRK4(multipleFiles("file", 900, SD_SECTORSIZE));
// now deleting them in reverse
for (i = 900 - 1; i >= 0; i--) {
sprintf(fileName, "file%05d", i);
FAIL_BRK4(sfs_rm(fileName));
}
// one very big file
FAIL_BRK4(singleBigFile("huge.txt", (SD_NUMSECTORS - 100) * SD_SECTORSIZE));
// and now it is gone
FAIL_BRK4(sfs_rm("huge.txt"));
// lots of flat folders
FAIL_BRK4(createFolders("dir", 900));
for (i = 0; i < 900; i++) {
sprintf(dirName, "dir%04d", i);
FAIL_BRK4(sfs_rm(dirName));
}
// lots of nested folders
FAIL_BRK4(nestedFolders("dir", 900));
// going to the bottom
FAIL_BRK3(sfs_fcd("/"), stdout, "Error: cd / failed\n");
for (i = 0; i < 900; i++) {
sprintf(dirName, "dir%04d", i);
FAIL_BRK3(sfs_fcd(dirName), stdout , "Error: fcd to (%s) failed\n", dirName);
}
// deleting them on the way out
for (i = 900 - 1; i >= 0; i--) {
sprintf(dirName, "dir%04d", i);
FAIL_BRK3(sfs_fcd(".."), stdout, "Error: cd .. failed\n");
FAIL_BRK3(sfs_rm(dirName), stdout, "Error: rm (%s) failed\n", dirName);
}
Fail:
SAFE_FREE(fileName);
SAFE_FREE(dirName);
printf("COMPETITION RESULTS: ");
saveAndCloseDisk();
PRINT_RESULTS("Performance Test");
return hr;
}
/**
* Initialize the Disk with random content.
*/
int initDisk(void) {
int hr = SUCCESS;
int i, j;
char *randomBuf;
// initialize disk
LOG(stdout, "Initialize disk before using it\n");
FAIL_BRK3(SD_initDisk(), stderr, "Problems initializing disk %d\n", sderrno);
LOG(stdout, "Write random garbage to disk\n");
randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE);
for (i = 0; i < SD_NUMSECTORS; i++) {
// fill the buffer with random junk
for (j = 0; j < SD_SECTORSIZE; j++) {
randomBuf[j] = (char) rand();
}
// continue trying to write for number of times
int numTries = 10;
while (SD_write(i, (void*) randomBuf) && numTries) {
fprintf(stdout, "Error %d while writing block %d\n", sderrno, i);
numTries--;
}
FAIL_BRK3((numTries == 0), stderr,
"Final attempt to write %d block to disk FAILED.", i);
LOG(stdout, "Block written (%d) \n", i);
}
Fail:
SAFE_FREE(randomBuf);
return hr;
}
/**
* Save the disk to a file and finalize the driver.
*/
int saveAndCloseDisk() {
int hr = SUCCESS;
FAIL_BRK3(SD_saveDisk(gsDiskFName), stdout,
"Error %d while saving disk image to %s\n", sderrno, gsDiskFName);
LOG(stdout, "Disk image saved to %s\n", gsDiskFName);
FAIL_BRK3(SD_finalizeDisk(), stdout,
"Error %d while during SD_finalizeDisk()\n", sderrno);
Fail: return hr;
}
/**
* Initialize the disk and load the content from the file.
*/
int initAndLoadDisk() {
int hr = SUCCESS;
FAIL_BRK3(SD_initDisk(), stdout, "Error %d while SD_initDisk()\n", sderrno);
FAIL_BRK3(SD_loadDisk(gsDiskFName), stdout,
"Error %d while reading disk image from %s\n", sderrno, gsDiskFName);
LOG(stdout, "Disk image restored from %s\n", gsDiskFName);
Fail: return hr;
}
/**
* Use sfs_mkfs() to initialize the filesystem
*/
int initFS() {
int hr = SUCCESS;
// initialize fs
LOG(stdout, "Initialize file system...\n");
FAIL_BRK3(sfs_mkfs(), stdout, "Error making file system...aborting\n");
LOG(stdout, "File System created successfully ...\n");
Fail: return hr;
}
/**
* Opens a file, writes some data to it and closes it.
*/
int createSmallFile(char *fileName, char *data, int fsize) {
int hr = SUCCESS;
// create one file, write to it, read it back
// open file
int fd = sfs_fopen(fileName);
FAIL_BRK3((fd == -1), stdout,
"Error: Unable to open file %s with intention to write\n", fileName);
// write file
FAIL_BRK3((sfs_fwrite(fd, data, fsize) != fsize), stdout,
"Error: failed to write %d bytes to %s, fd:%d\n", fsize, fileName,
fd);
// close file
FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Unable to close fd: %d\n", fd);
Fail: return hr;
}
/**
* Checks if the contents of the file match the give data.
*/
int verifyFile(char *fileName, char *data, int fsize) {
int hr = SUCCESS;
/* open a file and verify the contents of the file are correct */
char data2[fsize];
// open file
int fd = sfs_fopen(fileName);
FAIL_BRK3((fd == -1), stdout,
"Error: Unable to open file %s with intention to read\n", fileName);
// read file
FAIL_BRK3((sfs_fread(fd, data2, fsize) != fsize), stdout,
"Error: failed to read %d bytes to %s, fd:%d\n", fsize, fileName,
fd);
FAIL_BRK3(checkBuffers(data, data2, fsize, 0), stdout,
"Error: contents didn't match\n");
// TODO: We should not allow reading pass the end of the file...
//int bread;
//FAIL_BRK( ((bread = sfs_fread(fd, data2, 1)) == 1), stdout, "Error: successfully reading more data when we didn't expected it %d\n", bread);
// close file
FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Unable to close fd: %d\n", fd);
Fail: return hr;
}
/**
* Test a single big file
*/
int singleBigFile(char *fileName, int fsize) {
int hr = SUCCESS;