diff options
Diffstat (limited to 'kernel/include/test')
-rw-r--r-- | kernel/include/test/driverstest.h | 3 | ||||
-rw-r--r-- | kernel/include/test/kshell/io.h | 61 | ||||
-rw-r--r-- | kernel/include/test/kshell/kshell.h | 52 | ||||
-rw-r--r-- | kernel/include/test/proctest.h | 3 | ||||
-rw-r--r-- | kernel/include/test/s5fstest.h | 3 | ||||
-rw-r--r-- | kernel/include/test/usertest.h | 51 | ||||
-rw-r--r-- | kernel/include/test/vfstest/vfstest.h | 156 |
7 files changed, 329 insertions, 0 deletions
diff --git a/kernel/include/test/driverstest.h b/kernel/include/test/driverstest.h new file mode 100644 index 0000000..16e0bc5 --- /dev/null +++ b/kernel/include/test/driverstest.h @@ -0,0 +1,3 @@ +#pragma once + +long driverstest_main(long, void*);
\ No newline at end of file diff --git a/kernel/include/test/kshell/io.h b/kernel/include/test/kshell/io.h new file mode 100644 index 0000000..72ac92a --- /dev/null +++ b/kernel/include/test/kshell/io.h @@ -0,0 +1,61 @@ +#pragma once + +#include "test/kshell/kshell.h" + +/* + * When writing a kernel shell command, make sure to use the following + * I/O functions. + * + * Before VFS is not enabled, the kernel shell will use functions from + * chardev.h to get a pointer the the chardev_t struct for the TTY. + * + * When VFS is enabled, the kernel shell will use the functions from + * vfs_syscall.h to open and close the TTY and perform I/O operations + * on the TTY. + * + * If you use the functions below, this process will be completely + * transparent. + */ + +/** + * Replacement for do_write. + * + * @param ksh the kshell to write to + * @param buf the buffer to write out to the kshell + * @param nbytes the maximum number of bytes to write + * @return number of bytes written on sucess and <0 on error + */ +long kshell_write(kshell_t *ksh, const void *buf, size_t nbytes); + +/** + * Replacement for do_read. + * + * @param ksh the kshell to read from + * @param buf the buffer to store data read from the kshell + * @param nbytes the maximum number of bytes to read + * @param number of bytes read on success and <0 on error + */ +long kshell_read(kshell_t *ksh, void *buf, size_t nbytes); + +/* Unless an error occurs, guarantees that all of buf will be + * written */ +/** + * Writes a specified number of bytes from a buffer to the + * kshell. Unlike kshell_write, this function guarantees it will write + * out the desired number of bytes. + * + * @param ksh the kshell to write to + * @param buf the buffer to write out to the kshell + * @param nbytes the number of bytes to write + * @return number of bytes written on success and <0 on error + */ +long kshell_write_all(kshell_t *ksh, void *buf, size_t nbytes); + +/* Replacement for printf */ +/** + * Write output to a kshell according to a format string. + * + * @param ksh the kshell to write to + * @param fmt the format string + */ +void kprintf(kshell_t *ksh, const char *fmt, ...); diff --git a/kernel/include/test/kshell/kshell.h b/kernel/include/test/kshell/kshell.h new file mode 100644 index 0000000..9baf4f5 --- /dev/null +++ b/kernel/include/test/kshell/kshell.h @@ -0,0 +1,52 @@ +#pragma once + +#include "types.h" + +typedef struct kshell kshell_t; + +typedef long (*kshell_cmd_func_t)(kshell_t *, size_t argc, char **argv); + +/** + * Process init function for a new kshell. + */ +void *kshell_proc_run(long tty, void *arg2); + +/** + * Adds a command to the global command table for kernel shells. + * + * Note: When writing commands for the kernel shell, you _MUST_ use + * the I/O functions from kshell_io.h instead of normal I/O + * functions. See comment in kshell_io.h for more information. + * + * @param name the name of the command. Typing this name into the + * shell will execute the command. + * @param command the command to add to the shell + * @param desc a description of the command. This is what will be + * printed by the command 'help <command>' + */ +void kshell_add_command(const char *name, kshell_cmd_func_t command, + const char *desc); + +/** + * Allocates and initializes a kshell. + * + * @param bd the byte device the kshell will read from and write to + * @return a kshell + */ +kshell_t *kshell_create(uint8_t ttyid); + +/** + * Destroys a kshell. + * + * @param ksh the kshell to destroy + */ +void kshell_destroy(kshell_t *ksh); + +/** + * Reads from the kshell's byte device and attempts to execute a + * command. + * + * @param ksh the kshell to execute commands with + * @return the number of bytes read + */ +long kshell_execute_next(kshell_t *ksh); diff --git a/kernel/include/test/proctest.h b/kernel/include/test/proctest.h new file mode 100644 index 0000000..94b3d9c --- /dev/null +++ b/kernel/include/test/proctest.h @@ -0,0 +1,3 @@ +#pragma once + +long proctest_main(long, void *);
\ No newline at end of file diff --git a/kernel/include/test/s5fstest.h b/kernel/include/test/s5fstest.h new file mode 100644 index 0000000..b6b5279 --- /dev/null +++ b/kernel/include/test/s5fstest.h @@ -0,0 +1,3 @@ +#pragma once + +long s5fstest_main(int, void *); diff --git a/kernel/include/test/usertest.h b/kernel/include/test/usertest.h new file mode 100644 index 0000000..3d2296f --- /dev/null +++ b/kernel/include/test/usertest.h @@ -0,0 +1,51 @@ +#pragma once + +#ifndef __KERNEL__ + +#include "sys/types.h" +#include "unistd.h" + +#else +#include "types.h" +#endif + +#include <stdarg.h> + +#define test_assert(expr, fmt, args...) \ + _test_assert(expr, __FILE__, __LINE__, #expr, fmt, ##args) + +#ifndef __KERNEL__ +#define test_fork_begin() \ + do \ + { \ + pid_t __test_pid = fork(); \ + if (0 == __test_pid) \ + { \ + do + +#define test_fork_end(status) \ + while (0) \ + ; \ + exit(0); \ + } /* if */ \ + waitpid(__test_pid, status, 0); \ + } \ + while (0) \ + ; +#endif + +void test_init(void); + +void test_fini(void); + +const char *test_errstr(int err); + +typedef void (*test_pass_func_t)(int val, const char *file, int line, + const char *name, const char *fmt, + va_list args); + +typedef void (*test_fail_func_t)(const char *file, int line, const char *name, + const char *fmt, va_list args); + +int _test_assert(int val, const char *file, int line, const char *name, + const char *fmt, ...); diff --git a/kernel/include/test/vfstest/vfstest.h b/kernel/include/test/vfstest/vfstest.h new file mode 100644 index 0000000..4f86563 --- /dev/null +++ b/kernel/include/test/vfstest/vfstest.h @@ -0,0 +1,156 @@ +#pragma once + +/* "kernel" utility things */ + +/* fprintf */ +#define fprintf(fd, fmt, args...) dbg(DBG_TEST, fmt, ##args) +#define printf(fmt, args...) dbg(DBG_TEST, fmt, ##args) + +/* errno */ +#define errno (curthr->kt_errno) + +/* malloc/free */ +#define malloc kmalloc +#define free kfree + +/* The "kernel" system calls */ +#define ksyscall(name, formal, actual) \ + static long ksys_##name formal \ + { \ + long ret = do_##name actual; \ + if (ret < 0) \ + { \ + errno = -ret; \ + return -1; \ + } \ + return ret; \ + } + +ksyscall(close, (int fd), (fd)) + + ksyscall(read, (int fd, void *buf, size_t nbytes), (fd, buf, nbytes)) + + ksyscall(write, (int fd, const void *buf, size_t nbytes), + (fd, buf, nbytes)) + + ksyscall(dup, (int fd), (fd)) + + ksyscall(dup2, (int ofd, int nfd), (ofd, nfd)) + + ksyscall(mkdir, (const char *path), (path)) + + ksyscall(rmdir, (const char *path), (path)) + + ksyscall(link, (const char *old, const char *new), + (old, new)) + + ksyscall(unlink, (const char *path), (path)) + + ksyscall(rename, + (const char *oldpath, + const char *newpath), + (oldpath, newpath)) + + ksyscall(chdir, (const char *path), + (path)) + + ksyscall(lseek, + (int fd, int offset, + int whence), + (fd, offset, whence)) + + ksyscall(getdent, + (int fd, + struct dirent *dirp), + (fd, dirp)) + + ksyscall(stat, + (const char *path, + struct stat *uf), + (path, uf)) + + ksyscall(open, + (const char + *filename, + int flags), + (filename, + flags)) +#define ksys_exit do_exit + + long ksys_getdents( + int fd, + struct dirent + *dirp, + unsigned int + count) +{ + size_t numbytesread = 0; + int nbr = 0; + dirent_t tempdirent; + + if (count < sizeof(dirent_t)) + { + curthr->kt_errno = EINVAL; + return -1; + } + + while (numbytesread < count) + { + if ((nbr = do_getdent(fd, &tempdirent)) < 0) + { + curthr->kt_errno = -nbr; + return -1; + } + if (nbr == 0) + { + return numbytesread; + } + memcpy(dirp, &tempdirent, sizeof(dirent_t)); + + KASSERT(nbr == sizeof(dirent_t)); + + dirp++; + numbytesread += nbr; + } + return numbytesread; +} + +/* + * Redirect system calls to kernel system calls. + */ +#define mkdir(a, b) ksys_mkdir(a) +#define rmdir ksys_rmdir +#define mount ksys_mount +#define umount ksys_umount +#define open(a, b, c) ksys_open(a, b) +#define close ksys_close +#define link ksys_link +#define rename ksys_rename +#define unlink ksys_unlink +#define read ksys_read +#define write ksys_write +#define lseek ksys_lseek +#define dup ksys_dup +#define dup2 ksys_dup2 +#define chdir ksys_chdir +#define stat(a, b) ksys_stat(a, b) +#define getdents(a, b, c) ksys_getdents(a, b, c) +#define exit(a) ksys_exit(a) + +/* Random numbers */ +/* Random int between lo and hi inclusive */ +#define RAND_MAX INT_MAX +#define RANDOM(lo, hi) \ + ((lo) + \ + (((hi) - (lo) + 1) * (randseed = (randseed * 4096 + 150889) % 714025)) / \ + 714025) + +static unsigned long long randseed = 123456L; + +static unsigned long long rand(void) +{ + randseed = (randseed * 4096 + 150889) % RAND_MAX; + return randseed; +} + +static void srand(unsigned int seed) { randseed = seed; } |