if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with open(filename, "w") as f:
+ with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
# There really isn't an LWP Cookies 2.0 format, but this indicates
# that there is extra information in here (domain_dot and
# port_spec) while still being compatible with libwww-perl, I hope.
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with open(filename, "w") as f:
+ with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
f.write(NETSCAPE_HEADER_TEXT)
now = time.time()
for cookie in self:
"""Tests for http/cookiejar.py."""
import os
+import stat
+import sys
import re
import test.support
from test.support import os_helper
reach, is_HDN, domain_match, user_domain_match, request_path,
request_port, request_host)
+mswindows = (sys.platform == "win32")
class DateTimeTests(unittest.TestCase):
c = LWPCookieJar()
c.load(filename, ignore_discard=True)
finally:
- try: os.unlink(filename)
- except OSError: pass
+ os_helper.unlink(filename)
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
+ @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
+ @os_helper.skip_unless_working_chmod
+ def test_lwp_filepermissions(self):
+ # Cookie file should only be readable by the creator
+ filename = os_helper.TESTFN
+ c = LWPCookieJar()
+ interact_netscape(c, "http://www.acme.com/", 'boo')
+ try:
+ c.save(filename, ignore_discard=True)
+ st = os.stat(filename)
+ self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
+ finally:
+ os_helper.unlink(filename)
+
+ @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
+ @os_helper.skip_unless_working_chmod
+ def test_mozilla_filepermissions(self):
+ # Cookie file should only be readable by the creator
+ filename = os_helper.TESTFN
+ c = MozillaCookieJar()
+ interact_netscape(c, "http://www.acme.com/", 'boo')
+ try:
+ c.save(filename, ignore_discard=True)
+ st = os.stat(filename)
+ self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
+ finally:
+ os_helper.unlink(filename)
+
def test_bad_magic(self):
# OSErrors (eg. file doesn't exist) are allowed to propagate
filename = os_helper.TESTFN
c = cookiejar_class()
self.assertRaises(LoadError, c.load, filename)
finally:
- try: os.unlink(filename)
- except OSError: pass
+ os_helper.unlink(filename)
class CookieTests(unittest.TestCase):
# XXX
c = MozillaCookieJar(filename)
c.revert(ignore_expires=True, ignore_discard=True)
finally:
- os.unlink(c.filename)
+ os_helper.unlink(c.filename)
# cookies unchanged apart from lost info re. whether path was specified
self.assertEqual(
repr(c),
c = LWPCookieJar(policy=pol)
c.load(filename, ignore_discard=True)
finally:
- try: os.unlink(filename)
- except OSError: pass
+ os_helper.unlink(filename)
self.assertEqual(old, repr(c))
DefaultCookiePolicy(rfc2965=True))
new_c.load(ignore_discard=ignore_discard)
finally:
- try: os.unlink(filename)
- except OSError: pass
+ os_helper.unlink(filename)
return new_c
new_c = save_and_restore(c, True)