summaryrefslogtreecommitdiff
path: root/tests/volume_info.py
blob: 84d8c915e9ced04c485968e30178e747f8e06857 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Tests for Volume_Info."""

import os
import unittest

import pytsk3

import test_lib


# mmls ../test_data/tsk_volume_system.raw
# DOS Partition Table
# Offset Sector: 0
# Units are in 512-byte sectors
#
#      Slot    Start        End          Length       Description
# 00:  Meta    0000000000   0000000000   0000000001   Primary Table (#0)
# 01:  -----   0000000000   0000000000   0000000001   Unallocated
# 02:  00:00   0000000001   0000000350   0000000350   Linux (0x83)
# 03:  Meta    0000000351   0000002879   0000002529   DOS Extended (0x05)
# 04:  Meta    0000000351   0000000351   0000000001   Extended Table (#1)
# 05:  -----   0000000351   0000000351   0000000001   Unallocated
# 06:  01:00   0000000352   0000002879   0000002528   Linux (0x83)


class TSKVolumeInfoTestCase(unittest.TestCase):
  """Volume_Info test case."""

  maxDiff = None

  def _testInitialize(self, volume_info):
    """Test the initialize functionality.

    Args:
      volume_info: the Volume_Info object.
    """
    self.assertNotEquals(volume_info, None)

  def _testIterate(self, volume_info):
    """Test the iterate functionality.

    Args:
      volume_info: the Volume_Info object.
    """
    self.assertNotEquals(volume_info, None)
    self.assertNotEquals(getattr(volume_info, 'info', None), None)

    self.assertEquals(str(volume_info.info.vstype), 'TSK_VS_TYPE_DOS')

    parts = []

    for part in volume_info:
      part_string = (
          u'{0:02d}:  {1:010d}   {2:010d}   {3:010d}   {4:s}\n').format(
              part.addr, part.start, part.start + part.len - 1, part.len,
              part.desc.decode('utf-8'))
      parts.append(part_string)

    self.assertEquals(len(parts), 7)

    expected_parts_string = (
        u'00:  0000000000   0000000000   0000000001   Primary Table (#0)\n'
        u'01:  0000000000   0000000000   0000000001   Unallocated\n'
        u'02:  0000000001   0000000350   0000000350   Linux (0x83)\n'
        u'03:  0000000351   0000002879   0000002529   DOS Extended (0x05)\n'
        u'04:  0000000351   0000000351   0000000001   Extended Table (#1)\n'
        u'05:  0000000351   0000000351   0000000001   Unallocated\n'
        u'06:  0000000352   0000002879   0000002528   Linux (0x83)\n')

    self.assertEquals(u''.join(parts), expected_parts_string)


class TSKVolumeInfoTest(TSKVolumeInfoTestCase):
  """Volume_Info for testing."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    test_file = os.path.join('test_data', 'tsk_volume_system.raw')
    self._img_info = pytsk3.Img_Info(test_file)

  def testInitialize(self):
    """Test the initialize functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testInitialize(volume_info)

  def testIterate(self):
    """Test the iterate functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testIterate(volume_info)


class TSKVolumeInfoBogusTest(TSKVolumeInfoTestCase):
  """Volume_Info for testing that fails."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    test_file = os.path.join('test_data', 'bogus.raw')
    self._img_info = pytsk3.Img_Info(test_file)

  def testInitialize(self):
    """Test the initialize functionality."""
    with self.assertRaises(IOError):
      pytsk3.Volume_Info(self._img_info)


class TSKVolumeInfoFileObjectTest(TSKVolumeInfoTestCase):
  """Tests the Volume_Info object using an Img_Info file-like object."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    test_file = os.path.join('test_data', 'tsk_volume_system.raw')
    self._file_object = open(test_file, 'rb')

    stat_info = os.stat(test_file)
    self._file_size = stat_info.st_size
    self._img_info = test_lib.FileObjectImageInfo(
        self._file_object, self._file_size)

  def testInitialize(self):
    """Test the initialize functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testInitialize(volume_info)

  def testIterate(self):
    """Test the iterate functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testIterate(volume_info)


class TSKVolumeInfoFileObjectWithDetectTest(TSKVolumeInfoTestCase):
  """Tests the Volume_Info object with auto-detect Img_Info."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    test_file = os.path.join('test_data', 'tsk_volume_system.raw')
    self._file_object = open(test_file, 'rb')

    stat_info = os.stat(test_file)
    self._file_size = stat_info.st_size
    self._img_info = test_lib.FileObjectImageInfo(
        self._file_object, self._file_size,
        image_type=pytsk3.TSK_IMG_TYPE_DETECT)

  def testInitialize(self):
    """Test the initialize functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testInitialize(volume_info)

  def testIterate(self):
    """Test the iterate functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testIterate(volume_info)


class TSKVolumeInfoFileObjectWithLargeSize(TSKVolumeInfoTestCase):
  """Tests the Volume_Info object with a large size Img_Info."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    test_file = os.path.join('test_data', 'tsk_volume_system.raw')
    self._file_object = open(test_file, 'rb')

    self._file_size = 1024 * 1024 * 1024 * 1024
    self._img_info = test_lib.FileObjectImageInfo(
        self._file_object, self._file_size)

  def testInitialize(self):
    """Test the initialize functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)
    self._testInitialize(volume_info)

  def testIterate(self):
    """Test the iterate functionality."""
    volume_info = pytsk3.Volume_Info(self._img_info)

    self.assertNotEquals(volume_info, None)
    self.assertNotEquals(getattr(volume_info, 'info', None), None)

    self.assertEquals(str(volume_info.info.vstype), 'TSK_VS_TYPE_DOS')

    parts = []

    for part in volume_info:
      part_string = (
          u'{0:02d}:  {1:010d}   {2:010d}   {3:010d}   {4:s}\n').format(
              part.addr, part.start, part.start + part.len - 1, part.len,
              part.desc.decode('utf-8'))
      parts.append(part_string)

    # Note that due to the size the SleuthKit will add a non-existing part:
    # 07:  0000002880   2147483647   2147480768   Unallocated

    self.assertEquals(len(parts), 8)

    expected_parts_string = (
        u'00:  0000000000   0000000000   0000000001   Primary Table (#0)\n'
        u'01:  0000000000   0000000000   0000000001   Unallocated\n'
        u'02:  0000000001   0000000350   0000000350   Linux (0x83)\n'
        u'03:  0000000351   0000002879   0000002529   DOS Extended (0x05)\n'
        u'04:  0000000351   0000000351   0000000001   Extended Table (#1)\n'
        u'05:  0000000351   0000000351   0000000001   Unallocated\n'
        u'06:  0000000352   0000002879   0000002528   Linux (0x83)\n'
        u'07:  0000002880   2147483647   2147480768   Unallocated\n')

    self.assertEquals(u''.join(parts), expected_parts_string)


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