]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_cli.py
CLI: rework webopen, and moreover its tests w.r.t. path handling
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_cli.py
1
2 import os.path
3 from twisted.trial import unittest
4 from cStringIO import StringIO
5 import urllib
6
7 from pycryptopp.publickey import ecdsa
8 from allmydata.util import fileutil, hashutil, base32
9 from allmydata import uri
10
11 # at least import the CLI scripts, even if we don't have any real tests for
12 # them yet.
13 from allmydata.scripts import tahoe_ls, tahoe_get, tahoe_put, tahoe_rm
14 from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases
15 _hush_pyflakes = [tahoe_ls, tahoe_get, tahoe_put, tahoe_rm]
16
17 from allmydata.scripts import cli, debug, runner
18 from allmydata.test.common import SystemTestMixin
19 from twisted.internet import threads # CLI tests use deferToThread
20
21 class CLI(unittest.TestCase):
22     # this test case only looks at argument-processing and simple stuff.
23     def test_options(self):
24         fileutil.rm_dir("cli/test_options")
25         fileutil.make_dirs("cli/test_options")
26         fileutil.make_dirs("cli/test_options/private")
27         open("cli/test_options/node.url","w").write("http://localhost:8080/\n")
28         filenode_uri = uri.WriteableSSKFileURI(writekey="\x00"*16,
29                                                fingerprint="\x00"*32)
30         private_uri = uri.NewDirectoryURI(filenode_uri).to_string()
31         open("cli/test_options/private/root_dir.cap", "w").write(private_uri + "\n")
32         o = cli.ListOptions()
33         o.parseOptions(["--node-directory", "cli/test_options"])
34         self.failUnlessEqual(o['node-url'], "http://localhost:8080/")
35         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], private_uri)
36         self.failUnlessEqual(o.where, "")
37
38         o = cli.ListOptions()
39         o.parseOptions(["--node-directory", "cli/test_options",
40                         "--node-url", "http://example.org:8111/"])
41         self.failUnlessEqual(o['node-url'], "http://example.org:8111/")
42         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], private_uri)
43         self.failUnlessEqual(o.where, "")
44
45         o = cli.ListOptions()
46         o.parseOptions(["--node-directory", "cli/test_options",
47                         "--dir-cap", "root"])
48         self.failUnlessEqual(o['node-url'], "http://localhost:8080/")
49         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], "root")
50         self.failUnlessEqual(o.where, "")
51
52         o = cli.ListOptions()
53         other_filenode_uri = uri.WriteableSSKFileURI(writekey="\x11"*16,
54                                                      fingerprint="\x11"*32)
55         other_uri = uri.NewDirectoryURI(other_filenode_uri).to_string()
56         o.parseOptions(["--node-directory", "cli/test_options",
57                         "--dir-cap", other_uri])
58         self.failUnlessEqual(o['node-url'], "http://localhost:8080/")
59         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], other_uri)
60         self.failUnlessEqual(o.where, "")
61
62         o = cli.ListOptions()
63         o.parseOptions(["--node-directory", "cli/test_options",
64                         "--dir-cap", other_uri, "subdir"])
65         self.failUnlessEqual(o['node-url'], "http://localhost:8080/")
66         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], other_uri)
67         self.failUnlessEqual(o.where, "subdir")
68
69     def _dump_cap(self, *args):
70         config = debug.DumpCapOptions()
71         config.stdout,config.stderr = StringIO(), StringIO()
72         config.parseOptions(args)
73         debug.dump_cap(config)
74         self.failIf(config.stderr.getvalue())
75         output = config.stdout.getvalue()
76         return output
77
78     def test_dump_cap_chk(self):
79         key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
80         storage_index = hashutil.storage_index_hash(key)
81         uri_extension_hash = hashutil.uri_extension_hash("stuff")
82         needed_shares = 25
83         total_shares = 100
84         size = 1234
85         u = uri.CHKFileURI(key=key,
86                            uri_extension_hash=uri_extension_hash,
87                            needed_shares=needed_shares,
88                            total_shares=total_shares,
89                            size=size)
90         output = self._dump_cap(u.to_string())
91         self.failUnless("CHK File:" in output, output)
92         self.failUnless("key: aaaqeayeaudaocajbifqydiob4" in output, output)
93         self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output)
94         self.failUnless("size: 1234" in output, output)
95         self.failUnless("k/N: 25/100" in output, output)
96         self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output)
97
98         output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa",
99                                 u.to_string())
100         self.failUnless("client renewal secret: znxmki5zdibb5qlt46xbdvk2t55j7hibejq3i5ijyurkr6m6jkhq" in output, output)
101
102         output = self._dump_cap(u.get_verifier().to_string())
103         self.failIf("key: " in output, output)
104         self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output)
105         self.failUnless("size: 1234" in output, output)
106         self.failUnless("k/N: 25/100" in output, output)
107         self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output)
108
109         prefixed_u = "http://127.0.0.1/uri/%s" % urllib.quote(u.to_string())
110         output = self._dump_cap(prefixed_u)
111         self.failUnless("CHK File:" in output, output)
112         self.failUnless("key: aaaqeayeaudaocajbifqydiob4" in output, output)
113         self.failUnless("UEB hash: nf3nimquen7aeqm36ekgxomalstenpkvsdmf6fplj7swdatbv5oa" in output, output)
114         self.failUnless("size: 1234" in output, output)
115         self.failUnless("k/N: 25/100" in output, output)
116         self.failUnless("storage index: hdis5iaveku6lnlaiccydyid7q" in output, output)
117
118     def test_dump_cap_lit(self):
119         u = uri.LiteralFileURI("this is some data")
120         output = self._dump_cap(u.to_string())
121         self.failUnless("Literal File URI:" in output, output)
122         self.failUnless("data: this is some data" in output, output)
123
124     def test_dump_cap_ssk(self):
125         writekey = "\x01" * 16
126         fingerprint = "\xfe" * 32
127         u = uri.WriteableSSKFileURI(writekey, fingerprint)
128
129         output = self._dump_cap(u.to_string())
130         self.failUnless("SSK Writeable URI:" in output, output)
131         self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output, output)
132         self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output)
133         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output)
134         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
135
136         output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa",
137                                 u.to_string())
138         self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output)
139
140         fileutil.make_dirs("cli/test_dump_cap/private")
141         f = open("cli/test_dump_cap/private/secret", "w")
142         f.write("5s33nk3qpvnj2fw3z4mnm2y6fa\n")
143         f.close()
144         output = self._dump_cap("--client-dir", "cli/test_dump_cap",
145                                 u.to_string())
146         self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output)
147
148         output = self._dump_cap("--client-dir", "cli/test_dump_cap_BOGUS",
149                                 u.to_string())
150         self.failIf("file renewal secret:" in output, output)
151
152         output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j",
153                                 u.to_string())
154         self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output)
155         self.failIf("file renewal secret:" in output, output)
156
157         output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j",
158                                 "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa",
159                                 u.to_string())
160         self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output)
161         self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output)
162         self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output)
163
164         u = u.get_readonly()
165         output = self._dump_cap(u.to_string())
166         self.failUnless("SSK Read-only URI:" in output, output)
167         self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output)
168         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output)
169         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
170
171         u = u.get_verifier()
172         output = self._dump_cap(u.to_string())
173         self.failUnless("SSK Verifier URI:" in output, output)
174         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output)
175         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
176
177     def test_dump_cap_directory(self):
178         writekey = "\x01" * 16
179         fingerprint = "\xfe" * 32
180         u1 = uri.WriteableSSKFileURI(writekey, fingerprint)
181         u = uri.NewDirectoryURI(u1)
182
183         output = self._dump_cap(u.to_string())
184         self.failUnless("Directory Writeable URI:" in output, output)
185         self.failUnless("writekey: aeaqcaibaeaqcaibaeaqcaibae" in output,
186                         output)
187         self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output)
188         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output,
189                         output)
190         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
191
192         output = self._dump_cap("--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa",
193                                 u.to_string())
194         self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output)
195
196         output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j",
197                                 u.to_string())
198         self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output)
199         self.failIf("file renewal secret:" in output, output)
200
201         output = self._dump_cap("--nodeid", "tqc35esocrvejvg4mablt6aowg6tl43j",
202                                 "--client-secret", "5s33nk3qpvnj2fw3z4mnm2y6fa",
203                                 u.to_string())
204         self.failUnless("write_enabler: mgcavriox2wlb5eer26unwy5cw56elh3sjweffckkmivvsxtaknq" in output, output)
205         self.failUnless("file renewal secret: arpszxzc2t6kb4okkg7sp765xgkni5z7caavj7lta73vmtymjlxq" in output, output)
206         self.failUnless("lease renewal secret: 7pjtaumrb7znzkkbvekkmuwpqfjyfyamznfz4bwwvmh4nw33lorq" in output, output)
207
208         u = u.get_readonly()
209         output = self._dump_cap(u.to_string())
210         self.failUnless("Directory Read-only URI:" in output, output)
211         self.failUnless("readkey: nvgh5vj2ekzzkim5fgtb4gey5y" in output, output)
212         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output)
213         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
214
215         u = u.get_verifier()
216         output = self._dump_cap(u.to_string())
217         self.failUnless("Directory Verifier URI:" in output, output)
218         self.failUnless("storage index: nt4fwemuw7flestsezvo2eveke" in output, output)
219         self.failUnless("fingerprint: 737p57x6737p57x6737p57x6737p57x6737p57x6737p57x6737a" in output, output)
220
221 class CLITestMixin:
222     def do_cli(self, verb, *args, **kwargs):
223         nodeargs = [
224             "--node-directory", self.getdir("client0"),
225             ]
226         argv = [verb] + nodeargs + list(args)
227         stdin = kwargs.get("stdin", "")
228         stdout, stderr = StringIO(), StringIO()
229         d = threads.deferToThread(runner.runner, argv, run_by_human=False,
230                                   stdin=StringIO(stdin),
231                                   stdout=stdout, stderr=stderr)
232         def _done(res):
233             return stdout.getvalue(), stderr.getvalue()
234         d.addCallback(_done)
235         return d
236
237 class CreateAlias(SystemTestMixin, CLITestMixin, unittest.TestCase):
238
239     def _test_webopen(self, args, expected_url):
240         woo = cli.WebopenOptions()
241         all_args = ["--node-directory", self.getdir("client0")] + list(args)
242         woo.parseOptions(all_args)
243         urls = []
244         rc = cli.webopen(woo, urls.append)
245         self.failUnlessEqual(rc, 0)
246         self.failUnlessEqual(len(urls), 1)
247         self.failUnlessEqual(urls[0], expected_url)
248
249     def test_create(self):
250         self.basedir = os.path.dirname(self.mktemp())
251         d = self.set_up_nodes()
252         d.addCallback(lambda res: self.do_cli("create-alias", "tahoe"))
253         def _done((stdout,stderr)):
254             self.failUnless("Alias 'tahoe' created" in stdout)
255             self.failIf(stderr)
256             aliases = get_aliases(self.getdir("client0"))
257             self.failUnless("tahoe" in aliases)
258             self.failUnless(aliases["tahoe"].startswith("URI:DIR2:"))
259         d.addCallback(_done)
260         d.addCallback(lambda res: self.do_cli("create-alias", "two"))
261         def _stash_urls(res):
262             aliases = get_aliases(self.getdir("client0"))
263             node_url_file = os.path.join(self.getdir("client0"), "node.url")
264             nodeurl = open(node_url_file, "r").read().strip()
265             uribase = nodeurl + "uri/"
266             self.tahoe_url = uribase + urllib.quote(aliases["tahoe"])
267             self.tahoe_subdir_url = self.tahoe_url + "/subdir"
268             self.two_url = uribase + urllib.quote(aliases["two"])
269         d.addCallback(_stash_urls)
270
271         def _test_urls(junk):
272             self._test_webopen([], self.tahoe_url)
273             self._test_webopen(["/"], self.tahoe_url)
274             self._test_webopen(["tahoe:"], self.tahoe_url)
275             self._test_webopen(["tahoe:/"], self.tahoe_url)
276             self._test_webopen(["tahoe:subdir"], self.tahoe_subdir_url)
277             self._test_webopen(["tahoe:subdir/"], self.tahoe_subdir_url + '/')
278             self._test_webopen(["tahoe:subdir/file"], self.tahoe_subdir_url + '/file')
279             # if "file" is indeed a file, then the url produced by webopen in this
280             # case is disallowed by the webui. but by design, webopen passes through
281             # the mistake from the user to the resultant webopened url
282             self._test_webopen(["tahoe:subdir/file/"], self.tahoe_subdir_url + '/file/')
283             self._test_webopen(["two:"], self.two_url)
284         d.addCallback(_test_urls)
285
286         return d
287
288 class Put(SystemTestMixin, CLITestMixin, unittest.TestCase):
289
290     def test_unlinked_immutable_stdin(self):
291         # tahoe get `echo DATA | tahoe put`
292         # tahoe get `echo DATA | tahoe put -`
293
294         self.basedir = self.mktemp()
295         DATA = "data" * 100
296         d = self.set_up_nodes()
297         d.addCallback(lambda res: self.do_cli("put", stdin=DATA))
298         def _uploaded(res):
299             (stdout, stderr) = res
300             self.failUnless("waiting for file data on stdin.." in stderr)
301             self.failUnless("200 OK" in stderr)
302             self.readcap = stdout
303             self.failUnless(self.readcap.startswith("URI:CHK:"))
304         d.addCallback(_uploaded)
305         d.addCallback(lambda res: self.do_cli("get", self.readcap))
306         def _downloaded(res):
307             (stdout, stderr) = res
308             self.failUnlessEqual(stderr, "")
309             self.failUnlessEqual(stdout, DATA)
310         d.addCallback(_downloaded)
311         d.addCallback(lambda res: self.do_cli("put", "-", stdin=DATA))
312         d.addCallback(lambda (stdout,stderr):
313                       self.failUnlessEqual(stdout, self.readcap))
314         return d
315
316     def test_unlinked_immutable_from_file(self):
317         # tahoe put file.txt
318         # tahoe put ./file.txt
319         # tahoe put /tmp/file.txt
320         # tahoe put ~/file.txt
321         self.basedir = os.path.dirname(self.mktemp())
322         # this will be "allmydata.test.test_cli/Put/test_put_from_file/RANDOM"
323         # and the RANDOM directory will exist. Raw mktemp returns a filename.
324
325         rel_fn = os.path.join(self.basedir, "DATAFILE")
326         abs_fn = os.path.abspath(rel_fn)
327         # we make the file small enough to fit in a LIT file, for speed
328         f = open(rel_fn, "w")
329         f.write("short file")
330         f.close()
331         d = self.set_up_nodes()
332         d.addCallback(lambda res: self.do_cli("put", rel_fn))
333         def _uploaded((stdout,stderr)):
334             readcap = stdout
335             self.failUnless(readcap.startswith("URI:LIT:"))
336             self.readcap = readcap
337         d.addCallback(_uploaded)
338         d.addCallback(lambda res: self.do_cli("put", "./" + rel_fn))
339         d.addCallback(lambda (stdout,stderr):
340                       self.failUnlessEqual(stdout, self.readcap))
341         d.addCallback(lambda res: self.do_cli("put", abs_fn))
342         d.addCallback(lambda (stdout,stderr):
343                       self.failUnlessEqual(stdout, self.readcap))
344         # we just have to assume that ~ is handled properly
345         return d
346
347     def test_immutable_from_file(self):
348         # tahoe put file.txt uploaded.txt
349         # tahoe - uploaded.txt
350         # tahoe put file.txt subdir/uploaded.txt
351         # tahoe put file.txt tahoe:uploaded.txt
352         # tahoe put file.txt tahoe:subdir/uploaded.txt
353         # tahoe put file.txt DIRCAP:./uploaded.txt
354         # tahoe put file.txt DIRCAP:./subdir/uploaded.txt
355         self.basedir = os.path.dirname(self.mktemp())
356
357         rel_fn = os.path.join(self.basedir, "DATAFILE")
358         abs_fn = os.path.abspath(rel_fn)
359         # we make the file small enough to fit in a LIT file, for speed
360         DATA = "short file"
361         DATA2 = "short file two"
362         f = open(rel_fn, "w")
363         f.write(DATA)
364         f.close()
365
366         d = self.set_up_nodes()
367         d.addCallback(lambda res: self.do_cli("create-alias", "tahoe"))
368
369         d.addCallback(lambda res:
370                       self.do_cli("put", rel_fn, "uploaded.txt"))
371         def _uploaded((stdout,stderr)):
372             readcap = stdout.strip()
373             self.failUnless(readcap.startswith("URI:LIT:"))
374             self.failUnless("201 Created" in stderr, stderr)
375             self.readcap = readcap
376         d.addCallback(_uploaded)
377         d.addCallback(lambda res:
378                       self.do_cli("get", "tahoe:uploaded.txt"))
379         d.addCallback(lambda (stdout,stderr):
380                       self.failUnlessEqual(stdout, DATA))
381
382         d.addCallback(lambda res:
383                       self.do_cli("put", "-", "uploaded.txt", stdin=DATA2))
384         def _replaced((stdout,stderr)):
385             readcap = stdout.strip()
386             self.failUnless(readcap.startswith("URI:LIT:"))
387             self.failUnless("200 OK" in stderr, stderr)
388         d.addCallback(_replaced)
389
390         d.addCallback(lambda res:
391                       self.do_cli("put", rel_fn, "subdir/uploaded2.txt"))
392         d.addCallback(lambda res: self.do_cli("get", "subdir/uploaded2.txt"))
393         d.addCallback(lambda (stdout,stderr):
394                       self.failUnlessEqual(stdout, DATA))
395
396         d.addCallback(lambda res:
397                       self.do_cli("put", rel_fn, "tahoe:uploaded3.txt"))
398         d.addCallback(lambda res: self.do_cli("get", "tahoe:uploaded3.txt"))
399         d.addCallback(lambda (stdout,stderr):
400                       self.failUnlessEqual(stdout, DATA))
401
402         d.addCallback(lambda res:
403                       self.do_cli("put", rel_fn, "tahoe:subdir/uploaded4.txt"))
404         d.addCallback(lambda res:
405                       self.do_cli("get", "tahoe:subdir/uploaded4.txt"))
406         d.addCallback(lambda (stdout,stderr):
407                       self.failUnlessEqual(stdout, DATA))
408
409         def _get_dircap(res):
410             self.dircap = get_aliases(self.getdir("client0"))["tahoe"]
411         d.addCallback(_get_dircap)
412
413         d.addCallback(lambda res:
414                       self.do_cli("put", rel_fn,
415                                   self.dircap+":./uploaded5.txt"))
416         d.addCallback(lambda res:
417                       self.do_cli("get", "tahoe:uploaded5.txt"))
418         d.addCallback(lambda (stdout,stderr):
419                       self.failUnlessEqual(stdout, DATA))
420
421         d.addCallback(lambda res:
422                       self.do_cli("put", rel_fn,
423                                   self.dircap+":./subdir/uploaded6.txt"))
424         d.addCallback(lambda res:
425                       self.do_cli("get", "tahoe:subdir/uploaded6.txt"))
426         d.addCallback(lambda (stdout,stderr):
427                       self.failUnlessEqual(stdout, DATA))
428
429         return d
430
431     def test_mutable_unlinked(self):
432         # FILECAP = `echo DATA | tahoe put --mutable`
433         # tahoe get FILECAP, compare against DATA
434         # echo DATA2 | tahoe put - FILECAP
435         # tahoe get FILECAP, compare against DATA2
436         # tahoe put file.txt FILECAP
437         self.basedir = os.path.dirname(self.mktemp())
438         DATA = "data" * 100
439         DATA2 = "two" * 100
440         rel_fn = os.path.join(self.basedir, "DATAFILE")
441         abs_fn = os.path.abspath(rel_fn)
442         DATA3 = "three" * 100
443         f = open(rel_fn, "w")
444         f.write(DATA3)
445         f.close()
446
447         d = self.set_up_nodes()
448
449         d.addCallback(lambda res: self.do_cli("put", "--mutable", stdin=DATA))
450         def _created(res):
451             (stdout, stderr) = res
452             self.failUnless("waiting for file data on stdin.." in stderr)
453             self.failUnless("200 OK" in stderr)
454             self.filecap = stdout
455             self.failUnless(self.filecap.startswith("URI:SSK:"))
456         d.addCallback(_created)
457         d.addCallback(lambda res: self.do_cli("get", self.filecap))
458         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA))
459
460         d.addCallback(lambda res: self.do_cli("put", "-", self.filecap, stdin=DATA2))
461         def _replaced(res):
462             (stdout, stderr) = res
463             self.failUnless("waiting for file data on stdin.." in stderr)
464             self.failUnless("200 OK" in stderr)
465             self.failUnlessEqual(self.filecap, stdout)
466         d.addCallback(_replaced)
467         d.addCallback(lambda res: self.do_cli("get", self.filecap))
468         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA2))
469
470         d.addCallback(lambda res: self.do_cli("put", rel_fn, self.filecap))
471         def _replaced2(res):
472             (stdout, stderr) = res
473             self.failUnless("200 OK" in stderr)
474             self.failUnlessEqual(self.filecap, stdout)
475         d.addCallback(_replaced2)
476         d.addCallback(lambda res: self.do_cli("get", self.filecap))
477         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA3))
478
479         return d
480
481     def test_mutable(self):
482         # echo DATA1 | tahoe put --mutable - uploaded.txt
483         # echo DATA2 | tahoe put - uploaded.txt # should modify-in-place
484         # tahoe get uploaded.txt, compare against DATA2
485
486         self.basedir = os.path.dirname(self.mktemp())
487         DATA1 = "data" * 100
488         fn1 = os.path.join(self.basedir, "DATA1")
489         f = open(fn1, "w")
490         f.write(DATA1)
491         f.close()
492         DATA2 = "two" * 100
493         fn2 = os.path.join(self.basedir, "DATA2")
494         f = open(fn2, "w")
495         f.write(DATA2)
496         f.close()
497
498         d = self.set_up_nodes()
499         d.addCallback(lambda res: self.do_cli("create-alias", "tahoe"))
500         d.addCallback(lambda res:
501                       self.do_cli("put", "--mutable", fn1, "tahoe:uploaded.txt"))
502         d.addCallback(lambda res:
503                       self.do_cli("put", fn2, "tahoe:uploaded.txt"))
504         d.addCallback(lambda res:
505                       self.do_cli("get", "tahoe:uploaded.txt"))
506         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA2))
507         return d
508
509 class Admin(unittest.TestCase):
510     def do_cli(self, *args, **kwargs):
511         argv = list(args)
512         stdin = kwargs.get("stdin", "")
513         stdout, stderr = StringIO(), StringIO()
514         d = threads.deferToThread(runner.runner, argv, run_by_human=False,
515                                   stdin=StringIO(stdin),
516                                   stdout=stdout, stderr=stderr)
517         def _done(res):
518             return stdout.getvalue(), stderr.getvalue()
519         d.addCallback(_done)
520         return d
521
522     def test_generate_keypair(self):
523         import sys
524         if sys.platform == "darwin":
525             # pycryptopp-0.5.7's ECDSA is broken on OS-X, it raises a C++
526             # exception, which halts the whole process. So skip this test.
527             raise unittest.SkipTest("pycryptopp-0.5.7's ECDSA raises a C++ exception on OS-X")
528         d = self.do_cli("admin", "generate-keypair")
529         def _done( (stdout, stderr) ):
530             lines = stdout.split("\n")
531             privkey_line = lines[0].strip()
532             pubkey_line = lines[1].strip()
533             sk_header = "private: priv-v0-"
534             vk_header = "public: pub-v0-"
535             self.failUnless(privkey_line.startswith(sk_header), privkey_line)
536             self.failUnless(pubkey_line.startswith(vk_header), pubkey_line)
537             privkey_b = base32.a2b(privkey_line[len(sk_header):])
538             pubkey_b = base32.a2b(pubkey_line[len(vk_header):])
539             sk = ecdsa.create_signing_key_from_string(privkey_b)
540             vk = ecdsa.create_verifying_key_from_string(pubkey_b)
541             self.failUnlessEqual(sk.get_verifying_key().serialize(),
542                                  vk.serialize())
543         d.addCallback(_done)
544         return d
545