tarwriter.py 5.7 KB

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