File: redirection_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (86 lines) | stat: -rw-r--r-- 2,368 bytes parent folder | download | duplicates (4)
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
84
85
86
#include <test.h>

#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <libgen.h> // dirname

/*
 * This test checks for different types of IPC to check that redirection of
 * STDIN will work.
 * This test is only significant in Linux, because this is only used by the
 * hub process that only runs on Linux.
 */
static char redirection[] = "/tmp/redirectionXXXXXX";
static char path[PATH_MAX];
static char *message = "This is the message to be written by our helper";
static char message_back[128];
static void test_fd_redirection(void)
{
    assert_true(path != NULL);
    /*
     * Create a file for writing, then start a new process and wait for it
     * to write to the file.
     * Check the contents of the file.
     */
    int fd = -1;
    fd = mkstemp(redirection);
    assert_int_not_equal(-1, fd);
    /* Start the new process */
    int pid = 0;
    pid = fork();
    assert_int_not_equal(-1, pid);
    if (pid == 0)
    {
        /* Child */
        dup2(fd, STDIN_FILENO);
        char *argv[] = { path, message, NULL };
        char *envp[] = { NULL };
        printf("path: %s\n", path);
        execve(path, argv, envp);
        printf("execve failed: %s\n", strerror(errno));
        exit(-1);
    }
    else
    {
        /* Parent */
        int status = 0;
        int options = 0;
        /* Wait for the child to be done */
        assert_int_equal(waitpid(pid, &status, options), pid);
        /* Did it exit correctly? */
        assert_true(WIFEXITED(status));
        assert_int_equal(WEXITSTATUS(status), 0);
        /* Rewind the file so we can check the message */
        lseek(fd, 0, SEEK_SET);
        /* Read back the message */
        assert_int_equal(strlen(message), read(fd, message_back, strlen(message)));
        /* Compare it */
        assert_string_equal(message, message_back);
    }
    close (fd);
}

void tests_teardown()
{
    unlink (redirection);
}

int main(int argc, char **argv)
{
    PRINT_TEST_BANNER();
    assert(argc >= 1);
    /* Find the proper path for our helper */
    char *base = dirname(argv[0]);
    char *helper = "/../redirection_test_stub";
    sprintf(path, "%s%s", base, helper);
    const UnitTest tests[] =
    {
        unit_test(test_fd_redirection)
    };
    tests_teardown();
    return run_tests(tests);
}