tarwriter.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import os
  22. import subprocess
  23. import tempfile
  24. import shutil
  25. import qubes.tarwriter
  26. import qubes.tests
  27. class TC_00_TarWriter(qubes.tests.QubesTestCase):
  28. def setUp(self):
  29. super(TC_00_TarWriter, self).setUp()
  30. self.input_path = tempfile.mktemp()
  31. self.output_path = tempfile.mktemp()
  32. self.extract_dir = tempfile.mkdtemp()
  33. def tearDown(self):
  34. if os.path.exists(self.input_path):
  35. os.unlink(self.input_path)
  36. if os.path.exists(self.output_path):
  37. os.unlink(self.output_path)
  38. if os.path.exists(self.extract_dir):
  39. shutil.rmtree(self.extract_dir)
  40. return super(TC_00_TarWriter, self).tearDown()
  41. def assertTarExtractable(self, expected_name=None):
  42. if expected_name is None:
  43. expected_name = self.input_path
  44. with self.assertNotRaises(subprocess.CalledProcessError):
  45. tar_output = subprocess.check_output(
  46. ['tar', 'xvf', self.output_path],
  47. cwd=self.extract_dir,
  48. stderr=subprocess.STDOUT)
  49. expected_output = expected_name + '\n'
  50. if expected_name[0] == '/':
  51. expected_output = (
  52. 'tar: Removing leading `/\' from member names\n' +
  53. expected_output)
  54. self.assertEqual(tar_output.decode(), expected_output)
  55. extracted_path = os.path.join(self.extract_dir,
  56. expected_name.lstrip('/'))
  57. with self.assertNotRaises(subprocess.CalledProcessError):
  58. subprocess.check_call(
  59. ['diff', '-q', self.input_path, extracted_path])
  60. # make sure the file is still sparse
  61. orig_stat = os.stat(self.input_path)
  62. extracted_stat = os.stat(extracted_path)
  63. self.assertEqual(orig_stat.st_blocks, extracted_stat.st_blocks)
  64. self.assertEqual(orig_stat.st_size, extracted_stat.st_size)
  65. def write_sparse_chunks(self, num_chunks):
  66. with open(self.input_path, 'w') as f:
  67. for i in range(num_chunks):
  68. f.seek(8192 * i)
  69. f.write('a' * 4096)
  70. def test_000_simple(self):
  71. self.write_sparse_chunks(1)
  72. with open(self.input_path, 'w') as f:
  73. f.write('a' * 4096)
  74. qubes.tarwriter.main([self.input_path, self.output_path])
  75. self.assertTarExtractable()
  76. def test_001_simple_sparse2(self):
  77. self.write_sparse_chunks(2)
  78. qubes.tarwriter.main([self.input_path, self.output_path])
  79. self.assertTarExtractable()
  80. def test_002_simple_sparse3(self):
  81. # tar header contains info about 4 chunks, check for off-by-one errors
  82. self.write_sparse_chunks(3)
  83. qubes.tarwriter.main([self.input_path, self.output_path])
  84. self.assertTarExtractable()
  85. def test_003_simple_sparse4(self):
  86. # tar header contains info about 4 chunks, check for off-by-one errors
  87. self.write_sparse_chunks(4)
  88. qubes.tarwriter.main([self.input_path, self.output_path])
  89. self.assertTarExtractable()
  90. def test_004_simple_sparse5(self):
  91. # tar header contains info about 4 chunks, check for off-by-one errors
  92. self.write_sparse_chunks(5)
  93. qubes.tarwriter.main([self.input_path, self.output_path])
  94. self.assertTarExtractable()
  95. def test_005_simple_sparse24(self):
  96. # tar header contains info about 4 chunks, next header contains 21 of
  97. # them, check for off-by-one errors
  98. self.write_sparse_chunks(24)
  99. qubes.tarwriter.main([self.input_path, self.output_path])
  100. self.assertTarExtractable()
  101. def test_006_simple_sparse25(self):
  102. # tar header contains info about 4 chunks, next header contains 21 of
  103. # them, check for off-by-one errors
  104. self.write_sparse_chunks(25)
  105. qubes.tarwriter.main([self.input_path, self.output_path])
  106. self.assertTarExtractable()
  107. def test_007_simple_sparse26(self):
  108. # tar header contains info about 4 chunks, next header contains 21 of
  109. # them, check for off-by-one errors
  110. self.write_sparse_chunks(26)
  111. qubes.tarwriter.main([self.input_path, self.output_path])
  112. self.assertTarExtractable()
  113. def test_010_override_name(self):
  114. self.write_sparse_chunks(1)
  115. qubes.tarwriter.main(['--override-name',
  116. 'different-name', self.input_path, self.output_path])
  117. self.assertTarExtractable(expected_name='different-name')
  118. def test_011_empty(self):
  119. self.write_sparse_chunks(0)
  120. qubes.tarwriter.main([self.input_path, self.output_path])
  121. self.assertTarExtractable()
  122. def test_012_gzip(self):
  123. self.write_sparse_chunks(0)
  124. qubes.tarwriter.main([
  125. '--use-compress-program=gzip', self.input_path, self.output_path])
  126. with self.assertNotRaises(subprocess.CalledProcessError):
  127. subprocess.check_call(['gzip', '--test', self.output_path])
  128. self.assertTarExtractable()