Imported Upstream version 1.16.10
[services/dpkg.git] / scripts / t / 800_Dpkg_IPC.t
1 # -*- mode: cperl -*-
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 <http://www.gnu.org/licenses/>.
15
16 use Test::More tests => 8;
17
18 use strict;
19 use warnings;
20 use File::Temp qw(tempfile);
21
22 use_ok('Dpkg::IPC');
23
24 $/ = undef;
25
26 my ($tmp_fh, $tmp_name) = tempfile;
27 my ($tmp2_fh, $tmp2_name) = tempfile;
28
29 my $string = "foo\nbar\n";
30 my $string2;
31
32 open TMP, '>', $tmp_name;
33 print TMP $string;
34 close TMP;
35
36 my $pid = spawn(exec => "cat",
37                 from_string => \$string,
38                 to_string => \$string2);
39
40 ok($pid);
41
42 is($string2, $string, "{from,to}_string");
43
44 $pid = spawn(exec => "cat",
45              from_handle => $tmp_fh,
46              to_handle => $tmp2_fh);
47
48 ok($pid);
49
50 wait_child($pid);
51
52 open TMP, '<', $tmp2_name;
53 $string2 = <TMP>;
54 close TMP;
55
56 is($string2, $string, "{from,to}_handle");
57
58 $pid = spawn(exec => "cat",
59              from_file => $tmp_name,
60              to_file => $tmp2_name,
61              wait_child => 1,
62              timeout => 5);
63
64 ok($pid);
65
66 open TMP, '<', $tmp2_name;
67 $string2 = <TMP>;
68 close TMP;
69
70 is($string2, $string, "{from,to}_file");
71
72 eval {
73     $pid = spawn(exec => ["sleep", "10"],
74                  wait_child => 1,
75                  timeout => 5);
76 };
77 ok($@, "fails on timeout");
78
79 unlink($tmp_name);
80 unlink($tmp2_name);