File: su_test.py

package info (click to toggle)
python-mitogen 0.3.3-9%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,816 kB
  • sloc: python: 22,086; sh: 171; makefile: 74; perl: 19; ansic: 18; javascript: 5
file content (66 lines) | stat: -rw-r--r-- 2,083 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
import os

import mitogen.core
import mitogen.su

import testlib


class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
    stub_su_path = testlib.data_path('stubs/stub-su.py')

    def run_su(self, **kwargs):
        context = self.router.su(
            su_path=self.stub_su_path,
            **kwargs
        )
        argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
        return context, argv

    def test_basic(self):
        context, argv = self.run_su()
        self.assertEqual(argv[1], 'root')
        self.assertEqual(argv[2], '-c')


class SuTest(testlib.DockerMixin, testlib.TestCase):
    stub_su_path = testlib.data_path('stubs/stub-su.py')

    def test_slow_auth_failure(self):
        # #363: old input loop would fail to spot auth failure because of
        # scheduling vs. su calling write() twice.
        os.environ['DO_SLOW_AUTH_FAILURE'] = '1'
        try:
            self.assertRaises(mitogen.su.PasswordError,
                lambda: self.router.su(su_path=self.stub_su_path)
            )
        finally:
            del os.environ['DO_SLOW_AUTH_FAILURE']

    def test_password_required(self):
        ssh = self.docker_ssh(
            username='mitogen__has_sudo',
            password='has_sudo_password',
        )
        e = self.assertRaises(mitogen.core.StreamError,
            lambda: self.router.su(via=ssh)
        )
        self.assertTrue(mitogen.su.password_required_msg in str(e))

    def test_password_incorrect(self):
        ssh = self.docker_ssh(
            username='mitogen__has_sudo',
            password='has_sudo_password',
        )
        e = self.assertRaises(mitogen.core.StreamError,
            lambda: self.router.su(via=ssh, password='x')
        )
        self.assertTrue(mitogen.su.password_incorrect_msg in str(e))

    def test_password_okay(self):
        ssh = self.docker_ssh(
            username='mitogen__has_sudo',
            password='has_sudo_password',
        )
        context = self.router.su(via=ssh, password='rootpassword')
        self.assertEqual(0, context.call(os.getuid))