File: interface.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 (90 lines) | stat: -rw-r--r-- 2,562 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the CLI argument helper interface."""

from __future__ import unicode_literals

import locale
import sys
import unittest

from plaso.lib import errors

from tests.cli import test_lib as cli_test_lib
from tests.cli.helpers import test_lib


class HelperManagerTest(unittest.TestCase):
  """Tests the parsers manager."""

  # pylint: disable=protected-access

  def testParseNumericOption(self):
    """Tests the _ParseNumericOption function."""
    test_helper = test_lib.TestHelper()

    expected_integer = 123
    options = cli_test_lib.TestOptions()
    options.test = expected_integer

    integer = test_helper._ParseNumericOption(options, 'test')
    self.assertEqual(integer, expected_integer)

    options = cli_test_lib.TestOptions()

    integer = test_helper._ParseNumericOption(options, 'test')
    self.assertIsNone(integer)

    integer = test_helper._ParseNumericOption(
        options, 'test', default_value=expected_integer)
    self.assertEqual(integer, expected_integer)

    options = cli_test_lib.TestOptions()
    options.test = b'abc'

    with self.assertRaises(errors.BadConfigOption):
      test_helper._ParseNumericOption(options, 'test')

  def testParseStringOption(self):
    """Tests the _ParseStringOption function."""
    encoding = sys.stdin.encoding

    # Note that sys.stdin.encoding can be None.
    if not encoding:
      encoding = locale.getpreferredencoding()

    test_helper = test_lib.TestHelper()

    expected_string = 'Test Unicode string'
    options = cli_test_lib.TestOptions()
    options.test = expected_string

    string = test_helper._ParseStringOption(options, 'test')
    self.assertEqual(string, expected_string)

    options = cli_test_lib.TestOptions()

    string = test_helper._ParseStringOption(options, 'test')
    self.assertIsNone(string)

    string = test_helper._ParseStringOption(
        options, 'test', default_value=expected_string)
    self.assertEqual(string, expected_string)

    options = cli_test_lib.TestOptions()
    options.test = expected_string.encode(encoding)

    string = test_helper._ParseStringOption(options, 'test')
    self.assertEqual(string, expected_string)

    if encoding and encoding == 'UTF-8':
      options = cli_test_lib.TestOptions()
      options.test = (
          b'\xad\xfd\xab\x73\x99\xc7\xb4\x78\xd0\x8c\x8a\xee\x6d\x6a\xcb\x90')

      with self.assertRaises(errors.BadConfigOption):
        test_helper._ParseStringOption(options, 'test')


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