Skip to content

Commit be34977

Browse files
committed
run test_security(), test_file_not_found_in_home() and
test_home_not_set() in temporary directory
1 parent cc061d0 commit be34977

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

Lib/test/test_netrc.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -109,62 +109,68 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self):
109109
def test_security(self):
110110
# This test is incomplete since we are normally not run as root and
111111
# therefore can't test the file ownership being wrong.
112-
d = os_helper.TESTFN
113-
os.mkdir(d)
114-
self.addCleanup(os_helper.rmtree, d)
115-
fn = os.path.join(d, '.netrc')
116-
with open(fn, 'wt') as f:
117-
f.write("""\
118-
machine foo.domain.com login bar password pass
119-
default login foo password pass
120-
""")
121-
with os_helper.EnvironmentVarGuard() as environ:
122-
environ.set('HOME', d)
123-
os.chmod(fn, 0o600)
124-
nrc = netrc.netrc()
125-
self.assertEqual(nrc.hosts['foo.domain.com'],
126-
('bar', None, 'pass'))
127-
os.chmod(fn, 0o622)
128-
self.assertRaises(netrc.NetrcParseError, netrc.netrc)
112+
with tempfile.TemporaryDirectory() as tmpdir:
113+
with os_helper.change_cwd(tmpdir):
114+
d = os_helper.TESTFN
115+
os.mkdir(d)
116+
self.addCleanup(os_helper.rmtree, d)
117+
fn = os.path.join(d, '.netrc')
118+
with open(fn, 'wt') as f:
119+
f.write("""\
120+
machine foo.domain.com login bar password pass
121+
default login foo password pass
122+
""")
123+
with os_helper.EnvironmentVarGuard() as environ:
124+
environ.set('HOME', d)
125+
os.chmod(fn, 0o600)
126+
nrc = netrc.netrc()
127+
self.assertEqual(nrc.hosts['foo.domain.com'],
128+
('bar', None, 'pass'))
129+
os.chmod(fn, 0o622)
130+
self.assertRaises(netrc.NetrcParseError, netrc.netrc)
129131

130132
def test_file_not_found_in_home(self):
131-
d = os_helper.TESTFN
132-
os.mkdir(d)
133-
self.addCleanup(os_helper.rmtree, d)
134-
with os_helper.EnvironmentVarGuard() as environ:
135-
environ.set('HOME', d)
136-
self.assertRaises(FileNotFoundError, netrc.netrc)
133+
with tempfile.TemporaryDirectory() as tmpdir:
134+
with os_helper.change_cwd(tmpdir):
135+
d = os_helper.TESTFN
136+
os.mkdir(d)
137+
self.addCleanup(os_helper.rmtree, d)
138+
with os_helper.EnvironmentVarGuard() as environ:
139+
environ.set('HOME', d)
140+
self.assertRaises(FileNotFoundError, netrc.netrc)
137141

138142
def test_file_not_found_explicit(self):
139143
self.assertRaises(FileNotFoundError, netrc.netrc,
140144
file='unlikely_netrc')
141145

142146
def test_home_not_set(self):
143-
fake_home = os_helper.TESTFN
144-
os.mkdir(fake_home)
145-
self.addCleanup(os_helper.rmtree, fake_home)
146-
fake_netrc_path = os.path.join(fake_home, '.netrc')
147-
with open(fake_netrc_path, 'w') as f:
148-
f.write('machine foo.domain.com login bar password pass')
149-
os.chmod(fake_netrc_path, 0o600)
150-
151-
orig_expanduser = os.path.expanduser
152-
called = []
153-
154-
def fake_expanduser(s):
155-
called.append(s)
156-
with os_helper.EnvironmentVarGuard() as environ:
157-
environ.set('HOME', fake_home)
158-
environ.set('USERPROFILE', fake_home)
159-
result = orig_expanduser(s)
160-
return result
161-
162-
with support.swap_attr(os.path, 'expanduser', fake_expanduser):
163-
nrc = netrc.netrc()
164-
login, account, password = nrc.authenticators('foo.domain.com')
165-
self.assertEqual(login, 'bar')
166-
167-
self.assertTrue(called)
147+
with tempfile.TemporaryDirectory() as tmpdir:
148+
with os_helper.change_cwd(tmpdir):
149+
fake_home = os_helper.TESTFN
150+
os.mkdir(fake_home)
151+
self.addCleanup(os_helper.rmtree, fake_home)
152+
fake_netrc_path = os.path.join(fake_home, '.netrc')
153+
with open(fake_netrc_path, 'w') as f:
154+
f.write('machine foo.domain.com login bar password pass')
155+
os.chmod(fake_netrc_path, 0o600)
156+
157+
orig_expanduser = os.path.expanduser
158+
called = []
159+
160+
def fake_expanduser(s):
161+
called.append(s)
162+
with os_helper.EnvironmentVarGuard() as environ:
163+
environ.set('HOME', fake_home)
164+
environ.set('USERPROFILE', fake_home)
165+
result = orig_expanduser(s)
166+
return result
167+
168+
with support.swap_attr(os.path, 'expanduser', fake_expanduser):
169+
nrc = netrc.netrc()
170+
login, account, password = nrc.authenticators('foo.domain.com')
171+
self.assertEqual(login, 'bar')
172+
173+
self.assertTrue(called)
168174

169175

170176
if __name__ == "__main__":

0 commit comments

Comments
 (0)