This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix dist/IO/t/cachepropagate-unix.t
[perl5.git] / dist / IO / t / cachepropagate-unix.t
CommitLineData
93a5d7bf
TC
1#!/usr/bin/perl
2
3use warnings;
4use strict;
5
6use File::Temp qw(tempdir);
7use File::Spec::Functions;
8use IO::Socket;
9use IO::Socket::UNIX;
10use Socket;
11use Config;
12use Test::More;
13
14plan skip_all => "UNIX domain sockets not implemented on $^O"
099fcb99 15 if ($^O =~ m/^(?:qnx|nto|vos|MSWin32|VMS)$/);
93a5d7bf
TC
16
17plan tests => 15;
18
19my $socketpath = catfile(tempdir( CLEANUP => 1 ), 'testsock');
20
21# start testing stream sockets:
22my $listener = IO::Socket::UNIX->new(Type => SOCK_STREAM,
23 Listen => 1,
24 Local => $socketpath);
25ok(defined($listener), 'stream socket created');
26
27my $p = $listener->protocol();
28ok(defined($p), 'protocol defined');
29my $d = $listener->sockdomain();
30ok(defined($d), 'domain defined');
31my $s = $listener->socktype();
32ok(defined($s), 'type defined');
33
34SKIP: {
35 skip "fork not available", 4
36 unless $Config{d_fork} || $Config{d_pseudofork};
37
38 my $cpid = fork();
39 if (0 == $cpid) {
40 # the child:
41 sleep(1);
42 my $connector = IO::Socket::UNIX->new(Peer => $socketpath);
43 exit(0);
44 } else {
45 ok(defined($cpid), 'spawned a child');
46 }
47
48 my $new = $listener->accept();
49
93a5d7bf
TC
50 is($new->sockdomain(), $d, 'domain match');
51 SKIP: {
52 skip "no Socket::SO_PROTOCOL", 1 if !defined(eval { Socket::SO_PROTOCOL });
92fd341d
FP
53 skip "SO_PROTOCOL defined but not implemented", 1
54 if !defined $new->sockopt(Socket::SO_PROTOCOL);
93a5d7bf
TC
55 is($new->protocol(), $p, 'protocol match');
56 }
57 SKIP: {
58 skip "no Socket::SO_TYPE", 1 if !defined(eval { Socket::SO_TYPE });
92fd341d
FP
59 skip "SO_TYPE defined but not implemented", 1
60 if !defined $new->sockopt(Socket::SO_TYPE);
93a5d7bf
TC
61 is($new->socktype(), $s, 'type match');
62 }
63
64 unlink($socketpath);
65 wait();
66}
67
68undef $TODO;
93a5d7bf 69SKIP: {
4e1acb29
TC
70 skip "datagram unix sockets not supported on $^O", 7
71 if $^O eq "haiku";
72 # now test datagram sockets:
73 $listener = IO::Socket::UNIX->new(Type => SOCK_DGRAM,
74 Local => $socketpath);
75 ok(defined($listener), 'datagram socket created');
76
77 $p = $listener->protocol();
78 ok(defined($p), 'protocol defined');
79 $d = $listener->sockdomain();
80 ok(defined($d), 'domain defined');
81 $s = $listener->socktype();
82 ok(defined($s), 'type defined');
83
84 my $new = IO::Socket::UNIX->new_from_fd($listener->fileno(), 'r+');
85
86 is($new->sockdomain(), $d, 'domain match');
87 SKIP: {
88 skip "no Socket::SO_PROTOCOL", 1 if !defined(eval { Socket::SO_PROTOCOL });
92fd341d
FP
89 skip "SO_PROTOCOL defined but not implemented", 1
90 if !defined $new->sockopt(Socket::SO_PROTOCOL);
4e1acb29
TC
91 is($new->protocol(), $p, 'protocol match');
92 }
93 SKIP: {
94 skip "no Socket::SO_TYPE", 1 if !defined(eval { Socket::SO_TYPE });
92fd341d
FP
95 skip "SO_TYPE defined but not implemented", 1
96 if !defined $new->sockopt(Socket::SO_TYPE);
4e1acb29
TC
97 is($new->socktype(), $s, 'type match');
98 }
93a5d7bf
TC
99}
100unlink($socketpath);