Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_netrc.py

    r2 r391  
    1 
    2 import netrc, os, unittest, sys
     1import netrc, os, unittest, sys, textwrap
    32from test import test_support
    4 
    5 TEST_NETRC = """
    6 machine foo login log1 password pass1 account acct1
    7 
    8 macdef macro1
    9 line1
    10 line2
    11 
    12 macdef macro2
    13 line3
    14 line4
    15 
    16 default login log2 password pass2
    17 
    18 """
    193
    204temp_filename = test_support.TESTFN
     
    226class NetrcTestCase(unittest.TestCase):
    237
    24     def setUp (self):
     8    def make_nrc(self, test_data):
     9        test_data = textwrap.dedent(test_data)
    2510        mode = 'w'
    26         if sys.platform not in ['cygwin']:
     11        if sys.platform != 'cygwin':
    2712            mode += 't'
    28         fp = open(temp_filename, mode)
    29         fp.write(TEST_NETRC)
    30         fp.close()
    31         self.netrc = netrc.netrc(temp_filename)
     13        with open(temp_filename, mode) as fp:
     14            fp.write(test_data)
     15        self.addCleanup(os.unlink, temp_filename)
     16        return netrc.netrc(temp_filename)
    3217
    33     def tearDown (self):
    34         del self.netrc
    35         os.unlink(temp_filename)
     18    def test_default(self):
     19        nrc = self.make_nrc("""\
     20            machine host1.domain.com login log1 password pass1 account acct1
     21            default login log2 password pass2
     22            """)
     23        self.assertEqual(nrc.hosts['host1.domain.com'],
     24                         ('log1', 'acct1', 'pass1'))
     25        self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
    3626
    37     def test_case_1(self):
    38         self.assert_(self.netrc.macros == {'macro1':['line1\n', 'line2\n'],
    39                                            'macro2':['line3\n', 'line4\n']}
    40                                            )
    41         self.assert_(self.netrc.hosts['foo'] == ('log1', 'acct1', 'pass1'))
    42         self.assert_(self.netrc.hosts['default'] == ('log2', None, 'pass2'))
     27    def test_macros(self):
     28        nrc = self.make_nrc("""\
     29            macdef macro1
     30            line1
     31            line2
     32
     33            macdef macro2
     34            line3
     35            line4
     36            """)
     37        self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],
     38                                      'macro2': ['line3\n', 'line4\n']})
     39
     40    def _test_passwords(self, nrc, passwd):
     41        nrc = self.make_nrc(nrc)
     42        self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))
     43
     44    def test_password_with_leading_hash(self):
     45        self._test_passwords("""\
     46            machine host.domain.com login log password #pass account acct
     47            """, '#pass')
     48
     49    def test_password_with_trailing_hash(self):
     50        self._test_passwords("""\
     51            machine host.domain.com login log password pass# account acct
     52            """, 'pass#')
     53
     54    def test_password_with_internal_hash(self):
     55        self._test_passwords("""\
     56            machine host.domain.com login log password pa#ss account acct
     57            """, 'pa#ss')
     58
     59    def _test_comment(self, nrc, passwd='pass'):
     60        nrc = self.make_nrc(nrc)
     61        self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))
     62        self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))
     63
     64    def test_comment_before_machine_line(self):
     65        self._test_comment("""\
     66            # comment
     67            machine foo.domain.com login bar password pass
     68            machine bar.domain.com login foo password pass
     69            """)
     70
     71    def test_comment_before_machine_line_no_space(self):
     72        self._test_comment("""\
     73            #comment
     74            machine foo.domain.com login bar password pass
     75            machine bar.domain.com login foo password pass
     76            """)
     77
     78    def test_comment_before_machine_line_hash_only(self):
     79        self._test_comment("""\
     80            #
     81            machine foo.domain.com login bar password pass
     82            machine bar.domain.com login foo password pass
     83            """)
     84
     85    def test_comment_at_end_of_machine_line(self):
     86        self._test_comment("""\
     87            machine foo.domain.com login bar password pass # comment
     88            machine bar.domain.com login foo password pass
     89            """)
     90
     91    def test_comment_at_end_of_machine_line_no_space(self):
     92        self._test_comment("""\
     93            machine foo.domain.com login bar password pass #comment
     94            machine bar.domain.com login foo password pass
     95            """)
     96
     97    def test_comment_at_end_of_machine_line_pass_has_hash(self):
     98        self._test_comment("""\
     99            machine foo.domain.com login bar password #pass #comment
     100            machine bar.domain.com login foo password pass
     101            """, '#pass')
     102
     103
     104    @unittest.skipUnless(os.name == 'posix', 'POSIX only test')
     105    def test_security(self):
     106        # This test is incomplete since we are normally not run as root and
     107        # therefore can't test the file ownership being wrong.
     108        d = test_support.TESTFN
     109        os.mkdir(d)
     110        self.addCleanup(test_support.rmtree, d)
     111        fn = os.path.join(d, '.netrc')
     112        with open(fn, 'wt') as f:
     113            f.write("""\
     114                machine foo.domain.com login bar password pass
     115                default login foo password pass
     116                """)
     117        with test_support.EnvironmentVarGuard() as environ:
     118            environ.set('HOME', d)
     119            os.chmod(fn, 0600)
     120            nrc = netrc.netrc()
     121            self.assertEqual(nrc.hosts['foo.domain.com'],
     122                             ('bar', None, 'pass'))
     123            os.chmod(fn, 0o622)
     124            self.assertRaises(netrc.NetrcParseError, netrc.netrc)
    43125
    44126def test_main():
Note: See TracChangeset for help on using the changeset viewer.