File: gzipfile.py

package info (click to toggle)
dfvfs 20240505-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 475,508 kB
  • sloc: python: 36,533; vhdl: 1,922; sh: 448; xml: 52; makefile: 16
file content (183 lines) | stat: -rw-r--r-- 5,410 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for gzip compressed stream file."""

# Note: do not rename file to gzip.py this can cause the exception:
# AttributeError: 'module' object has no attribute 'GzipFile'
# when using pip.

import os
import unittest

from dfvfs.lib import definitions
from dfvfs.lib import gzipfile
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import resolver

from tests import test_lib as shared_test_lib


# TODO: add tests for _GzipDecompressorState
# TODO: add tests for GzipMember


class GzipCompressedStreamTest(shared_test_lib.BaseTestCase):
  """Tests a gzip compressed stream file-like object."""

  def testOpenClose(self):
    """Test the Open and Close functions."""
    test_path = self._GetTestFilePath(['syslog.gz'])
    self._SkipIfPathNotExists(test_path)

    test_file = gzipfile.GzipCompressedStream()

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    file_object = resolver.Resolver.OpenFileObject(test_os_path_spec)

    test_file.Open(file_object)

    try:
      self.assertEqual(len(test_file.members), 1)

    finally:
      test_file.close()

  def testSeek(self):
    """Test the seek functionality."""
    test_path = self._GetTestFilePath(['syslog.gz'])
    self._SkipIfPathNotExists(test_path)

    test_file = gzipfile.GzipCompressedStream()

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    file_object = resolver.Resolver.OpenFileObject(test_os_path_spec)

    test_file.Open(file_object)

    try:
      test_file.seek(177)
      self.assertEqual(test_file.read(5), b'53:01')

      self.assertEqual(test_file.get_offset(), 182)

      test_file.seek(-10, os.SEEK_END)
      self.assertEqual(test_file.read(5), b'times')

      test_file.seek(2, os.SEEK_CUR)
      self.assertEqual(test_file.read(2), b'--')

      # Conforming to the POSIX seek the offset can exceed the file size
      # but reading will result in no data being returned.
      test_file.seek(2000, os.SEEK_SET)
      self.assertEqual(test_file.get_offset(), 2000)
      self.assertEqual(test_file.read(2), b'')

      # Test with an invalid offset.
      with self.assertRaises(IOError):
        test_file.seek(-10, os.SEEK_SET)

      # On error the offset should not change.
      self.assertEqual(test_file.get_offset(), 2000)

      # Test with an invalid whence.
      with self.assertRaises(IOError):
        test_file.seek(10, 5)

      # On error the offset should not change.
      self.assertEqual(test_file.get_offset(), 2000)

    finally:
      test_file.close()

  def testRead(self):
    """Test the read functionality."""
    test_path = self._GetTestFilePath(['syslog.gz'])
    self._SkipIfPathNotExists(test_path)

    test_file = gzipfile.GzipCompressedStream()

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    file_object = resolver.Resolver.OpenFileObject(test_os_path_spec)

    test_file.Open(file_object)

    try:
      test_file.seek(167, os.SEEK_SET)

      self.assertEqual(test_file.get_offset(), 167)

      expected_data = (
          b'Jan 22 07:53:01 myhostname.myhost.com CRON[31051]: (root) CMD '
          b'(touch /var/run/crond.somecheck)\n')

      data = test_file.read(95)
      self.assertEqual(data, expected_data)

      self.assertEqual(test_file.get_offset(), 262)

    finally:
      test_file.close()

  def testReadCorrupt(self):
    """Tests reading a file that is corrupt."""
    # The corrupt gzip has no member footer.
    test_path = self._GetTestFilePath(['corrupt1.gz'])
    self._SkipIfPathNotExists(test_path)

    test_file = gzipfile.GzipCompressedStream()

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    file_object = resolver.Resolver.OpenFileObject(test_os_path_spec)

    test_file.Open(file_object)

    try:
      self.assertEqual(test_file.uncompressed_data_size, 2994187)

    finally:
      test_file.close()

  def testReadMultipleMembers(self):
    """Tests reading a file that contains multiple gzip members."""
    test_path = self._GetTestFilePath(['fsevents_000000000000b208'])
    self._SkipIfPathNotExists(test_path)

    test_file = gzipfile.GzipCompressedStream()

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    file_object = resolver.Resolver.OpenFileObject(test_os_path_spec)

    test_file.Open(file_object)

    try:
      self.assertEqual(len(test_file.members), 2)

      file_start = test_file.read(4)
      self.assertEqual(file_start, b'1SLD')

      # Read the end of the second member
      test_file.seek(506631 - 4)
      file_end = test_file.read(4)
      self.assertEqual(file_end, b'\x02\x00\x80\x00')

      # Seek backwards, and read across a member boundary.
      test_file.seek(28530)
      self.assertEqual(test_file.read(6), b'OS\x00P\x07\x00')

      # Read with a size greater than the file size.
      test_file.seek(0)
      data = test_file.read(size=506631 + 4)
      self.assertEqual(len(data), 506631)
      self.assertEqual(data[-4:], b'\x02\x00\x80\x00')

    finally:
      test_file.close()


if __name__ == '__main__':
  unittest.main()