from zope.interface import implements
-from allmydata.interfaces import IFileNode, IFileURI
+from twisted.internet import defer
+from allmydata.interfaces import IFileNode, IFileURI, IURI
+from allmydata import uri
class FileNode:
implements(IFileNode)
downloader = self._client.getServiceNamed("downloader")
return downloader.download_to_data(self.uri)
+
+
+class LiteralFileNode:
+ implements(IFileNode)
+
+ def __init__(self, my_uri, client):
+ u = IFileURI(my_uri)
+ assert isinstance(u, uri.LiteralFileURI)
+ self.uri = u.to_string()
+ self._client = client
+
+ def get_uri(self):
+ return self.uri
+
+ def is_readonly(self):
+ return True
+
+ def get_readonly_uri(self):
+ return self.uri
+
+ def get_size(self):
+ return len(IURI(self.uri).data)
+
+ def __hash__(self):
+ return hash((self.__class__, self.uri))
+ def __cmp__(self, them):
+ if cmp(type(self), type(them)):
+ return cmp(type(self), type(them))
+ if cmp(self.__class__, them.__class__):
+ return cmp(self.__class__, them.__class__)
+ return cmp(self.uri, them.uri)
+
+ def get_verifier(self):
+ return None
+
+ def check(self):
+ return None
+
+ def download(self, target):
+ data = IURI(self.uri).data
+ target.open(len(data))
+ target.write(data)
+ target.close()
+ return defer.maybeDeferred(target.finish)
+
+ def download_to_data(self):
+ data = IURI(self.uri).data
+ return defer.succeed(data)
--- /dev/null
+
+from twisted.trial import unittest
+from allmydata import filenode, uri, download
+
+class NotANode:
+ pass
+
+class Node(unittest.TestCase):
+ def test_chk_filenode(self):
+ u = uri.CHKFileURI(key="\x00"*16,
+ uri_extension_hash="\x00"*32,
+ needed_shares=3,
+ total_shares=10,
+ size=1000)
+ c = None
+ fn1 = filenode.FileNode(u, c)
+ fn2 = filenode.FileNode(u.to_string(), c)
+ self.failUnlessEqual(fn1, fn2)
+ self.failIfEqual(fn1, "I am not a filenode")
+ self.failIfEqual(fn1, NotANode())
+ self.failUnlessEqual(fn1.get_uri(), u.to_string())
+ self.failUnlessEqual(fn1.is_readonly(), True)
+ self.failUnlessEqual(fn1.get_readonly_uri(), u.to_string())
+ self.failUnlessEqual(fn1.get_size(), 1000)
+ d = {}
+ d[fn1] = 1 # exercise __hash__
+ v = fn1.get_verifier()
+ self.failUnless(isinstance(v, uri.CHKFileVerifierURI))
+
+ def test_literal_filenode(self):
+ DATA = "I am a short file."
+ u = uri.LiteralFileURI(data=DATA)
+ c = None
+ fn1 = filenode.LiteralFileNode(u, c)
+ fn2 = filenode.LiteralFileNode(u.to_string(), c)
+ self.failUnlessEqual(fn1, fn2)
+ self.failIfEqual(fn1, "I am not a filenode")
+ self.failIfEqual(fn1, NotANode())
+ self.failUnlessEqual(fn1.get_uri(), u.to_string())
+ self.failUnlessEqual(fn1.is_readonly(), True)
+ self.failUnlessEqual(fn1.get_readonly_uri(), u.to_string())
+ self.failUnlessEqual(fn1.get_size(), len(DATA))
+ d = {}
+ d[fn1] = 1 # exercise __hash__
+
+ v = fn1.get_verifier()
+ self.failUnlessEqual(v, None)
+
+ self.failUnlessEqual(fn1.check(), None)
+ target = download.Data()
+ d = fn1.download(target)
+ def _check(res):
+ self.failUnlessEqual(res, DATA)
+ d.addCallback(_check)
+
+ d.addCallback(lambda res: fn1.download_to_data())
+ d.addCallback(_check)
+ return d
+
from twisted.trial import unittest
from twisted.internet import defer
from twisted.python import failure, log
-from allmydata import mutable, uri, dirnode2
+from allmydata import mutable, uri, dirnode2, upload
from allmydata.dirnode2 import split_netstring
from allmydata.util.hashutil import netstring, tagged_hash
from allmydata.encode import NotEnoughPeersError
from allmydata.interfaces import IURI, INewDirectoryURI, \
- IMutableFileURI
-
+ IMutableFileURI, IFileNode, IUploadable, IFileURI
+from allmydata.filenode import LiteralFileNode
import sha
class Netstring(unittest.TestCase):
u = IURI(u)
if INewDirectoryURI.providedBy(u):
return self.create_dirnode_from_uri(u)
- assert IMutableFileURI.providedBy(u)
+ if IFileURI.providedBy(u):
+ if isinstance(u, uri.LiteralFileURI):
+ return LiteralFileNode(u, self)
+ else:
+ # CHK
+ raise RuntimeError("not simulated")
+ assert IMutableFileURI.providedBy(u), u
res = FakeFilenode(self).init_from_uri(u)
return res
results.sort()
return results
+ def upload(self, uploadable, wait_for_numpeers=None):
+ assert IUploadable.providedBy(uploadable)
+ d = uploadable.get_size()
+ d.addCallback(lambda length: uploadable.read(length))
+ #d.addCallback(self.create_mutable_file)
+ def _got_data(datav):
+ data = "".join(datav)
+ #newnode = FakeFilenode(self)
+ return uri.LiteralFileURI(data)
+ d.addCallback(_got_data)
+ return d
+
class Filenode(unittest.TestCase):
def setUp(self):
self.client = FakeClient()
self.failUnlessEqual(sorted(children.keys()),
sorted(["child"])))
+ uploadable = upload.Data("some data")
+ d.addCallback(lambda res: n.add_file("newfile", uploadable))
+ d.addCallback(lambda newnode:
+ self.failUnless(IFileNode.providedBy(newnode)))
+ d.addCallback(lambda res: n.list())
+ d.addCallback(lambda children:
+ self.failUnlessEqual(sorted(children.keys()),
+ sorted(["child", "newfile"])))
+
+ d.addCallback(lambda res: n.create_empty_directory("subdir2"))
+ def _created2(subdir2):
+ self.subdir2 = subdir2
+ d.addCallback(_created2)
+
+ d.addCallback(lambda res:
+ n.move_child_to("child", self.subdir2))
+ d.addCallback(lambda res: n.list())
+ d.addCallback(lambda children:
+ self.failUnlessEqual(sorted(children.keys()),
+ sorted(["newfile", "subdir2"])))
+ d.addCallback(lambda res: self.subdir2.list())
+ d.addCallback(lambda children:
+ self.failUnlessEqual(sorted(children.keys()),
+ sorted(["child"])))
+
return d
d.addCallback(_then)
def create_node_from_uri(self, uri):
return self.my_nodes[uri]
-
+
def create_empty_dirnode(self, wait_for_numpeers=None):
n = FakeDirectoryNode(self)
r = defer.succeed(n.fake_create(wait_for_numpeers=1))
self.failUnless("rw_uri" in data[1]) # mutable
self.failUnlessEqual(data[1]["rw_uri"], self._foo_uri)
self.failUnlessEqual(data[1]["ro_uri"], self._foo_readonly_uri)
-
+
kidnames = sorted(data[1]["children"])
self.failUnlessEqual(kidnames,
["bar.txt", "blockingfile", "empty", "sub"])