Dpkg_IPC.t 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. use strict;
  16. use warnings;
  17. use Test::More tests => 8;
  18. use File::Temp qw(tempfile);
  19. use_ok('Dpkg::IPC');
  20. $/ = undef;
  21. my ($tmp1_fh, $tmp1_name) = tempfile(UNLINK => 1);
  22. my ($tmp2_fh, $tmp2_name) = tempfile(UNLINK => 1);
  23. my $tmp_fh;
  24. my $string1 = "foo\nbar\n";
  25. my $string2;
  26. open $tmp_fh, '>', $tmp1_name
  27. or die "cannot open $tmp1_name: $!";
  28. print { $tmp_fh } $string1;
  29. close $tmp_fh;
  30. my $pid = spawn(exec => 'cat',
  31. from_string => \$string1,
  32. to_string => \$string2);
  33. ok($pid, 'execute cat program, I/O to variables');
  34. is($string2, $string1, '{from,to}_string');
  35. $pid = spawn(exec => 'cat',
  36. from_handle => $tmp1_fh,
  37. to_handle => $tmp2_fh);
  38. ok($pid, 'execute cat program, I/O to filehandles');
  39. wait_child($pid);
  40. open $tmp_fh, '<', $tmp2_name
  41. or die "cannot open $tmp2_name: $!";
  42. $string2 = <$tmp_fh>;
  43. close $tmp_fh;
  44. is($string2, $string1, '{from,to}_handle');
  45. $pid = spawn(exec => 'cat',
  46. from_file => $tmp1_name,
  47. to_file => $tmp2_name,
  48. wait_child => 1,
  49. timeout => 5);
  50. ok($pid, 'execute cat program, I/O to filenames, wait and timeout');
  51. open $tmp_fh, '<', $tmp2_name
  52. or die "cannot open $tmp2_name: $!";
  53. $string2 = <$tmp_fh>;
  54. close $tmp_fh;
  55. is($string2, $string1, '{from,to}_file');
  56. eval {
  57. $pid = spawn(exec => ['sleep', '10'],
  58. wait_child => 1,
  59. timeout => 5);
  60. };
  61. ok($@, 'fails on timeout');