-
Notifications
You must be signed in to change notification settings - Fork 51
/
validate_api.c
2620 lines (2024 loc) · 49.7 KB
/
validate_api.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
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <setjmp.h>
#include "util.h"
#include "symposium.h"
#include "tinyoslib.h"
#include "unit_testing.h"
/*
*
* TESTS
*
*/
/*
test_boot
Test that the boot function executes the boot task and returns.
*/
struct test_cpu_rec {
uint ncores;
uint core;
uint nterm;
int argl;
void* args;
struct test_cpu_rec* rec;
};
int test_boot_boot(int argl, void* args) {
typedef struct test_cpu_rec* REC;
ASSERT(argl==sizeof(REC));
REC rec;
memcpy(&rec, args, argl);
rec->rec = rec;
rec->args = args;
rec->argl = argl;
rec->ncores = cpu_cores();
rec->core = cpu_core_id;
rec->nterm = bios_serial_ports();
return 0;
}
BARE_TEST(test_boot,
"Test that the boot(...) function initializes the VM\n"
"and passes arguments to the init task correctly.")
{
struct test_cpu_rec cpu_rec;
struct test_cpu_rec* cpu_rec_ptr = &cpu_rec;
FUDGE(cpu_rec);
boot(1,0, test_boot_boot, sizeof(cpu_rec_ptr), &cpu_rec_ptr);
ASSERT(cpu_rec.argl == sizeof(cpu_rec_ptr));
ASSERT( (struct test_cpu_rec **)cpu_rec.args != &cpu_rec_ptr);
ASSERT( cpu_rec.rec == &cpu_rec );
ASSERT(cpu_rec.nterm == 0);
ASSERT(cpu_rec.ncores == 1);
ASSERT(cpu_rec.core == 0);
}
/*********************************************
*
*
*
* Process tests
*
*
*
*********************************************/
/*
Test that the child process created, gets the same pid as the
parent got returned from exec.
*/
BOOT_TEST(test_pid_of_init_is_one,
"Test that the pid of the init task is 1. This may\n"
"not be according to spec, but this is something\n"
"we will correct in the next update."
)
{
ASSERT(GetPid()==1);
return 0;
}
static void waitchild_error()
{
/* Cannot wait on myself */
ASSERT(WaitChild(GetPid(),NULL)==NOPROC);
ASSERT(WaitChild(MAX_PROC, NULL)==NOPROC);
ASSERT(WaitChild(GetPid()+1, NULL)==NOPROC);
}
static int subprocess(int argl, void* args)
{
ASSERT(GetPid()!=1);
waitchild_error();
return 0;
}
BOOT_TEST(test_waitchild_error_on_invalid_pid,
"Test that WaitChild returns an error when the pid is invalid."
)
{
waitchild_error();
Pid_t cpid = Exec(subprocess, 0, NULL);
ASSERT(WaitChild(NOPROC, NULL)==cpid);
return 0;
}
static int void_child(int argl, void* args) { return 0; }
static int bad_child(int argl, void* args)
{
Pid_t cpid = *(Pid_t*)args;
ASSERT(WaitChild(cpid, NULL)==NOPROC);
return 0;
}
BOOT_TEST(test_waitchild_error_on_nonchild,
"Test that WaitChild returns an error when the process is not\n"
"its child."
)
{
Pid_t cpid = Exec(void_child, 0, NULL);
Pid_t badpid = Exec(bad_child, sizeof(cpid), &cpid);
ASSERT(badpid != NOPROC);
ASSERT(WaitChild(badpid, NULL)==badpid);
ASSERT(WaitChild(cpid, NULL)==cpid);
return 0;
}
/* used to pass information to parent */
struct test_pid_rec {
Pid_t pid;
int level;
};
BOOT_TEST(test_exec_getpid_wait,
"Test that Exec returns the same pid as the child sees\n"
"by calling GetPid(). Also, that WaitChild with a given pid\n"
"returns the correct status.",
.timeout=20
)
{
struct test_pid_rec myrec; /* only used by init task */
struct test_pid_rec* prec;
if(argl==0) {
ASSERT(GetPid()==1);
prec = &myrec;
prec->level = 7; /* 4^7 = 2^14 = 16384 children will be run */
} else {
ASSERT(argl==sizeof(struct test_pid_rec*));
prec = *(struct test_pid_rec**)args;
}
prec->pid = GetPid();
if(prec->level>0) {
for(int i=0;i<3;i++) {
/* Prepare rec for child */
struct test_pid_rec rec;
rec.level = prec->level - 1;
/* Exec child */
struct test_pid_rec* arg = &rec;
Pid_t cpid = Exec(test_exec_getpid_wait.boot, sizeof(arg), &arg);
ASSERT(cpid != NOPROC);
/* Wait for child and verify */
if(cpid != NOPROC) {
int status;
Pid_t wpid = WaitChild(cpid, &status);
ASSERT(wpid==cpid);
ASSERT(status==cpid);
}
}
}
return GetPid();
}
static int copyarg_child(int argl, void* args)
{
*(int*)args = 1;
return 0;
}
BOOT_TEST(test_exec_copies_arguments,
"Test that Exec creates of copy of the arguments of the new process."
)
{
Pid_t cpid;
int value = 0;
ASSERT((cpid = Exec(copyarg_child, sizeof(value), &value))!=NOPROC);
WaitChild(cpid, NULL);
ASSERT(value==0);
return 0;
}
BOOT_TEST(test_wait_for_any_child,
"Test WaitChild when called to wait on any child."
)
{
#define NCHILDREN 5
#define NLEVELS 3
struct test_pid_rec myrec;
struct test_pid_rec* prec;
if(argl==0) {
ASSERT(GetPid()==1);
prec = &myrec;
prec->level = NLEVELS;
} else {
prec = *(struct test_pid_rec**)args;
prec->pid = GetPid();
}
if(prec->level>0) {
struct test_pid_rec rec[NCHILDREN];
/* Test many execs */
for(int i=0; i<NCHILDREN; i++) {
struct test_pid_rec* arg = &rec[i];
rec[i].level = prec->level - 1;
Pid_t cpid = Exec(test_wait_for_any_child.boot, sizeof(arg), &arg);
ASSERT(cpid!=NOPROC);
}
for(int i=0; i<NCHILDREN; i++) {
Pid_t cpid = WaitChild(NOPROC, NULL);
ASSERT(cpid != NOPROC);
/* try to find cpid in array */
int j;
for(j=0; j<NCHILDREN;j++)
if(rec[j].pid == cpid) break;
ASSERT(j < NCHILDREN);
rec[j].pid = NOPROC; /* Reset it so we don't find it again! */
}
ASSERT(WaitChild(NOPROC, NULL)==NOPROC);
}
return 0;
#undef NCHILDREN
#undef NLEVELS
}
int exiting_child(int arg, void* args) {
Exit(GetPid());
ASSERT(0);
return 1;
}
BOOT_TEST(test_exit_returns_status,
"Test that the exit status is returned by Exit"
)
{
Pid_t children[100];
for(int i=0;i<100;i++)
children[i] = Exec(exiting_child, 0, NULL);
for(int i=0;i<100;i++) {
int status;
WaitChild(children[i], &status);
ASSERT(status==children[i]);
}
return 0;
}
static int pid_returning_child(int arg, void* args) {
return GetPid();
}
BOOT_TEST(test_main_return_returns_status,
"Test that the exit status is returned by return from main task"
)
{
const int N=10;
Pid_t children[N];
for(int i=0;i<N;i++)
children[i] = Exec(pid_returning_child, 0, NULL);
for(int i=0;i<N;i++) {
int status;
WaitChild(children[i], &status);
ASSERT(status==children[i]);
}
return 0;
}
static int orphan_grandchild(int argl, void* args)
{
return 1;
}
static int dying_child(int arg, void* args)
{
for(int i=0;i<5;i++)
ASSERT(Exec(orphan_grandchild, 0, NULL)!=NOPROC);
return 100;
}
BOOT_TEST(test_orphans_adopted_by_init,
"Test that when a process exits leaving orphans, init becomes the new parent."
)
{
for(int i=0;i<3; i++)
ASSERT(Exec(dying_child,0,NULL)!=NOPROC);
/* Now wait for 18 children (3 child + 15 grandchild) */
int sum = 0;
for(int i=0;i<18;i++) {
int status;
ASSERT(WaitChild(NOPROC, &status) != NOPROC);
sum += status;
}
/* Check that we have no more */
ASSERT(WaitChild(NOPROC, NULL) == NOPROC);
ASSERT(sum == 315);
return 0;
}
/*********************************************
*
*
*
* Synchronization tests
*
*
*
*********************************************/
/*
Test that a timed wait on a condition variable terminates after the timeout.
*/
static unsigned long tspec2msec(struct timespec t)
{
return 1000ul*t.tv_sec + t.tv_nsec/1000000ul;
}
static int do_timeout(int argl, void* args) {
timeout_t t = *((timeout_t *) args);
Mutex mx = MUTEX_INIT;
CondVar cv = COND_INIT;
struct timespec t1, t2;
clock_gettime(CLOCK_REALTIME, &t1);
Mutex_Lock(&mx);
Cond_TimedWait(&mx, &cv, t);
clock_gettime(CLOCK_REALTIME, &t2);
unsigned long Dt = tspec2msec(t2)-tspec2msec(t1);
/* Allow a large, 20% error */
ASSERT(abs(Dt-t)*5 <= Dt);
return 0;
}
BOOT_TEST(test_cond_timedwait_timeout,
"Test that timed waits on a condition variable terminate without blocking after the timeout."
)
{
for(timeout_t t=500; t < 1000; t+=100) {
Exec(do_timeout, sizeof(t), &t);
}
for(timeout_t t=550; t < 1000; t+=100) {
Exec(do_timeout, sizeof(t), &t);
}
/*
Wait all child processes, before leaving the current stack frame!
Else, the local functions may cause a crash!
*/
while(WaitChild(NOPROC,NULL)!=NOPROC);
return 0;
}
/*
Test that a timed wait on a condition variable terminates at a signal.
*/
struct long_blocking_args {
Mutex* m;
CondVar* cv;
CondVar* pcv;
int* flag;
};
static int long_blocking(int argl, void* args)
{
struct long_blocking_args A = *(struct long_blocking_args*)args;
Mutex_Lock(A.m);
* A.flag = 1;
Cond_Signal(A.cv);
Cond_TimedWait(A.m, A.cv, 10000000); // 3 hour wait
Mutex_Unlock(A.m);
return 0;
}
BOOT_TEST(test_cond_timedwait_signal,
"Test that timed waits on a condition variable terminates immediately on signal."
)
{
Mutex m = MUTEX_INIT;
CondVar cv = COND_INIT;
int flag=0;
struct long_blocking_args A = {.m = &m, .cv=&cv, .flag=&flag};
Pid_t child = Exec(long_blocking, sizeof(A), &A);
Mutex_Lock(&m);
while(! flag)
Cond_Wait(&m, &cv);
Cond_Signal(&cv);
Mutex_Unlock(&m);
WaitChild(child, NULL);
return 0;
}
static int long_blocking2(int argl, void* args)
{
struct long_blocking_args A = *(struct long_blocking_args*)args;
Mutex_Lock(A.m);
(* A.flag) ++;
Cond_Signal(A.pcv);
Cond_TimedWait(A.m, A.cv, 10000000); // 3 hour wait
Mutex_Unlock(A.m);
return 0;
}
BOOT_TEST(test_cond_timedwait_broadcast,
"Test that timed waits on a condition variable terminate immediately on broadcast."
)
{
Mutex m = MUTEX_INIT;
CondVar cv = COND_INIT;
CondVar pcv = COND_INIT;
int flag=0;
const int N=100; // spawn 100 children
struct long_blocking_args A = {.m=&m, .cv=&cv, .pcv=&pcv, .flag=&flag };
// create N children
for(int i=0; i<N; i++) Exec(long_blocking2, sizeof(A), &A);
Mutex_Lock(&m);
// wait for all children to sleep
while(flag!=N) Cond_Wait(&m, &pcv);
// wake all children up!
Cond_Broadcast(&cv);
Mutex_Unlock(&m);
// wait all children
while(WaitChild(NOPROC, NULL)!=NOPROC);
return 0;
}
/*********************************************
*
*
*
* I/O tests
*
*
*
*********************************************/
BOOT_TEST(test_get_terminals,
"Test that the number returned by GetTerminalDevices() is equal to the\n"
"number of serial ports in the VM."
)
{
ASSERT(bios_serial_ports()==GetTerminalDevices());
return 0;
}
BOOT_TEST(test_dup2_error_on_nonfile,
"Test that Dup2 will return an error if oldfd is not a file.")
{
for(Fid_t fid = 0; fid < MAX_FILEID; fid++)
ASSERT(Dup2(fid, MAX_FILEID-1-fid)==-1);
return 0;
}
BOOT_TEST(test_dup2_error_on_invalid_fid,
"Test that Dup2 returns error when some fid is invalid."
)
{
ASSERT(Dup2(NOFILE, 3)==-1);
ASSERT(Dup2(MAX_FILEID, 3)==-1);
Fid_t fid = OpenNull(0);
assert(fid!=NOFILE);
ASSERT(Dup2(fid, NOFILE)==-1);
ASSERT(Dup2(fid, MAX_FILEID)==-1);
return 0;
}
BOOT_TEST(test_open_terminals,
"Test that every legal terminal can be opened."
)
{
Fid_t term[MAX_TERMINALS];
for(uint i=0; i<GetTerminalDevices(); i++) {
term[i] = OpenTerminal(i);
ASSERT(term[i]!=NOFILE);
}
return 0;
}
BOOT_TEST(test_close_error_on_invalid_fid,
"Test that Close returns error on invalid fid."
)
{
ASSERT(Close(NOFILE)==-1);
ASSERT(Close(MAX_FILEID)==-1);
return 0;
}
BOOT_TEST(test_close_success_on_valid_nonfile_fid,
"Test that Close returns success on valid fid, even if there is no\n"
"open file for this id."
)
{
for(Fid_t i=0; i<MAX_FILEID; i++)
ASSERT(Close(i)==0);
return 0;
}
BOOT_TEST(test_close_terminals,
"Test that terminals can be opened and then closed without error."
)
{
Fid_t term[MAX_TERMINALS];
for(uint i=0; i<GetTerminalDevices(); i++) {
term[i] = OpenTerminal(i);
ASSERT(term[i]!=NOFILE);
}
for(uint i=0; i<GetTerminalDevices(); i++) {
ASSERT(Close(term[i])==0);
}
return 0;
}
void checked_read(Fid_t fid, const char* message)
{
int mlen = strlen(message);
char buffer[mlen];
ASSERT(Read(fid, buffer, mlen)==mlen);
ASSERT(memcmp(buffer, message, mlen)==0);
}
BOOT_TEST(test_read_kbd,
"Test that we can read a few bytes from the keyboard on terminal 0.",
.minimum_terminals = 1
)
{
assert(GetTerminalDevices()>0);
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
sendme(0, "Hello");
checked_read(fterm, "Hello");
return 0;
}
BOOT_TEST(test_read_kbd_big,
"Test that we can read massively from the keyboard on terminal 0.",
.minimum_terminals = 1, .timeout = 20
)
{
assert(GetTerminalDevices()>0);
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
char bytes[1025];
FUDGE(bytes);
bytes[1024]='\0';
/* send me 1Mbyte */
for(int i=0; i<1024; i++)
sendme(0, bytes);
/* Read 16kb bytes at a time */
char buffer[16384];
uint count = 0;
uint total = 1<<20;
while(count < total)
{
int remain = total-count;
int rc = Read(fterm, buffer, (remain<16384)? remain: 16384);
ASSERT(rc>0);
count += rc;
}
return 0;
}
BOOT_TEST(test_dup2_copies_file,
"This test copies that Dup2 copies the file to another file descriptor.",
.minimum_terminals = 1
)
{
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
if(fterm!=0) {
ASSERT(Dup2(fterm, 0)==0);
Close(fterm);
}
sendme(0, "zavarakatranemia");
ASSERT(Dup2(0,1)==0);
ASSERT(Dup2(0,2)==0);
ASSERT(Dup2(0,3)==0);
ASSERT(Dup2(0,4)==0);
checked_read(1, "zava");
checked_read(3, "raka");
checked_read(2, "trane");
checked_read(4, "mia");
return 0;
}
BOOT_TEST(test_read_error_on_bad_fid,
"Test that Read will return an error when called on a bad fid"
)
{
char buffer[10];
ASSERT(Read(0, buffer, 10)==-1);
return 0;
}
BOOT_TEST(test_read_from_many_terminals,
"Test that Read can read from all terminals",
.minimum_terminals = 2
)
{
Fid_t term[MAX_TERMINALS];
for(uint i = 0; i < GetTerminalDevices(); i++) {
term[i] = OpenTerminal(i);
ASSERT(term[i]!=NOFILE);
}
for(uint i = 0; i < GetTerminalDevices(); i++) {
char message[32];
sprintf(message, "This is terminal %d", i);
sendme(i, message);
}
for(uint i = 0; i < GetTerminalDevices(); i++) {
char message[32];
sprintf(message, "This is terminal %d", i);
checked_read(term[i], message);
}
return 0;
}
static int greeted_child(int argl, void* args)
{
checked_read(0, "Hello child");
checked_read(0, "Hello again");
return 0;
}
BOOT_TEST(test_child_inherits_files,
"Test that a child process inherits files.",
.minimum_terminals = 1
)
{
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
if(fterm!=0)
ASSERT(Dup2(fterm, 0)==0);
sendme(0, "Hello child");
Pid_t cpid = Exec(greeted_child, 0, NULL);
ASSERT(Close(0)==0);
ASSERT(Close(fterm)==0);
sendme(0, "Hello again");
ASSERT(cpid!=NOPROC);
ASSERT(WaitChild(NOPROC, NULL)==cpid);
return 0;
}
BOOT_TEST(test_null_device,
"Test the null device."
)
{
void test_read(Fid_t fid)
{
char z[] = "zavarakatranemia";
char z1[] = "\0\0\0\0\0\0\0\0\0\0anemia";
ASSERT(Read(fid, z, 10)==10);
ASSERT(memcmp(z,z1, 17)==0);
}
Fid_t fn = OpenNull();
ASSERT(fn!=-1);
test_read(fn);
ASSERT(Write(fn, NULL, 123456)==123456);
ASSERT(Close(fn)==0);
return 0;
}
/***********************************************************************************8
*************************************************/
void checked_write(Fid_t fid, const char* message)
{
int mlen = strlen(message);
for(int count=0; count < mlen;) {
int wno = Write(fid, message+count, 1);
ASSERT(wno>0);
count += wno;
}
}
BOOT_TEST(test_write_con,
"Test that we can write a few bytes to the console on terminal 0.",
.minimum_terminals = 1
)
{
assert(GetTerminalDevices()>0);
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
expect(0, "Hello");
checked_write(fterm, "Hello");
return 0;
}
BOOT_TEST(test_write_con_big,
"Test that we can write massively to the console on terminal 0.",
.minimum_terminals = 1
)
{
assert(GetTerminalDevices()>0);
Fid_t fterm = OpenTerminal(0);
ASSERT(fterm!=NOFILE);
char bytes[1025];
FUDGE(bytes);
bytes[1024]='\0';
/* send me 1Mbyte */
for(int i=0; i<1024; i++)
expect(0, bytes);
/* Create a 16kb block */
char buffer[16384];
FUDGE(buffer);
int total = 1<<20;
int count = 0;
while(count < total)
{
int remain = total-count;
int rc = Write(fterm, buffer, (remain<16384)? remain: 16384);
ASSERT(rc>0);
count += rc;
}
return 0;
}
BOOT_TEST(test_write_error_on_bad_fid,
"Test that Write will return an error when called on a bad fid"
)
{
/* The compiler is a bit overzealous here, so we are forced to suppress the
warning */
#pragma GCC diagnostic push // save the actual diag context
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
char buffer[10];
ASSERT(Write(0, buffer, 10)==-1);
#pragma GCC diagnostic pop
return 0;
}
BOOT_TEST(test_write_to_many_terminals,
"Test that Write can send to all terminals",
.minimum_terminals = 2
)
{
Fid_t term[MAX_TERMINALS];
for(uint i = 0; i < GetTerminalDevices(); i++) {
term[i] = OpenTerminal(i);
ASSERT(term[i]!=NOFILE);
}
for(uint i = 0; i < GetTerminalDevices(); i++) {
char message[32];
sprintf(message, "This is terminal %d", i);
expect(i, message);
}
for(uint i = 0; i < GetTerminalDevices(); i++) {
char message[32];
sprintf(message, "This is terminal %d", i);
checked_write(term[i], message);
}
return 0;
}
TEST_SUITE(basic_tests,
"A suite of basic tests, focusing on the functional behaviour of the\n"
"tinyos3 API, but not the operational (concurrency and I/O multiplexing)."
)
{
&test_boot,
&test_pid_of_init_is_one,
&test_waitchild_error_on_nonchild,
&test_waitchild_error_on_invalid_pid,
&test_exec_getpid_wait,
&test_exec_copies_arguments,
&test_exit_returns_status,
&test_main_return_returns_status,
&test_wait_for_any_child,
&test_orphans_adopted_by_init,
&test_cond_timedwait_timeout,
&test_cond_timedwait_signal,
&test_cond_timedwait_broadcast,
&test_null_device,
&test_get_terminals,
&test_open_terminals,
&test_dup2_error_on_nonfile,
&test_dup2_error_on_invalid_fid,
&test_dup2_copies_file,
&test_close_error_on_invalid_fid,
&test_close_success_on_valid_nonfile_fid,
&test_close_terminals,
&test_read_kbd,
&test_read_kbd_big,
&test_read_error_on_bad_fid,
&test_read_from_many_terminals,
&test_write_con,
&test_write_con_big,
&test_write_error_on_bad_fid,
&test_write_to_many_terminals,
&test_child_inherits_files,
NULL
};
/*********************************************
*
*
*
* Thread tests
*
*
*
*********************************************/
void sleep_thread(int sec) {
Mutex mx = MUTEX_INIT;
CondVar cond = COND_INIT;
Mutex_Lock(&mx);
ASSERT(Cond_TimedWait(&mx,&cond,1000*sec)==0);
}
/*
Helper that spawns a process, waits for its completion
and returns its status.
*/
int run_get_status(Task task, int argl, void* args)
{
Pid_t pid = Exec(task, argl, args);
ASSERT(pid!=NOPROC);
int exitval;
ASSERT(WaitChild(pid, &exitval)==pid);
return exitval;
}
BOOT_TEST(test_threadself,
"Test that ThreadSelf is somewhat sane")
{
ASSERT(ThreadSelf() != NOTHREAD);
ASSERT(ThreadSelf() == ThreadSelf());
return 0;
}
BOOT_TEST(test_join_illegal_tid_gives_error,
"Test that ThreadJoin rejects an illegal Tid")
{
int* illegal_ptr = (int*) -1;
ASSERT(ThreadJoin(NOTHREAD, illegal_ptr)==-1);
/* Test with random numbers. Since we only have one thread, any call is an illegal call. */
for(int i=0; i<100; i++) {