File: opentest.c

package info (click to toggle)
tinyssh 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,244 kB
  • sloc: ansic: 12,106; sh: 1,168; python: 479; makefile: 42
file content (66 lines) | stat: -rw-r--r-- 1,200 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
/*
20140410
Jan Mojzis
Public domain.
*/

#include <signal.h>
#include <unistd.h>
#include "run.h"
#include "fail.h"
#include "open.h"

static void cat(void) {
    char *catcmd[2] = { (char *)"cat", 0 };
    execvp(*catcmd, catcmd);
}

/* test if close-on-exec works for open_read() */
static void test1(void) {

    int fd;

    close(0);
    fd = open_read("opentest.c");
    if (fd != 0) fail("unable to open opentest.c for reading");
    cat();
}

/* test if close-on-exec works for open_write() */
static void test2(void) {

    int fd;

    close(1);
    fd = open_write("opentest.data");
    if (fd != 1) fail("unable to open opentest.data for writing");
    cat();
}

/* test if close-on-exec works for open_pipe() */
static void test3(void) {

    int fd, pi[2];

    close(0);
    close(1);
    fd = open_pipe(pi);
    if (fd == -1) fail("unable to open pipe");
    if (pi[0] != 0) fail("unable to open pipe");
    if (pi[1] != 1) fail("unable to open pipe");
    cat();
}

/* dummy test */
static void testdummy(void) {
    _exit(0);
}

int main(void) {
    alarm(10);
    run_mustfail(test1);
    run_mustfail(test2);
    run_mustfail(test3);
    run_mustpass(testdummy);
    _exit(0);
}