Dpkg_Compression.t 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 => 13;
  18. use_ok('Dpkg::Compression');
  19. use_ok('Dpkg::Compression::FileHandle');
  20. my $tmpdir = 't.tmp/Dpkg_Compression';
  21. mkdir $tmpdir;
  22. my @lines = ("One\n", "Two\n", "Three\n");
  23. my $fh;
  24. sub test_write {
  25. my ($filename, $check_result) = @_;
  26. $fh = Dpkg::Compression::FileHandle->new();
  27. open $fh, '>', $filename or die 'open failed';
  28. print { $fh } $lines[0];
  29. syswrite($fh, $lines[1]);
  30. printf { $fh } '%s', $lines[2];
  31. close $fh or die 'close failed';
  32. &$check_result($filename, 'std functions');
  33. unlink $filename or die "cannot unlink $filename";
  34. $fh = Dpkg::Compression::FileHandle->new();
  35. $fh->open($filename, 'w');
  36. $fh->print($lines[0]);
  37. $fh->write($lines[1], length($lines[1]));
  38. $fh->printf('%s', $lines[2]);
  39. $fh->close() or die 'close failed';
  40. &$check_result($filename, 'IO::Handle methods');
  41. }
  42. sub check_uncompressed {
  43. my ($filename, $method) = @_;
  44. open(my $read_fh, '<', $filename) or die "cannot read $filename";
  45. my @read = <$read_fh>;
  46. close $read_fh or die 'cannot close';
  47. is_deeply(\@lines, \@read, "$filename correctly written ($method)");
  48. }
  49. sub check_compressed {
  50. my ($filename, $method) = @_;
  51. open my $read_fh, '-|', 'zcat', "$tmpdir/myfile.gz"
  52. or die 'cannot fork zcat';
  53. my @read = <$read_fh>;
  54. close $read_fh or die 'cannot close';
  55. is_deeply(\@lines, \@read, "$filename correctly written ($method)");
  56. }
  57. sub test_read {
  58. my $filename = shift;
  59. $fh = Dpkg::Compression::FileHandle->new();
  60. open($fh, '<', $filename) or die 'open failed';
  61. my @read = <$fh>;
  62. close $fh or die 'close failed';
  63. is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
  64. @read = ();
  65. $fh = Dpkg::Compression::FileHandle->new();
  66. $fh->open($filename, 'r') or die 'open failed';
  67. @read = $fh->getlines();
  68. $fh->close() or die 'close failed';
  69. is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
  70. }
  71. # Test changing the default compression levels
  72. my $old_level = compression_get_default_level();
  73. compression_set_default_level(1);
  74. is(compression_get_default_level(), 1, 'change default compression level');
  75. compression_set_default_level(5);
  76. is(compression_get_default_level(), 5, 'change default compression level');
  77. compression_set_default_level(undef);
  78. is(compression_get_default_level(), $old_level, 'reset default compression level');
  79. # Test write on uncompressed file
  80. test_write("$tmpdir/myfile", \&check_uncompressed);
  81. # Test write on compressed file
  82. test_write("$tmpdir/myfile.gz", \&check_compressed);
  83. # Test read on uncompressed file
  84. test_read("$tmpdir/myfile");
  85. # Test read on compressed file
  86. test_read("$tmpdir/myfile.gz");