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" 15 if ($^O =~ m/^(?:qnx|nto|vos|MSWin32|VMS)$/); 16 17my $socketpath = catfile(tempdir( CLEANUP => 1 ), 'testsock'); 18 19# check the socketpath fits in sun_path. 20# 21# pack_sockaddr_un() just truncates the path, this may change, but how 22# it will handle such a condition is undetermined (and we might need 23# to work with older versions of Socket outside of a perl build) 24# https://rt.cpan.org/Ticket/Display.html?id=116819 25 26my $name = eval { pack_sockaddr_un($socketpath) }; 27if (defined $name) { 28 my ($packed_name) = eval { unpack_sockaddr_un($name) }; 29 if (!defined $packed_name || $packed_name ne $socketpath) { 30 plan skip_all => "socketpath too long for sockaddr_un"; 31 } 32} 33 34plan tests => 15; 35 36# start testing stream sockets: 37my $listener = IO::Socket::UNIX->new(Type => SOCK_STREAM, 38 Listen => 1, 39 Local => $socketpath); 40ok(defined($listener), 'stream socket created'); 41 42my $p = $listener->protocol(); 43ok(defined($p), 'protocol defined'); 44my $d = $listener->sockdomain(); 45ok(defined($d), 'domain defined'); 46my $s = $listener->socktype(); 47ok(defined($s), 'type defined'); 48 49SKIP: { 50 skip "fork not available", 4 51 unless $Config{d_fork} || $Config{d_pseudofork}; 52 53 my $cpid = fork(); 54 if (0 == $cpid) { 55 # the child: 56 sleep(1); 57 my $connector = IO::Socket::UNIX->new(Peer => $socketpath); 58 exit(0); 59 } else { 60 ok(defined($cpid), 'spawned a child'); 61 } 62 63 my $new = $listener->accept(); 64 65 is($new->sockdomain(), $d, 'domain match'); 66 SKIP: { 67 skip "no Socket::SO_PROTOCOL", 1 if !defined(eval { Socket::SO_PROTOCOL }); 68 skip "SO_PROTOCOL defined but not implemented", 1 69 if !defined $new->sockopt(Socket::SO_PROTOCOL); 70 is($new->protocol(), $p, 'protocol match'); 71 } 72 SKIP: { 73 skip "no Socket::SO_TYPE", 1 if !defined(eval { Socket::SO_TYPE }); 74 skip "SO_TYPE defined but not implemented", 1 75 if !defined $new->sockopt(Socket::SO_TYPE); 76 is($new->socktype(), $s, 'type match'); 77 } 78 79 unlink($socketpath); 80 wait(); 81} 82 83undef $TODO; 84SKIP: { 85 skip "datagram unix sockets not supported on $^O", 7 86 if $^O eq "haiku"; 87 # now test datagram sockets: 88 $listener = IO::Socket::UNIX->new(Type => SOCK_DGRAM, 89 Local => $socketpath); 90 ok(defined($listener), 'datagram socket created'); 91 92 $p = $listener->protocol(); 93 ok(defined($p), 'protocol defined'); 94 $d = $listener->sockdomain(); 95 ok(defined($d), 'domain defined'); 96 $s = $listener->socktype(); 97 ok(defined($s), 'type defined'); 98 99 my $new = IO::Socket::UNIX->new_from_fd($listener->fileno(), 'r+'); 100 101 is($new->sockdomain(), $d, 'domain match'); 102 SKIP: { 103 skip "no Socket::SO_PROTOCOL", 1 if !defined(eval { Socket::SO_PROTOCOL }); 104 skip "SO_PROTOCOL defined but not implemented", 1 105 if !defined $new->sockopt(Socket::SO_PROTOCOL); 106 skip "SO_PROTOCOL returns chosen protocol on OpenBSD" 107 if $^O eq 'openbsd'; 108 is($new->protocol(), $p, 'protocol match'); 109 } 110 SKIP: { 111 skip "AIX: getsockopt(SO_TYPE) is badly broken on UDP/UNIX sockets", 1 112 if $^O eq "aix"; 113 skip "no Socket::SO_TYPE", 1 if !defined(eval { Socket::SO_TYPE }); 114 skip "SO_TYPE defined but not implemented", 1 115 if !defined $new->sockopt(Socket::SO_TYPE); 116 is($new->socktype(), $s, 'type match'); 117 } 118} 119unlink($socketpath); 120