chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / scripts / t / Dpkg_IPC.t
1 #!/usr/bin/perl
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 use strict;
17 use warnings;
18
19 use Test::More tests => 8;
20
21 use File::Temp qw(tempfile);
22
23 use_ok('Dpkg::IPC');
24
25 $/ = undef;
26
27 my ($tmp1_fh, $tmp1_name) = tempfile(UNLINK => 1);
28 my ($tmp2_fh, $tmp2_name) = tempfile(UNLINK => 1);
29 my $tmp_fh;
30
31 my $string1 = "foo\nbar\n";
32 my $string2;
33
34 open $tmp_fh, '>', $tmp1_name
35     or die "cannot open $tmp1_name: $!";
36 print { $tmp_fh } $string1;
37 close $tmp_fh;
38
39 my $pid = spawn(exec => 'cat',
40                 from_string => \$string1,
41                 to_string => \$string2);
42
43 ok($pid, 'execute cat program, I/O to variables');
44
45 is($string2, $string1, '{from,to}_string');
46
47 $pid = spawn(exec => 'cat',
48              from_handle => $tmp1_fh,
49              to_handle => $tmp2_fh);
50
51 ok($pid, 'execute cat program, I/O to filehandles');
52
53 wait_child($pid);
54
55 open $tmp_fh, '<', $tmp2_name
56     or die "cannot open $tmp2_name: $!";
57 $string2 = <$tmp_fh>;
58 close $tmp_fh;
59
60 is($string2, $string1, '{from,to}_handle');
61
62 $pid = spawn(exec => 'cat',
63              from_file => $tmp1_name,
64              to_file => $tmp2_name,
65              wait_child => 1);
66
67 ok($pid, 'execute cat program, I/O to filenames and wait');
68
69 open $tmp_fh, '<', $tmp2_name
70     or die "cannot open $tmp2_name: $!";
71 $string2 = <$tmp_fh>;
72 close $tmp_fh;
73
74 is($string2, $string1, '{from,to}_file');
75
76 eval {
77     $pid = spawn(exec => ['sleep', '10'],
78                  wait_child => 1,
79                  timeout => 1);
80 };
81 ok($@, 'fails on timeout');