File: interface.py

package info (click to toggle)
plaso 20241006-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 673,224 kB
  • sloc: python: 91,831; sh: 557; xml: 97; makefile: 17; sql: 14; vhdl: 11
file content (95 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the CLI argument helper interface."""

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_option_value = 123
    options = cli_test_lib.TestOptions()
    options.test = expected_option_value

    option_value = test_helper._ParseNumericOption(options, 'test')
    self.assertEqual(option_value, expected_option_value)

    options = cli_test_lib.TestOptions()

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

    option_value = test_helper._ParseNumericOption(
        options, 'test', default_value=expected_option_value)
    self.assertEqual(option_value, expected_option_value)

    expected_option_value = 123.456
    options = cli_test_lib.TestOptions()
    options.test = expected_option_value

    option_value = test_helper._ParseNumericOption(options, 'test')
    self.assertEqual(option_value, expected_option_value)

    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_option_value = 'Test Unicode string'
    options = cli_test_lib.TestOptions()
    options.test = expected_option_value

    option_value = test_helper._ParseStringOption(options, 'test')
    self.assertEqual(option_value, expected_option_value)

    options = cli_test_lib.TestOptions()

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

    option_value = test_helper._ParseStringOption(
        options, 'test', default_value=expected_option_value)
    self.assertEqual(option_value, expected_option_value)

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

    option_value = test_helper._ParseStringOption(options, 'test')
    self.assertEqual(option_value, expected_option_value)

    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()