]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_cli.py
21e1ea7d5848dd9d74d68fb9730e7547879c2b71
[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         d.addCallback(lambda res: self._test_webopen([], self.tahoe_url))
272         d.addCallback(lambda res: self._test_webopen(["/"], self.tahoe_url))
273         d.addCallback(lambda res: self._test_webopen(["tahoe:"], self.tahoe_url))
274         d.addCallback(lambda res: self._test_webopen(["tahoe:/"], self.tahoe_url))
275         d.addCallback(lambda res: self._test_webopen(["tahoe:subdir"],
276                                                      self.tahoe_subdir_url))
277         d.addCallback(lambda res: self._test_webopen(["tahoe:subdir/"],
278                                                      self.tahoe_subdir_url))
279         d.addCallback(lambda res: self._test_webopen(["two:"], self.two_url))
280
281         return d
282
283 class Put(SystemTestMixin, CLITestMixin, unittest.TestCase):
284
285     def test_unlinked_immutable_stdin(self):
286         # tahoe get `echo DATA | tahoe put`
287         # tahoe get `echo DATA | tahoe put -`
288
289         self.basedir = self.mktemp()
290         DATA = "data" * 100
291         d = self.set_up_nodes()
292         d.addCallback(lambda res: self.do_cli("put", stdin=DATA))
293         def _uploaded(res):
294             (stdout, stderr) = res
295             self.failUnless("waiting for file data on stdin.." in stderr)
296             self.failUnless("200 OK" in stderr)
297             self.readcap = stdout
298             self.failUnless(self.readcap.startswith("URI:CHK:"))
299         d.addCallback(_uploaded)
300         d.addCallback(lambda res: self.do_cli("get", self.readcap))
301         def _downloaded(res):
302             (stdout, stderr) = res
303             self.failUnlessEqual(stderr, "")
304             self.failUnlessEqual(stdout, DATA)
305         d.addCallback(_downloaded)
306         d.addCallback(lambda res: self.do_cli("put", "-", stdin=DATA))
307         d.addCallback(lambda (stdout,stderr):
308                       self.failUnlessEqual(stdout, self.readcap))
309         return d
310
311     def test_unlinked_immutable_from_file(self):
312         # tahoe put file.txt
313         # tahoe put ./file.txt
314         # tahoe put /tmp/file.txt
315         # tahoe put ~/file.txt
316         self.basedir = os.path.dirname(self.mktemp())
317         # this will be "allmydata.test.test_cli/Put/test_put_from_file/RANDOM"
318         # and the RANDOM directory will exist. Raw mktemp returns a filename.
319
320         rel_fn = os.path.join(self.basedir, "DATAFILE")
321         abs_fn = os.path.abspath(rel_fn)
322         # we make the file small enough to fit in a LIT file, for speed
323         f = open(rel_fn, "w")
324         f.write("short file")
325         f.close()
326         d = self.set_up_nodes()
327         d.addCallback(lambda res: self.do_cli("put", rel_fn))
328         def _uploaded((stdout,stderr)):
329             readcap = stdout
330             self.failUnless(readcap.startswith("URI:LIT:"))
331             self.readcap = readcap
332         d.addCallback(_uploaded)
333         d.addCallback(lambda res: self.do_cli("put", "./" + rel_fn))
334         d.addCallback(lambda (stdout,stderr):
335                       self.failUnlessEqual(stdout, self.readcap))
336         d.addCallback(lambda res: self.do_cli("put", abs_fn))
337         d.addCallback(lambda (stdout,stderr):
338                       self.failUnlessEqual(stdout, self.readcap))
339         # we just have to assume that ~ is handled properly
340         return d
341
342     def test_immutable_from_file(self):
343         # tahoe put file.txt uploaded.txt
344         # tahoe - uploaded.txt
345         # tahoe put file.txt subdir/uploaded.txt
346         # tahoe put file.txt tahoe:uploaded.txt
347         # tahoe put file.txt tahoe:subdir/uploaded.txt
348         # tahoe put file.txt DIRCAP:./uploaded.txt
349         # tahoe put file.txt DIRCAP:./subdir/uploaded.txt
350         self.basedir = os.path.dirname(self.mktemp())
351
352         rel_fn = os.path.join(self.basedir, "DATAFILE")
353         abs_fn = os.path.abspath(rel_fn)
354         # we make the file small enough to fit in a LIT file, for speed
355         DATA = "short file"
356         DATA2 = "short file two"
357         f = open(rel_fn, "w")
358         f.write(DATA)
359         f.close()
360
361         d = self.set_up_nodes()
362         d.addCallback(lambda res: self.do_cli("create-alias", "tahoe"))
363
364         d.addCallback(lambda res:
365                       self.do_cli("put", rel_fn, "uploaded.txt"))
366         def _uploaded((stdout,stderr)):
367             readcap = stdout.strip()
368             self.failUnless(readcap.startswith("URI:LIT:"))
369             self.failUnless("201 Created" in stderr, stderr)
370             self.readcap = readcap
371         d.addCallback(_uploaded)
372         d.addCallback(lambda res:
373                       self.do_cli("get", "tahoe:uploaded.txt"))
374         d.addCallback(lambda (stdout,stderr):
375                       self.failUnlessEqual(stdout, DATA))
376
377         d.addCallback(lambda res:
378                       self.do_cli("put", "-", "uploaded.txt", stdin=DATA2))
379         def _replaced((stdout,stderr)):
380             readcap = stdout.strip()
381             self.failUnless(readcap.startswith("URI:LIT:"))
382             self.failUnless("200 OK" in stderr, stderr)
383         d.addCallback(_replaced)
384
385         d.addCallback(lambda res:
386                       self.do_cli("put", rel_fn, "subdir/uploaded2.txt"))
387         d.addCallback(lambda res: self.do_cli("get", "subdir/uploaded2.txt"))
388         d.addCallback(lambda (stdout,stderr):
389                       self.failUnlessEqual(stdout, DATA))
390
391         d.addCallback(lambda res:
392                       self.do_cli("put", rel_fn, "tahoe:uploaded3.txt"))
393         d.addCallback(lambda res: self.do_cli("get", "tahoe:uploaded3.txt"))
394         d.addCallback(lambda (stdout,stderr):
395                       self.failUnlessEqual(stdout, DATA))
396
397         d.addCallback(lambda res:
398                       self.do_cli("put", rel_fn, "tahoe:subdir/uploaded4.txt"))
399         d.addCallback(lambda res:
400                       self.do_cli("get", "tahoe:subdir/uploaded4.txt"))
401         d.addCallback(lambda (stdout,stderr):
402                       self.failUnlessEqual(stdout, DATA))
403
404         def _get_dircap(res):
405             self.dircap = get_aliases(self.getdir("client0"))["tahoe"]
406         d.addCallback(_get_dircap)
407
408         d.addCallback(lambda res:
409                       self.do_cli("put", rel_fn,
410                                   self.dircap+":./uploaded5.txt"))
411         d.addCallback(lambda res:
412                       self.do_cli("get", "tahoe:uploaded5.txt"))
413         d.addCallback(lambda (stdout,stderr):
414                       self.failUnlessEqual(stdout, DATA))
415
416         d.addCallback(lambda res:
417                       self.do_cli("put", rel_fn,
418                                   self.dircap+":./subdir/uploaded6.txt"))
419         d.addCallback(lambda res:
420                       self.do_cli("get", "tahoe:subdir/uploaded6.txt"))
421         d.addCallback(lambda (stdout,stderr):
422                       self.failUnlessEqual(stdout, DATA))
423
424         return d
425
426     def test_mutable_unlinked(self):
427         # FILECAP = `echo DATA | tahoe put --mutable`
428         # tahoe get FILECAP, compare against DATA
429         # echo DATA2 | tahoe put - FILECAP
430         # tahoe get FILECAP, compare against DATA2
431         # tahoe put file.txt FILECAP
432         self.basedir = os.path.dirname(self.mktemp())
433         DATA = "data" * 100
434         DATA2 = "two" * 100
435         rel_fn = os.path.join(self.basedir, "DATAFILE")
436         abs_fn = os.path.abspath(rel_fn)
437         DATA3 = "three" * 100
438         f = open(rel_fn, "w")
439         f.write(DATA3)
440         f.close()
441
442         d = self.set_up_nodes()
443
444         d.addCallback(lambda res: self.do_cli("put", "--mutable", stdin=DATA))
445         def _created(res):
446             (stdout, stderr) = res
447             self.failUnless("waiting for file data on stdin.." in stderr)
448             self.failUnless("200 OK" in stderr)
449             self.filecap = stdout
450             self.failUnless(self.filecap.startswith("URI:SSK:"))
451         d.addCallback(_created)
452         d.addCallback(lambda res: self.do_cli("get", self.filecap))
453         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA))
454
455         d.addCallback(lambda res: self.do_cli("put", "-", self.filecap, stdin=DATA2))
456         def _replaced(res):
457             (stdout, stderr) = res
458             self.failUnless("waiting for file data on stdin.." in stderr)
459             self.failUnless("200 OK" in stderr)
460             self.failUnlessEqual(self.filecap, stdout)
461         d.addCallback(_replaced)
462         d.addCallback(lambda res: self.do_cli("get", self.filecap))
463         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA2))
464
465         d.addCallback(lambda res: self.do_cli("put", rel_fn, self.filecap))
466         def _replaced2(res):
467             (stdout, stderr) = res
468             self.failUnless("200 OK" in stderr)
469             self.failUnlessEqual(self.filecap, stdout)
470         d.addCallback(_replaced2)
471         d.addCallback(lambda res: self.do_cli("get", self.filecap))
472         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA3))
473
474         return d
475
476     def test_mutable(self):
477         # echo DATA1 | tahoe put --mutable - uploaded.txt
478         # echo DATA2 | tahoe put - uploaded.txt # should modify-in-place
479         # tahoe get uploaded.txt, compare against DATA2
480
481         self.basedir = os.path.dirname(self.mktemp())
482         DATA1 = "data" * 100
483         fn1 = os.path.join(self.basedir, "DATA1")
484         f = open(fn1, "w")
485         f.write(DATA1)
486         f.close()
487         DATA2 = "two" * 100
488         fn2 = os.path.join(self.basedir, "DATA2")
489         f = open(fn2, "w")
490         f.write(DATA2)
491         f.close()
492
493         d = self.set_up_nodes()
494         d.addCallback(lambda res: self.do_cli("create-alias", "tahoe"))
495         d.addCallback(lambda res:
496                       self.do_cli("put", "--mutable", fn1, "tahoe:uploaded.txt"))
497         d.addCallback(lambda res:
498                       self.do_cli("put", fn2, "tahoe:uploaded.txt"))
499         d.addCallback(lambda res:
500                       self.do_cli("get", "tahoe:uploaded.txt"))
501         d.addCallback(lambda (out,err): self.failUnlessEqual(out, DATA2))
502         return d
503
504 class Admin(unittest.TestCase):
505     def do_cli(self, *args, **kwargs):
506         argv = list(args)
507         stdin = kwargs.get("stdin", "")
508         stdout, stderr = StringIO(), StringIO()
509         d = threads.deferToThread(runner.runner, argv, run_by_human=False,
510                                   stdin=StringIO(stdin),
511                                   stdout=stdout, stderr=stderr)
512         def _done(res):
513             return stdout.getvalue(), stderr.getvalue()
514         d.addCallback(_done)
515         return d
516
517     def test_generate_keypair(self):
518         import sys
519         if sys.platform == "darwin":
520             # pycryptopp-0.5.7's ECDSA is broken on OS-X, it raises a C++
521             # exception, which halts the whole process. So skip this test.
522             raise unittest.SkipTest("pycryptopp-0.5.7's ECDSA raises a C++ exception on OS-X")
523         d = self.do_cli("admin", "generate-keypair")
524         def _done( (stdout, stderr) ):
525             lines = stdout.split("\n")
526             privkey_line = lines[0].strip()
527             pubkey_line = lines[1].strip()
528             sk_header = "private: priv-v0-"
529             vk_header = "public: pub-v0-"
530             self.failUnless(privkey_line.startswith(sk_header), privkey_line)
531             self.failUnless(pubkey_line.startswith(vk_header), pubkey_line)
532             privkey_b = base32.a2b(privkey_line[len(sk_header):])
533             pubkey_b = base32.a2b(pubkey_line[len(vk_header):])
534             sk = ecdsa.create_signing_key_from_string(privkey_b)
535             vk = ecdsa.create_verifying_key_from_string(pubkey_b)
536             self.failUnlessEqual(sk.get_verifying_key().serialize(),
537                                  vk.serialize())
538         d.addCallback(_done)
539         return d
540