|
|
@@ -14,7 +14,7 @@ from klein import Klein |
|
|
|
from kleintest import * |
|
|
|
from twisted.trial import unittest |
|
|
|
from twisted.web.iweb import IRequest |
|
|
|
from cli import _asn1coder, Persona, MDBase |
|
|
|
from cli import _asn1coder, Persona, MDBase, MetaData |
|
|
|
|
|
|
|
import hashlib |
|
|
|
import mock |
|
|
@@ -27,6 +27,7 @@ defaultfile = 'mediaserver.store.pasn1' |
|
|
|
class MEDAServer: |
|
|
|
def __init__(self, fname): |
|
|
|
self._fname = fname |
|
|
|
self._hashes = {} |
|
|
|
try: |
|
|
|
data = _asn1coder.loads(open(fname).read()) |
|
|
|
self._trustedkeys = {} |
|
|
@@ -58,6 +59,13 @@ class MEDAServer: |
|
|
|
with open(self._fname, 'w') as fp: |
|
|
|
fp.write(_asn1coder.dumps(obj)) |
|
|
|
|
|
|
|
@app.route('/lookup/<hash>') |
|
|
|
def lookup(self, request, hash): |
|
|
|
if hash in self._hashes: |
|
|
|
return |
|
|
|
|
|
|
|
request.setResponseCode(404) |
|
|
|
|
|
|
|
@app.route('/obj/<id>') |
|
|
|
def obj_lookup(self, request, id): |
|
|
|
try: |
|
|
@@ -68,6 +76,15 @@ class MEDAServer: |
|
|
|
except KeyError: # no object |
|
|
|
request.setResponseCode(404) |
|
|
|
|
|
|
|
def _storeobj(self, obj): |
|
|
|
self._objstore.setdefault(obj.uuid, []).append(obj) |
|
|
|
try: |
|
|
|
hashes = obj.hashes |
|
|
|
for i in hashes: |
|
|
|
self._hashes.setdefault(i, []).append(obj.uuid) |
|
|
|
except AttributeError: |
|
|
|
pass |
|
|
|
|
|
|
|
@app.route('/store') |
|
|
|
def storeobj(self, request): |
|
|
|
try: |
|
|
@@ -81,7 +98,7 @@ class MEDAServer: |
|
|
|
persona = self._trustedkeys[keyuuid] |
|
|
|
persona.verify(obj) |
|
|
|
|
|
|
|
self._objstore.setdefault(obj.uuid, []).append(obj) |
|
|
|
self._storeobj(obj) |
|
|
|
|
|
|
|
request.setResponseCode(201) |
|
|
|
|
|
|
@@ -236,6 +253,23 @@ class _TestPostConfig(_BaseServerTest): |
|
|
|
self.medaserver.addpubkey(persona.get_pubkey()) |
|
|
|
|
|
|
|
def test_hashlookup(self): |
|
|
|
# that the hash of a file |
|
|
|
h = hashlib.sha256(open('fixtures/testfiles/test.txt').read()).hexdigest() |
|
|
|
|
|
|
|
# when looked up |
|
|
|
r = self.requests.get('/lookup/%s' % h) |
|
|
|
|
|
|
|
# returns a 404 |
|
|
|
self.assertEqual(r.status_code, 404) |
|
|
|
|
|
|
|
# but when the metadata object |
|
|
|
mdobj = self.persona.MetaData(hashes=[ h ], mimetype=[ 'text/plain' ]) |
|
|
|
|
|
|
|
# is in the server |
|
|
|
self.medaserver._storeobj(mdobj) |
|
|
|
|
|
|
|
# when looked up |
|
|
|
r = self.requests.get('/lookup/%s' % h) |
|
|
|
|
|
|
|
# it is found |
|
|
|
self.assertEqual(r.status_code, 200) |