| 1 | """ |
|---|
| 2 | Ported to Python 3. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from io import StringIO |
|---|
| 6 | import re |
|---|
| 7 | from six import ensure_text |
|---|
| 8 | |
|---|
| 9 | import os.path |
|---|
| 10 | from urllib.parse import quote as url_quote |
|---|
| 11 | |
|---|
| 12 | from twisted.trial import unittest |
|---|
| 13 | from twisted.internet.testing import ( |
|---|
| 14 | MemoryReactor, |
|---|
| 15 | ) |
|---|
| 16 | from twisted.internet.test.modulehelpers import ( |
|---|
| 17 | AlternateReactor, |
|---|
| 18 | ) |
|---|
| 19 | import allmydata |
|---|
| 20 | from allmydata.crypto import ed25519 |
|---|
| 21 | from allmydata.util import fileutil, hashutil, base32 |
|---|
| 22 | from allmydata import uri |
|---|
| 23 | from allmydata.immutable import upload |
|---|
| 24 | from allmydata.dirnode import normalize |
|---|
| 25 | from allmydata.scripts.common_http import socket_error |
|---|
| 26 | import allmydata.scripts.common_http |
|---|
| 27 | |
|---|
| 28 | # Test that the scripts can be imported. |
|---|
| 29 | from allmydata.scripts import create_node, debug, \ |
|---|
| 30 | tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, \ |
|---|
| 31 | tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen, \ |
|---|
| 32 | tahoe_run |
|---|
| 33 | _hush_pyflakes = [create_node, debug, |
|---|
| 34 | tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, |
|---|
| 35 | tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen, |
|---|
| 36 | tahoe_run] |
|---|
| 37 | |
|---|
| 38 | from allmydata.scripts import common |
|---|
| 39 | from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases, get_alias, \ |
|---|
| 40 | DefaultAliasMarker |
|---|
| 41 | |
|---|
| 42 | from allmydata.scripts import cli, debug, runner |
|---|
| 43 | from allmydata.test.common_util import (ReallyEqualMixin, skip_if_cannot_represent_filename, |
|---|
| 44 | run_cli) |
|---|
| 45 | from allmydata.test.no_network import GridTestMixin |
|---|
| 46 | from allmydata.test.cli.common import CLITestMixin, parse_options |
|---|
| 47 | from twisted.python import usage |
|---|
| 48 | |
|---|
| 49 | from allmydata.util.encodingutil import listdir_unicode, get_io_encoding |
|---|
| 50 | |
|---|
| 51 | class CLI(CLITestMixin, unittest.TestCase): |
|---|
| 52 | def _dump_cap(self, *args): |
|---|
| 53 | args = [ensure_text(s) for s in args] |
|---|
| 54 | config = debug.DumpCapOptions() |
|---|
| 55 | config.stdout,config.stderr = StringIO(), StringIO() |
|---|
| 56 | config.parseOptions(args) |
|---|
| 57 | debug.dump_cap(config) |
|---|
| 58 | self.failIf(config.stderr.getvalue()) |
|---|
| 59 | output = config.stdout.getvalue() |
|---|
| 60 | return output |
|---|
| 61 | |
|---|
| 62 | def test_dump_cap_chk(self): |
|---|
| 63 | key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
|---|
| 64 | uri_extension_hash = hashutil.uri_extension_hash(b"stuff") |
|---|
| 65 | needed_shares = 25 |
|---|
| 66 | total_shares = 100 |
|---|
| 67 | size = 1234 |
|---|
| 68 | u = uri.CHKFileURI(key=key, |
|---|
| 69 | uri_extension_hash=uri_extension_hash, |
|---|
| 70 | needed_shares=needed_shares, |
|---|
| 71 | total_shares=total_shares, |
|---|
| 72 | size=size) |
|---|
| 73 | output = self._dump_cap(u.to_string()) |
|---|
| 74 | self.failUnless("CHK File:" in output, output) |
|---|
| 75 | self.failUnless("key: aaaqeayeaudaocajbifqydiob4" in output, output) |
|---|
| 76 | self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output) |
|---|
| 77 | self.failUnless("size: 1234" in output, output) |
|---|
| 78 | self.failUnless("k/N: 25/100" in output, output) |
|---|
| 79 | self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output) |
|---|
| 80 | |
|---|
| 81 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 82 | u.to_string()) |
|---|
| 83 | self.failUnless("client renewal secret: znxmki5zdibb5qlt46xbdvk2t55j7hibejq3i5ijyurkr6m6jkhq" in output, output) |
|---|
| 84 | |
|---|
| 85 | output = self._dump_cap(str(u.get_verify_cap().to_string(), "ascii")) |
|---|
| 86 | self.failIf("key: " in output, output) |
|---|
| 87 | self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output) |
|---|
| 88 | self.failUnless("size: 1234" in output, output) |
|---|
| 89 | self.failUnless("k/N: 25/100" in output, output) |
|---|
| 90 | self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output) |
|---|
| 91 | |
|---|
| 92 | prefixed_u = "http://127.0.0.1/uri/%s" % url_quote(u.to_string()) |
|---|
| 93 | output = self._dump_cap(prefixed_u) |
|---|
| 94 | self.failUnless("CHK File:" in output, output) |
|---|
| 95 | self.failUnless("key: aaaqeayeaudaocajbifqydiob4" in output, output) |
|---|
| 96 | self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output) |
|---|
| 97 | self.failUnless("size: 1234" in output, output) |
|---|
| 98 | self.failUnless("k/N: 25/100" in output, output) |
|---|
| 99 | self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output) |
|---|
| 100 | |
|---|
| 101 | def test_dump_cap_lit(self): |
|---|
| 102 | u = uri.LiteralFileURI(b"this is some data") |
|---|
| 103 | output = self._dump_cap(u.to_string()) |
|---|
| 104 | self.failUnless("Literal File URI:" in output, output) |
|---|
| 105 | self.failUnless("data: 'this is some data'" in output, output) |
|---|
| 106 | |
|---|
| 107 | def test_dump_cap_sdmf(self): |
|---|
| 108 | writekey = b"\x01" * 16 |
|---|
| 109 | fingerprint = b"\xfe" * 32 |
|---|
| 110 | u = uri.WriteableSSKFileURI(writekey, fingerprint) |
|---|
| 111 | |
|---|
| 112 | output = self._dump_cap(u.to_string()) |
|---|
| 113 | self.failUnless("SDMF Writeable URI:" in output, output) |
|---|
| 114 | self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output, output) |
|---|
| 115 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 116 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 117 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 118 | |
|---|
| 119 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 120 | u.to_string()) |
|---|
| 121 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 122 | |
|---|
| 123 | fileutil.make_dirs("cli/test_dump_cap/private") |
|---|
| 124 | fileutil.write("cli/test_dump_cap/private/secret", "5s33nk3qpvnj2fw3z4mnm2y6fa\n") |
|---|
| 125 | output = self._dump_cap("--client-dir", "cli/test_dump_cap", |
|---|
| 126 | u.to_string()) |
|---|
| 127 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 128 | |
|---|
| 129 | output = self._dump_cap("--client-dir", "cli/test_dump_cap_BOGUS", |
|---|
| 130 | u.to_string()) |
|---|
| 131 | self.failIf("file renewal secret:" in output, output) |
|---|
| 132 | |
|---|
| 133 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 134 | u.to_string()) |
|---|
| 135 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 136 | self.failIf("file renewal secret:" in output, output) |
|---|
| 137 | |
|---|
| 138 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 139 | "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 140 | u.to_string()) |
|---|
| 141 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 142 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 143 | self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output) |
|---|
| 144 | |
|---|
| 145 | u = u.get_readonly() |
|---|
| 146 | output = self._dump_cap(u.to_string()) |
|---|
| 147 | self.failUnless("SDMF Read-only URI:" in output, output) |
|---|
| 148 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 149 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 150 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 151 | |
|---|
| 152 | u = u.get_verify_cap() |
|---|
| 153 | output = self._dump_cap(u.to_string()) |
|---|
| 154 | self.failUnless("SDMF Verifier URI:" in output, output) |
|---|
| 155 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 156 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 157 | |
|---|
| 158 | def test_dump_cap_mdmf(self): |
|---|
| 159 | writekey = b"\x01" * 16 |
|---|
| 160 | fingerprint = b"\xfe" * 32 |
|---|
| 161 | u = uri.WriteableMDMFFileURI(writekey, fingerprint) |
|---|
| 162 | |
|---|
| 163 | output = self._dump_cap(u.to_string()) |
|---|
| 164 | self.failUnless("MDMF Writeable URI:" in output, output) |
|---|
| 165 | self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output, output) |
|---|
| 166 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 167 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 168 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 169 | |
|---|
| 170 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 171 | u.to_string()) |
|---|
| 172 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 173 | |
|---|
| 174 | fileutil.make_dirs("cli/test_dump_cap/private") |
|---|
| 175 | fileutil.write("cli/test_dump_cap/private/secret", "5s33nk3qpvnj2fw3z4mnm2y6fa\n") |
|---|
| 176 | output = self._dump_cap("--client-dir", "cli/test_dump_cap", |
|---|
| 177 | u.to_string()) |
|---|
| 178 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 179 | |
|---|
| 180 | output = self._dump_cap("--client-dir", "cli/test_dump_cap_BOGUS", |
|---|
| 181 | u.to_string()) |
|---|
| 182 | self.failIf("file renewal secret:" in output, output) |
|---|
| 183 | |
|---|
| 184 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 185 | u.to_string()) |
|---|
| 186 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 187 | self.failIf("file renewal secret:" in output, output) |
|---|
| 188 | |
|---|
| 189 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 190 | "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 191 | u.to_string()) |
|---|
| 192 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 193 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 194 | self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output) |
|---|
| 195 | |
|---|
| 196 | u = u.get_readonly() |
|---|
| 197 | output = self._dump_cap(u.to_string()) |
|---|
| 198 | self.failUnless("MDMF Read-only URI:" in output, output) |
|---|
| 199 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 200 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 201 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 202 | |
|---|
| 203 | u = u.get_verify_cap() |
|---|
| 204 | output = self._dump_cap(u.to_string()) |
|---|
| 205 | self.failUnless("MDMF Verifier URI:" in output, output) |
|---|
| 206 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 207 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | def test_dump_cap_chk_directory(self): |
|---|
| 211 | key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
|---|
| 212 | uri_extension_hash = hashutil.uri_extension_hash(b"stuff") |
|---|
| 213 | needed_shares = 25 |
|---|
| 214 | total_shares = 100 |
|---|
| 215 | size = 1234 |
|---|
| 216 | u1 = uri.CHKFileURI(key=key, |
|---|
| 217 | uri_extension_hash=uri_extension_hash, |
|---|
| 218 | needed_shares=needed_shares, |
|---|
| 219 | total_shares=total_shares, |
|---|
| 220 | size=size) |
|---|
| 221 | u = uri.ImmutableDirectoryURI(u1) |
|---|
| 222 | |
|---|
| 223 | output = self._dump_cap(u.to_string()) |
|---|
| 224 | self.failUnless("CHK Directory URI:" in output, output) |
|---|
| 225 | self.failUnless("key: aaaqeayeaudaocajbifqydiob4" in output, output) |
|---|
| 226 | self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output) |
|---|
| 227 | self.failUnless("size: 1234" in output, output) |
|---|
| 228 | self.failUnless("k/N: 25/100" in output, output) |
|---|
| 229 | self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output) |
|---|
| 230 | |
|---|
| 231 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 232 | u.to_string()) |
|---|
| 233 | self.failUnless("file renewal secret: csrvkjgomkyyyil5yo4yk5np37p6oa2ve2hg6xmk2dy7kaxsu6xq" in output, output) |
|---|
| 234 | |
|---|
| 235 | u = u.get_verify_cap() |
|---|
| 236 | output = self._dump_cap(u.to_string()) |
|---|
| 237 | self.failUnless("CHK Directory Verifier URI:" in output, output) |
|---|
| 238 | self.failIf("key: " in output, output) |
|---|
| 239 | self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output) |
|---|
| 240 | self.failUnless("size: 1234" in output, output) |
|---|
| 241 | self.failUnless("k/N: 25/100" in output, output) |
|---|
| 242 | self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output) |
|---|
| 243 | |
|---|
| 244 | def test_dump_cap_sdmf_directory(self): |
|---|
| 245 | writekey = b"\x01" * 16 |
|---|
| 246 | fingerprint = b"\xfe" * 32 |
|---|
| 247 | u1 = uri.WriteableSSKFileURI(writekey, fingerprint) |
|---|
| 248 | u = uri.DirectoryURI(u1) |
|---|
| 249 | |
|---|
| 250 | output = self._dump_cap(u.to_string()) |
|---|
| 251 | self.failUnless("Directory Writeable URI:" in output, output) |
|---|
| 252 | self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output, |
|---|
| 253 | output) |
|---|
| 254 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 255 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, |
|---|
| 256 | output) |
|---|
| 257 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 258 | |
|---|
| 259 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 260 | u.to_string()) |
|---|
| 261 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 262 | |
|---|
| 263 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 264 | u.to_string()) |
|---|
| 265 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 266 | self.failIf("file renewal secret:" in output, output) |
|---|
| 267 | |
|---|
| 268 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 269 | "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 270 | u.to_string()) |
|---|
| 271 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 272 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 273 | self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output) |
|---|
| 274 | |
|---|
| 275 | u = u.get_readonly() |
|---|
| 276 | output = self._dump_cap(u.to_string()) |
|---|
| 277 | self.failUnless("Directory Read-only URI:" in output, output) |
|---|
| 278 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 279 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 280 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 281 | |
|---|
| 282 | u = u.get_verify_cap() |
|---|
| 283 | output = self._dump_cap(u.to_string()) |
|---|
| 284 | self.failUnless("Directory Verifier URI:" in output, output) |
|---|
| 285 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 286 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 287 | |
|---|
| 288 | def test_dump_cap_mdmf_directory(self): |
|---|
| 289 | writekey = b"\x01" * 16 |
|---|
| 290 | fingerprint = b"\xfe" * 32 |
|---|
| 291 | u1 = uri.WriteableMDMFFileURI(writekey, fingerprint) |
|---|
| 292 | u = uri.MDMFDirectoryURI(u1) |
|---|
| 293 | |
|---|
| 294 | output = self._dump_cap(u.to_string()) |
|---|
| 295 | self.failUnless("Directory Writeable URI:" in output, output) |
|---|
| 296 | self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output, |
|---|
| 297 | output) |
|---|
| 298 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 299 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, |
|---|
| 300 | output) |
|---|
| 301 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 302 | |
|---|
| 303 | output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 304 | u.to_string()) |
|---|
| 305 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 306 | |
|---|
| 307 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 308 | u.to_string()) |
|---|
| 309 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 310 | self.failIf("file renewal secret:" in output, output) |
|---|
| 311 | |
|---|
| 312 | output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j", |
|---|
| 313 | "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa", |
|---|
| 314 | u.to_string()) |
|---|
| 315 | self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output) |
|---|
| 316 | self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output) |
|---|
| 317 | self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output) |
|---|
| 318 | |
|---|
| 319 | u = u.get_readonly() |
|---|
| 320 | output = self._dump_cap(u.to_string()) |
|---|
| 321 | self.failUnless("Directory Read-only URI:" in output, output) |
|---|
| 322 | self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output) |
|---|
| 323 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 324 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 325 | |
|---|
| 326 | u = u.get_verify_cap() |
|---|
| 327 | output = self._dump_cap(u.to_string()) |
|---|
| 328 | self.failUnless("Directory Verifier URI:" in output, output) |
|---|
| 329 | self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output) |
|---|
| 330 | self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output) |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | def _catalog_shares(self, *basedirs): |
|---|
| 334 | o = debug.CatalogSharesOptions() |
|---|
| 335 | o.stdout,o.stderr = StringIO(), StringIO() |
|---|
| 336 | args = list(basedirs) |
|---|
| 337 | o.parseOptions(args) |
|---|
| 338 | debug.catalog_shares(o) |
|---|
| 339 | out = o.stdout.getvalue() |
|---|
| 340 | err = o.stderr.getvalue() |
|---|
| 341 | return out, err |
|---|
| 342 | |
|---|
| 343 | def test_catalog_shares_error(self): |
|---|
| 344 | nodedir1 = "cli/test_catalog_shares/node1" |
|---|
| 345 | sharedir = os.path.join(nodedir1, "storage", "shares", "mq", "mqfblse6m5a6dh45isu2cg7oji") |
|---|
| 346 | fileutil.make_dirs(sharedir) |
|---|
| 347 | fileutil.write("cli/test_catalog_shares/node1/storage/shares/mq/not-a-dir", "") |
|---|
| 348 | # write a bogus share that looks a little bit like CHK |
|---|
| 349 | fileutil.write(os.path.join(sharedir, "8"), |
|---|
| 350 | b"\x00\x00\x00\x01" + b"\xff" * 200) # this triggers an assert |
|---|
| 351 | |
|---|
| 352 | nodedir2 = "cli/test_catalog_shares/node2" |
|---|
| 353 | fileutil.make_dirs(nodedir2) |
|---|
| 354 | fileutil.write("cli/test_catalog_shares/node1/storage/shares/not-a-dir", "") |
|---|
| 355 | |
|---|
| 356 | # now make sure that the 'catalog-shares' commands survives the error |
|---|
| 357 | out, err = self._catalog_shares(nodedir1, nodedir2) |
|---|
| 358 | self.assertEqual(out, "") |
|---|
| 359 | self.failUnless("Error processing " in err, |
|---|
| 360 | "didn't see 'error processing' in '%s'" % err) |
|---|
| 361 | #self.failUnless(nodedir1 in err, |
|---|
| 362 | # "didn't see '%s' in '%s'" % (nodedir1, err)) |
|---|
| 363 | # windows mangles the path, and os.path.join isn't enough to make |
|---|
| 364 | # up for it, so just look for individual strings |
|---|
| 365 | self.failUnless("node1" in err, |
|---|
| 366 | "didn't see 'node1' in '%s'" % err) |
|---|
| 367 | self.failUnless("mqfblse6m5a6dh45isu2cg7oji" in err, |
|---|
| 368 | "didn't see 'mqfblse6m5a6dh45isu2cg7oji' in '%s'" % err) |
|---|
| 369 | |
|---|
| 370 | def test_alias(self): |
|---|
| 371 | def s128(c): return base32.b2a(c*(128//8)) |
|---|
| 372 | def s256(c): return base32.b2a(c*(256//8)) |
|---|
| 373 | TA = b"URI:DIR2:%s:%s" % (s128(b"T"), s256(b"T")) |
|---|
| 374 | WA = b"URI:DIR2:%s:%s" % (s128(b"W"), s256(b"W")) |
|---|
| 375 | CA = b"URI:DIR2:%s:%s" % (s128(b"C"), s256(b"C")) |
|---|
| 376 | aliases = {"tahoe": TA, |
|---|
| 377 | "work": WA, |
|---|
| 378 | "c": CA} |
|---|
| 379 | def ga1(path): |
|---|
| 380 | return get_alias(aliases, path, u"tahoe") |
|---|
| 381 | uses_lettercolon = common.platform_uses_lettercolon_drivename() |
|---|
| 382 | self.failUnlessReallyEqual(ga1(u"bare"), (TA, b"bare")) |
|---|
| 383 | self.failUnlessReallyEqual(ga1(u"baredir/file"), (TA, b"baredir/file")) |
|---|
| 384 | self.failUnlessReallyEqual(ga1(u"baredir/file:7"), (TA, b"baredir/file:7")) |
|---|
| 385 | self.failUnlessReallyEqual(ga1(u"tahoe:"), (TA, b"")) |
|---|
| 386 | self.failUnlessReallyEqual(ga1(u"tahoe:file"), (TA, b"file")) |
|---|
| 387 | self.failUnlessReallyEqual(ga1(u"tahoe:dir/file"), (TA, b"dir/file")) |
|---|
| 388 | self.failUnlessReallyEqual(ga1(u"work:"), (WA, b"")) |
|---|
| 389 | self.failUnlessReallyEqual(ga1(u"work:file"), (WA, b"file")) |
|---|
| 390 | self.failUnlessReallyEqual(ga1(u"work:dir/file"), (WA, b"dir/file")) |
|---|
| 391 | # default != None means we really expect a tahoe path, regardless of |
|---|
| 392 | # whether we're on windows or not. This is what 'tahoe get' uses. |
|---|
| 393 | self.failUnlessReallyEqual(ga1(u"c:"), (CA, b"")) |
|---|
| 394 | self.failUnlessReallyEqual(ga1(u"c:file"), (CA, b"file")) |
|---|
| 395 | self.failUnlessReallyEqual(ga1(u"c:dir/file"), (CA, b"dir/file")) |
|---|
| 396 | self.failUnlessReallyEqual(ga1(u"URI:stuff"), (b"URI:stuff", b"")) |
|---|
| 397 | self.failUnlessReallyEqual(ga1(u"URI:stuff/file"), (b"URI:stuff", b"file")) |
|---|
| 398 | self.failUnlessReallyEqual(ga1(u"URI:stuff:./file"), (b"URI:stuff", b"file")) |
|---|
| 399 | self.failUnlessReallyEqual(ga1(u"URI:stuff/dir/file"), (b"URI:stuff", b"dir/file")) |
|---|
| 400 | self.failUnlessReallyEqual(ga1(u"URI:stuff:./dir/file"), (b"URI:stuff", b"dir/file")) |
|---|
| 401 | self.failUnlessRaises(common.UnknownAliasError, ga1, u"missing:") |
|---|
| 402 | self.failUnlessRaises(common.UnknownAliasError, ga1, u"missing:dir") |
|---|
| 403 | self.failUnlessRaises(common.UnknownAliasError, ga1, u"missing:dir/file") |
|---|
| 404 | |
|---|
| 405 | def ga2(path): |
|---|
| 406 | return get_alias(aliases, path, None) |
|---|
| 407 | self.failUnlessReallyEqual(ga2(u"bare"), (DefaultAliasMarker, b"bare")) |
|---|
| 408 | self.failUnlessReallyEqual(ga2(u"baredir/file"), |
|---|
| 409 | (DefaultAliasMarker, b"baredir/file")) |
|---|
| 410 | self.failUnlessReallyEqual(ga2(u"baredir/file:7"), |
|---|
| 411 | (DefaultAliasMarker, b"baredir/file:7")) |
|---|
| 412 | self.failUnlessReallyEqual(ga2(u"baredir/sub:1/file:7"), |
|---|
| 413 | (DefaultAliasMarker, b"baredir/sub:1/file:7")) |
|---|
| 414 | self.failUnlessReallyEqual(ga2(u"tahoe:"), (TA, b"")) |
|---|
| 415 | self.failUnlessReallyEqual(ga2(u"tahoe:file"), (TA, b"file")) |
|---|
| 416 | self.failUnlessReallyEqual(ga2(u"tahoe:dir/file"), (TA, b"dir/file")) |
|---|
| 417 | # on windows, we really want c:foo to indicate a local file. |
|---|
| 418 | # default==None is what 'tahoe cp' uses. |
|---|
| 419 | if uses_lettercolon: |
|---|
| 420 | self.failUnlessReallyEqual(ga2(u"c:"), (DefaultAliasMarker, b"c:")) |
|---|
| 421 | self.failUnlessReallyEqual(ga2(u"c:file"), (DefaultAliasMarker, b"c:file")) |
|---|
| 422 | self.failUnlessReallyEqual(ga2(u"c:dir/file"), |
|---|
| 423 | (DefaultAliasMarker, b"c:dir/file")) |
|---|
| 424 | else: |
|---|
| 425 | self.failUnlessReallyEqual(ga2(u"c:"), (CA, b"")) |
|---|
| 426 | self.failUnlessReallyEqual(ga2(u"c:file"), (CA, b"file")) |
|---|
| 427 | self.failUnlessReallyEqual(ga2(u"c:dir/file"), (CA, b"dir/file")) |
|---|
| 428 | self.failUnlessReallyEqual(ga2(u"work:"), (WA, b"")) |
|---|
| 429 | self.failUnlessReallyEqual(ga2(u"work:file"), (WA, b"file")) |
|---|
| 430 | self.failUnlessReallyEqual(ga2(u"work:dir/file"), (WA, b"dir/file")) |
|---|
| 431 | self.failUnlessReallyEqual(ga2(u"URI:stuff"), (b"URI:stuff", b"")) |
|---|
| 432 | self.failUnlessReallyEqual(ga2(u"URI:stuff/file"), (b"URI:stuff", b"file")) |
|---|
| 433 | self.failUnlessReallyEqual(ga2(u"URI:stuff:./file"), (b"URI:stuff", b"file")) |
|---|
| 434 | self.failUnlessReallyEqual(ga2(u"URI:stuff/dir/file"), (b"URI:stuff", b"dir/file")) |
|---|
| 435 | self.failUnlessReallyEqual(ga2(u"URI:stuff:./dir/file"), (b"URI:stuff", b"dir/file")) |
|---|
| 436 | self.failUnlessRaises(common.UnknownAliasError, ga2, u"missing:") |
|---|
| 437 | self.failUnlessRaises(common.UnknownAliasError, ga2, u"missing:dir") |
|---|
| 438 | self.failUnlessRaises(common.UnknownAliasError, ga2, u"missing:dir/file") |
|---|
| 439 | |
|---|
| 440 | def ga3(path): |
|---|
| 441 | old = common.pretend_platform_uses_lettercolon |
|---|
| 442 | try: |
|---|
| 443 | common.pretend_platform_uses_lettercolon = True |
|---|
| 444 | retval = get_alias(aliases, path, None) |
|---|
| 445 | finally: |
|---|
| 446 | common.pretend_platform_uses_lettercolon = old |
|---|
| 447 | return retval |
|---|
| 448 | self.failUnlessReallyEqual(ga3(u"bare"), (DefaultAliasMarker, b"bare")) |
|---|
| 449 | self.failUnlessReallyEqual(ga3(u"baredir/file"), |
|---|
| 450 | (DefaultAliasMarker, b"baredir/file")) |
|---|
| 451 | self.failUnlessReallyEqual(ga3(u"baredir/file:7"), |
|---|
| 452 | (DefaultAliasMarker, b"baredir/file:7")) |
|---|
| 453 | self.failUnlessReallyEqual(ga3(u"baredir/sub:1/file:7"), |
|---|
| 454 | (DefaultAliasMarker, b"baredir/sub:1/file:7")) |
|---|
| 455 | self.failUnlessReallyEqual(ga3(u"tahoe:"), (TA, b"")) |
|---|
| 456 | self.failUnlessReallyEqual(ga3(u"tahoe:file"), (TA, b"file")) |
|---|
| 457 | self.failUnlessReallyEqual(ga3(u"tahoe:dir/file"), (TA, b"dir/file")) |
|---|
| 458 | self.failUnlessReallyEqual(ga3(u"c:"), (DefaultAliasMarker, b"c:")) |
|---|
| 459 | self.failUnlessReallyEqual(ga3(u"c:file"), (DefaultAliasMarker, b"c:file")) |
|---|
| 460 | self.failUnlessReallyEqual(ga3(u"c:dir/file"), |
|---|
| 461 | (DefaultAliasMarker, b"c:dir/file")) |
|---|
| 462 | self.failUnlessReallyEqual(ga3(u"work:"), (WA, b"")) |
|---|
| 463 | self.failUnlessReallyEqual(ga3(u"work:file"), (WA, b"file")) |
|---|
| 464 | self.failUnlessReallyEqual(ga3(u"work:dir/file"), (WA, b"dir/file")) |
|---|
| 465 | self.failUnlessReallyEqual(ga3(u"URI:stuff"), (b"URI:stuff", b"")) |
|---|
| 466 | self.failUnlessReallyEqual(ga3(u"URI:stuff:./file"), (b"URI:stuff", b"file")) |
|---|
| 467 | self.failUnlessReallyEqual(ga3(u"URI:stuff:./dir/file"), (b"URI:stuff", b"dir/file")) |
|---|
| 468 | self.failUnlessRaises(common.UnknownAliasError, ga3, u"missing:") |
|---|
| 469 | self.failUnlessRaises(common.UnknownAliasError, ga3, u"missing:dir") |
|---|
| 470 | self.failUnlessRaises(common.UnknownAliasError, ga3, u"missing:dir/file") |
|---|
| 471 | # calling get_alias with a path that doesn't include an alias and |
|---|
| 472 | # default set to something that isn't in the aliases argument should |
|---|
| 473 | # raise an UnknownAliasError. |
|---|
| 474 | def ga4(path): |
|---|
| 475 | return get_alias(aliases, path, u"badddefault:") |
|---|
| 476 | self.failUnlessRaises(common.UnknownAliasError, ga4, u"afile") |
|---|
| 477 | self.failUnlessRaises(common.UnknownAliasError, ga4, u"a/dir/path/") |
|---|
| 478 | |
|---|
| 479 | def ga5(path): |
|---|
| 480 | old = common.pretend_platform_uses_lettercolon |
|---|
| 481 | try: |
|---|
| 482 | common.pretend_platform_uses_lettercolon = True |
|---|
| 483 | retval = get_alias(aliases, path, u"baddefault:") |
|---|
| 484 | finally: |
|---|
| 485 | common.pretend_platform_uses_lettercolon = old |
|---|
| 486 | return retval |
|---|
| 487 | self.failUnlessRaises(common.UnknownAliasError, ga5, u"C:\\Windows") |
|---|
| 488 | |
|---|
| 489 | def test_alias_tolerance(self): |
|---|
| 490 | def s128(c): return base32.b2a(c*(128//8)) |
|---|
| 491 | def s256(c): return base32.b2a(c*(256//8)) |
|---|
| 492 | TA = b"URI:DIR2:%s:%s" % (s128(b"T"), s256(b"T")) |
|---|
| 493 | aliases = {"present": TA, |
|---|
| 494 | "future": b"URI-FROM-FUTURE:ooh:aah"} |
|---|
| 495 | def ga1(path): |
|---|
| 496 | return get_alias(aliases, path, u"tahoe") |
|---|
| 497 | self.failUnlessReallyEqual(ga1(u"present:file"), (TA, b"file")) |
|---|
| 498 | # this throws, via assert IDirnodeURI.providedBy(), since get_alias() |
|---|
| 499 | # wants a dirnode, and the future cap gives us UnknownURI instead. |
|---|
| 500 | self.failUnlessRaises(AssertionError, ga1, u"future:stuff") |
|---|
| 501 | |
|---|
| 502 | def test_listdir_unicode_good(self): |
|---|
| 503 | filenames = [u'L\u00F4zane', u'Bern', u'Gen\u00E8ve'] # must be NFC |
|---|
| 504 | |
|---|
| 505 | for name in filenames: |
|---|
| 506 | skip_if_cannot_represent_filename(name) |
|---|
| 507 | |
|---|
| 508 | basedir = "cli/common/listdir_unicode_good" |
|---|
| 509 | fileutil.make_dirs(basedir) |
|---|
| 510 | |
|---|
| 511 | for name in filenames: |
|---|
| 512 | open(os.path.join(str(basedir), name), "wb").close() |
|---|
| 513 | |
|---|
| 514 | for file in listdir_unicode(str(basedir)): |
|---|
| 515 | self.failUnlessIn(normalize(file), filenames) |
|---|
| 516 | |
|---|
| 517 | def test_exception_catcher(self): |
|---|
| 518 | """ |
|---|
| 519 | An exception that is otherwise unhandled during argument dispatch is |
|---|
| 520 | written to stderr and causes the process to exit with code 1. |
|---|
| 521 | """ |
|---|
| 522 | self.basedir = "cli/exception_catcher" |
|---|
| 523 | |
|---|
| 524 | exc = Exception("canary") |
|---|
| 525 | class BrokenOptions: |
|---|
| 526 | def parseOptions(self, argv): |
|---|
| 527 | raise exc |
|---|
| 528 | |
|---|
| 529 | stderr = StringIO() |
|---|
| 530 | |
|---|
| 531 | reactor = MemoryReactor() |
|---|
| 532 | |
|---|
| 533 | with AlternateReactor(reactor): |
|---|
| 534 | with self.assertRaises(SystemExit) as ctx: |
|---|
| 535 | runner.run( |
|---|
| 536 | configFactory=BrokenOptions, |
|---|
| 537 | argv=["tahoe"], |
|---|
| 538 | stderr=stderr, |
|---|
| 539 | ) |
|---|
| 540 | |
|---|
| 541 | self.assertTrue(reactor.hasRun) |
|---|
| 542 | self.assertFalse(reactor.running) |
|---|
| 543 | |
|---|
| 544 | self.failUnlessIn(str(exc), stderr.getvalue()) |
|---|
| 545 | self.assertEqual(1, ctx.exception.code) |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | class Help(unittest.TestCase): |
|---|
| 549 | def failUnlessInNormalized(self, x, y): |
|---|
| 550 | # helper function to deal with the --help output being wrapped to |
|---|
| 551 | # various widths, depending on the $COLUMNS environment variable |
|---|
| 552 | self.failUnlessIn(x.replace("\n", " "), y.replace("\n", " ")) |
|---|
| 553 | |
|---|
| 554 | def test_get(self): |
|---|
| 555 | help = str(cli.GetOptions()) |
|---|
| 556 | self.failUnlessIn("[options] REMOTE_FILE LOCAL_FILE", help) |
|---|
| 557 | self.failUnlessIn("% tahoe get FOO |less", help) |
|---|
| 558 | |
|---|
| 559 | def test_put(self): |
|---|
| 560 | help = str(cli.PutOptions()) |
|---|
| 561 | self.failUnlessIn("[options] LOCAL_FILE REMOTE_FILE", help) |
|---|
| 562 | self.failUnlessIn("% cat FILE | tahoe put", help) |
|---|
| 563 | |
|---|
| 564 | def test_ls(self): |
|---|
| 565 | help = str(cli.ListOptions()) |
|---|
| 566 | self.failUnlessIn("[options] [PATH]", help) |
|---|
| 567 | |
|---|
| 568 | def test_unlink(self): |
|---|
| 569 | help = str(cli.UnlinkOptions()) |
|---|
| 570 | self.failUnlessIn("[options] REMOTE_FILE", help) |
|---|
| 571 | |
|---|
| 572 | def test_mv(self): |
|---|
| 573 | help = str(cli.MvOptions()) |
|---|
| 574 | self.failUnlessIn("[options] FROM TO", help) |
|---|
| 575 | self.failUnlessInNormalized("Use 'tahoe mv' to move files", help) |
|---|
| 576 | |
|---|
| 577 | def test_cp(self): |
|---|
| 578 | help = str(cli.CpOptions()) |
|---|
| 579 | self.failUnlessIn("[options] FROM.. TO", help) |
|---|
| 580 | self.failUnlessInNormalized("Use 'tahoe cp' to copy files", help) |
|---|
| 581 | |
|---|
| 582 | def test_ln(self): |
|---|
| 583 | help = str(cli.LnOptions()) |
|---|
| 584 | self.failUnlessIn("[options] FROM_LINK TO_LINK", help) |
|---|
| 585 | self.failUnlessInNormalized("Use 'tahoe ln' to duplicate a link", help) |
|---|
| 586 | |
|---|
| 587 | def test_mkdir(self): |
|---|
| 588 | help = str(cli.MakeDirectoryOptions()) |
|---|
| 589 | self.failUnlessIn("[options] [REMOTE_DIR]", help) |
|---|
| 590 | self.failUnlessInNormalized("Create a new directory", help) |
|---|
| 591 | |
|---|
| 592 | def test_backup(self): |
|---|
| 593 | help = str(cli.BackupOptions()) |
|---|
| 594 | self.failUnlessIn("[options] FROM ALIAS:TO", help) |
|---|
| 595 | |
|---|
| 596 | def test_webopen(self): |
|---|
| 597 | help = str(cli.WebopenOptions()) |
|---|
| 598 | self.failUnlessIn("[options] [ALIAS:PATH]", help) |
|---|
| 599 | |
|---|
| 600 | def test_manifest(self): |
|---|
| 601 | help = str(cli.ManifestOptions()) |
|---|
| 602 | self.failUnlessIn("[options] [ALIAS:PATH]", help) |
|---|
| 603 | |
|---|
| 604 | def test_stats(self): |
|---|
| 605 | help = str(cli.StatsOptions()) |
|---|
| 606 | self.failUnlessIn("[options] [ALIAS:PATH]", help) |
|---|
| 607 | |
|---|
| 608 | def test_check(self): |
|---|
| 609 | help = str(cli.CheckOptions()) |
|---|
| 610 | self.failUnlessIn("[options] [ALIAS:PATH]", help) |
|---|
| 611 | |
|---|
| 612 | def test_deep_check(self): |
|---|
| 613 | help = str(cli.DeepCheckOptions()) |
|---|
| 614 | self.failUnlessIn("[options] [ALIAS:PATH]", help) |
|---|
| 615 | |
|---|
| 616 | def test_create_alias(self): |
|---|
| 617 | help = str(cli.CreateAliasOptions()) |
|---|
| 618 | self.failUnlessIn("[options] ALIAS[:]", help) |
|---|
| 619 | |
|---|
| 620 | def test_add_alias(self): |
|---|
| 621 | help = str(cli.AddAliasOptions()) |
|---|
| 622 | self.failUnlessIn("[options] ALIAS[:] DIRCAP", help) |
|---|
| 623 | |
|---|
| 624 | def test_list_aliases(self): |
|---|
| 625 | help = str(cli.ListAliasesOptions()) |
|---|
| 626 | self.failUnlessIn("[options]", help) |
|---|
| 627 | |
|---|
| 628 | def test_run(self): |
|---|
| 629 | help = str(tahoe_run.RunOptions()) |
|---|
| 630 | self.failUnlessIn("[options] [NODEDIR [twistd-options]]", help) |
|---|
| 631 | |
|---|
| 632 | def test_create_client(self): |
|---|
| 633 | help = str(create_node.CreateClientOptions()) |
|---|
| 634 | self.failUnlessIn("[options] [NODEDIR]", help) |
|---|
| 635 | |
|---|
| 636 | def test_create_node(self): |
|---|
| 637 | help = str(create_node.CreateNodeOptions()) |
|---|
| 638 | self.failUnlessIn("[options] [NODEDIR]", help) |
|---|
| 639 | |
|---|
| 640 | def test_create_introducer(self): |
|---|
| 641 | help = str(create_node.CreateIntroducerOptions()) |
|---|
| 642 | self.failUnlessIn("[options] NODEDIR", help) |
|---|
| 643 | |
|---|
| 644 | def test_debug_flogtool(self): |
|---|
| 645 | options = debug.FlogtoolOptions() |
|---|
| 646 | help = str(options) |
|---|
| 647 | self.failUnlessIn(" [global-options] debug flogtool ", help) |
|---|
| 648 | self.failUnlessInNormalized("The 'tahoe debug flogtool' command uses the correct imports", help) |
|---|
| 649 | |
|---|
| 650 | for (option, shortcut, oClass, desc) in options.subCommands: |
|---|
| 651 | subhelp = str(oClass()) |
|---|
| 652 | self.failUnlessIn(" [global-options] debug flogtool %s " % (option,), subhelp) |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | class Ln(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 656 | def _create_test_file(self): |
|---|
| 657 | data = "puppies" * 1000 |
|---|
| 658 | path = os.path.join(self.basedir, "datafile") |
|---|
| 659 | fileutil.write(path, data) |
|---|
| 660 | self.datafile = path |
|---|
| 661 | |
|---|
| 662 | def test_ln_without_alias(self): |
|---|
| 663 | # if invoked without an alias when the 'tahoe' alias doesn't |
|---|
| 664 | # exist, 'tahoe ln' should output a useful error message and not |
|---|
| 665 | # a stack trace |
|---|
| 666 | self.basedir = "cli/Ln/ln_without_alias" |
|---|
| 667 | self.set_up_grid(oneshare=True) |
|---|
| 668 | d = self.do_cli("ln", "from", "to") |
|---|
| 669 | def _check(args): |
|---|
| 670 | (rc, out, err) = args |
|---|
| 671 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 672 | self.failUnlessIn("error:", err) |
|---|
| 673 | self.assertEqual(out, "") |
|---|
| 674 | d.addCallback(_check) |
|---|
| 675 | # Make sure that validation extends to the "to" parameter |
|---|
| 676 | d.addCallback(lambda ign: self.do_cli("create-alias", "havasu")) |
|---|
| 677 | d.addCallback(lambda ign: self._create_test_file()) |
|---|
| 678 | d.addCallback(lambda ign: self.do_cli("put", self.datafile, |
|---|
| 679 | "havasu:from")) |
|---|
| 680 | d.addCallback(lambda ign: self.do_cli("ln", "havasu:from", "to")) |
|---|
| 681 | d.addCallback(_check) |
|---|
| 682 | return d |
|---|
| 683 | |
|---|
| 684 | def test_ln_with_nonexistent_alias(self): |
|---|
| 685 | # If invoked with aliases that don't exist, 'tahoe ln' should |
|---|
| 686 | # output a useful error message and not a stack trace. |
|---|
| 687 | self.basedir = "cli/Ln/ln_with_nonexistent_alias" |
|---|
| 688 | self.set_up_grid(oneshare=True) |
|---|
| 689 | d = self.do_cli("ln", "havasu:from", "havasu:to") |
|---|
| 690 | def _check(args): |
|---|
| 691 | (rc, out, err) = args |
|---|
| 692 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 693 | self.failUnlessIn("error:", err) |
|---|
| 694 | d.addCallback(_check) |
|---|
| 695 | # Make sure that validation occurs on the to parameter if the |
|---|
| 696 | # from parameter passes. |
|---|
| 697 | d.addCallback(lambda ign: self.do_cli("create-alias", "havasu")) |
|---|
| 698 | d.addCallback(lambda ign: self._create_test_file()) |
|---|
| 699 | d.addCallback(lambda ign: self.do_cli("put", self.datafile, |
|---|
| 700 | "havasu:from")) |
|---|
| 701 | d.addCallback(lambda ign: self.do_cli("ln", "havasu:from", "huron:to")) |
|---|
| 702 | d.addCallback(_check) |
|---|
| 703 | return d |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | class Admin(unittest.TestCase): |
|---|
| 707 | def test_generate_keypair(self): |
|---|
| 708 | d = run_cli("admin", "generate-keypair") |
|---|
| 709 | def _done(args): |
|---|
| 710 | (rc, stdout, stderr) = args |
|---|
| 711 | lines = [line.strip() for line in stdout.splitlines()] |
|---|
| 712 | privkey_bits = lines[0].split() |
|---|
| 713 | pubkey_bits = lines[1].split() |
|---|
| 714 | sk_header = "private:" |
|---|
| 715 | vk_header = "public:" |
|---|
| 716 | self.failUnlessEqual(privkey_bits[0], sk_header, lines[0]) |
|---|
| 717 | self.failUnlessEqual(pubkey_bits[0], vk_header, lines[1]) |
|---|
| 718 | self.failUnless(privkey_bits[1].startswith("priv-v0-"), lines[0]) |
|---|
| 719 | self.failUnless(pubkey_bits[1].startswith("pub-v0-"), lines[1]) |
|---|
| 720 | sk, pk = ed25519.signing_keypair_from_string( |
|---|
| 721 | privkey_bits[1].encode("ascii")) |
|---|
| 722 | vk_bytes = pubkey_bits[1].encode("ascii") |
|---|
| 723 | self.assertEqual( |
|---|
| 724 | ed25519.string_from_verifying_key(pk), |
|---|
| 725 | vk_bytes, |
|---|
| 726 | ) |
|---|
| 727 | d.addCallback(_done) |
|---|
| 728 | return d |
|---|
| 729 | |
|---|
| 730 | def test_derive_pubkey(self): |
|---|
| 731 | priv_key, pub_key = ed25519.create_signing_keypair() |
|---|
| 732 | priv_key_str = str(ed25519.string_from_signing_key(priv_key), "ascii") |
|---|
| 733 | pub_key_str = str(ed25519.string_from_verifying_key(pub_key), "ascii") |
|---|
| 734 | d = run_cli("admin", "derive-pubkey", priv_key_str) |
|---|
| 735 | def _done(args): |
|---|
| 736 | (rc, stdout, stderr) = args |
|---|
| 737 | lines = stdout.split("\n") |
|---|
| 738 | privkey_line = lines[0].strip() |
|---|
| 739 | pubkey_line = lines[1].strip() |
|---|
| 740 | sk_header = "private: priv-v0-" |
|---|
| 741 | vk_header = "public: pub-v0-" |
|---|
| 742 | self.failUnless(privkey_line.startswith(sk_header), privkey_line) |
|---|
| 743 | self.failUnless(pubkey_line.startswith(vk_header), pubkey_line) |
|---|
| 744 | pub_key_str2 = pubkey_line[len(vk_header):] |
|---|
| 745 | self.assertEqual("pub-v0-" + pub_key_str2, pub_key_str) |
|---|
| 746 | d.addCallback(_done) |
|---|
| 747 | return d |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | class Errors(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 751 | def test_get(self): |
|---|
| 752 | self.basedir = "cli/Errors/get" |
|---|
| 753 | self.set_up_grid() |
|---|
| 754 | c0 = self.g.clients[0] |
|---|
| 755 | self.fileurls = {} |
|---|
| 756 | DATA = b"data" * 100 |
|---|
| 757 | d = c0.upload(upload.Data(DATA, convergence=b"")) |
|---|
| 758 | def _stash_bad(ur): |
|---|
| 759 | self.uri_1share = ur.get_uri() |
|---|
| 760 | self.delete_shares_numbered(ur.get_uri(), list(range(1,10))) |
|---|
| 761 | d.addCallback(_stash_bad) |
|---|
| 762 | |
|---|
| 763 | # the download is abandoned as soon as it's clear that we won't get |
|---|
| 764 | # enough shares. The one remaining share might be in either the |
|---|
| 765 | # COMPLETE or the PENDING state. |
|---|
| 766 | in_complete_msg = "ran out of shares: complete=sh0 pending= overdue= unused= need 3" |
|---|
| 767 | in_pending_msg_regex = r"ran out of shares: complete= pending=Share\(.+\) overdue= unused= need 3" |
|---|
| 768 | |
|---|
| 769 | d.addCallback(lambda ign: self.do_cli("get", self.uri_1share)) |
|---|
| 770 | def _check1(args): |
|---|
| 771 | (rc, out, err) = args |
|---|
| 772 | self.failIfEqual(rc, 0) |
|---|
| 773 | self.failUnless("410 Gone" in err, err) |
|---|
| 774 | self.failUnlessIn("NotEnoughSharesError: ", err) |
|---|
| 775 | self.failUnless(in_complete_msg in err or re.search(in_pending_msg_regex, err)) |
|---|
| 776 | d.addCallback(_check1) |
|---|
| 777 | |
|---|
| 778 | targetf = os.path.join(self.basedir, "output") |
|---|
| 779 | d.addCallback(lambda ign: self.do_cli("get", self.uri_1share, targetf)) |
|---|
| 780 | def _check2(args): |
|---|
| 781 | (rc, out, err) = args |
|---|
| 782 | self.failIfEqual(rc, 0) |
|---|
| 783 | self.failUnless("410 Gone" in err, err) |
|---|
| 784 | self.failUnlessIn("NotEnoughSharesError: ", err) |
|---|
| 785 | self.failUnless(in_complete_msg in err or re.search(in_pending_msg_regex, err)) |
|---|
| 786 | self.failIf(os.path.exists(targetf)) |
|---|
| 787 | d.addCallback(_check2) |
|---|
| 788 | |
|---|
| 789 | return d |
|---|
| 790 | |
|---|
| 791 | def test_broken_socket(self): |
|---|
| 792 | # When the http connection breaks (such as when node.url is overwritten |
|---|
| 793 | # by a confused user), a user friendly error message should be printed. |
|---|
| 794 | self.basedir = "cli/Errors/test_broken_socket" |
|---|
| 795 | self.set_up_grid(oneshare=True) |
|---|
| 796 | |
|---|
| 797 | # Simulate a connection error |
|---|
| 798 | def _socket_error(*args, **kwargs): |
|---|
| 799 | raise socket_error('test error') |
|---|
| 800 | self.patch(allmydata.scripts.common_http.http_client.HTTPConnection, |
|---|
| 801 | "endheaders", _socket_error) |
|---|
| 802 | |
|---|
| 803 | d = self.do_cli("mkdir") |
|---|
| 804 | def _check_invalid(args): |
|---|
| 805 | (rc, stdout, stderr) = args |
|---|
| 806 | self.failIfEqual(rc, 0) |
|---|
| 807 | self.failUnlessIn("Error trying to connect to http://127.0.0.1", stderr) |
|---|
| 808 | d.addCallback(_check_invalid) |
|---|
| 809 | return d |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | class Get(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 813 | def test_get_without_alias(self): |
|---|
| 814 | # 'tahoe get' should output a useful error message when invoked |
|---|
| 815 | # without an explicit alias and when the default 'tahoe' alias |
|---|
| 816 | # hasn't been created yet. |
|---|
| 817 | self.basedir = "cli/Get/get_without_alias" |
|---|
| 818 | self.set_up_grid(oneshare=True) |
|---|
| 819 | d = self.do_cli('get', 'file') |
|---|
| 820 | def _check(args): |
|---|
| 821 | (rc, out, err) = args |
|---|
| 822 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 823 | self.failUnlessIn("error:", err) |
|---|
| 824 | self.assertEqual(out, "") |
|---|
| 825 | d.addCallback(_check) |
|---|
| 826 | return d |
|---|
| 827 | |
|---|
| 828 | def test_get_with_nonexistent_alias(self): |
|---|
| 829 | # 'tahoe get' should output a useful error message when invoked with |
|---|
| 830 | # an explicit alias that doesn't exist. |
|---|
| 831 | self.basedir = "cli/Get/get_with_nonexistent_alias" |
|---|
| 832 | self.set_up_grid(oneshare=True) |
|---|
| 833 | d = self.do_cli("get", "nonexistent:file") |
|---|
| 834 | def _check(args): |
|---|
| 835 | (rc, out, err) = args |
|---|
| 836 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 837 | self.failUnlessIn("error:", err) |
|---|
| 838 | self.failUnlessIn("nonexistent", err) |
|---|
| 839 | self.assertEqual(out, "") |
|---|
| 840 | d.addCallback(_check) |
|---|
| 841 | return d |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | class Manifest(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 845 | def test_manifest_without_alias(self): |
|---|
| 846 | # 'tahoe manifest' should output a useful error message when invoked |
|---|
| 847 | # without an explicit alias when the default 'tahoe' alias is |
|---|
| 848 | # missing. |
|---|
| 849 | self.basedir = "cli/Manifest/manifest_without_alias" |
|---|
| 850 | self.set_up_grid(oneshare=True) |
|---|
| 851 | d = self.do_cli("manifest") |
|---|
| 852 | def _check(args): |
|---|
| 853 | (rc, out, err) = args |
|---|
| 854 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 855 | self.failUnlessIn("error:", err) |
|---|
| 856 | self.assertEqual(out, "") |
|---|
| 857 | d.addCallback(_check) |
|---|
| 858 | return d |
|---|
| 859 | |
|---|
| 860 | def test_manifest_with_nonexistent_alias(self): |
|---|
| 861 | # 'tahoe manifest' should output a useful error message when invoked |
|---|
| 862 | # with an explicit alias that doesn't exist. |
|---|
| 863 | self.basedir = "cli/Manifest/manifest_with_nonexistent_alias" |
|---|
| 864 | self.set_up_grid(oneshare=True) |
|---|
| 865 | d = self.do_cli("manifest", "nonexistent:") |
|---|
| 866 | def _check(args): |
|---|
| 867 | (rc, out, err) = args |
|---|
| 868 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 869 | self.failUnlessIn("error:", err) |
|---|
| 870 | self.failUnlessIn("nonexistent", err) |
|---|
| 871 | self.assertEqual(out, "") |
|---|
| 872 | d.addCallback(_check) |
|---|
| 873 | return d |
|---|
| 874 | |
|---|
| 875 | |
|---|
| 876 | class Mkdir(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 877 | def test_mkdir(self): |
|---|
| 878 | self.basedir = os.path.dirname(self.mktemp()) |
|---|
| 879 | self.set_up_grid(oneshare=True) |
|---|
| 880 | |
|---|
| 881 | d = self.do_cli("create-alias", "tahoe") |
|---|
| 882 | d.addCallback(lambda res: self.do_cli("mkdir", "test")) |
|---|
| 883 | def _check(args): |
|---|
| 884 | (rc, out, err) = args |
|---|
| 885 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 886 | self.assertEqual(err, "") |
|---|
| 887 | self.failUnlessIn("URI:", out) |
|---|
| 888 | d.addCallback(_check) |
|---|
| 889 | |
|---|
| 890 | return d |
|---|
| 891 | |
|---|
| 892 | def test_mkdir_mutable_type(self): |
|---|
| 893 | self.basedir = os.path.dirname(self.mktemp()) |
|---|
| 894 | self.set_up_grid(oneshare=True) |
|---|
| 895 | d = self.do_cli("create-alias", "tahoe") |
|---|
| 896 | def _check(args, st): |
|---|
| 897 | (rc, out, err) = args |
|---|
| 898 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 899 | self.assertEqual(err, "") |
|---|
| 900 | self.failUnlessIn(st, out) |
|---|
| 901 | return out |
|---|
| 902 | |
|---|
| 903 | def _mkdir(ign, mutable_type, uri_prefix, dirname): |
|---|
| 904 | """ |
|---|
| 905 | :param str mutable_type: 'sdmf' or 'mdmf' (or uppercase versions) |
|---|
| 906 | :param str uri_prefix: kind of URI |
|---|
| 907 | :param str dirname: the directory alias |
|---|
| 908 | """ |
|---|
| 909 | d2 = self.do_cli("mkdir", "--format={}".format(mutable_type), dirname) |
|---|
| 910 | d2.addCallback(_check, uri_prefix) |
|---|
| 911 | def _stash_filecap(cap): |
|---|
| 912 | u = uri.from_string(cap) |
|---|
| 913 | fn_uri = u.get_filenode_cap() |
|---|
| 914 | self._filecap = fn_uri.to_string() |
|---|
| 915 | d2.addCallback(_stash_filecap) |
|---|
| 916 | d2.addCallback(lambda ign: self.do_cli("ls", "--json", dirname)) |
|---|
| 917 | d2.addCallback(_check, uri_prefix) |
|---|
| 918 | d2.addCallback(lambda ign: self.do_cli("ls", "--json", self._filecap)) |
|---|
| 919 | d2.addCallback(_check, '"format": "%s"' % (mutable_type.upper(),)) |
|---|
| 920 | return d2 |
|---|
| 921 | |
|---|
| 922 | d.addCallback(_mkdir, "sdmf", "URI:DIR2", "tahoe:foo") |
|---|
| 923 | d.addCallback(_mkdir, "SDMF", "URI:DIR2", "tahoe:foo2") |
|---|
| 924 | d.addCallback(_mkdir, "mdmf", "URI:DIR2-MDMF", "tahoe:bar") |
|---|
| 925 | d.addCallback(_mkdir, "MDMF", "URI:DIR2-MDMF", "tahoe:bar2") |
|---|
| 926 | return d |
|---|
| 927 | |
|---|
| 928 | def test_mkdir_mutable_type_unlinked(self): |
|---|
| 929 | self.basedir = os.path.dirname(self.mktemp()) |
|---|
| 930 | self.set_up_grid(oneshare=True) |
|---|
| 931 | d = self.do_cli("mkdir", "--format=SDMF") |
|---|
| 932 | def _check(args, st): |
|---|
| 933 | (rc, out, err) = args |
|---|
| 934 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 935 | self.assertEqual(err, "") |
|---|
| 936 | self.failUnlessIn(st, out) |
|---|
| 937 | return out |
|---|
| 938 | d.addCallback(_check, "URI:DIR2") |
|---|
| 939 | def _stash_dircap(cap): |
|---|
| 940 | self._dircap = cap |
|---|
| 941 | # Now we're going to feed the cap into uri.from_string... |
|---|
| 942 | u = uri.from_string(cap) |
|---|
| 943 | # ...grab the underlying filenode uri. |
|---|
| 944 | fn_uri = u.get_filenode_cap() |
|---|
| 945 | # ...and stash that. |
|---|
| 946 | self._filecap = fn_uri.to_string() |
|---|
| 947 | d.addCallback(_stash_dircap) |
|---|
| 948 | d.addCallback(lambda res: self.do_cli("ls", "--json", |
|---|
| 949 | self._filecap)) |
|---|
| 950 | d.addCallback(_check, '"format": "SDMF"') |
|---|
| 951 | d.addCallback(lambda res: self.do_cli("mkdir", "--format=MDMF")) |
|---|
| 952 | d.addCallback(_check, "URI:DIR2-MDMF") |
|---|
| 953 | d.addCallback(_stash_dircap) |
|---|
| 954 | d.addCallback(lambda res: self.do_cli("ls", "--json", |
|---|
| 955 | self._filecap)) |
|---|
| 956 | d.addCallback(_check, '"format": "MDMF"') |
|---|
| 957 | return d |
|---|
| 958 | |
|---|
| 959 | def test_mkdir_bad_mutable_type(self): |
|---|
| 960 | o = cli.MakeDirectoryOptions() |
|---|
| 961 | self.failUnlessRaises(usage.UsageError, |
|---|
| 962 | o.parseOptions, |
|---|
| 963 | ["--format=LDMF"]) |
|---|
| 964 | |
|---|
| 965 | def test_mkdir_unicode(self): |
|---|
| 966 | self.basedir = os.path.dirname(self.mktemp()) |
|---|
| 967 | self.set_up_grid(oneshare=True) |
|---|
| 968 | |
|---|
| 969 | try: |
|---|
| 970 | motorhead_arg = u"tahoe:Mot\u00F6rhead".encode(get_io_encoding()) |
|---|
| 971 | except UnicodeEncodeError: |
|---|
| 972 | raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.") |
|---|
| 973 | |
|---|
| 974 | d = self.do_cli("create-alias", "tahoe") |
|---|
| 975 | d.addCallback(lambda res: self.do_cli("mkdir", motorhead_arg)) |
|---|
| 976 | def _check(args): |
|---|
| 977 | (rc, out, err) = args |
|---|
| 978 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 979 | self.assertEqual(err, "") |
|---|
| 980 | self.failUnlessIn("URI:", out) |
|---|
| 981 | d.addCallback(_check) |
|---|
| 982 | |
|---|
| 983 | return d |
|---|
| 984 | |
|---|
| 985 | def test_mkdir_with_nonexistent_alias(self): |
|---|
| 986 | # when invoked with an alias that doesn't exist, 'tahoe mkdir' should |
|---|
| 987 | # output a sensible error message rather than a stack trace. |
|---|
| 988 | self.basedir = "cli/Mkdir/mkdir_with_nonexistent_alias" |
|---|
| 989 | self.set_up_grid(oneshare=True) |
|---|
| 990 | d = self.do_cli("mkdir", "havasu:") |
|---|
| 991 | def _check(args): |
|---|
| 992 | (rc, out, err) = args |
|---|
| 993 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 994 | self.failUnlessIn("error:", err) |
|---|
| 995 | self.assertEqual(out, "") |
|---|
| 996 | d.addCallback(_check) |
|---|
| 997 | return d |
|---|
| 998 | |
|---|
| 999 | |
|---|
| 1000 | class Unlink(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 1001 | command = "unlink" |
|---|
| 1002 | |
|---|
| 1003 | def _create_test_file(self): |
|---|
| 1004 | data = "puppies" * 1000 |
|---|
| 1005 | path = os.path.join(self.basedir, "datafile") |
|---|
| 1006 | fileutil.write(path, data) |
|---|
| 1007 | self.datafile = path |
|---|
| 1008 | |
|---|
| 1009 | def test_unlink_without_alias(self): |
|---|
| 1010 | # 'tahoe unlink' should behave sensibly when invoked without an explicit |
|---|
| 1011 | # alias before the default 'tahoe' alias has been created. |
|---|
| 1012 | self.basedir = "cli/Unlink/%s_without_alias" % (self.command,) |
|---|
| 1013 | self.set_up_grid(oneshare=True) |
|---|
| 1014 | d = self.do_cli(self.command, "afile") |
|---|
| 1015 | def _check(args): |
|---|
| 1016 | (rc, out, err) = args |
|---|
| 1017 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1018 | self.failUnlessIn("error:", err) |
|---|
| 1019 | self.assertEqual(out, "") |
|---|
| 1020 | d.addCallback(_check) |
|---|
| 1021 | |
|---|
| 1022 | d.addCallback(lambda ign: self.do_cli(self.command, "afile")) |
|---|
| 1023 | d.addCallback(_check) |
|---|
| 1024 | return d |
|---|
| 1025 | |
|---|
| 1026 | def test_unlink_with_nonexistent_alias(self): |
|---|
| 1027 | # 'tahoe unlink' should behave sensibly when invoked with an explicit |
|---|
| 1028 | # alias that doesn't exist. |
|---|
| 1029 | self.basedir = "cli/Unlink/%s_with_nonexistent_alias" % (self.command,) |
|---|
| 1030 | self.set_up_grid(oneshare=True) |
|---|
| 1031 | d = self.do_cli(self.command, "nonexistent:afile") |
|---|
| 1032 | def _check(args): |
|---|
| 1033 | (rc, out, err) = args |
|---|
| 1034 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1035 | self.failUnlessIn("error:", err) |
|---|
| 1036 | self.failUnlessIn("nonexistent", err) |
|---|
| 1037 | self.assertEqual(out, "") |
|---|
| 1038 | d.addCallback(_check) |
|---|
| 1039 | |
|---|
| 1040 | d.addCallback(lambda ign: self.do_cli(self.command, "nonexistent:afile")) |
|---|
| 1041 | d.addCallback(_check) |
|---|
| 1042 | return d |
|---|
| 1043 | |
|---|
| 1044 | def test_unlink_without_path(self): |
|---|
| 1045 | # 'tahoe unlink' should give a sensible error message when invoked without a path. |
|---|
| 1046 | self.basedir = "cli/Unlink/%s_without_path" % (self.command,) |
|---|
| 1047 | self.set_up_grid(oneshare=True) |
|---|
| 1048 | self._create_test_file() |
|---|
| 1049 | d = self.do_cli("create-alias", "tahoe") |
|---|
| 1050 | d.addCallback(lambda ign: self.do_cli("put", self.datafile, "tahoe:test")) |
|---|
| 1051 | def _do_unlink(args): |
|---|
| 1052 | (rc, out, err) = args |
|---|
| 1053 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 1054 | self.failUnless(out.startswith("URI:"), out) |
|---|
| 1055 | return self.do_cli(self.command, out.strip('\n')) |
|---|
| 1056 | d.addCallback(_do_unlink) |
|---|
| 1057 | |
|---|
| 1058 | def _check(args): |
|---|
| 1059 | (rc, out, err) = args |
|---|
| 1060 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1061 | self.failUnlessIn("'tahoe %s'" % (self.command,), err) |
|---|
| 1062 | self.failUnlessIn("path must be given", err) |
|---|
| 1063 | self.assertEqual(out, "") |
|---|
| 1064 | d.addCallback(_check) |
|---|
| 1065 | return d |
|---|
| 1066 | |
|---|
| 1067 | |
|---|
| 1068 | class Stats(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 1069 | def test_empty_directory(self): |
|---|
| 1070 | self.basedir = "cli/Stats/empty_directory" |
|---|
| 1071 | self.set_up_grid(oneshare=True) |
|---|
| 1072 | c0 = self.g.clients[0] |
|---|
| 1073 | self.fileurls = {} |
|---|
| 1074 | d = c0.create_dirnode() |
|---|
| 1075 | def _stash_root(n): |
|---|
| 1076 | self.rootnode = n |
|---|
| 1077 | self.rooturi = n.get_uri() |
|---|
| 1078 | d.addCallback(_stash_root) |
|---|
| 1079 | |
|---|
| 1080 | # make sure we can get stats on an empty directory too |
|---|
| 1081 | d.addCallback(lambda ign: self.do_cli("stats", self.rooturi)) |
|---|
| 1082 | def _check_stats(args): |
|---|
| 1083 | (rc, out, err) = args |
|---|
| 1084 | self.assertEqual(err, "") |
|---|
| 1085 | self.failUnlessReallyEqual(rc, 0) |
|---|
| 1086 | lines = out.splitlines() |
|---|
| 1087 | self.failUnlessIn(" count-immutable-files: 0", lines) |
|---|
| 1088 | self.failUnlessIn(" count-mutable-files: 0", lines) |
|---|
| 1089 | self.failUnlessIn(" count-literal-files: 0", lines) |
|---|
| 1090 | self.failUnlessIn(" count-directories: 1", lines) |
|---|
| 1091 | self.failUnlessIn(" size-immutable-files: 0", lines) |
|---|
| 1092 | self.failIfIn("Size Histogram:", lines) |
|---|
| 1093 | d.addCallback(_check_stats) |
|---|
| 1094 | |
|---|
| 1095 | return d |
|---|
| 1096 | |
|---|
| 1097 | def test_stats_without_alias(self): |
|---|
| 1098 | # when invoked with no explicit alias and before the default 'tahoe' |
|---|
| 1099 | # alias is created, 'tahoe stats' should output an informative error |
|---|
| 1100 | # message, not a stack trace. |
|---|
| 1101 | self.basedir = "cli/Stats/stats_without_alias" |
|---|
| 1102 | self.set_up_grid(oneshare=True) |
|---|
| 1103 | d = self.do_cli("stats") |
|---|
| 1104 | def _check(args): |
|---|
| 1105 | (rc, out, err) = args |
|---|
| 1106 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1107 | self.failUnlessIn("error:", err) |
|---|
| 1108 | self.assertEqual(out, "") |
|---|
| 1109 | d.addCallback(_check) |
|---|
| 1110 | return d |
|---|
| 1111 | |
|---|
| 1112 | def test_stats_with_nonexistent_alias(self): |
|---|
| 1113 | # when invoked with an explicit alias that doesn't exist, |
|---|
| 1114 | # 'tahoe stats' should output a useful error message. |
|---|
| 1115 | self.basedir = "cli/Stats/stats_with_nonexistent_alias" |
|---|
| 1116 | self.set_up_grid(oneshare=True) |
|---|
| 1117 | d = self.do_cli("stats", "havasu:") |
|---|
| 1118 | def _check(args): |
|---|
| 1119 | (rc, out, err) = args |
|---|
| 1120 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1121 | self.failUnlessIn("error:", err) |
|---|
| 1122 | self.assertEqual(out, "") |
|---|
| 1123 | d.addCallback(_check) |
|---|
| 1124 | return d |
|---|
| 1125 | |
|---|
| 1126 | |
|---|
| 1127 | class Webopen(GridTestMixin, CLITestMixin, unittest.TestCase): |
|---|
| 1128 | def test_webopen_with_nonexistent_alias(self): |
|---|
| 1129 | # when invoked with an alias that doesn't exist, 'tahoe webopen' |
|---|
| 1130 | # should output an informative error message instead of a stack |
|---|
| 1131 | # trace. |
|---|
| 1132 | self.basedir = "cli/Webopen/webopen_with_nonexistent_alias" |
|---|
| 1133 | self.set_up_grid(oneshare=True) |
|---|
| 1134 | d = self.do_cli("webopen", "fake:") |
|---|
| 1135 | def _check(args): |
|---|
| 1136 | (rc, out, err) = args |
|---|
| 1137 | self.failUnlessReallyEqual(rc, 1) |
|---|
| 1138 | self.failUnlessIn("error:", err) |
|---|
| 1139 | self.assertEqual(out, "") |
|---|
| 1140 | d.addCallback(_check) |
|---|
| 1141 | return d |
|---|
| 1142 | |
|---|
| 1143 | def test_webopen(self): |
|---|
| 1144 | # TODO: replace with @patch that supports Deferreds. |
|---|
| 1145 | import webbrowser |
|---|
| 1146 | def call_webbrowser_open(url): |
|---|
| 1147 | self.failUnlessIn(str(self.alias_uri, "ascii").replace(':', '%3A'), url) |
|---|
| 1148 | self.webbrowser_open_called = True |
|---|
| 1149 | def _cleanup(res): |
|---|
| 1150 | webbrowser.open = self.old_webbrowser_open |
|---|
| 1151 | return res |
|---|
| 1152 | |
|---|
| 1153 | self.old_webbrowser_open = webbrowser.open |
|---|
| 1154 | try: |
|---|
| 1155 | webbrowser.open = call_webbrowser_open |
|---|
| 1156 | |
|---|
| 1157 | self.basedir = "cli/Webopen/webopen" |
|---|
| 1158 | self.set_up_grid(oneshare=True) |
|---|
| 1159 | d = self.do_cli("create-alias", "alias:") |
|---|
| 1160 | def _check_alias(args): |
|---|
| 1161 | (rc, out, err) = args |
|---|
| 1162 | self.failUnlessReallyEqual(rc, 0, repr((rc, out, err))) |
|---|
| 1163 | self.failUnlessIn("Alias 'alias' created", out) |
|---|
| 1164 | self.assertEqual(err, "") |
|---|
| 1165 | self.alias_uri = get_aliases(self.get_clientdir())["alias"] |
|---|
| 1166 | d.addCallback(_check_alias) |
|---|
| 1167 | d.addCallback(lambda res: self.do_cli("webopen", "alias:")) |
|---|
| 1168 | def _check_webopen(args): |
|---|
| 1169 | (rc, out, err) = args |
|---|
| 1170 | self.failUnlessReallyEqual(rc, 0, repr((rc, out, err))) |
|---|
| 1171 | self.assertEqual(out, "") |
|---|
| 1172 | self.assertEqual(err, "") |
|---|
| 1173 | self.failUnless(self.webbrowser_open_called) |
|---|
| 1174 | d.addCallback(_check_webopen) |
|---|
| 1175 | d.addBoth(_cleanup) |
|---|
| 1176 | except: |
|---|
| 1177 | _cleanup(None) |
|---|
| 1178 | raise |
|---|
| 1179 | return d |
|---|
| 1180 | |
|---|
| 1181 | class Options(ReallyEqualMixin, unittest.TestCase): |
|---|
| 1182 | # this test case only looks at argument-processing and simple stuff. |
|---|
| 1183 | |
|---|
| 1184 | def parse(self, args, stdout=None): |
|---|
| 1185 | o = runner.Options() |
|---|
| 1186 | if stdout is not None: |
|---|
| 1187 | o.stdout = stdout |
|---|
| 1188 | o.parseOptions(args) |
|---|
| 1189 | while hasattr(o, "subOptions"): |
|---|
| 1190 | o = o.subOptions |
|---|
| 1191 | return o |
|---|
| 1192 | |
|---|
| 1193 | def test_list(self): |
|---|
| 1194 | fileutil.rm_dir("cli/test_options") |
|---|
| 1195 | fileutil.make_dirs("cli/test_options") |
|---|
| 1196 | fileutil.make_dirs("cli/test_options/private") |
|---|
| 1197 | fileutil.write("cli/test_options/node.url", "http://localhost:8080/\n") |
|---|
| 1198 | filenode_uri = uri.WriteableSSKFileURI(writekey=b"\x00"*16, |
|---|
| 1199 | fingerprint=b"\x00"*32) |
|---|
| 1200 | private_uri = uri.DirectoryURI(filenode_uri).to_string() |
|---|
| 1201 | fileutil.write("cli/test_options/private/root_dir.cap", private_uri + b"\n") |
|---|
| 1202 | def parse2(args): return parse_options("cli/test_options", "ls", args) |
|---|
| 1203 | o = parse2([]) |
|---|
| 1204 | self.failUnlessEqual(o['node-url'], "http://localhost:8080/") |
|---|
| 1205 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), private_uri) |
|---|
| 1206 | self.failUnlessEqual(o.where, u"") |
|---|
| 1207 | |
|---|
| 1208 | o = parse2(["--node-url", "http://example.org:8111/"]) |
|---|
| 1209 | self.failUnlessEqual(o['node-url'], "http://example.org:8111/") |
|---|
| 1210 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), private_uri) |
|---|
| 1211 | self.failUnlessEqual(o.where, u"") |
|---|
| 1212 | |
|---|
| 1213 | # -u for --node-url used to clash with -u for --uri (tickets #1949 and #2137). |
|---|
| 1214 | o = parse2(["-u", "http://example.org:8111/"]) |
|---|
| 1215 | self.failUnlessEqual(o['node-url'], "http://example.org:8111/") |
|---|
| 1216 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), private_uri) |
|---|
| 1217 | self.failUnlessEqual(o.where, u"") |
|---|
| 1218 | self.failIf(o["uri"]) |
|---|
| 1219 | |
|---|
| 1220 | o = parse2(["-u", "http://example.org:8111/", "--uri"]) |
|---|
| 1221 | self.failUnlessEqual(o['node-url'], "http://example.org:8111/") |
|---|
| 1222 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), private_uri) |
|---|
| 1223 | self.failUnlessEqual(o.where, u"") |
|---|
| 1224 | self.failUnless(o["uri"]) |
|---|
| 1225 | |
|---|
| 1226 | o = parse2(["--dir-cap", "root"]) |
|---|
| 1227 | self.failUnlessEqual(o['node-url'], "http://localhost:8080/") |
|---|
| 1228 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], "root") |
|---|
| 1229 | self.failUnlessEqual(o.where, u"") |
|---|
| 1230 | |
|---|
| 1231 | other_filenode_uri = uri.WriteableSSKFileURI(writekey=b"\x11"*16, |
|---|
| 1232 | fingerprint=b"\x11"*32) |
|---|
| 1233 | other_uri = uri.DirectoryURI(other_filenode_uri).to_string() |
|---|
| 1234 | o = parse2(["--dir-cap", other_uri]) |
|---|
| 1235 | self.failUnlessEqual(o['node-url'], "http://localhost:8080/") |
|---|
| 1236 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), other_uri) |
|---|
| 1237 | self.failUnlessEqual(o.where, u"") |
|---|
| 1238 | |
|---|
| 1239 | o = parse2(["--dir-cap", other_uri, "subdir"]) |
|---|
| 1240 | self.failUnlessEqual(o['node-url'], "http://localhost:8080/") |
|---|
| 1241 | self.failUnlessEqual(o.aliases[DEFAULT_ALIAS].encode("ascii"), other_uri) |
|---|
| 1242 | self.failUnlessEqual(o.where, u"subdir") |
|---|
| 1243 | |
|---|
| 1244 | self.failUnlessRaises(usage.UsageError, parse2, |
|---|
| 1245 | ["--node-url", "NOT-A-URL"]) |
|---|
| 1246 | |
|---|
| 1247 | o = parse2(["--node-url", "http://localhost:8080"]) |
|---|
| 1248 | self.failUnlessEqual(o["node-url"], "http://localhost:8080/") |
|---|
| 1249 | |
|---|
| 1250 | o = parse2(["--node-url", "https://localhost/"]) |
|---|
| 1251 | self.failUnlessEqual(o["node-url"], "https://localhost/") |
|---|
| 1252 | |
|---|
| 1253 | def test_version(self): |
|---|
| 1254 | # "tahoe --version" dumps text to stdout and exits |
|---|
| 1255 | stdout = StringIO() |
|---|
| 1256 | self.failUnlessRaises(SystemExit, self.parse, ["--version"], stdout) |
|---|
| 1257 | self.failUnlessIn(allmydata.__full_version__, stdout.getvalue()) |
|---|
| 1258 | # but "tahoe SUBCOMMAND --version" should be rejected |
|---|
| 1259 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1260 | ["run", "--version"]) |
|---|
| 1261 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1262 | ["run", "--version-and-path"]) |
|---|
| 1263 | |
|---|
| 1264 | def test_quiet(self): |
|---|
| 1265 | # accepted as an overall option, but not on subcommands |
|---|
| 1266 | o = self.parse(["--quiet", "run"]) |
|---|
| 1267 | self.failUnless(o.parent["quiet"]) |
|---|
| 1268 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1269 | ["run", "--quiet"]) |
|---|
| 1270 | |
|---|
| 1271 | def test_basedir(self): |
|---|
| 1272 | # accept a --node-directory option before the verb, or a --basedir |
|---|
| 1273 | # option after, or a basedir argument after, but none in the wrong |
|---|
| 1274 | # place, and not more than one of the three. |
|---|
| 1275 | |
|---|
| 1276 | # Here is some option twistd recognizes but we don't. Depending on |
|---|
| 1277 | # where it appears, it should be passed through to twistd. It doesn't |
|---|
| 1278 | # really matter which option it is (it doesn't even have to be a valid |
|---|
| 1279 | # option). This test does not actually run any of the twistd argument |
|---|
| 1280 | # parsing. |
|---|
| 1281 | some_twistd_option = "--spew" |
|---|
| 1282 | |
|---|
| 1283 | o = self.parse(["run"]) |
|---|
| 1284 | self.failUnlessReallyEqual(o["basedir"], os.path.join(fileutil.abspath_expanduser_unicode(u"~"), |
|---|
| 1285 | u".tahoe")) |
|---|
| 1286 | o = self.parse(["run", "here"]) |
|---|
| 1287 | self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here")) |
|---|
| 1288 | o = self.parse(["run", "--basedir", "there"]) |
|---|
| 1289 | self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) |
|---|
| 1290 | o = self.parse(["--node-directory", "there", "run"]) |
|---|
| 1291 | self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) |
|---|
| 1292 | |
|---|
| 1293 | o = self.parse(["run", "here", some_twistd_option]) |
|---|
| 1294 | self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here")) |
|---|
| 1295 | |
|---|
| 1296 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1297 | ["--basedir", "there", "run"]) |
|---|
| 1298 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1299 | ["run", "--node-directory", "there"]) |
|---|
| 1300 | |
|---|
| 1301 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1302 | ["--node-directory=there", |
|---|
| 1303 | "run", "--basedir=here"]) |
|---|
| 1304 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1305 | ["run", "--basedir=here", "anywhere"]) |
|---|
| 1306 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1307 | ["--node-directory=there", |
|---|
| 1308 | "run", "anywhere"]) |
|---|
| 1309 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1310 | ["--node-directory=there", |
|---|
| 1311 | "run", "--basedir=here", "anywhere"]) |
|---|
| 1312 | |
|---|
| 1313 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1314 | ["--node-directory=there", "run", some_twistd_option]) |
|---|
| 1315 | self.failUnlessRaises(usage.UsageError, self.parse, |
|---|
| 1316 | ["run", "--basedir=here", some_twistd_option]) |
|---|