summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Shyba <victor1984@riseup.net>2017-09-20 03:54:51 -0300
committerVictor Shyba <victor1984@riseup.net>2017-10-05 05:41:40 -0300
commitaef72180f681a46534c0d934a62c1376ea2489db (patch)
tree91591d6e4d3cd91d4b97213aee3d1de3d65f71fe
parentfd99f15f1da1bed6c223876b7e2e72477ca8b2ee (diff)
[feature] improve preamble comparisons
We were comparing the raw content of preambles. This commit adds a way to compare excluding time so comparisons don't suffer from false negatives caused by time deltas. -- Resolves: #8920
-rw-r--r--src/leap/soledad/common/blobs/preamble.py19
-rw-r--r--tests/server/test_incoming_server.py9
2 files changed, 25 insertions, 3 deletions
diff --git a/src/leap/soledad/common/blobs/preamble.py b/src/leap/soledad/common/blobs/preamble.py
index d4065c38..a8446c0d 100644
--- a/src/leap/soledad/common/blobs/preamble.py
+++ b/src/leap/soledad/common/blobs/preamble.py
@@ -26,6 +26,7 @@ holds data about encryption scheme, iv, document id and sync related data.
str(self.rev), -> current revision
self._content_size) -> size, rounded to ceiling
"""
+import base64
import warnings
import struct
import time
@@ -66,8 +67,24 @@ class Preamble(object):
self.content_size)
return preamble
+ def __eq__(self, other):
+ # timestamp insn't included on comparison on purpose since it's not
+ # part of the document identity (you can have the very same document at
+ # two different times, but you can't have any other field changed and
+ # still be able to consider it as the same document).
+ fields = ['doc_id', 'rev', 'scheme', 'method', 'iv', 'magic',
+ 'content_size']
+ for field in fields:
+ if not hasattr(other, field):
+ return False
+ if getattr(self, field) != getattr(other, field):
+ return False
+ return True
-def decode_preamble(encoded_preamble):
+
+def decode_preamble(encoded_preamble, armored=False):
+ if armored:
+ encoded_preamble = base64.b64decode(encoded_preamble)
preamble_size = len(encoded_preamble)
try:
if preamble_size == LEGACY_PACMAN.size:
diff --git a/tests/server/test_incoming_server.py b/tests/server/test_incoming_server.py
index 241bc581..8c22b13b 100644
--- a/tests/server/test_incoming_server.py
+++ b/tests/server/test_incoming_server.py
@@ -31,6 +31,7 @@ from leap.soledad.server._incoming import IncomingResource
from leap.soledad.server._blobs import BlobsServerState
from leap.soledad.server._incoming import IncomingFormatter
from leap.soledad.common.crypto import EncryptionSchemes
+from leap.soledad.common.blobs.preamble import decode_preamble
from leap.soledad.common.blobs import Flags
from test_soledad.util import CouchServerStateForTests
from test_soledad.util import CouchDBTestCase
@@ -87,6 +88,10 @@ class IncomingOnCouchServerTestCase(CouchDBTestCase):
yield db.read_blob(user_id, doc_id, request, 'MX')
flags = db.get_flags(user_id, doc_id, request, 'MX')
flags = json.loads(flags)
- expected = formatter.preamble(content, doc_id) + ' ' + content
- self.assertEquals(expected, request.written[0])
+ expected_preamble = formatter.preamble(content, doc_id)
+ expected_preamble = decode_preamble(expected_preamble, True)
+ written_preamble, written_content = request.written[0].split()
+ written_preamble = decode_preamble(written_preamble, True)
+ self.assertEquals(expected_preamble, written_preamble)
+ self.assertEquals(content, written_content)
self.assertIn(Flags.PENDING, flags)