summaryrefslogtreecommitdiff
path: root/run_tests.c
blob: efabe25f89e278917405b45d07b4a223f4df0d98 (plain)
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
/* 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");
}