properly skip tests on MSWin32
[catagits/fcgi2.git] / perl / t / 02-unix_domain_socket.t
CommitLineData
0b895b96 1use strict;
2use warnings;
3
85d8cc6d 4use Config;
0b895b96 5use FCGI;
6use FCGI::Client;
7use File::Temp qw(tempfile);
85d8cc6d 8use IO::Socket;
f1b5eae5 9use Test::More 0.88;
10
11my $can_fork = $Config{d_fork}
12 || (
13 ($^O eq 'MSWin32' || $^O eq 'NetWare')
14 and $Config{useithreads}
15 and $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/
16 );
17if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
18 plan skip_all => 'Socket extension unavailable';
19} elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
20 plan skip_all => 'IO extension unavailable';
21} elsif ($^O eq 'os2') {
22 eval { IO::Socket::pack_sockaddr_un('/foo/bar') || 1 };
23 if ($@ !~ /not implemented/) {
24 plan skip_all => 'compiled without TCP/IP stack v4';
31a016a9 25 }
f1b5eae5 26} elsif ($^O =~ m/^(?:qnx|nto|vos)$/ ) {
27 plan skip_all => "UNIX domain sockets not implemented on $^O";
28} elsif (! $can_fork) {
29 plan skip_all => 'no fork';
30} elsif ($^O eq 'MSWin32') {
31 if ($ENV{CONTINUOUS_INTEGRATION}) {
32 # https://github.com/Perl/perl5/issues/17429
33 plan skip_all => 'Skipping on Windows CI';
34 } else {
35 # https://github.com/Perl/perl5/issues/17575
36 if (! eval { socket(my $sock, PF_UNIX, SOCK_STREAM, 0) }) {
37 plan skip_all => "AF_UNIX unavailable or disabled on this platform"
38 }
31a016a9 39 }
85d8cc6d 40}
41
0b895b96 42my (undef, $unix_socket_file) = tempfile();
43my $fcgi_socket = FCGI::OpenSocket($unix_socket_file, 5);
44
45# Client
46if (my $pid = fork()) {
31a016a9 47 my $right_ret = <<'END';
0b895b96 48Content-Type: text/plain
49
50END
51
31a016a9 52 my ($stdout, $stderr) = client_request($unix_socket_file);
53 is($stdout, $right_ret."0\n", 'Test first round on stdout.');
54 is($stderr, undef, 'Test first round on stderr.');
0b895b96 55
31a016a9 56 ($stdout, $stderr) = client_request($unix_socket_file);
57 is($stdout, $right_ret."1\n", 'Test second round on stdout.');
58 is($stderr, undef, 'Test second round on stderr.');
0b895b96 59
60# Server
61} elsif (defined $pid) {
31a016a9 62 my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $fcgi_socket);
0b895b96 63
31a016a9 64 # Only two cycles.
65 my $count = 0;
66 while ($count < 2 && $request->Accept() >= 0) {
67 print "Content-Type: text/plain\n\n";
68 print $count++."\n";
69 }
70 exit;
0b895b96 71
72} else {
31a016a9 73 die $!;
0b895b96 74}
75
76# Cleanup.
77FCGI::CloseSocket($fcgi_socket);
78unlink $unix_socket_file;
79
f1b5eae5 80done_testing;
81
0b895b96 82sub client_request {
f1b5eae5 83 my $unix_socket_file = shift;
0b895b96 84
f1b5eae5 85 my $sock = IO::Socket::UNIX->new(
86 Peer => $unix_socket_file,
87 ) or die $!;
88 my $client = FCGI::Client::Connection->new(sock => $sock);
89 my ($stdout, $stderr) = $client->request({
90 REQUEST_METHOD => 'GET',
91 }, '');
0b895b96 92
f1b5eae5 93 return ($stdout, $stderr);
0b895b96 94}