From 7edae210c67d04e01152adb9e91b1c0d03e2fd13 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Fri, 12 Apr 2013 17:22:15 +0100
Subject: [PATCH] CLI: tolerate caps-from-future in unused aliases. Closes
 #1643.

---
 src/allmydata/scripts/common.py | 11 +++--
 src/allmydata/test/test_cli.py  | 78 ++++++++++++++++++++-------------
 2 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/src/allmydata/scripts/common.py b/src/allmydata/scripts/common.py
index 27ea9af1..bd78430c 100644
--- a/src/allmydata/scripts/common.py
+++ b/src/allmydata/scripts/common.py
@@ -72,7 +72,6 @@ DEFAULT_ALIAS = u"tahoe"
 
 
 def get_aliases(nodedir):
-    from allmydata import uri
     aliases = {}
     aliasfile = os.path.join(nodedir, "private", "aliases")
     rootfile = os.path.join(nodedir, "private", "root_dir.cap")
@@ -80,7 +79,7 @@ def get_aliases(nodedir):
         f = open(rootfile, "r")
         rootcap = f.read().strip()
         if rootcap:
-            aliases[DEFAULT_ALIAS] = uri.from_string_dirnode(rootcap).to_string()
+            aliases[DEFAULT_ALIAS] = rootcap
     except EnvironmentError:
         pass
     try:
@@ -92,7 +91,7 @@ def get_aliases(nodedir):
             name, cap = line.split(u":", 1)
             # normalize it: remove http: prefix, urldecode
             cap = cap.strip().encode('utf-8')
-            aliases[name] = uri.from_string_dirnode(cap).to_string()
+            aliases[name] = cap
     except EnvironmentError:
         pass
     return aliases
@@ -158,7 +157,7 @@ def get_alias(aliases, path_unicode, default):
             raise UnknownAliasError("No alias specified, and the default %s alias doesn't exist. "
                                     "To create it, use 'tahoe create-alias %s'."
                                     % (quote_output(default), quote_output(default, quotemarks=False)))
-        return aliases[default], path
+        return uri.from_string_dirnode(aliases[default]).to_string(), path
     if colon == 1 and default is None and platform_uses_lettercolon_drivename():
         # treat C:\why\must\windows\be\so\weird as a local path, not a tahoe
         # file in the "C:" alias
@@ -175,11 +174,11 @@ def get_alias(aliases, path_unicode, default):
             raise UnknownAliasError("No alias specified, and the default %s alias doesn't exist. "
                                     "To create it, use 'tahoe create-alias %s'."
                                     % (quote_output(default), quote_output(default, quotemarks=False)))
-        return aliases[default], path
+        return uri.from_string_dirnode(aliases[default]).to_string(), path
     if alias not in aliases:
         raise UnknownAliasError("Unknown alias %s, please create it with 'tahoe add-alias' or 'tahoe create-alias'." %
                                 quote_output(alias))
-    return aliases[alias], path[colon+1:]
+    return uri.from_string_dirnode(aliases[alias]).to_string(), path[colon+1:]
 
 def escape_path(path):
     segments = path.split("/")
diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py
index 658d3ce5..0c06d189 100644
--- a/src/allmydata/test/test_cli.py
+++ b/src/allmydata/test/test_cli.py
@@ -397,26 +397,31 @@ class CLI(CLITestMixin, unittest.TestCase):
                         "didn't see 'mqfblse6m5a6dh45isu2cg7oji' in '%s'" % err)
 
     def test_alias(self):
-        aliases = {"tahoe": "TA",
-                   "work": "WA",
-                   "c": "CA"}
+        def s128(c): return base32.b2a(c*(128/8))
+        def s256(c): return base32.b2a(c*(256/8))
+        TA = "URI:DIR2:%s:%s" % (s128("T"), s256("T"))
+        WA = "URI:DIR2:%s:%s" % (s128("W"), s256("W"))
+        CA = "URI:DIR2:%s:%s" % (s128("C"), s256("C"))
+        aliases = {"tahoe": TA,
+                   "work": WA,
+                   "c": CA}
         def ga1(path):
             return get_alias(aliases, path, u"tahoe")
         uses_lettercolon = common.platform_uses_lettercolon_drivename()
-        self.failUnlessReallyEqual(ga1(u"bare"), ("TA", "bare"))
-        self.failUnlessReallyEqual(ga1(u"baredir/file"), ("TA", "baredir/file"))
-        self.failUnlessReallyEqual(ga1(u"baredir/file:7"), ("TA", "baredir/file:7"))
-        self.failUnlessReallyEqual(ga1(u"tahoe:"), ("TA", ""))
-        self.failUnlessReallyEqual(ga1(u"tahoe:file"), ("TA", "file"))
-        self.failUnlessReallyEqual(ga1(u"tahoe:dir/file"), ("TA", "dir/file"))
-        self.failUnlessReallyEqual(ga1(u"work:"), ("WA", ""))
-        self.failUnlessReallyEqual(ga1(u"work:file"), ("WA", "file"))
-        self.failUnlessReallyEqual(ga1(u"work:dir/file"), ("WA", "dir/file"))
+        self.failUnlessReallyEqual(ga1(u"bare"), (TA, "bare"))
+        self.failUnlessReallyEqual(ga1(u"baredir/file"), (TA, "baredir/file"))
+        self.failUnlessReallyEqual(ga1(u"baredir/file:7"), (TA, "baredir/file:7"))
+        self.failUnlessReallyEqual(ga1(u"tahoe:"), (TA, ""))
+        self.failUnlessReallyEqual(ga1(u"tahoe:file"), (TA, "file"))
+        self.failUnlessReallyEqual(ga1(u"tahoe:dir/file"), (TA, "dir/file"))
+        self.failUnlessReallyEqual(ga1(u"work:"), (WA, ""))
+        self.failUnlessReallyEqual(ga1(u"work:file"), (WA, "file"))
+        self.failUnlessReallyEqual(ga1(u"work:dir/file"), (WA, "dir/file"))
         # default != None means we really expect a tahoe path, regardless of
         # whether we're on windows or not. This is what 'tahoe get' uses.
-        self.failUnlessReallyEqual(ga1(u"c:"), ("CA", ""))
-        self.failUnlessReallyEqual(ga1(u"c:file"), ("CA", "file"))
-        self.failUnlessReallyEqual(ga1(u"c:dir/file"), ("CA", "dir/file"))
+        self.failUnlessReallyEqual(ga1(u"c:"), (CA, ""))
+        self.failUnlessReallyEqual(ga1(u"c:file"), (CA, "file"))
+        self.failUnlessReallyEqual(ga1(u"c:dir/file"), (CA, "dir/file"))
         self.failUnlessReallyEqual(ga1(u"URI:stuff"), ("URI:stuff", ""))
         self.failUnlessReallyEqual(ga1(u"URI:stuff/file"), ("URI:stuff", "file"))
         self.failUnlessReallyEqual(ga1(u"URI:stuff:./file"), ("URI:stuff", "file"))
@@ -435,9 +440,9 @@ class CLI(CLITestMixin, unittest.TestCase):
                              (DefaultAliasMarker, "baredir/file:7"))
         self.failUnlessReallyEqual(ga2(u"baredir/sub:1/file:7"),
                              (DefaultAliasMarker, "baredir/sub:1/file:7"))
-        self.failUnlessReallyEqual(ga2(u"tahoe:"), ("TA", ""))
-        self.failUnlessReallyEqual(ga2(u"tahoe:file"), ("TA", "file"))
-        self.failUnlessReallyEqual(ga2(u"tahoe:dir/file"), ("TA", "dir/file"))
+        self.failUnlessReallyEqual(ga2(u"tahoe:"), (TA, ""))
+        self.failUnlessReallyEqual(ga2(u"tahoe:file"), (TA, "file"))
+        self.failUnlessReallyEqual(ga2(u"tahoe:dir/file"), (TA, "dir/file"))
         # on windows, we really want c:foo to indicate a local file.
         # default==None is what 'tahoe cp' uses.
         if uses_lettercolon:
@@ -446,12 +451,12 @@ class CLI(CLITestMixin, unittest.TestCase):
             self.failUnlessReallyEqual(ga2(u"c:dir/file"),
                                  (DefaultAliasMarker, "c:dir/file"))
         else:
-            self.failUnlessReallyEqual(ga2(u"c:"), ("CA", ""))
-            self.failUnlessReallyEqual(ga2(u"c:file"), ("CA", "file"))
-            self.failUnlessReallyEqual(ga2(u"c:dir/file"), ("CA", "dir/file"))
-        self.failUnlessReallyEqual(ga2(u"work:"), ("WA", ""))
-        self.failUnlessReallyEqual(ga2(u"work:file"), ("WA", "file"))
-        self.failUnlessReallyEqual(ga2(u"work:dir/file"), ("WA", "dir/file"))
+            self.failUnlessReallyEqual(ga2(u"c:"), (CA, ""))
+            self.failUnlessReallyEqual(ga2(u"c:file"), (CA, "file"))
+            self.failUnlessReallyEqual(ga2(u"c:dir/file"), (CA, "dir/file"))
+        self.failUnlessReallyEqual(ga2(u"work:"), (WA, ""))
+        self.failUnlessReallyEqual(ga2(u"work:file"), (WA, "file"))
+        self.failUnlessReallyEqual(ga2(u"work:dir/file"), (WA, "dir/file"))
         self.failUnlessReallyEqual(ga2(u"URI:stuff"), ("URI:stuff", ""))
         self.failUnlessReallyEqual(ga2(u"URI:stuff/file"), ("URI:stuff", "file"))
         self.failUnlessReallyEqual(ga2(u"URI:stuff:./file"), ("URI:stuff", "file"))
@@ -476,16 +481,16 @@ class CLI(CLITestMixin, unittest.TestCase):
                              (DefaultAliasMarker, "baredir/file:7"))
         self.failUnlessReallyEqual(ga3(u"baredir/sub:1/file:7"),
                              (DefaultAliasMarker, "baredir/sub:1/file:7"))
-        self.failUnlessReallyEqual(ga3(u"tahoe:"), ("TA", ""))
-        self.failUnlessReallyEqual(ga3(u"tahoe:file"), ("TA", "file"))
-        self.failUnlessReallyEqual(ga3(u"tahoe:dir/file"), ("TA", "dir/file"))
+        self.failUnlessReallyEqual(ga3(u"tahoe:"), (TA, ""))
+        self.failUnlessReallyEqual(ga3(u"tahoe:file"), (TA, "file"))
+        self.failUnlessReallyEqual(ga3(u"tahoe:dir/file"), (TA, "dir/file"))
         self.failUnlessReallyEqual(ga3(u"c:"), (DefaultAliasMarker, "c:"))
         self.failUnlessReallyEqual(ga3(u"c:file"), (DefaultAliasMarker, "c:file"))
         self.failUnlessReallyEqual(ga3(u"c:dir/file"),
                              (DefaultAliasMarker, "c:dir/file"))
-        self.failUnlessReallyEqual(ga3(u"work:"), ("WA", ""))
-        self.failUnlessReallyEqual(ga3(u"work:file"), ("WA", "file"))
-        self.failUnlessReallyEqual(ga3(u"work:dir/file"), ("WA", "dir/file"))
+        self.failUnlessReallyEqual(ga3(u"work:"), (WA, ""))
+        self.failUnlessReallyEqual(ga3(u"work:file"), (WA, "file"))
+        self.failUnlessReallyEqual(ga3(u"work:dir/file"), (WA, "dir/file"))
         self.failUnlessReallyEqual(ga3(u"URI:stuff"), ("URI:stuff", ""))
         self.failUnlessReallyEqual(ga3(u"URI:stuff:./file"), ("URI:stuff", "file"))
         self.failUnlessReallyEqual(ga3(u"URI:stuff:./dir/file"), ("URI:stuff", "dir/file"))
@@ -510,6 +515,19 @@ class CLI(CLITestMixin, unittest.TestCase):
             return retval
         self.failUnlessRaises(common.UnknownAliasError, ga5, u"C:\\Windows")
 
+    def test_alias_tolerance(self):
+        def s128(c): return base32.b2a(c*(128/8))
+        def s256(c): return base32.b2a(c*(256/8))
+        TA = "URI:DIR2:%s:%s" % (s128("T"), s256("T"))
+        aliases = {"present": TA,
+                   "future": "URI-FROM-FUTURE:ooh:aah"}
+        def ga1(path):
+            return get_alias(aliases, path, u"tahoe")
+        self.failUnlessReallyEqual(ga1(u"present:file"), (TA, "file"))
+        # this throws, via assert IDirnodeURI.providedBy(), since get_alias()
+        # wants a dirnode, and the future cap gives us UnknownURI instead.
+        #ga1(u"future:stuff")
+
     def test_listdir_unicode_good(self):
         filenames = [u'L\u00F4zane', u'Bern', u'Gen\u00E8ve']  # must be NFC
 
-- 
2.45.2