diff options
Diffstat (limited to 'run_tests.c')
| -rw-r--r-- | run_tests.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/run_tests.c b/run_tests.c new file mode 100644 index 0000000..efabe25 --- /dev/null +++ b/run_tests.c @@ -0,0 +1,83 @@ +/* Copyright (C) 2017 Jakob Kreuze, All Rights Reserved. + + Skullfuck is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at + your option) any later version. + + Skullfuck is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Skullfuck. If not, see <http://www.gnu.org/licenses/>. */ + +#include <sys/types.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + + +/* Exits with a failing status code if a test binary does not exist. */ +static void assert_test_existence(void) { + if (access("hello", F_OK) == -1) { + fprintf(stderr, "Hello world binary does not exist.\n"); + exit(EXIT_FAILURE); + } else if (access("rot13", F_OK) == -1) { + fprintf(stderr, "rot13 binary does not exist.\n"); + exit(EXIT_FAILURE); + } +} + + +/* Prints `msg` to error output and quits with a failing status code. */ +static void panic(char *msg) { + fprintf(stderr, msg); + fprintf(stderr, "Tests failed! Terminating!"); + exit(EXIT_FAILURE); +} + + +/* Tests the hello world binary. */ +static void test_hello_world(void) { + int pipefd[2]; + char *buf; + pid_t pid; + + if (pipe(pipefd) == -1) { + perror("pipe"); + exit(EXIT_FAILURE); + } + + if ((pid = fork()) == -1) { + perror("fork"); + exit(EXIT_FAILURE); + } + + if (pid == 0) { + dup2(pipefd[1], 1); + close(pipefd[0]); + execl("hello", "hello", NULL); + } else { + buf = malloc(0x100); + dup2(pipefd[0], 0); + close(pipefd[1]); + fgets(buf, 0x100, stdin); + if (!strcmp(buf, "Hello World!")) + panic("Hello World binary did not properly output text.\n"); + free(buf); + } +} + + +int main(int argc, char **argv) { + assert_test_existence(); + test_hello_world(); + printf("All tests passed.\n"); +} |