summaryrefslogtreecommitdiff
path: root/t/lib/io_unix.t
blob: 62569a58447ae89b25f55de8826ed67405dd9e90 (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
84
85
86
87
88
89
90
91
92
93
94
#!./perl

BEGIN {
    unless(grep /blib/, @INC) {
        chdir 't' if -d 't';
        unshift @INC, '../lib' if -d '../lib';
    }
    # ``use IO::Socket'' executes too early below in the os2 block
    if ($^O eq 'dos') {
        print "1..0 # Skip: no fork\n";
	exit 0;
    }
}

use Config;

BEGIN {
    if(-d "lib" && -f "TEST") {
	my $reason;
	if (! $Config{'d_fork'}) {
	    $reason = 'no fork';
	}
	elsif ($Config{'extensions'} !~ /\bSocket\b/) {
	    $reason = 'Socket extension unavailable';
	}
	elsif ($Config{'extensions'} !~ /\bIO\b/) {
	    $reason = 'IO extension unavailable';
	}
	elsif ($^O eq 'os2') {
	    use IO::Socket;

	    eval {IO::Socket::pack_sockaddr_un('/tmp/foo') || 1}
	      or $@ !~ /not implemented/ or
		$reason = 'compiled without TCP/IP stack v4';
	} elsif ($^O eq 'qnx') {
	    $reason = 'Not implemented';
	}
	undef $reason if $^O eq 'VMS' and $Config{d_socket};
	if ($reason) {
	    print "1..0 # Skip: $reason\n";
	    exit 0;
        }
    }
}

$PATH = "/tmp/sock-$$";

# Test if we can create the file within the tmp directory
if (-e $PATH or not open(TEST, ">$PATH") and $^O ne 'os2') {
    print "1..0 # Skip: cannot open '$PATH' for write\n";
    exit 0;
}
close(TEST);
unlink($PATH) or $^O eq 'os2' or die "Can't unlink $PATH: $!";

# Start testing
$| = 1;
print "1..5\n";

use IO::Socket;

$listen = IO::Socket::UNIX->new(Local=>$PATH, Listen=>0) || die "$!";
print "ok 1\n";

if($pid = fork()) {

    $sock = $listen->accept();
    print "ok 2\n";

    print $sock->getline();

    print $sock "ok 4\n";

    $sock->close;

    waitpid($pid,0);
    unlink($PATH) || $^O eq 'os2' || warn "Can't unlink $PATH: $!";

    print "ok 5\n";

} elsif(defined $pid) {

    $sock = IO::Socket::UNIX->new(Peer => $PATH) or die "$!";

    print $sock "ok 3\n";

    print $sock->getline();

    $sock->close;

    exit;
} else {
 die;
}