]> Sergey Matveev's repositories - public-inbox.git/blob - t/cmd_ipc.t
tests: replace select/usleep calls with tick()
[public-inbox.git] / t / cmd_ipc.t
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use Socket qw(AF_UNIX SOCK_STREAM MSG_EOR);
9 pipe(my ($r, $w)) or BAIL_OUT;
10 my ($send, $recv);
11 require_ok 'PublicInbox::Spawn';
12 my $SOCK_SEQPACKET = eval { Socket::SOCK_SEQPACKET() } // undef;
13
14 my $do_test = sub { SKIP: {
15         my ($type, $flag, $desc) = @_;
16         defined $type or skip 'SOCK_SEQPACKET missing', 7;
17         my ($s1, $s2);
18         my $src = 'some payload' x 40;
19         socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
20         my $sfds = [ fileno($r), fileno($w), fileno($s1) ];
21         $send->($s1, $sfds, $src, $flag);
22         my (@fds) = $recv->($s2, my $buf, length($src) + 1);
23         is($buf, $src, 'got buffer payload '.$desc);
24         my ($r1, $w1, $s1a);
25         my $opens = sub {
26                 ok(open($r1, '<&=', $fds[0]), 'opened received $r');
27                 ok(open($w1, '>&=', $fds[1]), 'opened received $w');
28                 ok(open($s1a, '+>&=', $fds[2]), 'opened received $s1');
29         };
30         $opens->();
31         my @exp = stat $r;
32         my @cur = stat $r1;
33         is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$r dev/ino matches');
34         @exp = stat $w;
35         @cur = stat $w1;
36         is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$w dev/ino matches');
37         @exp = stat $s1;
38         @cur = stat $s1a;
39         is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$s1 dev/ino matches');
40         if (defined($SOCK_SEQPACKET) && $type == $SOCK_SEQPACKET) {
41                 $r1 = $w1 = $s1a = undef;
42                 $src = (',' x 1023) . '-' .('.' x 1024);
43                 $send->($s1, $sfds, $src, $flag);
44                 (@fds) = $recv->($s2, $buf, 1024);
45                 is($buf, (',' x 1023) . '-', 'silently truncated buf');
46                 $opens->();
47                 $r1 = $w1 = $s1a = undef;
48
49                 $s2->blocking(0);
50                 @fds = $recv->($s2, $buf, length($src) + 1);
51                 ok($!{EAGAIN}, "EAGAIN set by ($desc)");
52                 is_deeply(\@fds, [ undef ], "EAGAIN $desc");
53                 $s2->blocking(1);
54
55                 if ($ENV{TEST_ALRM}) {
56                         my $alrm = 0;
57                         local $SIG{ALRM} = sub { $alrm++ };
58                         my $tgt = $$;
59                         my $pid = fork // xbail "fork: $!";
60                         if ($pid == 0) {
61                                 # need to loop since Perl signals are racy
62                                 # (the interpreter doesn't self-pipe)
63                                 while (tick(0.01)) {
64                                         kill 'ALRM', $tgt;
65                                 }
66                         }
67                         @fds = $recv->($s2, $buf, length($src) + 1);
68                         ok($!{EINTR}, "EINTR set by ($desc)");
69                         kill('KILL', $pid);
70                         waitpid($pid, 0);
71                         is_deeply(\@fds, [ undef ], "EINTR $desc");
72                         ok($alrm, 'SIGALRM hit');
73                 }
74
75                 close $s1;
76                 @fds = $recv->($s2, $buf, length($src) + 1);
77                 is_deeply(\@fds, [], "no FDs on EOF $desc");
78                 is($buf, '', "buffer cleared on EOF ($desc)");
79
80                 socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
81                 $s1->blocking(0);
82                 my $nsent = 0;
83                 while (defined(my $n = $send->($s1, $sfds, $src, $flag))) {
84                         $nsent += $n;
85                         fail "sent 0 bytes" if $n == 0;
86                 }
87                 ok($!{EAGAIN} || $!{ETOOMANYREFS},
88                         "hit EAGAIN || ETOOMANYREFS on send $desc") or
89                         diag "send failed with: $!";
90                 ok($nsent > 0, 'sent some bytes');
91
92                 socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
93                 is($send->($s1, [], $src, $flag), length($src), 'sent w/o FDs');
94                 $buf = 'nope';
95                 @fds = $recv->($s2, $buf, length($src));
96                 is(scalar(@fds), 0, 'no FDs received');
97                 is($buf, $src, 'recv w/o FDs');
98
99                 my $nr = 2 * 1024 * 1024;
100                 while (1) {
101                         vec(my $vec = '', $nr * 8 - 1, 1) = 1;
102                         my $n = $send->($s1, [], $vec, $flag);
103                         if (defined($n)) {
104                                 $n == length($vec) or
105                                         fail "short send: $n != ".length($vec);
106                                 diag "sent $nr, retrying with more";
107                                 $nr += 2 * 1024 * 1024;
108                         } else {
109                                 ok($!{EMSGSIZE} || $!{ENOBUFS},
110                                         'got EMSGSIZE or ENOBUFS') or
111                                         diag "$nr bytes fails with: $!";
112                                 last;
113                         }
114                 }
115         }
116 } };
117
118 my $send_ic = PublicInbox::Spawn->can('send_cmd4');
119 my $recv_ic = PublicInbox::Spawn->can('recv_cmd4');
120 SKIP: {
121         ($send_ic && $recv_ic) or skip 'Inline::C not installed/enabled', 12;
122         $send = $send_ic;
123         $recv = $recv_ic;
124         $do_test->(SOCK_STREAM, 0, 'Inline::C stream');
125         $do_test->($SOCK_SEQPACKET, MSG_EOR, 'Inline::C seqpacket');
126 }
127
128 SKIP: {
129         require_mods('Socket::MsgHdr', 13);
130         require_ok 'PublicInbox::CmdIPC4';
131         $send = PublicInbox::CmdIPC4->can('send_cmd4');
132         $recv = PublicInbox::CmdIPC4->can('recv_cmd4');
133         $do_test->(SOCK_STREAM, 0, 'MsgHdr stream');
134         $do_test->($SOCK_SEQPACKET, MSG_EOR, 'MsgHdr seqpacket');
135         SKIP: {
136                 ($send_ic && $recv_ic) or
137                         skip 'Inline::C not installed/enabled', 12;
138                 $recv = $recv_ic;
139                 $do_test->(SOCK_STREAM, 0, 'Inline::C -> MsgHdr stream');
140                 $do_test->($SOCK_SEQPACKET, 0, 'Inline::C -> MsgHdr seqpacket');
141         }
142 }
143
144 SKIP: {
145         skip 'not Linux', 1 if $^O ne 'linux';
146         require_ok 'PublicInbox::Syscall';
147         $send = PublicInbox::Syscall->can('send_cmd4') or
148                 skip 'send_cmd4 not defined for arch';
149         $recv = PublicInbox::Syscall->can('recv_cmd4') or
150                 skip 'recv_cmd4 not defined for arch';
151         $do_test->(SOCK_STREAM, 0, 'PP Linux stream');
152         $do_test->($SOCK_SEQPACKET, MSG_EOR, 'PP Linux seqpacket');
153 }
154
155 done_testing;