(self)
| 278 | @unittest.skipUnless(hasattr(os, 'getuid'), "os.getuid is required") |
| 279 | @os_helper.skip_unless_working_chmod |
| 280 | def test_security(self): |
| 281 | # This test is incomplete since we are normally not run as root and |
| 282 | # therefore can't test the file ownership being wrong. |
| 283 | d = os_helper.TESTFN |
| 284 | os.mkdir(d) |
| 285 | self.addCleanup(os_helper.rmtree, d) |
| 286 | fn = os.path.join(d, '.netrc') |
| 287 | with open(fn, 'wt') as f: |
| 288 | f.write("""\ |
| 289 | machine foo.domain.com login bar password pass |
| 290 | default login foo password pass |
| 291 | """) |
| 292 | with os_helper.EnvironmentVarGuard() as environ: |
| 293 | environ.set('HOME', d) |
| 294 | os.chmod(fn, 0o600) |
| 295 | nrc = netrc.netrc() |
| 296 | self.assertEqual(nrc.hosts['foo.domain.com'], |
| 297 | ('bar', '', 'pass')) |
| 298 | os.chmod(fn, 0o622) |
| 299 | self.assertRaises(netrc.NetrcParseError, netrc.netrc) |
| 300 | with open(fn, 'wt') as f: |
| 301 | f.write("""\ |
| 302 | machine foo.domain.com login anonymous password pass |
| 303 | default login foo password pass |
| 304 | """) |
| 305 | with os_helper.EnvironmentVarGuard() as environ: |
| 306 | environ.set('HOME', d) |
| 307 | os.chmod(fn, 0o600) |
| 308 | nrc = netrc.netrc() |
| 309 | self.assertEqual(nrc.hosts['foo.domain.com'], |
| 310 | ('anonymous', '', 'pass')) |
| 311 | os.chmod(fn, 0o622) |
| 312 | self.assertEqual(nrc.hosts['foo.domain.com'], |
| 313 | ('anonymous', '', 'pass')) |
| 314 | |
| 315 | @unittest.skipUnless(os.name == 'posix', 'POSIX only test') |
| 316 | @unittest.skipUnless(hasattr(os, 'getuid'), "os.getuid is required") |
nothing calls this directly
no test coverage detected