2006-12-01 09:54:28 +00:00
|
|
|
|
|
|
|
from twisted.trial import unittest
|
2007-03-30 21:54:33 +00:00
|
|
|
from twisted.python import log
|
|
|
|
from twisted.python.failure import Failure
|
2006-12-01 09:54:28 +00:00
|
|
|
from twisted.internet import defer
|
2006-12-02 02:17:26 +00:00
|
|
|
from cStringIO import StringIO
|
2006-12-01 09:54:28 +00:00
|
|
|
|
2007-03-30 21:54:33 +00:00
|
|
|
from foolscap import eventual
|
|
|
|
|
2007-01-17 04:34:34 +00:00
|
|
|
from allmydata import upload
|
2007-01-17 04:29:59 +00:00
|
|
|
from allmydata.uri import unpack_uri
|
2006-12-01 09:54:28 +00:00
|
|
|
|
2007-03-30 21:54:33 +00:00
|
|
|
from test_encode import FakePeer
|
|
|
|
|
2007-03-30 17:52:19 +00:00
|
|
|
class FakeStorageServer:
|
2007-03-30 21:54:33 +00:00
|
|
|
def __init__(self, mode):
|
|
|
|
self.mode = mode
|
|
|
|
def callRemote(self, methname, *args, **kwargs):
|
|
|
|
def _call():
|
|
|
|
meth = getattr(self, methname)
|
|
|
|
return meth(*args, **kwargs)
|
|
|
|
d = eventual.fireEventually()
|
|
|
|
d.addCallback(lambda res: _call())
|
|
|
|
return d
|
|
|
|
def allocate_buckets(self, verifierid, sharenums, shareize, blocksize, canary):
|
|
|
|
if self.mode == "full":
|
|
|
|
return (set(), {},)
|
|
|
|
elif self.mode == "already got them":
|
|
|
|
return (set(sharenums), {},)
|
|
|
|
else:
|
|
|
|
return (set(), dict([(shnum, FakePeer(),) for shnum in sharenums]),)
|
2006-12-01 09:54:28 +00:00
|
|
|
|
|
|
|
class FakeClient:
|
2007-03-30 21:54:33 +00:00
|
|
|
def __init__(self, mode="good"):
|
|
|
|
self.mode = mode
|
2007-03-30 17:52:19 +00:00
|
|
|
def get_permuted_peers(self, verifierid):
|
2007-03-30 21:54:33 +00:00
|
|
|
return [ ("%20d"%fakeid, "%20d"%fakeid, FakeStorageServer(self.mode),) for fakeid in range(50) ]
|
2007-01-16 04:22:22 +00:00
|
|
|
|
2007-03-30 21:54:33 +00:00
|
|
|
class GoodServer(unittest.TestCase):
|
2007-01-16 04:22:22 +00:00
|
|
|
def setUp(self):
|
2007-03-30 21:54:33 +00:00
|
|
|
self.node = FakeClient(mode="good")
|
2007-03-30 17:52:19 +00:00
|
|
|
self.u = upload.Uploader()
|
|
|
|
self.u.running = True
|
|
|
|
self.u.parent = self.node
|
2007-01-16 04:22:22 +00:00
|
|
|
|
|
|
|
def _check(self, uri):
|
|
|
|
self.failUnless(isinstance(uri, str))
|
|
|
|
self.failUnless(uri.startswith("URI:"))
|
2007-01-17 04:29:59 +00:00
|
|
|
codec_name, codec_params, verifierid = unpack_uri(uri)
|
2007-01-16 04:22:22 +00:00
|
|
|
self.failUnless(isinstance(verifierid, str))
|
|
|
|
self.failUnlessEqual(len(verifierid), 20)
|
2007-01-17 04:29:59 +00:00
|
|
|
self.failUnless(isinstance(codec_params, str))
|
2007-01-16 04:22:22 +00:00
|
|
|
peers = self.node.peers
|
|
|
|
self.failUnlessEqual(peers[0].allocated_size,
|
|
|
|
len(peers[0].data))
|
|
|
|
def testData(self):
|
|
|
|
data = "This is some data to upload"
|
|
|
|
d = self.u.upload_data(data)
|
|
|
|
d.addCallback(self._check)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def testFileHandle(self):
|
|
|
|
data = "This is some data to upload"
|
|
|
|
d = self.u.upload_filehandle(StringIO(data))
|
|
|
|
d.addCallback(self._check)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def testFilename(self):
|
|
|
|
fn = "Uploader-testFilename.data"
|
|
|
|
f = open(fn, "w")
|
|
|
|
data = "This is some data to upload"
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
d = self.u.upload_filename(fn)
|
|
|
|
d.addCallback(self._check)
|
|
|
|
return d
|
2007-03-30 21:54:33 +00:00
|
|
|
|
|
|
|
class FullServer(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.node = FakeClient(mode="full")
|
|
|
|
self.u = upload.Uploader()
|
|
|
|
self.u.running = True
|
|
|
|
self.u.parent = self.node
|
|
|
|
|
|
|
|
def _should_fail(self, f):
|
|
|
|
self.failUnless(isinstance(f, Failure) and f.check(upload.NotEnoughPeersError))
|
|
|
|
|
|
|
|
def testData(self):
|
|
|
|
data = "This is some data to upload"
|
|
|
|
d = self.u.upload_data(data)
|
|
|
|
d.addBoth(self._should_fail)
|
|
|
|
return d
|
|
|
|
|