2 import os, stat, sys, time
3 from twisted.trial import unittest
4 from twisted.internet import defer
5 from twisted.python import log
7 from foolscap.eventual import flushEventualQueue
8 from twisted.application import service
9 from allmydata.node import Node, formatTimeTahoeStyle
10 from allmydata.util import fileutil
11 import common_util as testutil
13 class LoggingMultiService(service.MultiService):
14 def log(self, msg, **kw):
18 CERTFILE='DEFAULT_CERTFILE_BLANK'
19 PORTNUMFILE='DEFAULT_PORTNUMFILE_BLANK'
21 class TestCase(unittest.TestCase, testutil.SignalMixin):
23 self.parent = LoggingMultiService()
24 self.parent.startService()
26 log.msg("%s.tearDown" % self.__class__.__name__)
27 d = defer.succeed(None)
28 d.addCallback(lambda res: self.parent.stopService())
29 d.addCallback(flushEventualQueue)
32 def test_location(self):
33 basedir = "test_node/test_location"
34 fileutil.make_dirs(basedir)
35 f = open(os.path.join(basedir, 'tahoe.cfg'), 'wt')
37 f.write("tub.location = 1.2.3.4:5\n")
41 n.setServiceParent(self.parent)
42 d = n.when_tub_ready()
44 def _check_addresses(ignored_result):
45 furl = n.tub.registerReference(n)
46 self.failUnless("1.2.3.4:5" in furl, furl)
48 d.addCallback(_check_addresses)
51 def test_location2(self):
52 basedir = "test_node/test_location2"
53 fileutil.make_dirs(basedir)
54 f = open(os.path.join(basedir, 'tahoe.cfg'), 'wt')
56 f.write("tub.location = 1.2.3.4:5,example.org:8091\n")
60 n.setServiceParent(self.parent)
61 d = n.when_tub_ready()
63 def _check_addresses(ignored_result):
64 furl = n.tub.registerReference(n)
65 self.failUnless("1.2.3.4:5" in furl, furl)
66 self.failUnless("example.org:8091" in furl, furl)
68 d.addCallback(_check_addresses)
71 def test_timestamp(self):
72 # this modified logger doesn't seem to get used during the tests,
73 # probably because we don't modify the LogObserver that trial
74 # installs (only the one that twistd installs). So manually exercise
76 t = formatTimeTahoeStyle("ignored", time.time())
77 self.failUnless("Z" in t)
78 t2 = formatTimeTahoeStyle("ignored", int(time.time()))
79 self.failUnless("Z" in t2)
81 def test_secrets_dir(self):
82 basedir = "test_node/test_secrets_dir"
83 fileutil.make_dirs(basedir)
85 self.failUnless(os.path.exists(os.path.join(basedir, "private")))
87 def test_secrets_dir_protected(self):
88 if "win32" in sys.platform.lower() or "cygwin" in sys.platform.lower():
89 # We don't know how to test that unprivileged users can't read this
90 # thing. (Also we don't know exactly how to set the permissions so
91 # that unprivileged users can't read this thing.)
92 raise unittest.SkipTest("We don't know how to set permissions on Windows.")
93 basedir = "test_node/test_secrets_dir_protected"
94 fileutil.make_dirs(basedir)
96 privdir = os.path.join(basedir, "private")
98 bits = stat.S_IMODE(st[stat.ST_MODE])
99 self.failUnless(bits & 0001 == 0, bits)