aboutsummaryrefslogtreecommitdiff
path: root/test.py
blob: 7a784bef449be6df66933d9f80afecaee66bf099 (plain) (blame)
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
#!/usr/bin/python3
import configparser
import unittest

from ssh_negotiate_term import SSHNegotiateTerm

class TestSSHNegotiateTerm(unittest.TestCase):

    def setUp(self):
        config = configparser.ConfigParser()
        config['patterns'] = SSHNegotiateTerm.DEFAULT_PATTERNS
        config['ssh'] = {}
        config['ssh']['path'] = SSHNegotiateTerm.DEFAULT_SSH
        config['translations'] = SSHNegotiateTerm.DEFAULT_TRANSLATIONS
        self.snt_config = config

    def test_instantiate(self):
        snt = SSHNegotiateTerm('foo', [], self.snt_config)
        self.assertIsInstance(snt, SSHNegotiateTerm)

    def test_hostname(self):
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'examplehost'], self.snt_config)
        self.assertEqual(snt.term, 'screen-256color')

    def test_fqdn(self):
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'host.example.net.'], self.snt_config)
        self.assertEqual(snt.term, 'screen-256color')

    def test_ipv4(self):
        snt = SSHNegotiateTerm('screen-256color', ['ssh', '192.0.2.1'], self.snt_config)
        self.assertEqual(snt.term, 'screen')

    def test_ipv6(self):
        snt = SSHNegotiateTerm('screen-256color', ['ssh', '2001:0db8::1'], self.snt_config)
        self.assertEqual(snt.term, 'screen')

    def test_complex_command_line(self):
        snt = SSHNegotiateTerm('screen-256color', ['ssh', '-4', '-NfoSetEnv=FOO=BAR', '-NtT', '--', '192.0.2.1', 'runcmd'], self.snt_config)
        self.assertEqual(snt.term, 'screen')
        snt = SSHNegotiateTerm('screen-256color', ['ssh', '-4', '-NfoSetEnv=FOO=BAR', '-NtT', '--', 'examplehost', 'runcmd'], self.snt_config)
        self.assertEqual(snt.term, 'screen-256color')

    def test_null_term(self):
        snt = SSHNegotiateTerm(None, ['ssh', '192.0.2.1'], self.snt_config)
        self.assertEqual(snt.term, None)

    def test_unknown_term(self):
        snt = SSHNegotiateTerm('qwyjibo', ['ssh', '192.0.2.1'], self.snt_config)
        self.assertEqual(snt.term, 'qwyjibo')

    def test_untranslated_term(self):
        snt = SSHNegotiateTerm('screen', ['ssh', '192.0.2.1'], self.snt_config)
        self.assertEqual(snt.term, 'screen')

    def test_patterns_simple(self):
        config = self.snt_config
        self.snt_config['patterns']['examplehost'] = '^examplehost$'
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'examplehost'], self.snt_config)
        self.assertEqual(snt.term, 'screen')
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'examplehost2'], self.snt_config)
        self.assertEqual(snt.term, 'screen-256color')

    def test_pattern_complex(self):
        config = self.snt_config
        self.snt_config['patterns']['complexhost'] = '^[a-z]{3}-[a-z]{2,}-[a-z]{2,}-[0-9]'
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'abc-example-ar-1'], self.snt_config)
        self.assertEqual(snt.term, 'screen')
        snt = SSHNegotiateTerm('screen-256color', ['ssh', 'abc-example-ar-a1'], self.snt_config)
        self.assertEqual(snt.term, 'screen-256color')

    def test_translations(self):
        for outer in self.snt_config['translations']:
            snt = SSHNegotiateTerm(outer, ['ssh', '192.0.2.1'], self.snt_config)
            inner = self.snt_config['translations'][outer]
            self.assertEqual(snt.term, inner)

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