Example #1
0
    def test_furl(self):
        basedir = "introducer.IntroducerNode.test_furl"
        os.mkdir(basedir)
        public_fn = os.path.join(basedir, "introducer.furl")
        private_fn = os.path.join(basedir, "private", "introducer.furl")

        q1 = create_introducer(basedir)
        del q1
        # new nodes create unguessable furls in private/introducer.furl
        ifurl = fileutil.read(private_fn)
        self.failUnless(ifurl)
        ifurl = ifurl.strip()
        self.failIf(ifurl.endswith("/introducer"), ifurl)

        # old nodes created guessable furls in BASEDIR/introducer.furl
        guessable = ifurl[:ifurl.rfind("/")] + "/introducer"
        fileutil.write(public_fn, guessable + "\n", mode="w")  # text

        # if we see both files, throw an error
        self.failUnlessRaises(FurlFileConflictError, create_introducer,
                              basedir)

        # when we see only the public one, move it to private/ and use
        # the existing furl instead of creating a new one
        os.unlink(private_fn)

        q2 = create_introducer(basedir)
        del q2
        self.failIf(os.path.exists(public_fn))
        ifurl2 = fileutil.read(private_fn)
        self.failUnless(ifurl2)
        self.failUnlessEqual(ifurl2.strip(), guessable)
Example #2
0
    def test_replace_file(self):
        workdir = fileutil.abspath_expanduser_unicode(u"test_replace_file")
        fileutil.make_dirs(workdir)

        replaced_path = os.path.join(workdir, "replaced")
        replacement_path = os.path.join(workdir, "replacement")

        # when none of the files exist
        self.failUnlessRaises(fileutil.ConflictError, fileutil.replace_file,
                              replaced_path, replacement_path)

        # when only replaced exists
        fileutil.write(replaced_path, b"foo")
        self.failUnlessRaises(fileutil.ConflictError, fileutil.replace_file,
                              replaced_path, replacement_path)
        self.failUnlessEqual(fileutil.read(replaced_path), b"foo")

        # when both replaced and replacement exist
        fileutil.write(replacement_path, b"bar")
        fileutil.replace_file(replaced_path, replacement_path)
        self.failUnlessEqual(fileutil.read(replaced_path), b"bar")
        self.failIf(os.path.exists(replacement_path))

        # when only replacement exists
        os.remove(replaced_path)
        fileutil.write(replacement_path, b"bar")
        fileutil.replace_file(replaced_path, replacement_path)
        self.failUnlessEqual(fileutil.read(replaced_path), b"bar")
        self.failIf(os.path.exists(replacement_path))
Example #3
0
    def test_rename_no_overwrite(self):
        workdir = fileutil.abspath_expanduser_unicode(
            u"test_rename_no_overwrite")
        fileutil.make_dirs(workdir)

        source_path = os.path.join(workdir, "source")
        dest_path = os.path.join(workdir, "dest")

        # when neither file exists
        self.failUnlessRaises(OSError, fileutil.rename_no_overwrite,
                              source_path, dest_path)

        # when only dest exists
        fileutil.write(dest_path, b"dest")
        self.failUnlessRaises(OSError, fileutil.rename_no_overwrite,
                              source_path, dest_path)
        self.failUnlessEqual(fileutil.read(dest_path), b"dest")

        # when both exist
        fileutil.write(source_path, b"source")
        self.failUnlessRaises(OSError, fileutil.rename_no_overwrite,
                              source_path, dest_path)
        self.failUnlessEqual(fileutil.read(source_path), b"source")
        self.failUnlessEqual(fileutil.read(dest_path), b"dest")

        # when only source exists
        os.remove(dest_path)
        fileutil.rename_no_overwrite(source_path, dest_path)
        self.failUnlessEqual(fileutil.read(dest_path), b"source")
        self.failIf(os.path.exists(source_path))
    def test_furl(self):
        basedir = "introducer.IntroducerNode.test_furl"
        os.mkdir(basedir)
        public_fn = os.path.join(basedir, "introducer.furl")
        private_fn = os.path.join(basedir, "private", "introducer.furl")

        q1 = IntroducerNode(basedir)
        del q1
        # new nodes create unguessable furls in private/introducer.furl
        ifurl = fileutil.read(private_fn)
        self.failUnless(ifurl)
        ifurl = ifurl.strip()
        self.failIf(ifurl.endswith("/introducer"), ifurl)

        # old nodes created guessable furls in BASEDIR/introducer.furl
        guessable = ifurl[:ifurl.rfind("/")] + "/introducer"
        fileutil.write(public_fn, guessable+"\n", mode="w") # text

        # if we see both files, throw an error
        self.failUnlessRaises(FurlFileConflictError,
                              IntroducerNode, basedir)

        # when we see only the public one, move it to private/ and use
        # the existing furl instead of creating a new one
        os.unlink(private_fn)

        q2 = IntroducerNode(basedir)
        del q2
        self.failIf(os.path.exists(public_fn))
        ifurl2 = fileutil.read(private_fn)
        self.failUnless(ifurl2)
        self.failUnlessEqual(ifurl2.strip(), guessable)
Example #5
0
 def check_config(self, client_num, local_dir):
     client_config = fileutil.read(os.path.join(self.get_clientdir(i=client_num), "tahoe.cfg"))
     mf_yaml = fileutil.read(os.path.join(self.get_clientdir(i=client_num), "private", "magic_folders.yaml"))
     local_dir_utf8 = local_dir.encode('utf-8')
     magic_folder_config = "[magic_folder]\nenabled = True"
     self.failUnlessIn(magic_folder_config, client_config)
     self.failUnlessIn(local_dir_utf8, mf_yaml)
Example #6
0
def _upgrade_magic_folder_config(basedir):
    """
    Helper that upgrades from single-magic-folder-only configs to
    multiple magic-folder configuration style (in YAML)
    """
    config_fname = os.path.join(basedir, "tahoe.cfg")
    config = configutil.get_config(config_fname)

    collective_fname = os.path.join(basedir, "private", "collective_dircap")
    upload_fname = os.path.join(basedir, "private", "magic_folder_dircap")
    magic_folders = {
        u"default": {
            u"directory": config.get("magic_folder", "local.directory").decode("utf-8"),
            u"collective_dircap": fileutil.read(collective_fname),
            u"upload_dircap": fileutil.read(upload_fname),
            u"poll_interval": int(config.get("magic_folder", "poll_interval")),
        },
    }
    fileutil.move_into_place(
        source=os.path.join(basedir, "private", "magicfolderdb.sqlite"),
        dest=os.path.join(basedir, "private", "magicfolder_default.sqlite"),
    )
    save_magic_folders(basedir, magic_folders)
    config.remove_option("magic_folder", "local.directory")
    config.remove_option("magic_folder", "poll_interval")
    configutil.write_config(os.path.join(basedir, 'tahoe.cfg'), config)
    fileutil.remove_if_possible(collective_fname)
    fileutil.remove_if_possible(upload_fname)
Example #7
0
 def get_caps_from_files(self, client_num):
     collective_dircap = fileutil.read(os.path.join(self.get_clientdir(i=client_num),
                                                    u"private", u"collective_dircap"))
     upload_dircap = fileutil.read(os.path.join(self.get_clientdir(i=client_num),
                                                u"private", u"magic_folder_dircap"))
     self.failIf(collective_dircap is None or upload_dircap is None)
     return collective_dircap, upload_dircap
Example #8
0
 def test_write_atomically(self):
     basedir = "util/FileUtil/test_write_atomically"
     fileutil.make_dirs(basedir)
     fn = os.path.join(basedir, "here")
     fileutil.write_atomically(fn, b"one", "b")
     self.failUnlessEqual(fileutil.read(fn), b"one")
     fileutil.write_atomically(fn, u"two", mode="")  # non-binary
     self.failUnlessEqual(fileutil.read(fn), b"two")
Example #9
0
 def check_config(self, client_num, local_dir):
     client_config = fileutil.read(
         os.path.join(self.get_clientdir(i=client_num), "tahoe.cfg"))
     mf_yaml = fileutil.read(
         os.path.join(self.get_clientdir(i=client_num), "private",
                      "magic_folders.yaml"))
     local_dir_utf8 = local_dir.encode('utf-8')
     magic_folder_config = "[magic_folder]\nenabled = True"
     self.failUnlessIn(magic_folder_config, client_config)
     self.failUnlessIn(local_dir_utf8, mf_yaml)
Example #10
0
        def _started(res):
            # read the introducer.furl and introducer.port files so we can check that their
            # contents don't change on restart
            self.furl = fileutil.read(INTRODUCER_FURL_FILE)
            self.failUnless(os.path.exists(PORTNUM_FILE))
            self.portnum = fileutil.read(PORTNUM_FILE)

            fileutil.write(HOTLINE_FILE, "")
            self.failUnless(os.path.exists(TWISTD_PID_FILE))
            self.failUnless(os.path.exists(NODE_URL_FILE))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(NODE_URL_FILE)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #11
0
        def _started(res):
            # read the introducer.furl and introducer.port files so we can
            # check that their contents don't change on restart
            self.furl = fileutil.read(introducer_furl_file)
            self.failUnless(os.path.exists(portnum_file))
            self.portnum = fileutil.read(portnum_file)

            fileutil.write(exit_trigger_file, "")
            self.failUnless(os.path.exists(twistd_pid_file))
            self.failUnless(os.path.exists(node_url_file))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(node_url_file)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #12
0
        def _started(res):
            # read the introducer.furl and introducer.port files so we can
            # check that their contents don't change on restart
            self.furl = fileutil.read(introducer_furl_file)
            self.failUnless(os.path.exists(portnum_file))
            self.portnum = fileutil.read(portnum_file)

            fileutil.write(exit_trigger_file, "")
            self.failUnless(os.path.exists(twistd_pid_file))
            self.failUnless(os.path.exists(node_url_file))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(node_url_file)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #13
0
        def _started(res):
            # read the introducer.furl and introducer.port files so we can check that their
            # contents don't change on restart
            self.furl = fileutil.read(INTRODUCER_FURL_FILE)
            self.failUnless(os.path.exists(PORTNUM_FILE))
            self.portnum = fileutil.read(PORTNUM_FILE)

            fileutil.write(HOTLINE_FILE, "")
            self.failUnless(os.path.exists(TWISTD_PID_FILE))
            self.failUnless(os.path.exists(NODE_URL_FILE))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(NODE_URL_FILE)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #14
0
    def test_create_long_path(self):
        """
        Even for paths with total length greater than 260 bytes,
        ``fileutil.abspath_expanduser_unicode`` produces a path on which other
        path-related APIs can operate.

        https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
        documents certain Windows-specific path length limitations this test
        is specifically intended to demonstrate can be overcome.
        """
        workdir = u"test_create_long_path"
        fileutil.make_dirs(workdir)
        base_path = fileutil.abspath_expanduser_unicode(workdir)
        base_length = len(base_path)

        # Construct a path /just/ long enough to exercise the important case.
        # It would be nice if we could just use a seemingly globally valid
        # long file name (the `x...` portion) here - for example, a name 255
        # bytes long- and a previous version of this test did just that.
        # However, aufs imposes a 242 byte length limit on file names.  Most
        # other POSIX filesystems do allow names up to 255 bytes.  It's not
        # clear there's anything we can *do* about lower limits, though, and
        # POSIX.1-2017 (and earlier) only requires that the maximum be at
        # least 14 (!!!)  bytes.
        long_path = os.path.join(base_path, u'x' * (261 - base_length))

        def _cleanup():
            fileutil.remove(long_path)
        self.addCleanup(_cleanup)

        fileutil.write(long_path, b"test")
        self.failUnless(os.path.exists(long_path))
        self.failUnlessEqual(fileutil.read(long_path), b"test")
        _cleanup()
        self.failIf(os.path.exists(long_path))
 def _remove_trailing_newline_and_create_alias(ign):
     # ticket #741 is about a manually-edited alias file (which
     # doesn't end in a newline) being corrupted by a subsequent
     # "tahoe create-alias"
     old = fileutil.read(aliasfile)
     fileutil.write(aliasfile, old.rstrip())
     return self.do_cli("create-alias", "un-corrupted1")
Example #16
0
 def check_config(self, client_num, local_dir):
     client_config = fileutil.read(
         os.path.join(self.get_clientdir(i=client_num), "tahoe.cfg"))
     local_dir_utf8 = local_dir.encode('utf-8')
     magic_folder_config = "[magic_folder]\nenabled = True\nlocal.directory = %s" % (
         local_dir_utf8, )
     self.failUnlessIn(magic_folder_config, client_config)
Example #17
0
    def read_config(self):
        self.error_about_old_config_files()
        self.config = ConfigParser.SafeConfigParser()

        tahoe_cfg = os.path.join(self.basedir, "tahoe.cfg")
        try:
            f = open(tahoe_cfg, "rb")
            try:
                # Skip any initial Byte Order Mark. Since this is an ordinary file, we
                # don't need to handle incomplete reads, and can assume seekability.
                if f.read(3) != '\xEF\xBB\xBF':
                    f.seek(0)
                self.config.readfp(f)
            finally:
                f.close()
        except EnvironmentError:
            if os.path.exists(tahoe_cfg):
                raise

        cfg_tubport = self.get_config("node", "tub.port", "")
        if not cfg_tubport:
            # For 'tub.port', tahoe.cfg overrides the individual file on
            # disk. So only read self._portnumfile if tahoe.cfg doesn't
            # provide a value.
            try:
                file_tubport = fileutil.read(self._portnumfile).strip()
                self.set_config("node", "tub.port", file_tubport)
            except EnvironmentError:
                if os.path.exists(self._portnumfile):
                    raise
Example #18
0
    def get_or_create_private_config(self, name, default=_None):
        """Try to get the (string) contents of a private config file (which
        is a config file that resides within the subdirectory named
        'private'), and return it. Any leading or trailing whitespace will be
        stripped from the data.

        If the file does not exist, and default is not given, report an error.
        If the file does not exist and a default is specified, try to create
        it using that default, and then return the value that was written.
        If 'default' is a string, use it as a default value. If not, treat it
        as a zero-argument callable that is expected to return a string.
        """
        privname = os.path.join(self._basedir, "private", name)
        try:
            value = fileutil.read(privname, mode="r")
        except EnvironmentError as e:
            if e.errno != errno.ENOENT:
                raise  # we only care about "file doesn't exist"
            if default is _None:
                raise MissingConfigEntry(
                    "The required configuration file %s is missing." %
                    (quote_output(privname), ))
            if isinstance(default, bytes):
                default = str(default, "utf-8")
            if isinstance(default, str):
                value = default
            else:
                value = default()
            fileutil.write(privname, value)
        return value.strip()
Example #19
0
    def get_or_create_private_config(self, name, default=_None):
        """Try to get the (string) contents of a private config file (which
        is a config file that resides within the subdirectory named
        'private'), and return it. Any leading or trailing whitespace will be
        stripped from the data.

        If the file does not exist, and default is not given, report an error.
        If the file does not exist and a default is specified, try to create
        it using that default, and then return the value that was written.
        If 'default' is a string, use it as a default value. If not, treat it
        as a zero-argument callable that is expected to return a string.
        """
        privname = os.path.join(self.basedir, "private", name)
        try:
            value = fileutil.read(privname)
        except EnvironmentError:
            if os.path.exists(privname):
                raise
            if default is _None:
                raise MissingConfigEntry("The required configuration file %s is missing."
                                         % (quote_output(privname),))
            if isinstance(default, basestring):
                value = default
            else:
                value = default()
            fileutil.write(privname, value)
        return value.strip()
Example #20
0
    def check_joined_config(self, client_num, upload_dircap):
        """Tests that our collective directory has the readonly cap of
        our upload directory.
        """
        collective_readonly_cap = fileutil.read(
            os.path.join(self.get_clientdir(i=client_num), u"private",
                         u"collective_dircap"))
        d = self.do_cli("ls",
                        "--json",
                        collective_readonly_cap,
                        client_num=client_num)

        def _done((rc, stdout, stderr)):
            self.failUnlessEqual(rc, 0)
            return (rc, stdout, stderr)

        d.addCallback(_done)

        def test_joined_magic_folder((rc, stdout, stderr)):
            readonly_cap = unicode(
                uri.from_string(upload_dircap).get_readonly().to_string(),
                'utf-8')
            s = re.search(readonly_cap, stdout)
            self.failUnless(s is not None)
            return None

        d.addCallback(test_joined_magic_folder)
        return d
Example #21
0
 def _remove_trailing_newline_and_create_alias(ign):
     # ticket #741 is about a manually-edited alias file (which
     # doesn't end in a newline) being corrupted by a subsequent
     # "tahoe create-alias"
     old = fileutil.read(aliasfile)
     fileutil.write(aliasfile, old.rstrip())
     return self.do_cli("create-alias", "un-corrupted1")
Example #22
0
 def _stash_urls(res):
     aliases = get_aliases(self.get_clientdir())
     node_url_file = os.path.join(self.get_clientdir(), "node.url")
     nodeurl = fileutil.read(node_url_file).strip()
     self.welcome_url = nodeurl
     uribase = nodeurl + "uri/"
     self.tahoe_url = uribase + urllib.quote(aliases["tahoe"])
     self.tahoe_subdir_url = self.tahoe_url + "/subdir"
     self.two_url = uribase + urllib.quote(aliases["two"])
     self.two_uri = aliases["two"]
 def _stash_urls(res):
     aliases = get_aliases(self.get_clientdir())
     node_url_file = os.path.join(self.get_clientdir(), "node.url")
     nodeurl = fileutil.read(node_url_file).strip()
     self.welcome_url = nodeurl
     uribase = nodeurl + "uri/"
     self.tahoe_url = uribase + urllib.quote(aliases["tahoe"])
     self.tahoe_subdir_url = self.tahoe_url + "/subdir"
     self.two_url = uribase + urllib.quote(aliases["two"])
     self.two_uri = aliases["two"]
Example #24
0
        def _started(res):
            # read the storage.furl file so we can check that its contents
            # don't change on restart
            self.storage_furl = fileutil.read(storage_furl_file)

            fileutil.write(exit_trigger_file, "")
            self.failUnless(os.path.exists(twistd_pid_file))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(node_url_file)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #25
0
        def _started(res):
            # read the storage.furl file so we can check that its contents
            # don't change on restart
            self.storage_furl = fileutil.read(storage_furl_file)

            fileutil.write(exit_trigger_file, "")
            self.failUnless(os.path.exists(twistd_pid_file))

            # rm this so we can detect when the second incarnation is ready
            os.unlink(node_url_file)
            return self.run_bintahoe(["--quiet", "restart", c1])
Example #26
0
        def _cb(res):
            out, err, rc_or_sig = res
            self.failUnlessEqual(rc_or_sig, 0)

            # Check that the --webport option worked.
            config = fileutil.read(config_file)
            self.failUnlessIn('\nweb.port = 0\n', config)

            # By writing this file, we get two minutes before the client will exit. This ensures
            # that even if the 'stop' command doesn't work (and the test fails), the client should
            # still terminate.
            fileutil.write(exit_trigger_file, "")
Example #27
0
 def get_config_from_file(self, name, required=False):
     """Get the (string) contents of a config file, or None if the file
     did not exist. If required=True, raise an exception rather than
     returning None. Any leading or trailing whitespace will be stripped
     from the data."""
     fn = os.path.join(self.basedir, name)
     try:
         return fileutil.read(fn).strip()
     except EnvironmentError:
         if not required:
             return None
         raise
Example #28
0
        def _cb(res):
            out, err, rc_or_sig = res
            self.failUnlessEqual(rc_or_sig, 0)

            # Check that the --webport option worked.
            config = fileutil.read(config_file)
            self.failUnlessIn('\nweb.port = 0\n', config)

            # By writing this file, we get two minutes before the client will exit. This ensures
            # that even if the 'stop' command doesn't work (and the test fails), the client should
            # still terminate.
            fileutil.write(exit_trigger_file, "")
Example #29
0
 def get_config_from_file(self, name, required=False):
     """Get the (string) contents of a config file, or None if the file
     did not exist. If required=True, raise an exception rather than
     returning None. Any leading or trailing whitespace will be stripped
     from the data."""
     fn = os.path.join(self.basedir, name)
     try:
         return fileutil.read(fn).strip()
     except EnvironmentError:
         if not required:
             return None
         raise
Example #30
0
 def get_tub_port(self):
     # return a descriptor string
     cfg_tubport = self.get_config("node", "tub.port", "")
     if cfg_tubport:
         return self._convert_tub_port(cfg_tubport)
     # For 'tub.port', tahoe.cfg overrides the individual file on disk. So
     # only read self._portnumfile if tahoe.cfg doesn't provide a value.
     if os.path.exists(self._portnumfile):
         file_tubport = fileutil.read(self._portnumfile).strip()
         return self._convert_tub_port(file_tubport)
     tubport = "tcp:%d" % iputil.allocate_tcp_port()
     fileutil.write_atomically(self._portnumfile, tubport + "\n", mode="")
     return tubport
Example #31
0
 def get_tub_port(self):
     # return a descriptor string
     cfg_tubport = self.get_config("node", "tub.port", "")
     if cfg_tubport:
         return self._convert_tub_port(cfg_tubport)
     # For 'tub.port', tahoe.cfg overrides the individual file on disk. So
     # only read self._portnumfile if tahoe.cfg doesn't provide a value.
     if os.path.exists(self._portnumfile):
         file_tubport = fileutil.read(self._portnumfile).strip()
         return self._convert_tub_port(file_tubport)
     tubport = "tcp:%d" % iputil.allocate_tcp_port()
     fileutil.write_atomically(self._portnumfile, tubport + "\n", mode="")
     return tubport
Example #32
0
        def _cb(res):
            out, err, rc_or_sig = res
            self.failUnlessEqual(rc_or_sig, 0)

            # This makes sure that node.url is written, which allows us to
            # detect when the introducer restarts in _node_has_restarted below.
            config = fileutil.read(config_file)
            self.failUnlessIn('\nweb.port = \n', config)
            fileutil.write(config_file, config.replace('\nweb.port = \n', '\nweb.port = 0\n'))

            # by writing this file, we get ten seconds before the node will
            # exit. This insures that even if the test fails (and the 'stop'
            # command doesn't work), the client should still terminate.
            fileutil.write(exit_trigger_file, "")
Example #33
0
        def _cb(res):
            out, err, rc_or_sig = res
            self.failUnlessEqual(rc_or_sig, 0)

            # This makes sure that node.url is written, which allows us to
            # detect when the introducer restarts in _node_has_restarted below.
            config = fileutil.read(config_file)
            self.failUnlessIn('\nweb.port = \n', config)
            fileutil.write(config_file, config.replace('\nweb.port = \n', '\nweb.port = 0\n'))

            # by writing this file, we get ten seconds before the node will
            # exit. This insures that even if the test fails (and the 'stop'
            # command doesn't work), the client should still terminate.
            fileutil.write(exit_trigger_file, "")
Example #34
0
    def parseArgs(self, alias, nickname=None, local_dir=None):
        BasedirOptions.parseArgs(self)
        alias = argv_to_unicode(alias)
        if not alias.endswith(u':'):
            raise usage.UsageError("An alias must end with a ':' character.")
        self.alias = alias[:-1]
        self.nickname = None if nickname is None else argv_to_unicode(nickname)

        # Expand the path relative to the current directory of the CLI command, not the node.
        self.local_dir = None if local_dir is None else argv_to_abspath(local_dir, long_path=False)

        if self.nickname and not self.local_dir:
            raise usage.UsageError("If NICKNAME is specified then LOCAL_DIR must also be specified.")
        node_url_file = os.path.join(self['node-directory'], u"node.url")
        self['node-url'] = fileutil.read(node_url_file).strip()
Example #35
0
 def get_private_config(self, name, default=_None):
     """Read the (string) contents of a private config file (which is a
     config file that resides within the subdirectory named 'private'),
     and return it. Return a default, or raise an error if one was not
     given.
     """
     privname = os.path.join(self.basedir, "private", name)
     try:
         return fileutil.read(privname)
     except EnvironmentError:
         if os.path.exists(privname):
             raise
         if default is _None:
             raise MissingConfigEntry("The required configuration file %s is missing." % (quote_output(privname),))
         return default
Example #36
0
    def read_config(self):
        self.error_about_old_config_files()
        self.config = ConfigParser.SafeConfigParser()
        self.config.read([os.path.join(self.basedir, "tahoe.cfg")])

        cfg_tubport = self.get_config("node", "tub.port", "")
        if not cfg_tubport:
            # For 'tub.port', tahoe.cfg overrides the individual file on
            # disk. So only read self._portnumfile if tahoe.cfg doesn't
            # provide a value.
            try:
                file_tubport = fileutil.read(self._portnumfile).strip()
                self.set_config("node", "tub.port", file_tubport)
            except EnvironmentError:
                if os.path.exists(self._portnumfile):
                    raise
Example #37
0
 def get_private_config(self, name, default=_None):
     """Read the (native string) contents of a private config file (a
     config file that resides within the subdirectory named 'private'),
     and return it. Return a default, or raise an error if one was not
     given.
     """
     privname = os.path.join(self._basedir, "private", name)
     try:
         return fileutil.read(privname, mode="r").strip()
     except EnvironmentError as e:
         if e.errno != errno.ENOENT:
             raise  # we only care about "file doesn't exist"
         if default is _None:
             raise MissingConfigEntry("The required configuration file %s is missing."
                                      % (quote_output(privname),))
         return default
Example #38
0
 def get_private_config(self, name, default=_None):
     """Read the (string) contents of a private config file (which is a
     config file that resides within the subdirectory named 'private'),
     and return it. Return a default, or raise an error if one was not
     given.
     """
     privname = os.path.join(self.basedir, "private", name)
     try:
         return fileutil.read(privname)
     except EnvironmentError:
         if os.path.exists(privname):
             raise
         if default is _None:
             raise MissingConfigEntry("The required configuration file %s is missing."
                                      % (quote_output(privname),))
         return default
Example #39
0
    def read_config(self):
        self.error_about_old_config_files()
        self.config = ConfigParser.SafeConfigParser()
        self.config.read([os.path.join(self.basedir, "tahoe.cfg")])

        cfg_tubport = self.get_config("node", "tub.port", "")
        if not cfg_tubport:
            # For 'tub.port', tahoe.cfg overrides the individual file on
            # disk. So only read self._portnumfile if tahoe.cfg doesn't
            # provide a value.
            try:
                file_tubport = fileutil.read(self._portnumfile).strip()
                self.set_config("node", "tub.port", file_tubport)
            except EnvironmentError:
                if os.path.exists(self._portnumfile):
                    raise
Example #40
0
 def check_output(self):
     # locate the files and directories created (if any) under to/
     top = os.path.join(self.basedir, "to")
     results = set()
     for (dirpath, dirnames, filenames) in os.walk(top):
         assert dirpath.startswith(top)
         here = "/".join(dirpath.split(os.sep)[len(top.split(os.sep))-1:])
         results.add(here+"/")
         for fn in filenames:
             contents = fileutil.read(os.path.join(dirpath, fn))
             if contents == self.FILE_CONTENTS:
                 results.add("%s/%s" % (here, fn))
             elif contents == self.FILE_CONTENTS_5:
                 results.add("%s/%s=5" % (here, fn))
             elif contents == self.FILE_CONTENTS_6:
                 results.add("%s/%s=6" % (here, fn))
     return results
 def check_output(self):
     # locate the files and directories created (if any) under to/
     top = os.path.join(self.basedir, "to")
     results = set()
     for (dirpath, dirnames, filenames) in os.walk(top):
         assert dirpath.startswith(top)
         here = "/".join(dirpath.split(os.sep)[len(top.split(os.sep))-1:])
         results.add(here+"/")
         for fn in filenames:
             contents = fileutil.read(os.path.join(dirpath, fn))
             if contents == self.FILE_CONTENTS:
                 results.add("%s/%s" % (here, fn))
             elif contents == self.FILE_CONTENTS_5:
                 results.add("%s/%s=5" % (here, fn))
             elif contents == self.FILE_CONTENTS_6:
                 results.add("%s/%s=6" % (here, fn))
     return results
 def check_joined_config(self, client_num, upload_dircap):
     """Tests that our collective directory has the readonly cap of
     our upload directory.
     """
     collective_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(i=client_num),
                                                          u"private", u"collective_dircap"))
     d = self.do_cli("ls", "--json", collective_readonly_cap, client_num=client_num)
     def _done((rc, stdout, stderr)):
         self.failUnlessEqual(rc, 0)
         return (rc, stdout, stderr)
     d.addCallback(_done)
     def test_joined_magic_folder((rc,stdout,stderr)):
         readonly_cap = unicode(uri.from_string(upload_dircap).get_readonly().to_string(), 'utf-8')
         s = re.search(readonly_cap, stdout)
         self.failUnless(s is not None)
         return None
     d.addCallback(test_joined_magic_folder)
     return d
Example #43
0
    def get_or_create_private_config(self, name, default):
        """Try to get the (string) contents of a private config file (which
        is a config file that resides within the subdirectory named
        'private'), and return it. Any leading or trailing whitespace will be
        stripped from the data.

        If the file does not exist, try to create it using default, and
        then return the value that was written. If 'default' is a string,
        use it as a default value. If not, treat it as a 0-argument callable
        which is expected to return a string.
        """
        privname = os.path.join(self.basedir, "private", name)
        try:
            value = fileutil.read(privname)
        except EnvironmentError:
            if isinstance(default, basestring):
                value = default
            else:
                value = default()
            fileutil.write(privname, value)
        return value.strip()
Example #44
0
def _tub_portlocation(config, get_local_addresses_sync, allocate_tcp_port):
    """
    Figure out the network location of the main tub for some configuration.

    :param get_local_addresses_sync: A function like
        ``iputil.get_local_addresses_sync``.

    :param allocate_tcp_port: A function like ``iputil.allocate_tcp_port``.

    :returns: None or tuple of (port, location) for the main tub based
        on the given configuration. May raise ValueError or PrivacyError
        if there are problems with the config
    """
    cfg_tubport = config.get_config("node", "tub.port", None)
    cfg_location = config.get_config("node", "tub.location", None)
    reveal_ip = config.get_config("node",
                                  "reveal-IP-address",
                                  True,
                                  boolean=True)
    tubport_disabled = False

    if cfg_tubport is not None:
        cfg_tubport = cfg_tubport.strip()
        if cfg_tubport == "":
            raise ValueError("tub.port must not be empty")
        if cfg_tubport == "disabled":
            tubport_disabled = True

    location_disabled = False
    if cfg_location is not None:
        cfg_location = cfg_location.strip()
        if cfg_location == "":
            raise ValueError("tub.location must not be empty")
        if cfg_location == "disabled":
            location_disabled = True

    if tubport_disabled and location_disabled:
        return None
    if tubport_disabled and not location_disabled:
        raise ValueError("tub.port is disabled, but not tub.location")
    if location_disabled and not tubport_disabled:
        raise ValueError("tub.location is disabled, but not tub.port")

    if cfg_tubport is None:
        # For 'tub.port', tahoe.cfg overrides the individual file on
        # disk. So only read config.portnum_fname if tahoe.cfg doesn't
        # provide a value.
        if os.path.exists(config.portnum_fname):
            file_tubport = fileutil.read(config.portnum_fname).strip()
            tubport = _convert_tub_port(file_tubport)
        else:
            tubport = "tcp:%d" % (allocate_tcp_port(), )
            fileutil.write_atomically(config.portnum_fname,
                                      tubport + "\n",
                                      mode="")
    else:
        tubport = _convert_tub_port(cfg_tubport)

    for port in tubport.split(","):
        if port in ("0", "tcp:0"):
            raise PortAssignmentRequired()

    if cfg_location is None:
        cfg_location = "AUTO"

    local_portnum = None  # needed to hush lgtm.com static analyzer
    # Replace the location "AUTO", if present, with the detected local
    # addresses. Don't probe for local addresses unless necessary.
    split_location = cfg_location.split(",")
    if "AUTO" in split_location:
        if not reveal_ip:
            raise PrivacyError("tub.location uses AUTO")
        local_addresses = get_local_addresses_sync()
        # tubport must be like "tcp:12345" or "tcp:12345:morestuff"
        local_portnum = int(tubport.split(":")[1])
    new_locations = []
    for loc in split_location:
        if loc == "AUTO":
            new_locations.extend(
                ["tcp:%s:%d" % (ip, local_portnum) for ip in local_addresses])
        else:
            if not reveal_ip:
                # Legacy hints are "host:port". We use Foolscap's utility
                # function to convert all hints into the modern format
                # ("tcp:host:port") because that's what the receiving
                # client will probably do. We test the converted hint for
                # TCP-ness, but publish the original hint because that
                # was the user's intent.
                from foolscap.connections.tcp import convert_legacy_hint
                converted_hint = convert_legacy_hint(loc)
                hint_type = converted_hint.split(":")[0]
                if hint_type == "tcp":
                    raise PrivacyError("tub.location includes tcp: hint")
            new_locations.append(loc)
    location = ",".join(new_locations)

    # Lacking this, Python 2 blows up in Foolscap when it is confused by a
    # Unicode FURL.
    location = location.encode("utf-8")

    return tubport, location
 def check_config(self, client_num, local_dir):
     client_config = fileutil.read(os.path.join(self.get_clientdir(i=client_num), "tahoe.cfg"))
     local_dir_utf8 = local_dir.encode('utf-8')
     magic_folder_config = "[magic_folder]\nenabled = True\nlocal.directory = %s" % (local_dir_utf8,)
     self.failUnlessIn(magic_folder_config, client_config)
 def _get_resp2((rc, out, err)):
     self.failUnlessReallyEqual(rc, 0)
     results = fileutil.read(fn3)
     self.failUnlessReallyEqual(results, DATA1)
Example #47
0
 def _get_resp2((rc, out, err)):
     self.failUnlessReallyEqual(rc, 0)
     results = fileutil.read(fn3)
     self.failUnlessReallyEqual(results, DATA1)
Example #48
0
 def _check_same_furl_and_port(res):
     self.failUnless(os.path.exists(INTRODUCER_FURL_FILE))
     self.failUnlessEqual(self.furl,
                          fileutil.read(INTRODUCER_FURL_FILE))
     self.failUnlessEqual(self.portnum, fileutil.read(PORTNUM_FILE))
 def _get_resp((rc, out, err)):
     self.failUnlessReallyEqual(rc, 0)
     results = fileutil.read(os.path.join(outdir, "test_file"))
     self.failUnlessReallyEqual(results, DATA1)
Example #50
0
 def _check_same_port(res):
     self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
Example #51
0
 def _check_same_furl(res):
     self.failUnlessEqual(self.storage_furl,
                          fileutil.read(storage_furl_file))
Example #52
0
 def _check_same_furl_and_port(res):
     self.failUnless(os.path.exists(introducer_furl_file))
     self.failUnlessEqual(self.furl, fileutil.read(introducer_furl_file))
     self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
 def _copy_file((rc, out, err)):
     self.failUnlessReallyEqual(rc, 0)
     results = fileutil.read(fn2)
     self.failUnlessReallyEqual(results, DATA1)
Example #54
0
    def do_create(self, kind):
        basedir = self.workdir("test_" + kind)
        command = "create-" + kind
        is_client = kind in ("node", "client")
        tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")

        n1 = os.path.join(basedir, command + "-n1")
        argv = ["--quiet", command, "--basedir", n1]
        rc, out, err = self.run_tahoe(argv)
        self.failUnlessEqual(err, "")
        self.failUnlessEqual(out, "")
        self.failUnlessEqual(rc, 0)
        self.failUnless(os.path.exists(n1))
        self.failUnless(os.path.exists(os.path.join(n1, tac)))

        if is_client:
            # tahoe.cfg should exist, and should have storage enabled for
            # 'create-node', and disabled for 'create-client'.
            tahoe_cfg = os.path.join(n1, "tahoe.cfg")
            self.failUnless(os.path.exists(tahoe_cfg))
            content = fileutil.read(tahoe_cfg).replace('\r\n', '\n')
            if kind == "client":
                self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = false\n", content), content)
            else:
                self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = true\n", content), content)
                self.failUnless("\nreserved_space = 1G\n" in content)

            self.failUnless(re.search(r"\n\[drop_upload\]\n#.*\nenabled = false\n", content), content)

        # creating the node a second time should be rejected
        rc, out, err = self.run_tahoe(argv)
        self.failIfEqual(rc, 0, str((out, err, rc)))
        self.failUnlessEqual(out, "")
        self.failUnless("is not empty." in err)

        # Fail if there is a non-empty line that doesn't end with a
        # punctuation mark.
        for line in err.splitlines():
            self.failIf(re.search("[\S][^\.!?]$", line), (line,))

        # test that the non --basedir form works too
        n2 = os.path.join(basedir, command + "-n2")
        argv = ["--quiet", command, n2]
        rc, out, err = self.run_tahoe(argv)
        self.failUnlessEqual(err, "")
        self.failUnlessEqual(out, "")
        self.failUnlessEqual(rc, 0)
        self.failUnless(os.path.exists(n2))
        self.failUnless(os.path.exists(os.path.join(n2, tac)))

        # test the --node-directory form
        n3 = os.path.join(basedir, command + "-n3")
        argv = ["--quiet", "--node-directory", n3, command]
        rc, out, err = self.run_tahoe(argv)
        self.failUnlessEqual(err, "")
        self.failUnlessEqual(out, "")
        self.failUnlessEqual(rc, 0)
        self.failUnless(os.path.exists(n3))
        self.failUnless(os.path.exists(os.path.join(n3, tac)))

        if kind in ("client", "node", "introducer"):
            # test that the output (without --quiet) includes the base directory
            n4 = os.path.join(basedir, command + "-n4")
            argv = [command, n4]
            rc, out, err = self.run_tahoe(argv)
            self.failUnlessEqual(err, "")
            self.failUnlessIn(" created in ", out)
            self.failUnlessIn(n4, out)
            self.failIfIn("\\\\?\\", out)
            self.failUnlessEqual(rc, 0)
            self.failUnless(os.path.exists(n4))
            self.failUnless(os.path.exists(os.path.join(n4, tac)))

        # make sure it rejects too many arguments
        argv = [command, "basedir", "extraarg"]
        self.failUnlessRaises(usage.UsageError,
                              runner.runner, argv,
                              run_by_human=False)

        # when creating a non-client, there is no default for the basedir
        if not is_client:
            argv = [command]
            self.failUnlessRaises(usage.UsageError,
                                  runner.runner, argv,
                                  run_by_human=False)
Example #55
0
def get_node_url(node_directory):
    node_url_file = os.path.join(node_directory, u"node.url")
    node_url = fileutil.read(node_url_file).strip()

    return node_url
Example #56
0
 def _check_same_port(res):
     self.failUnlessEqual(self.portnum, fileutil.read(PORTNUM_FILE))