Dpkg_Compression.t 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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") or die 'cannot fork zcat';
  52. my @read = <$read_fh>;
  53. close $read_fh or die 'cannot close';
  54. is_deeply(\@lines, \@read, "$filename correctly written ($method)");
  55. }
  56. sub test_read {
  57. my ($filename) = @_;
  58. $fh = Dpkg::Compression::FileHandle->new();
  59. open($fh, '<', $filename) or die 'open failed';
  60. my @read = <$fh>;
  61. close $fh or die 'close failed';
  62. is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
  63. @read = ();
  64. $fh = Dpkg::Compression::FileHandle->new();
  65. $fh->open($filename, 'r') or die 'open failed';
  66. @read = $fh->getlines();
  67. $fh->close() or die 'close failed';
  68. is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
  69. }
  70. # Test changing the default compression levels
  71. my $old_level = compression_get_default_level();
  72. compression_set_default_level(1);
  73. is(compression_get_default_level(), 1, 'change default compression level');
  74. compression_set_default_level(5);
  75. is(compression_get_default_level(), 5, 'change default compression level');
  76. compression_set_default_level(undef);
  77. is(compression_get_default_level(), $old_level, 'reset default compression level');
  78. # Test write on uncompressed file
  79. test_write("$tmpdir/myfile", \&check_uncompressed);
  80. # Test write on compressed file
  81. test_write("$tmpdir/myfile.gz", \&check_compressed);
  82. # Test read on uncompressed file
  83. test_read("$tmpdir/myfile");
  84. # Test read on compressed file
  85. test_read("$tmpdir/myfile.gz");