summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/src/leap/soledad/client/_blobs.py32
-rw-r--r--testing/tests/blobs/test_blobs.py12
-rw-r--r--testing/tests/blobs/test_local_backend.py27
-rw-r--r--testing/tests/server/test_blobs_server.py13
4 files changed, 38 insertions, 46 deletions
diff --git a/client/src/leap/soledad/client/_blobs.py b/client/src/leap/soledad/client/_blobs.py
index 383917ca..b7dee538 100644
--- a/client/src/leap/soledad/client/_blobs.py
+++ b/client/src/leap/soledad/client/_blobs.py
@@ -44,6 +44,7 @@ from _http import HTTPClient
logger = Logger()
+FIXED_REV = 'ImmutableRevision' # Blob content is immutable
class BlobAlreadyExistsError(SoledadError):
@@ -105,8 +106,8 @@ def check_http_status(code):
class DecrypterBuffer(object):
- def __init__(self, doc_id, rev, secret, tag):
- self.doc_info = DocInfo(doc_id, rev)
+ def __init__(self, blob_id, secret, tag):
+ self.doc_info = DocInfo(blob_id, FIXED_REV)
self.secret = secret
self.tag = tag
self.preamble_pipe = PreamblePipe(self._make_decryptor)
@@ -175,16 +176,16 @@ class BlobManager(object):
# In fact, some kind of pipe is needed here, where each write on db
# handle gets forwarded into a write on the connection handle
fd = yield self.local.get(doc.blob_id)
- yield self._encrypt_and_upload(doc.blob_id, doc.doc_id, doc.rev, fd)
+ yield self._encrypt_and_upload(doc.blob_id, fd)
@defer.inlineCallbacks
- def get(self, blob_id, doc_id, rev):
+ def get(self, blob_id):
local_blob = yield self.local.get(blob_id)
if local_blob:
logger.info("Found blob in local database: %s" % blob_id)
defer.returnValue(local_blob)
- result = yield self._download_and_decrypt(blob_id, doc_id, rev)
+ result = yield self._download_and_decrypt(blob_id)
if not result:
defer.returnValue(None)
@@ -205,7 +206,7 @@ class BlobManager(object):
logger.error('sorry, dunno what happened')
@defer.inlineCallbacks
- def _encrypt_and_upload(self, blob_id, doc_id, rev, fd):
+ def _encrypt_and_upload(self, blob_id, fd):
# TODO ------------------------------------------
# this is wrong, is doing 2 stages.
# the crypto producer can be passed to
@@ -214,7 +215,7 @@ class BlobManager(object):
# produce data to the treq request fd.
# ------------------------------------------------
logger.info("Staring upload of blob: %s" % blob_id)
- doc_info = DocInfo(doc_id, rev)
+ doc_info = DocInfo(blob_id, FIXED_REV)
uri = urljoin(self.remote, self.user + "/" + blob_id)
crypter = BlobEncryptor(doc_info, fd, secret=self.secret,
armor=False)
@@ -224,7 +225,7 @@ class BlobManager(object):
logger.info("Finished upload: %s" % (blob_id,))
@defer.inlineCallbacks
- def _download_and_decrypt(self, blob_id, doc_id, rev):
+ def _download_and_decrypt(self, blob_id):
logger.info("Staring download of blob: %s" % blob_id)
# TODO this needs to be connected in a tube
uri = urljoin(self.remote, self.user + '/' + blob_id)
@@ -238,7 +239,7 @@ class BlobManager(object):
defer.returnValue(None)
tag = data.headers.getRawHeaders('Tag')[0]
tag = base64.urlsafe_b64decode(tag)
- buf = DecrypterBuffer(doc_id, rev, self.secret, tag)
+ buf = DecrypterBuffer(blob_id, self.secret, tag)
# incrementally collect the body of the response
yield treq.collect(data, buf.write)
@@ -320,10 +321,9 @@ class BlobDoc(object):
# TODO probably not needed, but convenient for testing for now.
- def __init__(self, doc_id, rev, content, blob_id=None):
+ def __init__(self, content, blob_id):
- self.doc_id = doc_id
- self.rev = rev
+ self.blob_id = blob_id
self.is_blob = True
self.blob_fd = content
if blob_id is None:
@@ -401,14 +401,14 @@ def testit(reactor):
logger.info(":: Starting upload only: %s" % str((blob_id, payload)))
manager = _manager()
with open(payload, 'r') as fd:
- yield manager._encrypt_and_upload(blob_id, 'mydoc', '1', fd)
+ yield manager._encrypt_and_upload(blob_id, fd)
logger.info(":: Finished upload only: %s" % str((blob_id, payload)))
@defer.inlineCallbacks
def _download(blob_id):
logger.info(":: Starting download only: %s" % blob_id)
manager = _manager()
- result = yield manager._download_and_decrypt(blob_id, 'mydoc', '1')
+ result = yield manager._download_and_decrypt(blob_id)
logger.info(":: Result of download: %s" % str(result))
if result:
fd, _ = result
@@ -423,7 +423,7 @@ def testit(reactor):
manager = _manager()
size = os.path.getsize(payload)
with open(payload) as fd:
- doc = BlobDoc('mydoc', '1', fd, blob_id=blob_id)
+ doc = BlobDoc(fd, blob_id)
result = yield manager.put(doc, size=size)
logger.info(":: Result of put: %s" % str(result))
logger.info(":: Finished full put: %s" % blob_id)
@@ -432,7 +432,7 @@ def testit(reactor):
def _get(blob_id):
logger.info(":: Starting full get: %s" % blob_id)
manager = _manager()
- fd = yield manager.get(blob_id, 'mydoc', '1')
+ fd = yield manager.get(blob_id)
if fd:
logger.info(":: Result of get: " + fd.getvalue())
logger.info(":: Finished full get: %s" % blob_id)
diff --git a/testing/tests/blobs/test_blobs.py b/testing/tests/blobs/test_blobs.py
index 8dd3abbe..c99cc572 100644
--- a/testing/tests/blobs/test_blobs.py
+++ b/testing/tests/blobs/test_blobs.py
@@ -19,7 +19,7 @@ Tests for blobs handling.
"""
from twisted.trial import unittest
from twisted.internet import defer
-from leap.soledad.client._blobs import DecrypterBuffer, BlobManager
+from leap.soledad.client._blobs import DecrypterBuffer, BlobManager, FIXED_REV
from leap.soledad.client import _crypto
from io import BytesIO
from mock import Mock
@@ -28,8 +28,8 @@ from mock import Mock
class BlobTestCase(unittest.TestCase):
class doc_info:
- doc_id = 'D-deadbeef'
- rev = '397932e0c77f45fcb7c3732930e7e9b2:1'
+ doc_id = 'D-BLOB-ID'
+ rev = FIXED_REV
def setUp(self):
self.cleartext = BytesIO('rosa de foc')
@@ -42,9 +42,8 @@ class BlobTestCase(unittest.TestCase):
@defer.inlineCallbacks
def test_decrypt_buffer(self):
encrypted = (yield self.blob.encrypt()).getvalue()
- doc_id, rev = self.doc_info.doc_id, self.doc_info.rev
tag = encrypted[-16:]
- buf = DecrypterBuffer(doc_id, rev, self.secret, tag)
+ buf = DecrypterBuffer(self.doc_info.doc_id, self.secret, tag)
buf.write(encrypted)
fd, size = buf.close()
self.assertEquals(fd.getvalue(), 'rosa de foc')
@@ -63,7 +62,6 @@ class BlobTestCase(unittest.TestCase):
defer.returnValue(Mock(code=200))
manager = BlobManager('', '', self.secret, self.secret, 'user')
- doc_id, rev = self.doc_info.doc_id, self.doc_info.rev
fd = BytesIO('up and up')
manager._client.put = _check_result
- yield manager._encrypt_and_upload('blob_id', doc_id, rev, fd)
+ yield manager._encrypt_and_upload(self.doc_info.doc_id, fd)
diff --git a/testing/tests/blobs/test_local_backend.py b/testing/tests/blobs/test_local_backend.py
index 80696553..5bfece8f 100644
--- a/testing/tests/blobs/test_local_backend.py
+++ b/testing/tests/blobs/test_local_backend.py
@@ -19,7 +19,7 @@ Tests for sqlcipher backend on blobs client.
"""
from twisted.trial import unittest
from twisted.internet import defer
-from leap.soledad.client._blobs import BlobManager, BlobDoc
+from leap.soledad.client._blobs import BlobManager, BlobDoc, FIXED_REV
from io import BytesIO
from mock import Mock
import pytest
@@ -30,7 +30,7 @@ class BlobManagerTestCase(unittest.TestCase):
class doc_info:
doc_id = 'D-deadbeef'
- rev = '397932e0c77f45fcb7c3732930e7e9b2:1'
+ rev = FIXED_REV
def setUp(self):
self.cleartext = BytesIO('rosa de foc')
@@ -45,10 +45,10 @@ class BlobManagerTestCase(unittest.TestCase):
@pytest.mark.usefixtures("method_tmpdir")
def test_get_inexistent(self):
self.manager._download_and_decrypt = Mock(return_value=None)
- args = ('inexistent_blob_id', 'inexistent_doc_id', 'inexistent_rev')
- result = yield self.manager.get(*args)
+ bad_blob_id = 'inexsistent_id'
+ result = yield self.manager.get(bad_blob_id)
self.assertIsNone(result)
- self.manager._download_and_decrypt.assert_called_once_with(*args)
+ self.manager._download_and_decrypt.assert_called_once_with(bad_blob_id)
@defer.inlineCallbacks
@pytest.mark.usefixtures("method_tmpdir")
@@ -57,8 +57,7 @@ class BlobManagerTestCase(unittest.TestCase):
msg = "It's me, M4r10!"
yield self.manager.local.put('myblob_id', BytesIO(msg),
size=len(msg))
- args = ('myblob_id', 'mydoc_id', 'cool_rev')
- result = yield self.manager.get(*args)
+ result = yield self.manager.get('myblob_id')
self.assertEquals(result.getvalue(), msg)
self.assertNot(self.manager._download_and_decrypt.called)
@@ -67,8 +66,7 @@ class BlobManagerTestCase(unittest.TestCase):
def test_put_stores_on_local_db(self):
self.manager._encrypt_and_upload = Mock(return_value=None)
msg = "Hey Joe"
- doc = BlobDoc('mydoc_id', 'mydoc_rev', BytesIO(msg),
- blob_id='myblob_id')
+ doc = BlobDoc(BytesIO(msg), blob_id='myblob_id')
yield self.manager.put(doc, size=len(msg))
result = yield self.manager.local.get('myblob_id')
self.assertEquals(result.getvalue(), msg)
@@ -83,10 +81,9 @@ class BlobManagerTestCase(unittest.TestCase):
tmpfile = os.tmpfile()
tmpfile.write(msg)
tmpfile.seek(0)
- doc = BlobDoc('mydoc_id', 'mydoc_rev', tmpfile,
- blob_id='myblob_id')
+ doc = BlobDoc(tmpfile, 'myblob_id')
yield self.manager.put(doc, size=len(msg))
- result = yield self.manager.get(doc.blob_id, doc.doc_id, doc.rev)
+ result = yield self.manager.get(doc.blob_id)
self.assertEquals(result.getvalue(), msg)
self.assertTrue(self.manager._encrypt_and_upload.called)
self.assertFalse(self.manager._download_and_decrypt.called)
@@ -96,11 +93,9 @@ class BlobManagerTestCase(unittest.TestCase):
def test_local_list_blobs(self):
self.manager._encrypt_and_upload = Mock(return_value=None)
msg = "1337"
- doc = BlobDoc('mydoc_id', 'mydoc_rev', BytesIO(msg),
- blob_id='myblob_id')
+ doc = BlobDoc(BytesIO(msg), 'myblob_id')
yield self.manager.put(doc, size=len(msg))
- doc2 = BlobDoc('mydoc_id2', 'mydoc_rev2', BytesIO(msg),
- blob_id='myblob_id2')
+ doc2 = BlobDoc(BytesIO(msg), 'myblob_id2')
yield self.manager.put(doc2, size=len(msg))
blobs_list = yield self.manager.local_list()
diff --git a/testing/tests/server/test_blobs_server.py b/testing/tests/server/test_blobs_server.py
index 96037ea2..a0306504 100644
--- a/testing/tests/server/test_blobs_server.py
+++ b/testing/tests/server/test_blobs_server.py
@@ -46,9 +46,8 @@ class BlobServerTestCase(unittest.TestCase):
manager = BlobManager('', self.uri, self.secret,
self.secret, 'user')
fd = BytesIO("save me")
- yield manager._encrypt_and_upload('blob_id', 'mydoc', '1', fd)
- blob, size = yield manager._download_and_decrypt('blob_id',
- 'mydoc', '1')
+ yield manager._encrypt_and_upload('blob_id', fd)
+ blob, size = yield manager._download_and_decrypt('blob_id')
self.assertEquals(blob.getvalue(), "save me")
@defer.inlineCallbacks
@@ -56,8 +55,8 @@ class BlobServerTestCase(unittest.TestCase):
def test_upload_changes_remote_list(self):
manager = BlobManager('', self.uri, self.secret,
self.secret, 'user')
- yield manager._encrypt_and_upload('blob_id1', '1', '1', BytesIO("1"))
- yield manager._encrypt_and_upload('blob_id2', '2', '2', BytesIO("2"))
+ yield manager._encrypt_and_upload('blob_id1', BytesIO("1"))
+ yield manager._encrypt_and_upload('blob_id2', BytesIO("2"))
blobs_list = yield manager.remote_list()
self.assertEquals(set(['blob_id1', 'blob_id2']), set(blobs_list))
@@ -67,7 +66,7 @@ class BlobServerTestCase(unittest.TestCase):
manager = BlobManager('', self.uri, self.secret,
self.secret, 'user')
fd = BytesIO("save me")
- yield manager._encrypt_and_upload('blob_id', 'mydoc', '1', fd)
+ yield manager._encrypt_and_upload('blob_id', fd)
fd = BytesIO("save me")
with pytest.raises(BlobAlreadyExistsError):
- yield manager._encrypt_and_upload('blob_id', 'mydoc', '1', fd)
+ yield manager._encrypt_and_upload('blob_id', fd)