-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfs_tests.rs
More file actions
2060 lines (1630 loc) · 80.5 KB
/
fs_tests.rs
File metadata and controls
2060 lines (1630 loc) · 80.5 KB
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
#[allow(unused_parens)]
#[cfg(test)]
pub mod fs_tests {
use super::super::*;
use crate::interface;
use crate::safeposix::syscalls::fs_calls::*;
use crate::safeposix::{cage::*, dispatcher::*, filesystem};
use libc::c_void;
use std::fs::OpenOptions;
use std::os::unix::fs::PermissionsExt;
#[test]
pub fn ut_lind_fs_simple() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
assert_eq!(cage.access_syscall("/", F_OK), 0);
assert_eq!(cage.access_syscall("/", X_OK | R_OK), 0);
let mut statdata2 = StatData::default();
assert_eq!(cage.stat_syscall("/", &mut statdata2), 0);
//ensure that there are two hard links
assert_eq!(statdata2.st_nlink, 5); //2 for . and .., one for dev, and one so that it can never be removed
//ensure that there is no associated size
assert_eq!(statdata2.st_size, 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn rdwrtest() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let fd = cage.open_syscall("/foobar", O_CREAT | O_TRUNC | O_RDWR, S_IRWXA);
assert!(fd >= 0);
assert_eq!(cage.write_syscall(fd, str2cbuf("hello there!"), 12), 12);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_SET), 0);
let mut read_buf1 = sizecbuf(5);
assert_eq!(cage.read_syscall(fd, read_buf1.as_mut_ptr(), 5), 5);
assert_eq!(cbuf2str(&read_buf1), "hello");
assert_eq!(cage.write_syscall(fd, str2cbuf(" world"), 6), 6);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_SET), 0);
let mut read_buf2 = sizecbuf(12);
assert_eq!(cage.read_syscall(fd, read_buf2.as_mut_ptr(), 12), 12);
assert_eq!(cbuf2str(&read_buf2), "hello world!");
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn prdwrtest() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let fd = cage.open_syscall("/foobar2", O_CREAT | O_TRUNC | O_RDWR, S_IRWXA);
assert!(fd >= 0);
assert_eq!(cage.pwrite_syscall(fd, str2cbuf("hello there!"), 12, 0), 12);
let mut read_buf1 = sizecbuf(5);
assert_eq!(cage.pread_syscall(fd, read_buf1.as_mut_ptr(), 5, 0), 5);
assert_eq!(cbuf2str(&read_buf1), "hello");
assert_eq!(cage.pwrite_syscall(fd, str2cbuf(" world"), 6, 5), 6);
let mut read_buf2 = sizecbuf(12);
assert_eq!(cage.pread_syscall(fd, read_buf2.as_mut_ptr(), 12, 0), 12);
assert_eq!(cbuf2str(&read_buf2), "hello world!");
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn chardevtest() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let fd = cage.open_syscall("/dev/zero", O_RDWR, S_IRWXA);
assert!(fd >= 0);
assert_eq!(
cage.pwrite_syscall(
fd,
str2cbuf("Lorem ipsum dolor sit amet, consectetur adipiscing elit"),
55,
0
),
55
);
let mut read_bufzero = sizecbuf(1000);
assert_eq!(
cage.pread_syscall(fd, read_bufzero.as_mut_ptr(), 1000, 0),
1000
);
assert_eq!(
cbuf2str(&read_bufzero),
std::iter::repeat("\0")
.take(1000)
.collect::<String>()
.as_str()
);
assert_eq!(cage.chdir_syscall("dev"), 0);
assert_eq!(cage.close_syscall(fd), 0);
let fd2 = cage.open_syscall("./urandom", O_RDWR, S_IRWXA);
assert!(fd2 >= 0);
let mut read_bufrand = sizecbuf(1000);
assert_eq!(
cage.read_syscall(fd2, read_bufrand.as_mut_ptr(), 1000),
1000
);
assert_eq!(cage.close_syscall(fd2), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_broken_close() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
//testing a muck up with the inode table where the regular close does not work as intended
let cage = interface::cagetable_getref(1);
//write should work
let mut fd = cage.open_syscall("/broken_close_file", O_CREAT | O_EXCL | O_RDWR, S_IRWXA);
assert_eq!(cage.write_syscall(fd, str2cbuf("Hello There!"), 12), 12);
assert_eq!(cage.close_syscall(fd), 0);
//close the file and then open it again... and then close it again
fd = cage.open_syscall("/broken_close_file", O_RDWR, S_IRWXA);
assert_eq!(cage.close_syscall(fd), 0);
//let's try some things with connect
//we are going to open a socket with a UDP specification...
let sockfd = cage.socket_syscall(AF_INET, SOCK_STREAM, 0);
//bind should not be interesting
let mut sockad = interface::GenSockaddr::V4(interface::SockaddrV4::default());
sockad.set_family(AF_INET as u16);
assert_eq!(cage.bind_syscall(sockfd, &sockad), 0);
fd = cage.open_syscall("/broken_close_file", O_RDWR, S_IRWXA);
assert_eq!(cage.close_syscall(fd), 0);
fd = cage.open_syscall("/broken_close_file", O_RDWR, S_IRWXA);
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_chmod_valid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let flags: i32 = O_TRUNC | O_CREAT | O_RDWR;
//checking if `chmod_syscall()` works with a relative path that includes only normal components,
//e.g. without `.` or `..` references
let filepath = "/chmodTestFile1";
let mut statdata = StatData::default();
//checking if the file was successfully created with the specified initial flags
//set all mode bits to 0 to change them later
let fd = cage.open_syscall(filepath, flags, 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IFREG as u32);
//checking if owner read, write, and execute or search mode bits are correctly set
assert_eq!(cage.chmod_syscall(filepath, S_IRUSR | S_IWUSR | S_IXUSR), 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(
statdata.st_mode,
S_IRUSR | S_IWUSR | S_IXUSR | S_IFREG as u32
);
//resetting access mode bits
assert_eq!(cage.chmod_syscall(filepath, 0), 0);
//checking if group owners read, write, and execute or search mode bits are correctly set
assert_eq!(cage.chmod_syscall(filepath, S_IRGRP | S_IWGRP | S_IXGRP), 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(
statdata.st_mode,
S_IRGRP | S_IWGRP | S_IXGRP | S_IFREG as u32
);
//resetting access mode bits
assert_eq!(cage.chmod_syscall(filepath, 0), 0);
//checking if other users read, write, and execute or search mode bits are correctly set
assert_eq!(cage.chmod_syscall(filepath, S_IROTH | S_IWOTH | S_IXOTH), 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(
statdata.st_mode,
S_IROTH | S_IWOTH | S_IXOTH | S_IFREG as u32
);
assert_eq!(cage.close_syscall(fd), 0);
//checking if `chmod_syscall()` works with relative path that include parent directory reference
let newdir = "../testFolder";
assert_eq!(cage.mkdir_syscall(newdir, S_IRWXA), 0);
let filepath = "../testFolder/chmodTestFile";
//checking if the file was successfully created with the specified initial flags
//set all mode bits to 0 to set them later
let fd = cage.open_syscall(filepath, flags, 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IFREG as u32);
//checking if owner, group owners, and other users read, write, and execute or search
//mode bits are correctly set
assert_eq!(cage.chmod_syscall(filepath, S_IRWXA), 0);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IRWXA | S_IFREG as u32);
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_chmod_invalid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
//checking if passing a nonexistent pathname to `chmod_syscall()`
//correctly results in `A component of path does not name an existing file` error
let invalidpath = "/someInvalidPath/testFile";
assert_eq!(
cage.chmod_syscall(invalidpath, S_IRUSR | S_IWUSR | S_IXUSR),
-(Errno::ENOENT as i32)
);
//checking if passing an invalid set of mod bits to `chmod_syscall()`
//correctly results in `The value of the mode argument is invalid` error
let flags: i32 = O_TRUNC | O_CREAT | O_RDWR;
let filepath = "/chmodTestFile2";
let mut statdata = StatData::default();
let fd = cage.open_syscall(filepath, flags, S_IRWXA);
assert_eq!(cage.stat_syscall(filepath, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IRWXA | S_IFREG as u32);
//0o7777 is an arbitrary value that does not correspond to any combination of valid mode bits
assert_eq!(
cage.chmod_syscall(filepath, 0o7777 as u32),
-(Errno::EINVAL as i32)
);
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fchmod() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let flags: i32 = O_TRUNC | O_CREAT | O_RDWR;
//checking if `fchmod_syscall()` works with a valid file descriptor
let filepath = "/fchmodTestFile1";
let mut statdata = StatData::default();
//checking if the file was successfully created with the specified initial flags
//set all mode bits to 0 to change them later
let fd = cage.open_syscall(filepath, flags, 0);
assert_eq!(cage.fstat_syscall(fd, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IFREG as u32);
//checking if owner, group owners, and other users read, write, and execute or search
//mode bits are correctly set
assert_eq!(cage.fchmod_syscall(fd, S_IRWXA), 0);
assert_eq!(cage.fstat_syscall(fd, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IRWXA | S_IFREG as u32);
//checking if passing an invalid set of mod bits to `fchmod_syscall()`
//correctly results in `The value of the mode argument is invalid` error
//0o7777 is an arbitrary value that does not correspond to any combination of valid mode
//bits or supported file types
assert_eq!(
cage.fchmod_syscall(fd, 0o7777 as u32),
-(Errno::EINVAL as i32)
);
//checking if passing an invalid file descriptor to `fchmod_syscall` correctly
//results in `Invalid file descriptor` error.
//closing a previously opened file would make its file descriptor unused, and
//thus, invalid as `fchmod_syscall()` fd argument
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.fchmod_syscall(fd, S_IRWXA), -(Errno::EBADF as i32));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_dir_chdir() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
//testing the ability to make and change to directories
assert_eq!(cage.mkdir_syscall("/subdir1", S_IRWXA), 0);
assert_eq!(cage.mkdir_syscall("/subdir1/subdir2", S_IRWXA), 0);
assert_eq!(cage.mkdir_syscall("/subdir1/subdir2/subdir3", 0), 0);
assert_eq!(cage.access_syscall("subdir1", F_OK), 0);
assert_eq!(cage.chdir_syscall("subdir1"), 0);
assert_eq!(cage.access_syscall("subdir2", F_OK), 0);
assert_eq!(cage.chdir_syscall(".."), 0);
assert_eq!(cage.access_syscall("subdir1", F_OK), 0);
assert_eq!(cage.chdir_syscall("/subdir1/subdir2/subdir3"), 0);
assert_eq!(cage.access_syscall("../../../subdir1", F_OK), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_dir_mode() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let filepath1 = "/subdirDirMode1";
let filepath2 = "/subdirDirMode2";
let mut statdata = StatData::default();
assert_eq!(cage.mkdir_syscall(filepath1, S_IRWXA), 0);
assert_eq!(cage.stat_syscall(filepath1, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IRWXA | S_IFDIR as u32);
assert_eq!(cage.mkdir_syscall(filepath2, 0), 0);
assert_eq!(cage.stat_syscall(filepath2, &mut statdata), 0);
assert_eq!(statdata.st_mode, S_IFDIR as u32);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_dir_multiple() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
assert_eq!(cage.mkdir_syscall("/subdirMultiple1", S_IRWXA), 0);
assert_eq!(
cage.mkdir_syscall("/subdirMultiple1/subdirMultiple2", S_IRWXA),
0
);
assert_eq!(
cage.mkdir_syscall("/subdirMultiple1/subdirMultiple2/subdirMultiple3", 0),
0
);
let mut statdata = StatData::default();
//ensure that the file is a dir with all of the correct bits on for nodes
assert_eq!(
cage.stat_syscall("/subdirMultiple1/subdirMultiple2", &mut statdata),
0
);
assert_eq!(statdata.st_mode, S_IRWXA | S_IFDIR as u32);
assert_eq!(
cage.stat_syscall(
"/subdirMultiple1/subdirMultiple2/subdirMultiple3",
&mut statdata
),
0
);
assert_eq!(statdata.st_mode, S_IFDIR as u32);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_dup() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let flags: i32 = O_TRUNC | O_CREAT | O_RDWR;
let filepath = "/dupfile";
let fd = cage.open_syscall(filepath, flags, S_IRWXA);
let mut temp_buffer = sizecbuf(2);
assert!(fd >= 0);
assert_eq!(cage.write_syscall(fd, str2cbuf("12"), 2), 2);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_SET), 0);
assert_eq!(cage.read_syscall(fd, temp_buffer.as_mut_ptr(), 2), 2);
assert_eq!(cbuf2str(&temp_buffer), "12");
//duplicate the file descriptor
let fd2 = cage.dup_syscall(fd, None);
assert!(fd != fd2);
//essentially a no-op, but duplicate again -- they should be diff &fd's
let fd3 = cage.dup_syscall(fd, None);
assert!(fd != fd2 && fd != fd3);
//We don't need all three, though:
assert_eq!(cage.close_syscall(fd3), 0);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_END), 2);
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_END), 2);
// write some data to move the first position
assert_eq!(cage.write_syscall(fd, str2cbuf("34"), 2), 2);
//Make sure that they are still in the same place:
let mut buffer = sizecbuf(4);
assert_eq!(
cage.lseek_syscall(fd, 0, SEEK_SET),
cage.lseek_syscall(fd2, 0, SEEK_SET)
);
assert_eq!(cage.read_syscall(fd, buffer.as_mut_ptr(), 4), 4);
assert_eq!(cbuf2str(&buffer), "1234");
assert_eq!(cage.close_syscall(fd), 0);
//the other &fd should still work
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_END), 4);
assert_eq!(cage.write_syscall(fd2, str2cbuf("5678"), 4), 4);
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_SET), 0);
let mut buffer2 = sizecbuf(8);
assert_eq!(cage.read_syscall(fd2, buffer2.as_mut_ptr(), 8), 8);
assert_eq!(cage.close_syscall(fd2), 0);
assert_eq!(cbuf2str(&buffer2), "12345678");
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
fn ut_lind_fs_dup_invalid_fd() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
// Open a file and get a valid file descriptor
let fd = cage.open_syscall("/testfile", O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
// Close the file descriptor, making it invalid
assert_eq!(cage.close_syscall(fd), 0);
// Attempt to duplicate the invalid file descriptor
let new_fd = cage.dup_syscall(fd, None);
assert_eq!(new_fd, -(Errno::EBADF as i32));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
fn ut_lind_fs_dup_full_table() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
// Open a large number of files to fill the file descriptor table
for i in 0..1024 {
let fd = cage.open_syscall(&format!("/testfile{}", i), O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
}
// Attempt to duplicate a file descriptor, which should fail
let fd = cage.open_syscall("/testfile", O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
let new_fd = cage.dup_syscall(fd, None);
assert_eq!(new_fd, -(Errno::EBADF as i32));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_dup2() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let flags: i32 = O_TRUNC | O_CREAT | O_RDWR;
let filepath = "/dup2file";
let fd = cage.open_syscall(filepath, flags, S_IRWXA);
assert_eq!(cage.write_syscall(fd, str2cbuf("12"), 2), 2);
//trying to dup fd into fd + 1
let _fd2: i32 = cage.dup2_syscall(fd, fd + 1 as i32);
//should be a no-op since the last line did the same thing
let fd2: i32 = cage.dup2_syscall(fd, fd + 1 as i32);
//read/write tests for the files
assert_eq!(
cage.lseek_syscall(fd, 0, SEEK_END),
cage.lseek_syscall(fd2, 0, SEEK_END)
);
assert_eq!(cage.write_syscall(fd, str2cbuf("34"), 2), 2);
assert_eq!(
cage.lseek_syscall(fd, 0, SEEK_SET),
cage.lseek_syscall(fd2, 0, SEEK_SET)
);
let mut buffer = sizecbuf(4);
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_SET), 0);
assert_eq!(cage.read_syscall(fd, buffer.as_mut_ptr(), 4), 4);
assert_eq!(cbuf2str(&buffer), "1234");
assert_eq!(cage.close_syscall(fd), 0);
let mut buffer2 = sizecbuf(8);
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_END), 4);
assert_eq!(cage.write_syscall(fd2, str2cbuf("5678"), 4), 4);
assert_eq!(cage.lseek_syscall(fd2, 0, SEEK_SET), 0);
assert_eq!(cage.read_syscall(fd2, buffer2.as_mut_ptr(), 8), 8);
assert_eq!(cbuf2str(&buffer2), "12345678");
assert_eq!(cage.close_syscall(fd2), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
fn ut_lind_fs_dup2_invalid_fd() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
// Open a file
let fd = cage.open_syscall("/testfile", O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
// Close the file descriptor, making it invalid
assert_eq!(cage.close_syscall(fd), 0);
// Attempt to duplicate the invalid file descriptor
let new_fd = cage.dup2_syscall(fd, 5);
assert_eq!(new_fd, -(Errno::EBADF as i32));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
fn ut_lind_fs_dup2_full_table() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
// Open a large number of files to fill the file descriptor table
for i in 0..1024 {
let fd = cage.open_syscall(&format!("/testfile{}", i), O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
}
// Attempt to duplicate a file descriptor, which should fail
let fd = cage.open_syscall("/testfile", O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
let new_fd = cage.dup2_syscall(fd, 5); // Try to duplicate to an existing fd
assert_eq!(new_fd, -(Errno::EBADF as i32));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fcntl_valid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let sockfd = cage.socket_syscall(AF_INET, SOCK_STREAM, 0);
let filefd = cage.open_syscall("/fcntl_file_1", O_CREAT | O_EXCL, S_IRWXA);
//changing O_CLOEXEC file descriptor flag and checking if it was correctly set
assert_eq!(cage.fcntl_syscall(sockfd, F_SETFD, O_CLOEXEC), 0);
assert_eq!(cage.fcntl_syscall(sockfd, F_GETFD, 0), O_CLOEXEC);
//changing the file access mode to read-only, enabling the
//O_NONBLOCK file status flag, and checking if they were correctly set
assert_eq!(
cage.fcntl_syscall(filefd, F_SETFL, O_RDONLY | O_NONBLOCK),
0
);
assert_eq!(cage.fcntl_syscall(filefd, F_GETFL, 0), 2048);
//when provided with 'F_GETFD' or 'F_GETFL' command, 'arg' should be ignored, thus even
//negative arg values should produce nomal behavior
assert_eq!(cage.fcntl_syscall(sockfd, F_GETFD, -132), O_CLOEXEC);
assert_eq!(cage.fcntl_syscall(filefd, F_GETFL, -1998), 2048);
assert_eq!(cage.close_syscall(filefd), 0);
assert_eq!(cage.close_syscall(sockfd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fcntl_invalid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let filefd = cage.open_syscall("/fcntl_file_2", O_CREAT | O_EXCL, S_IRWXA);
//when presented with a nonexistent command, 'Invalid Argument' error should be thrown
//29 is an arbitrary number that does not correspond to any of the defined 'fcntl' commands
assert_eq!(cage.fcntl_syscall(filefd, 29, 0), -(Errno::EINVAL as i32));
//when a negative arg is provided with F_SETFD, F_SETFL, or F_DUPFD,
//Invalid Argument' error should be thrown as well
assert_eq!(
cage.fcntl_syscall(filefd, F_SETFD, -5),
-(Errno::EINVAL as i32)
);
assert_eq!(
cage.fcntl_syscall(filefd, F_SETFL, -5),
-(Errno::EINVAL as i32)
);
assert_eq!(
cage.fcntl_syscall(filefd, F_DUPFD, -5),
-(Errno::EINVAL as i32)
);
assert_eq!(cage.close_syscall(filefd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fcntl_dup() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let filefd1 = cage.open_syscall("/fcntl_file_4", O_CREAT | O_EXCL | O_RDWR, S_IRWXA);
//on success, returning the new file descriptor greater than or equal to 100
//and different from the original file descriptor
let filefd2 = cage.fcntl_syscall(filefd1, F_DUPFD, 100);
assert!(filefd2 >= 100 && filefd2 != filefd1);
//to check if both file descriptors refer to the same fie, we can write into a file
//using one file descriptor, read from the file using another file descriptor,
//and make sure that the contents are the same
let mut temp_buffer = sizecbuf(9);
assert_eq!(cage.write_syscall(filefd1, str2cbuf("Test text"), 9), 9);
assert_eq!(cage.read_syscall(filefd2, temp_buffer.as_mut_ptr(), 9), 9);
assert_eq!(cbuf2str(&temp_buffer), "Test text");
//file status flags are shared by duplicated file descriptors resulting from
//a single opening of the file
assert_eq!(
cage.fcntl_syscall(filefd1, F_GETFL, 0),
cage.fcntl_syscall(filefd2, F_GETFL, 0)
);
assert_eq!(cage.close_syscall(filefd1), 0);
assert_eq!(cage.close_syscall(filefd2), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_ioctl_valid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
//setting up two integer values (a zero value to test clearing nonblocking I/O behavior
//and a non-zero value to test setting nonblocking I/O behavior)
let mut arg0: i32 = 0;
let mut arg1: i32 = 1;
//ioctl requires a pointer to an integer to be passed with FIONBIO command
let union0: IoctlPtrUnion = IoctlPtrUnion { int_ptr: &mut arg0 };
let union1: IoctlPtrUnion = IoctlPtrUnion { int_ptr: &mut arg1 };
let sockfd = cage.socket_syscall(AF_INET, SOCK_STREAM, 0);
//calling ioctl with FIONBIO command and a pointer to a zero-valued integer
//to clear the socket's nonblocking I/O, and checking if the flag was correctly set
assert_eq!(cage.ioctl_syscall(sockfd, FIONBIO, union0), 0);
assert_eq!(cage.fcntl_syscall(sockfd, F_GETFL, 0) & O_NONBLOCK, 0);
//calling ioctl with FIONBIO command and a pointer to a non-zero-valued integer
//to set the socket's nonblocking I/O, and checking if the flag was correctly set
assert_eq!(cage.ioctl_syscall(sockfd, FIONBIO, union1), 0);
assert_eq!(
cage.fcntl_syscall(sockfd, F_GETFL, 0) & O_NONBLOCK,
O_NONBLOCK
);
assert_eq!(cage.close_syscall(sockfd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_ioctl_invalid_args() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
//setting up two integer values (a zero value to test clearing nonblocking I/O behavior on
//non-socket type and a non-zero value to test setting nonblocking I/O behavior
//on non-socket type)
let mut arg0: i32 = 0;
let mut arg1: i32 = 1;
//ioctl requires a pointer to an integer to be passed with FIONBIO command
let union0: IoctlPtrUnion = IoctlPtrUnion { int_ptr: &mut arg0 };
let union1: IoctlPtrUnion = IoctlPtrUnion { int_ptr: &mut arg1 };
let sockfd = cage.socket_syscall(AF_INET, SOCK_STREAM, 0);
let filefd = cage.open_syscall("/ioctl_file", O_CREAT | O_EXCL, S_IRWXA);
//trying to use FIONBIO command on a non-socket type (the file type in this case)
//for any 'ptrunion' value should throw a 'Not a typewriter' error
assert_eq!(
cage.ioctl_syscall(filefd, FIONBIO, union0),
-(Errno::ENOTTY as i32)
);
assert_eq!(
cage.ioctl_syscall(filefd, FIONBIO, union1),
-(Errno::ENOTTY as i32)
);
assert_eq!(cage.close_syscall(filefd), 0);
//calling 'ioctl' with a control function that is not implemented yet should
//return an 'Invalid argument' error
//21600 is an arbitrary integer that does not correspond to any implemented
//control functions for ioctl syscall
assert_eq!(
cage.ioctl_syscall(sockfd, 21600, union0),
-(Errno::EINVAL as i32)
);
//calling ioctl with FIONBIO command and a null pointer
//should return a 'Bad address' error
let null_ptr: *mut i32 = std::ptr::null_mut();
let union_null: IoctlPtrUnion = IoctlPtrUnion { int_ptr: null_ptr };
assert_eq!(
cage.ioctl_syscall(sockfd, FIONBIO, union_null),
-(Errno::EFAULT as i32)
);
//calling ioctl on a closed file descriptor should throw a 'Bad file number' error
assert_eq!(cage.close_syscall(sockfd), 0);
assert_eq!(
cage.fcntl_syscall(sockfd, F_GETFL, 0),
-(Errno::EBADF as i32)
);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fdflags() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let path = "/fdFlagsFile";
let fd = cage.creat_syscall(path, S_IRWXA);
assert_eq!(cage.close_syscall(fd), 0);
let read_fd = cage.open_syscall(path, O_RDONLY, S_IRWXA);
assert_eq!(cage.lseek_syscall(read_fd, 0, SEEK_SET), 0);
assert_eq!(
cage.write_syscall(read_fd, str2cbuf("Hello! This should not write."), 28),
-(Errno::EBADF as i32)
);
let mut buf = sizecbuf(100);
assert_eq!(cage.lseek_syscall(read_fd, 0, SEEK_SET), 0);
//this fails because nothing is written to the readfd (the previous write was unwritable)
assert_eq!(cage.read_syscall(read_fd, buf.as_mut_ptr(), 100), 0);
assert_eq!(cage.close_syscall(read_fd), 0);
let write_fd = cage.open_syscall(path, O_WRONLY, S_IRWXA);
let mut buf2 = sizecbuf(100);
assert_eq!(cage.lseek_syscall(write_fd, 0, SEEK_SET), 0);
assert_eq!(
cage.read_syscall(write_fd, buf2.as_mut_ptr(), 100),
-(Errno::EBADF as i32)
);
assert_eq!(cage.lseek_syscall(write_fd, 0, SEEK_SET), 0);
assert_eq!(
cage.write_syscall(write_fd, str2cbuf("Hello! This should write."), 24),
24
);
assert_eq!(cage.close_syscall(write_fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_file_link_unlink() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let path = "/fileLink";
let path2 = "/fileLink2";
let fd = cage.open_syscall(path, O_CREAT | O_EXCL | O_WRONLY, S_IRWXA);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_SET), 0);
assert_eq!(cage.write_syscall(fd, str2cbuf("hi"), 2), 2);
let mut statdata = StatData::default();
assert_eq!(cage.stat_syscall(path, &mut statdata), 0);
assert_eq!(statdata.st_size, 2);
assert_eq!(statdata.st_nlink, 1);
let mut statdata2 = StatData::default();
//make sure that this has the same traits as the other file that we linked
// and make sure that the link count on the orig file has increased
assert_eq!(cage.link_syscall(path, path2), 0);
assert_eq!(cage.stat_syscall(path, &mut statdata), 0);
assert_eq!(cage.stat_syscall(path2, &mut statdata2), 0);
assert!(statdata == statdata2);
assert_eq!(statdata.st_nlink, 2);
//now we unlink
assert_eq!(cage.unlink_syscall(path), 0);
assert_eq!(cage.stat_syscall(path2, &mut statdata2), 0);
assert_eq!(statdata2.st_nlink, 1);
//it shouldn't work to stat the orig since it is gone
assert_ne!(cage.stat_syscall(path, &mut statdata), 0);
assert_eq!(cage.unlink_syscall(path2), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_file_lseek_past_end() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let path = "/lseekPastEnd";
let fd = cage.open_syscall(path, O_CREAT | O_EXCL | O_RDWR, S_IRWXA);
assert_eq!(cage.write_syscall(fd, str2cbuf("hello"), 5), 5);
//seek past the end and then write
assert_eq!(cage.lseek_syscall(fd, 10, SEEK_SET), 10);
assert_eq!(cage.write_syscall(fd, str2cbuf("123456"), 6), 6);
let mut buf = sizecbuf(16);
assert_eq!(cage.lseek_syscall(fd, 0, SEEK_SET), 0);
assert_eq!(cage.read_syscall(fd, buf.as_mut_ptr(), 20), 16);
assert_eq!(cbuf2str(&buf), "hello\0\0\0\0\0123456");
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_fstat_complex() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let path = "/complexFile";
let fd = cage.open_syscall(path, O_CREAT | O_WRONLY, S_IRWXA);
assert_eq!(cage.write_syscall(fd, str2cbuf("testing"), 4), 4);
let mut statdata = StatData::default();
assert_eq!(cage.fstat_syscall(fd, &mut statdata), 0);
assert_eq!(statdata.st_size, 4);
assert_eq!(statdata.st_nlink, 1);
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_getuid() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
//let's get the initial -1s out of the way
cage.getgid_syscall();
cage.getegid_syscall();
cage.getuid_syscall();
cage.geteuid_syscall();
//testing to make sure that all of the gid and uid values are good to go when system is initialized
assert_eq!(cage.getgid_syscall() as u32, DEFAULT_GID);
assert_eq!(cage.getegid_syscall() as u32, DEFAULT_GID);
assert_eq!(cage.getuid_syscall() as u32, DEFAULT_UID);
assert_eq!(cage.geteuid_syscall() as u32, DEFAULT_UID);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_load_fs() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let mut statdata = StatData::default();
//testing that all of the dev files made it out safe and sound
cage.stat_syscall("/dev", &mut statdata);
assert_eq!(cage.stat_syscall("/dev/null", &mut statdata), 0);
assert_eq!(statdata.st_rdev, makedev(&DevNo { major: 1, minor: 3 }));
assert_eq!(cage.stat_syscall("/dev/random", &mut statdata), 0);
assert_eq!(statdata.st_rdev, makedev(&DevNo { major: 1, minor: 8 }));
assert_eq!(cage.stat_syscall("/dev/urandom", &mut statdata), 0);
assert_eq!(statdata.st_rdev, makedev(&DevNo { major: 1, minor: 9 }));
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
pub fn ut_lind_fs_mknod_empty_path() {
//acquiring a lock on TESTMUTEX prevents other tests from running concurrently, and also performs clean env setup
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);
let dev = makedev(&DevNo { major: 1, minor: 3 });