Ejemplo n.º 1
0
 def perform_vlob_update(self, intent):
     try:
         vlob = self.vlobs[intent.id]
         if vlob.write_trust_seed != intent.trust_seed:
             raise TrustSeedError('Invalid write trust seed.')
     except KeyError:
         raise VlobNotFound('Vlob not found.')
     if intent.version - 1 == len(vlob.blob_versions):
         vlob.blob_versions.append(intent.blob)
     else:
         raise VlobNotFound('Wrong blob version.')
     yield Effect(EEvent('vlob_updated', intent.id))
Ejemplo n.º 2
0
def test_perform_undelete(app, alice_identity, file):
    vlob = {'id': '2345', 'read_trust_seed': '42', 'write_trust_seed': '43'}
    blob = [{
        'blocks': [{
            'block': '4567',
            'digest': digest(b''),
            'size': 0
        }],
        'key': to_jsonb64(b'<dummy-key-00000000000000000001>')
    }]
    blob = ejson_dumps(blob).encode()
    blob = to_jsonb64(blob)
    eff = app.perform_delete(EDelete('/foo'))
    sequence = [
        (EIdentityGet(), const(alice_identity)),
        (EVlobRead(vlob['id'], vlob['read_trust_seed']),
         const({
             'id': vlob['id'],
             'blob': blob,
             'version': 1
         })), (EVlobList(), const([])),
        (EVlobRead(vlob['id'], vlob['read_trust_seed'],
                   1), const({
                       'id': vlob['id'],
                       'blob': blob,
                       'version': 1
                   })),
        (EBlockDelete('4567'), conste(BlockNotFound('Block not found.'))),
        (EVlobDelete('2345'), conste(VlobNotFound('Vlob not found.')))
    ]
    ret = perform_sequence(sequence, eff)
    eff = app.perform_undelete(EUndelete('2345'))
    sequence = [(EIdentityGet(), const(alice_identity))]
    ret = perform_sequence(sequence, eff)
    assert ret is None
Ejemplo n.º 3
0
 def perform_vlob_read(self, intent):
     try:
         vlob = self.vlobs[intent.id]
         if vlob.read_trust_seed != intent.trust_seed:
             raise TrustSeedError('Invalid read trust seed.')
     except KeyError:
         raise VlobNotFound('Vlob not found.')
     version = intent.version or len(vlob.blob_versions)
     try:
         return VlobAtom(id=vlob.id,
                         read_trust_seed=vlob.read_trust_seed,
                         write_trust_seed=vlob.write_trust_seed,
                         blob=vlob.blob_versions[version - 1],
                         version=version)
     except IndexError:
         raise VlobNotFound('Wrong blob version.')
Ejemplo n.º 4
0
 def test_read_bad_version(self):
     msg = {'id': '123', 'trust_seed': 'TS42', 'version': 2}
     eff = execute_cmd('vlob_read', msg)
     sequence = [(EVlobRead('123', 'TS42',
                            2), conste(VlobNotFound('Vlob not found.')))]
     ret = perform_sequence(sequence, eff)
     assert ret['status'] == 'vlob_not_found'
Ejemplo n.º 5
0
 def test_discard(self, file):
     content = b'This is a test content.'
     block_ids = ['4567', '5678', '6789']
     # Original content
     chunk_1 = content[:5]
     chunk_2 = content[5:14]
     chunk_3 = content[14:]
     blob = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }, {
             'block': block_ids[1],
             'digest': digest(chunk_2),
             'size': len(chunk_2)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000003>')
     }, {
         'blocks': [{
             'block': block_ids[2],
             'digest': digest(chunk_3),
             'size': len(chunk_3)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000004>')
     }]
     blob = ejson_dumps(blob).encode()
     blob = to_jsonb64(blob)
     # Already synchronized
     sequence = [
         (EVlobRead('1234', '42',
                    1), const({
                        'id': '1234',
                        'blob': blob,
                        'version': 1
                    })),
         (EBlockDelete('4567'), conste(BlockNotFound('Block not found.'))),
         (EBlockDelete('5678'), noop),
         (EBlockDelete('6789'), noop),
         (EVlobDelete('1234'), conste(VlobNotFound('Block not found.'))
          )  # TODO vlob OR block exceptin
     ]
     ret = perform_sequence(sequence, file.discard())
     assert ret is False
     # Not already synchronized
     file.dirty = True
     file.version = 0
     sequence = [(EVlobRead('1234', '42', 1),
                  const({
                      'id': '1234',
                      'blob': blob,
                      'version': 1
                  })), (EBlockDelete('4567'), noop),
                 (EBlockDelete('5678'), noop), (EBlockDelete('6789'), noop),
                 (EVlobDelete('1234'), noop)]
     ret = perform_sequence(sequence, file.discard())
     assert ret is True
     assert file.dirty is False
Ejemplo n.º 6
0
 async def _perform_vlob_update(self, intent):
     async with self.connection.acquire() as conn:
         async with conn.cursor() as cur:
             await cur.execute("SELECT version, read_trust_seed, write_trust_seed FROM "
                               "vlobs WHERE id=%s ORDER BY version DESC;", (intent.id, ))
             ret = await cur.fetchone()
             if ret is None:
                 raise VlobNotFound('Vlob not found.')
             last_version, rts, wts = ret
             if wts != intent.trust_seed:
                 raise TrustSeedError('Invalid write trust seed.')
             if intent.version != last_version + 1:
                 raise VlobNotFound('Wrong blob version.')
             # TODO: insertion doesn't do atomic check of version
             await cur.execute("INSERT INTO vlobs VALUES (%s, %s, %s, %s, %s);",
                 (intent.id, intent.version, rts, wts, intent.blob))
             await cur.execute("NOTIFY vlob_updated, %s", (intent.id, ))
Ejemplo n.º 7
0
 def perform_vlob_delete(self, intent):
     self.last_modified = arrow.utcnow()
     if (intent.id in self.vlobs and
             (not intent.version or intent.version == self.vlobs[intent.id]['version'])):
         del self.vlobs[intent.id]
     else:
         try:
             del self.vlob_cache[(intent.id, intent.version)]
         except KeyError:
             raise VlobNotFound('Vlob not found.')
Ejemplo n.º 8
0
 def test_update_bad_version(self):
     msg = {
         'id': '123',
         'trust_seed': 'WTS42',
         'version': 2,
         'blob': to_jsonb64(b'Next version.')
     }
     eff = execute_cmd('vlob_update', msg)
     sequence = [(EVlobUpdate('123', 2, 'WTS42', b'Next version.'),
                  conste(VlobNotFound('Vlob not found.')))]
     ret = perform_sequence(sequence, eff)
     assert ret['status'] == 'vlob_not_found'
Ejemplo n.º 9
0
 def test_vlob_update_not_found(self):
     blob = to_jsonb64(b'Next version.')
     eff = execute_cmd('vlob_update', {
         'id': '1234',
         'trust_seed': 'WTS4242',
         'version': 2,
         'blob': blob
     })
     sequence = [(EVlobUpdate('1234', 2, 'WTS4242', b'Next version.'),
                  conste(VlobNotFound('Vlob not found.')))]
     ret = perform_sequence(sequence, eff)
     assert ret == {'status': 'vlob_not_found', 'label': 'Vlob not found.'}
Ejemplo n.º 10
0
 async def perform_vlob_read(self, intent):
     async with self.connection.acquire() as conn:
         async with conn.cursor() as cur:
             if intent.version:
                 await cur.execute("SELECT * FROM vlobs WHERE "
                                   "id=%s AND version=%s;", (intent.id, intent.version))
             else:
                 await cur.execute("SELECT * FROM vlobs WHERE "
                                   "id=%s ORDER BY version DESC;", (intent.id, ))
             ret = await cur.fetchone()
     if not ret:
         raise VlobNotFound('Vlob not found.')
     _, version, rts, wts, blob = ret
     if rts != intent.trust_seed:
         raise TrustSeedError('Invalid read trust seed.')
     return VlobAtom(id=intent.id, version=version, read_trust_seed=rts,
                     write_trust_seed=wts, blob=bytes(blob))
Ejemplo n.º 11
0
def test_perform_dustbin_show(app, alice_identity, file):
    with freeze_time('2012-01-01') as frozen_datetime:
        vlob = {
            'id': '2345',
            'read_trust_seed': '42',
            'write_trust_seed': '43'
        }
        blob = [{
            'blocks': [{
                'block': '4567',
                'digest': digest(b''),
                'size': 0
            }],
            'key': to_jsonb64(b'<dummy-key-00000000000000000001>')
        }]
        blob = ejson_dumps(blob).encode()
        blob = to_jsonb64(blob)
        eff = app.perform_delete(EDelete('/foo'))
        sequence = [
            (EIdentityGet(), const(alice_identity)),
            (EVlobRead(vlob['id'], vlob['read_trust_seed']),
             const({
                 'id': vlob['id'],
                 'blob': blob,
                 'version': 1
             })),
            (EVlobList(), const([])),
            (EVlobRead(vlob['id'], vlob['read_trust_seed'],
                       1), const({
                           'id': vlob['id'],
                           'blob': blob,
                           'version': 1
                       })),
            (EBlockDelete('4567'), conste(BlockNotFound('Block not found.'))),
            (EVlobDelete('2345'), conste(VlobNotFound('Vlob not found.'))),
        ]
        perform_sequence(sequence, eff)
        eff = app.perform_dustbin_show(EDustbinShow())
        sequence = [(EIdentityGet(), const(alice_identity))]
        dustbin = perform_sequence(sequence, eff)
        vlob['path'] = '/foo'
        vlob['removed_date'] = frozen_datetime().isoformat()
        vlob['key'] = to_jsonb64(b'<dummy-key-00000000000000000002>')
        assert dustbin == [vlob]
Ejemplo n.º 12
0
 def test_vlob_read_not_found(self):
     eff = execute_cmd('vlob_read', {'id': '1234', 'trust_seed': 'TS4242'})
     sequence = [(EVlobRead('1234', 'TS4242'),
                  conste(VlobNotFound('Vlob not found.')))]
     ret = perform_sequence(sequence, eff)
     assert ret == {'status': 'vlob_not_found', 'label': 'Vlob not found.'}