Skip to content

Commit

Permalink
more unit tests for jsoncert, some fixes to litter.js and others
Browse files Browse the repository at this point in the history
  • Loading branch information
pstjuste committed Jan 10, 2012
1 parent b606471 commit 2872aa8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 deletions.
36 changes: 35 additions & 1 deletion src/jsoncert.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
import json, hashlib, base64, zlib, struct
import json, hashlib, base64, zlib, struct, unittest

from rsa import *

Expand All @@ -23,6 +23,7 @@ def __init__(self, content, pubkey=None, privkey=None):
self.pubkey = JsonCert.str_to_key(self.as_dict['key'])
else:
raise Exception("No public key in certificate")

if 'sig' in self.as_dict:
sig = self.as_dict['sig']
del self.as_dict['sig']
Expand Down Expand Up @@ -92,3 +93,36 @@ def deserialize(str_ob):
if crcval != crccomp:
raise Exception("CRC mismatch: computed(%i) != stored(%i)" % (crccomp, crcval))
return json.loads(str_ob[:-4].decode("utf-8"))

class JsonCertTest(unittest.TestCase):

def test(self):
import pickle, os

if os.path.exists('key.data'):
key = pickle.load(open("key.data","r"))
cert = JsonCert({}, key['pub'],key['priv'])
else:
cert = JsonCert.generate(1024,None)
key = { 'pub':cert.pubkey, 'priv':cert.privkey}
pickle.dump(key, open('key.data', 'w+'))

msg = "sign me"
signed_msg = cert.sign_object(msg)
org_msg = cert.unsign_object(signed_msg)
self.assertEqual(msg, org_msg)

ser_obj = JsonCert.serialize(cert.as_dict)
obj = JsonCert.deserialize(ser_obj)

# need to change from unicode to string
kvpairs = { 'key':str(obj['key']),'sig':str(obj['sig'])}
new_cert = JsonCert(kvpairs)

self.assertEqual(cert.pubkey, new_cert.pubkey)
self.assertEqual(cert.as_dict['sig'], new_cert.as_dict['sig'])


if __name__ == '__main__':
unittest.main()

11 changes: 1 addition & 10 deletions src/litter.py
Expand Up @@ -14,7 +14,7 @@
import logging
import urllib
import getopt
from litterstore import LitterStore, StoreError
from litterstore import LitterStore
from litterrouter import *

# Log everything, and send it to stderr.
Expand Down Expand Up @@ -247,15 +247,6 @@ def run(self):
data = json.dumps(response, ensure_ascii=False)
sender.send(data.encode("utf-8"))

except StoreError as ie:
if str(ie) == "column hashid is not unique":
#this means we got a duplicate, no need to log:
pass
else:
if isinstance(sender, HTTPSender):
sender.send_error(ex)
logging.exception(ie)

except Exception as ex:
if isinstance(sender, HTTPSender):
sender.send_error(ex)
Expand Down
13 changes: 8 additions & 5 deletions src/litterrouter.py
Expand Up @@ -35,7 +35,7 @@ def __init__(self, sock, intfs=None, dest=None):
def dest(self):
return self.__dest

def __repr__(self):
def __str__(self):
return "UDP Sender: %s" % (self.dest,)

def send(self, data, dest=None):
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(self, queue, dest=None):
def dest(self):
return self.__dest

def __repr__(self):
def __str__(self):
return "HTTP Sender: %s" % (self.dest,)

def send(self, data, dest=None):
Expand Down Expand Up @@ -211,9 +211,12 @@ def should_process(self, data, sender=None):
elif isinstance(headers, dict) and headers['htype'] == 'req' and \
headers['hid'] in self.__mid_to_addr: return False
elif headers != None:
self.send(data, sender)
#restore ttl since send decrements it
headers['httl'] += 1
try:
self.send(data, sender)
#restore ttl since send decrements it
headers['httl'] += 1
except RouterError as err:
logging.exception(err)

return True

Expand Down
45 changes: 36 additions & 9 deletions src/litterstore.py
Expand Up @@ -18,7 +18,7 @@ def __init__(self, msg):
self.msg = msg

def __str__(self):
return repr(self.msg)
return str(self.msg)


class LitterStore:
Expand Down Expand Up @@ -266,12 +266,20 @@ def process(self, request):

if 'posts' in request:
for post in request['posts']:
self.__post(*post)
try:
self.__post(*post)
except StoreError as err:
if str(err) != "column hashid is not unique":
logging.exception(err)

if 'query' in request:
meth = request['query']['m']

if meth == 'gen_push':
if meth == 'get':
begin = request['begin']
limit = request['limit']
result['posts'] = self.__get(begin=begin,limit=limit)
elif meth == 'gen_push':
result['posts'] = self.__get(self.uid)
elif meth == 'gen_pull':
result['query'] = self.__gen_pull()
Expand Down Expand Up @@ -307,12 +315,14 @@ def tearDown(self):

def test(self):
request = {'m':'gen_pull'}
result = self.litter_a.process(request)
result = self.litter_b.process(request)
self.assertEqual(result['headers']['hto'], 'all')
self.assertEqual(result['headers']['hfrom'], 'usera')
self.assertEqual(result['headers']['hfrom'], 'userb')
self.assertEqual(result['headers']['htype'], 'req')
self.assertEqual(result['headers']['httl'], 2)

result = self.litter_a.process(result)

request = {'posts':[]}
request['posts'].append(('this is my first post',))
request['posts'].append(('this is my second post',))
Expand All @@ -323,10 +333,27 @@ def test(self):
result = self.litter_a.process(request)
self.assertEqual(len(result['posts']),2)

try:
self.litter_a.process(result)
except Exception as ex:
print ex
request = {'m':'gen_pull'}
result = self.litter_b.process(request)
self.assertEqual(result['headers']['hto'], 'all')
self.assertEqual(result['headers']['hfrom'], 'userb')
self.assertEqual(result['headers']['htype'], 'req')
self.assertEqual(result['headers']['httl'], 2)

result = self.litter_a.process(result)
self.assertEqual(result['headers']['hto'], 'userb')
self.assertEqual(result['headers']['hfrom'], 'usera')
self.assertEqual(result['headers']['htype'], 'rep')
self.assertEqual(result['headers']['httl'], 4)

result = self.litter_b.process(result)
self.assertEqual(result, {'headers':None})

request = {'m':'get','begin':0,'limit':10}
result = self.litter_b.process(request)
self.assertEqual(result['headers'], None)
self.assertEqual(len(result['posts']),2)
self.assertEqual(result['posts'][0][1],'usera')


if __name__ == '__main__':
Expand Down
12 changes: 9 additions & 3 deletions src/web/litter.js
Expand Up @@ -83,7 +83,13 @@ function messageCount() {

function loadResults(state) {
for (var i = state.length-1; i >= 0; i--) {
addResult(state[i]);
var result = {}
result['msg'] = state[i][0]
result['uid'] = state[i][1]
result['txtime'] = state[i][2]
result['postid'] = state[i][3]
result['hashid'] = state[i][4]
addResult(result);
}
}

Expand Down Expand Up @@ -194,13 +200,13 @@ function doPost() {
alert("Message is longer than 140 characters");
return;
}
var ob = { "m" : "post", "msg" : msg };
var ob = { "posts": [[msg]] };
$("textarea#txt").val('');
$.ajax({type: "POST", url: "/api", dataType: 'json',
data : {'json' : JSON.stringify(ob)} ,
success: getState});
}

function processState(state) {
loadResults(state);
loadResults(state['posts']);
}

0 comments on commit 2872aa8

Please sign in to comment.