test: Move test_data_file() to test.h
[dpkg.git] / scripts / t / Dpkg_IPC.t
blob450d98d07aa8c1c65e96a98b15ef55526d0a6fee
1 #!/usr/bin/perl
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.
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.
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/>.
16 use strict;
17 use warnings;
19 use Test::More tests => 8;
21 use File::Temp qw(tempfile);
23 use Dpkg::File;
25 use_ok('Dpkg::IPC');
27 my ($tmp1_fh, $tmp1_name) = tempfile(UNLINK => 1);
28 my ($tmp2_fh, $tmp2_name) = tempfile(UNLINK => 1);
30 my $string1 = "foo\nbar\n";
31 my $string2;
33 file_dump($tmp1_name, $string1);
35 my $pid = spawn(exec => 'cat',
36 from_string => \$string1,
37 to_string => \$string2);
39 ok($pid, 'execute cat program, I/O to variables');
41 is($string2, $string1, '{from,to}_string');
43 $pid = spawn(exec => 'cat',
44 from_handle => $tmp1_fh,
45 to_handle => $tmp2_fh);
47 ok($pid, 'execute cat program, I/O to filehandles');
49 wait_child($pid);
51 $string2 = file_slurp($tmp2_name);
53 is($string2, $string1, '{from,to}_handle');
55 $pid = spawn(exec => 'cat',
56 from_file => $tmp1_name,
57 to_file => $tmp2_name,
58 wait_child => 1);
60 ok($pid, 'execute cat program, I/O to filenames and wait');
62 $string2 = file_slurp($tmp2_name);
64 is($string2, $string1, '{from,to}_file');
66 eval {
67 $pid = spawn(exec => ['sleep', '10'],
68 wait_child => 1,
69 timeout => 1);
71 ok($@, 'fails on timeout');