Skip to content
Snippets Groups Projects
Commit 979cd130 authored by gback's avatar gback
Browse files

added fd-management.c test

parent a43db403
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ write-boundary write-zero write-stdin write-bad-fd exec-once exec-arg \
exec-multiple exec-missing exec-bad-ptr wait-simple wait-twice \
wait-killed wait-bad-pid multi-recurse multi-child-fd rox-simple \
rox-child rox-multichild bad-read bad-write bad-read2 bad-write2 \
bad-jump bad-jump2)
bad-jump bad-jump2 fd-management)
tests/userprog_PROGS = $(tests/userprog_TESTS) $(addprefix \
tests/userprog/,child-simple child-args child-bad child-close child-rox)
......@@ -97,6 +97,7 @@ tests/userprog/child-args_SRC = tests/userprog/args.c
tests/userprog/child-bad_SRC = tests/userprog/child-bad.c tests/main.c
tests/userprog/child-close_SRC = tests/userprog/child-close.c
tests/userprog/child-rox_SRC = tests/userprog/child-rox.c
tests/userprog/fd-management_SRC = tests/userprog/fd-management.c tests/main.c
$(foreach prog,$(tests/userprog_PROGS),$(eval $(prog)_SRC += tests/lib.c))
......
......@@ -43,6 +43,9 @@ Functionality of system calls:
- Test "halt" system call.
3 halt
- Test combination of file descriptor related calls.
10 fd-management
- Test recursive execution of user programs.
15 multi-recurse
......
/* Tests multiple file descriptors that refer to created
* files.
*/
#include "tests/lib.h"
#include "tests/main.h"
#include <stdio.h>
#include <string.h>
#define N 6
#define FNAME_LEN 11
char filenames[N*3/2][FNAME_LEN];
char alphabet[2*26];
void
test_main (void)
{
int i, j;
for (i = 0; i < 2; i++)
for (j = 'A'; j <= 'Z'; j++)
alphabet[26*i+j-'A'] = j;
int fd[N*3/2];
// step 1. Create N files
for (i = 0; i < N; i++)
{
snprintf(filenames[i], FNAME_LEN, "quux%02d.dat", i);
CHECK (create (filenames[i], 26), "create %s", filenames[i]);
}
// step 2. Open N files and write data to them.
for (i = 0; i < N; i++)
{
CHECK (fd[i] = open (filenames[i]), "open %s", filenames[i]);
CHECK (write(fd[i], alphabet + i, 26) == 26, "write %s", filenames[i]);
}
// step 3. Close every other file
for (i = 0; i < N; i+=2)
{
msg ("close %s", filenames[i]);
close (fd[i]);
}
// step 4. Create and open more files.
for (i = N; i < N*3/2; i++)
{
snprintf(filenames[i], FNAME_LEN, "quux%02d.dat", i);
CHECK (create (filenames[i], 26), "create %s", filenames[i]);
CHECK (fd[i] = open (filenames[i]), "open %s", filenames[i]);
CHECK (write(fd[i], alphabet + i, 26) == 26, "write %s", filenames[i]);
}
// step 5. Rewind and read odd-numbered files
for (i = 1; i < N; i+=2)
{
char buf[26];
msg ("seek %s", filenames[i]);
seek (fd[i], 0);
read (fd[i], buf, 26);
CHECK (memcmp (buf, alphabet + i, 26) == 0, "memcmp %s", filenames[i]);
}
// step 6. Open and close 3000 files, which must work
// we don't output anything in order to not slow down the test too much
const int times = 3000;
msg ("starting %d open/close", times);
for (i = 0; i < times; i++)
{
int fd = open (filenames[0]);
if (fd < 0)
fail("open#%d %s", i, filenames[0]);
close (fd);
}
msg ("open/close completed");
}
# -*- perl -*-
use strict;
use warnings;
use tests::tests;
check_expected ([<<'EOF']);
(fd-management) begin
(fd-management) create quux00.dat
(fd-management) create quux01.dat
(fd-management) create quux02.dat
(fd-management) create quux03.dat
(fd-management) create quux04.dat
(fd-management) create quux05.dat
(fd-management) open quux00.dat
(fd-management) write quux00.dat
(fd-management) open quux01.dat
(fd-management) write quux01.dat
(fd-management) open quux02.dat
(fd-management) write quux02.dat
(fd-management) open quux03.dat
(fd-management) write quux03.dat
(fd-management) open quux04.dat
(fd-management) write quux04.dat
(fd-management) open quux05.dat
(fd-management) write quux05.dat
(fd-management) close quux00.dat
(fd-management) close quux02.dat
(fd-management) close quux04.dat
(fd-management) create quux06.dat
(fd-management) open quux06.dat
(fd-management) write quux06.dat
(fd-management) create quux07.dat
(fd-management) open quux07.dat
(fd-management) write quux07.dat
(fd-management) create quux08.dat
(fd-management) open quux08.dat
(fd-management) write quux08.dat
(fd-management) seek quux01.dat
(fd-management) memcmp quux01.dat
(fd-management) seek quux03.dat
(fd-management) memcmp quux03.dat
(fd-management) seek quux05.dat
(fd-management) memcmp quux05.dat
(fd-management) starting 3000 open/close
(fd-management) open/close completed
(fd-management) end
fd-management: exit(0)
EOF
pass;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment