File: 16-send_with_fd.t

package info (click to toggle)
os-autoinst 4.5.1527308405.8b586d5-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,688 kB
  • sloc: perl: 10,424; cpp: 1,527; python: 217; makefile: 211; sh: 71; xml: 11
file content (63 lines) | stat: -rwxr-xr-x 1,320 bytes parent folder | download
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
#!/usr/bin/perl

use 5.018;

use Test::More;
use Socket;
# This is the library we want to avoid, but it is OK just for testing
use Socket::MsgHdr;
use POSIX;

BEGIN {
    unshift @INC, '..';
}

use cv;

cv::init();
require tinycv;

# This test sends a pipe FD and message to a child process using a UNIX socket
# and SCM_RIGHTS. Then the child writes the message to the pipe and the parent
# confirms it is correct.

socketpair(my $ask, my $bsk, AF_UNIX, SOCK_STREAM, AF_UNSPEC)
  || die "Could not make socket pair: $!";

my $pid = fork || do {
    my $msg = Socket::MsgHdr->new(buflen => 1024, controllen => 64);

    recvmsg($ask, $msg);
    shutdown($ask, 2);

    my @cmsg = $msg->cmsghdr();
    my $fd = unpack('i', $cmsg[2]);

    POSIX::write($fd, $msg->buf(), 4)
      || die "Failed to write echo to pipe: $!";
    POSIX::close($fd);

    exit(0);
};

my ($afd, $bfd) = POSIX::pipe();
unless (defined $afd && defined $bfd) {
    die "Could not create pipe: $!";
}

ok(0 < tinycv::send_with_fd($bsk, 'echo', $bfd), 'Send file handle');
POSIX::close($bfd);
shutdown($bsk, 2);

my $buf = '';
POSIX::read($afd, $buf, 4)
  || die "Failed to read echo from pipe: $!";
ok($buf eq 'echo', "Receive echo on pipe FD we sent");
POSIX::close($afd);

wait;
ok(0 == $?, 'Child process exited cleanly');

done_testing();

1;