diff options
Diffstat (limited to 'driver/test')
-rw-r--r-- | driver/test/.gitignore | 1 | ||||
-rw-r--r-- | driver/test/main.c | 56 | ||||
-rw-r--r-- | driver/test/makefile | 2 | ||||
-rwxr-xr-x | driver/test/test.sh | 24 |
4 files changed, 83 insertions, 0 deletions
diff --git a/driver/test/.gitignore b/driver/test/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/driver/test/.gitignore @@ -0,0 +1 @@ +main diff --git a/driver/test/main.c b/driver/test/main.c new file mode 100644 index 0000000..b87e56c --- /dev/null +++ b/driver/test/main.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> + +int main(int argc, char** argv) { + if (geteuid() != 0) { + fprintf(stderr, "run me as root!\n"); + return 1; + } + + argc--; // argv[0] is the program name + if (argc == 0) { + fprintf(stderr, "usage: %s /dev/lork\n", argv[0]); + return 1; + } + + char* dev = argv[1]; + const char* input = "test"; + int fd; + ssize_t bytes; + + fd = open(dev, O_RDWR); + if (-1 == fd) { + fprintf(stderr, "open() failed with code %d\n", errno); + return 1; + } + + bytes = write(fd, input, strlen(input)); + if (-1 == bytes) { + fprintf(stderr, "write() failed with code %d\n", errno); + return 1; + } + fprintf(stderr, "input \"%s\" to %s (%ld bytes succesful)\n", input, dev, bytes); + + char buf[80]; + bytes = read(fd, buf, 80); + if (-1 == bytes) { + fprintf(stderr, "read() failed with code %d\n", errno); + return 1; + } + fprintf(stderr, "output from %s: (%ld bytes)\n", dev, bytes); + if (bytes > 0) { + if (bytes < 80) buf[bytes] = '\0'; // null terminate string for printf + printf("%s\n", buf); + } + + if (-1 == close(fd)) { + fprintf(stderr, "close() failed with code %d\n", errno); + return 1; + } + + return 0; +} + diff --git a/driver/test/makefile b/driver/test/makefile new file mode 100644 index 0000000..3491973 --- /dev/null +++ b/driver/test/makefile @@ -0,0 +1,2 @@ +main: main.o + diff --git a/driver/test/test.sh b/driver/test/test.sh new file mode 100755 index 0000000..7dba005 --- /dev/null +++ b/driver/test/test.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ $(id -u) -ne 0 ] ; then + echo "run me as root!" >&2 + exit 1 +fi + +if [ $# -eq 0 ] ; then + echo "usage: $0 /dev/lork" >&2 + exit 1 +fi + +DEV="$1" +INPUT="test" + +echo "input \"$INPUT\" to $DEV" >&2 +echo "$INPUT" > "$DEV" +echo "(EC $?)" >&2 + +echo "output from $DEV:" >&2 +cat "$DEV" +echo "(EC $?)" >&2 + +exit 0 |