summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_netrc.py
blob: 24ad78688c7032ac32c0cb5c4a79c42479bd7a4d (plain)
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

import netrc, os, unittest, sys
from test import test_support

TEST_NETRC = """
machine foo login log1 password pass1 account acct1

macdef macro1
line1
line2

macdef macro2
line3
line4

default login log2 password pass2

"""

temp_filename = test_support.TESTFN

class NetrcTestCase(unittest.TestCase):

    def setUp (self):
        mode = 'w'
        if sys.platform not in ['cygwin']:
            mode += 't'
        fp = open(temp_filename, mode)
        fp.write(TEST_NETRC)
        fp.close()
        self.netrc = netrc.netrc(temp_filename)

    def tearDown (self):
        del self.netrc
        test_support.unlink(temp_filename)

    def test_case_1(self):
        self.assert_(self.netrc.macros == {'macro1':['line1\n', 'line2\n'],
                                           'macro2':['line3\n', 'line4\n']}
                                           )
        self.assert_(self.netrc.hosts['foo'] == ('log1', 'acct1', 'pass1'))
        self.assert_(self.netrc.hosts['default'] == ('log2', None, 'pass2'))

    if os.name == 'posix':
        def test_security(self):
            # This test is incomplete since we are normally not run as root and
            # therefore can't test the file ownership being wrong.
            os.unlink(temp_filename)
            d = test_support.TESTFN
            try:
                os.mkdir(d)
                fn = os.path.join(d, '.netrc')
                with open(fn, 'wt') as f:
                    f.write(TEST_NETRC)
                with test_support.EnvironmentVarGuard() as environ:
                    environ.set('HOME', d)
                    os.chmod(fn, 0600)
                    self.netrc = netrc.netrc()
                    self.test_case_1()
                    os.chmod(fn, 0622)
                    self.assertRaises(netrc.NetrcParseError, netrc.netrc)
            finally:
                test_support.rmtree(d)

def test_main():
    test_support.run_unittest(NetrcTestCase)

if __name__ == "__main__":
    test_main()