File: line_reader_file.py

package info (click to toggle)
plaso 20201007-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 519,924 kB
  • sloc: python: 79,002; sh: 629; xml: 72; sql: 14; vhdl: 11; makefile: 10
file content (206 lines) | stat: -rw-r--r-- 6,720 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the binary line reader file-like object."""

from __future__ import unicode_literals

import unittest

from dfvfs.file_io import os_file_io
from dfvfs.path import os_path_spec
from dfvfs.resolver import context

from plaso.lib import line_reader_file

from tests import test_lib as shared_test_lib


class BinaryLineReaderTest(shared_test_lib.BaseTestCase):
  """Tests for the binary line reader."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    self._resolver_context = context.Context()

  def testReadline(self):
    """Test the readline() function."""
    test_file_path = self._GetTestFilePath(['another_file'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    line = line_reader.readline()
    self.assertEqual(line, b'This is another file.\n')

    offset = line_reader.tell()
    self.assertEqual(offset, 22)

    line_reader = line_reader_file.BinaryLineReader(file_object)

    line = line_reader.readline(size=11)
    self.assertEqual(line, b'This is ano')

    offset = line_reader.tell()
    self.assertEqual(offset, 11)

    file_object.close()

  def testReadlineMultipleLines(self):
    """Test the readline() function on multiple lines."""
    test_file_path = self._GetTestFilePath(['password.csv'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    line = line_reader.readline()
    self.assertEqual(line, b'place,user,password\n')

    offset = line_reader.tell()
    self.assertEqual(offset, 20)

    line = line_reader.readline(size=5)
    self.assertEqual(line, b'bank,')

    offset = line_reader.tell()
    self.assertEqual(offset, 25)

    line = line_reader.readline()
    self.assertEqual(line, b'joesmith,superrich\n')

    offset = line_reader.tell()
    self.assertEqual(offset, 44)

    line = line_reader.readline()
    self.assertEqual(line, b'alarm system,-,1234\n')

    offset = line_reader.tell()
    self.assertEqual(offset, 64)

    file_object.close()

  def testReadlines(self):
    """Test the readlines() function."""
    test_file_path = self._GetTestFilePath(['password.csv'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    lines = line_reader.readlines()

    self.assertEqual(len(lines), 5)
    self.assertEqual(lines[0], b'place,user,password\n')
    self.assertEqual(lines[1], b'bank,joesmith,superrich\n')
    self.assertEqual(lines[2], b'alarm system,-,1234\n')
    self.assertEqual(lines[3], b'treasure chest,-,1111\n')
    self.assertEqual(lines[4], b'uber secret laire,admin,admin\n')

    file_object.close()

  def testReadlinesWithSizeHint(self):
    """Test the readlines() function."""
    test_file_path = self._GetTestFilePath(['password.csv'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    lines = line_reader.readlines(sizehint=60)

    self.assertEqual(len(lines), 3)
    self.assertEqual(lines[0], b'place,user,password\n')
    self.assertEqual(lines[1], b'bank,joesmith,superrich\n')
    self.assertEqual(lines[2], b'alarm system,-,1234\n')

    file_object.close()

  def testReadlinesWithFileWithoutNewLineAtEnd(self):
    """Test reading lines from a file without a new line char at the end."""
    test_file_path = self._GetTestFilePath(['mactime.body'])
    self._SkipIfPathNotExists(test_file_path)

    test_file_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_file_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    lines = line_reader.readlines()

    self.assertEqual(len(lines), 21)

  def testIterator(self):
    """Test the iterator functionality."""
    test_file_path = self._GetTestFilePath(['password.csv'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    file_object = os_file_io.OSFile(self._resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    lines = []
    for line in line_reader:
      lines.append(line)

    self.assertEqual(len(lines), 5)
    self.assertEqual(lines[0], b'place,user,password\n')
    self.assertEqual(lines[1], b'bank,joesmith,superrich\n')
    self.assertEqual(lines[2], b'alarm system,-,1234\n')
    self.assertEqual(lines[3], b'treasure chest,-,1111\n')
    self.assertEqual(lines[4], b'uber secret laire,admin,admin\n')

    file_object.close()

  # TODO: Add a test which tests reading a file which is
  # larger than the buffer size, and read lines until it crosses
  # that original buffer size (to test if the buffer is correctly
  # filled).


class BinaryDSVReaderTest(shared_test_lib.BaseTestCase):
  """Tests for the binary delimited separated values reader."""

  def testIterator(self):
    """Tests the iterator functionality."""
    test_file_path = self._GetTestFilePath(['password.csv'])
    self._SkipIfPathNotExists(test_file_path)

    test_path_spec = os_path_spec.OSPathSpec(location=test_file_path)

    resolver_context = context.Context()
    file_object = os_file_io.OSFile(resolver_context)
    file_object.open(test_path_spec)
    line_reader = line_reader_file.BinaryLineReader(file_object)

    dsv_reader = line_reader_file.BinaryDSVReader(line_reader, delimiter=b',')

    rows = []
    for row in dsv_reader:
      rows.append(row)

    self.assertEqual(len(rows), 5)
    self.assertEqual(rows[0], [b'place', b'user', b'password'])
    self.assertEqual(rows[1], [b'bank', b'joesmith', b'superrich'])
    self.assertEqual(rows[2], [b'alarm system', b'-', b'1234'])
    self.assertEqual(rows[3], [b'treasure chest', b'-', b'1111'])
    self.assertEqual(rows[4], [b'uber secret laire', b'admin', b'admin'])


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