Commit f50ca4b0 authored by Jelte Jansen's avatar Jelte Jansen

address review comments part 3 of 3 (or 4, there were a few general comments...

address review comments part 3 of 3 (or 4, there were a few general comments that i'm not sure we should adress in this ticket)


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac172@2304 e5f2f494-b856-4b98-b285-d166d9295462
parent 8f0a2289
......@@ -30,8 +30,6 @@
using namespace std;
#define MAX_LABEL_LENGTH 255
namespace isc {
namespace data {
......
......@@ -70,8 +70,9 @@ TEST(Element, TypeNameConversion) {
}
TEST(Element, from_and_to_json) {
// this test checks whether the str() method returns the same
// string that was used for creation
// a set of inputs that are the same when converted to json and
// back to a string (tests for inputs that have equivalent, but
// different string representations when converted back are below)
ElementPtr el;
std::vector<std::string> sv;
......@@ -389,9 +390,15 @@ TEST(Element, to_and_from_wire) {
std::stringstream ss;
ss << "1";
EXPECT_EQ("1", Element::fromWire(ss, 1)->str());
// Some malformed JSON input
EXPECT_THROW(Element::fromJSON("{\":"), isc::data::JSONError);
EXPECT_THROW(Element::fromJSON("]"), isc::data::JSONError);
EXPECT_THROW(Element::fromJSON("[ 1, 2, }"), isc::data::JSONError);
}
ElementPtr efs(const std::string& str) {
static ElementPtr
efs(const std::string& str) {
return Element::fromJSON(str);
}
......
......@@ -24,9 +24,17 @@ import struct
import json
def to_wire(items):
'''Encodes the given python structure in JSON, and converts the
result to bytes. Raises a TypeError if the given structure is
not serializable with JSON.'''
return json.dumps(items).encode('utf8')
def from_wire(data):
'''Decodes the given bytes and parses it with the builtin JSON
parser. Raises a ValueError if the data is not valid JSON.
Raises an AttributeError if the given object has no decode()
method (which should return a string).
'''
return json.loads(data.decode('utf8'))
if __name__ == "__main__":
......
......@@ -21,11 +21,6 @@
import unittest
import isc.cc
#
# Umz, do we need anything more than abstraction from JSON?
# (i.e. a encode and decode function?)
#
class MessageTest(unittest.TestCase):
def setUp(self):
self.msg1 = { "just": [ "an", "arbitrary", "structure" ] }
......@@ -40,14 +35,20 @@ class MessageTest(unittest.TestCase):
self.assertEqual(self.msg1_wire, isc.cc.message.to_wire(self.msg1))
self.assertEqual(self.msg2_wire, isc.cc.message.to_wire(self.msg2))
self.assertRaises(TypeError, isc.cc.message.to_wire, NotImplemented)
def test_decode_json(self):
self.assertEqual(self.msg1, isc.cc.message.from_wire(self.msg1_wire))
self.assertEqual(self.msg2, isc.cc.message.from_wire(self.msg2_wire))
self.assertRaises(AttributeError, isc.cc.message.from_wire, 1)
self.assertRaises(ValueError, isc.cc.message.from_wire, b'\x001')
self.assertRaises(ValueError, isc.cc.message.from_wire, b'')
self.assertRaises(ValueError, isc.cc.message.from_wire, b'{"a": ')
self.assertRaises(ValueError, isc.cc.message.from_wire, b'[ 1 ')
self.assertRaises(ValueError, isc.cc.message.from_wire, b']')
if __name__ == '__main__':
#if not 'CONFIG_TESTDATA_PATH' in os.environ:
# print("You need to set the environment variable CONFIG_TESTDATA_PATH to point to the directory containing the test data files")
# exit(1)
unittest.main()
......@@ -39,10 +39,7 @@ class MySocket():
pass
def send(self, data):
print("[XX] send called:")
print(data)
self.sendqueue.extend(data);
pass
def readsent(self, length):
if length > len(self.sendqueue):
......@@ -70,7 +67,6 @@ class MySocket():
return result
def recv(self, length):
#print("[XX] recv(" + str(length) + ") of " + str(len(self.recvqueue)))
if length > len(self.recvqueue):
raise Exception("Buffer underrun in test, does the test provide the right data?")
result = self.recvqueue[:length]
......@@ -79,11 +75,19 @@ class MySocket():
#print("[XX] queue now: " + str(self.recvqueue))
return result
def addrecv(self, data):
self.recvqueue.extend(data)
def addrecvmsg(self, env, msg = None):
self.addrecv(self.preparemsg(env, msg))
def addrecv(self, env, msg = None):
if type(env) == dict:
env = isc.cc.message.to_wire(env)
if type(msg) == dict:
msg = isc.cc.message.to_wire(msg)
length = 2 + len(env);
if msg:
length += len(msg)
self.recvqueue.extend(struct.pack("!I", length))
self.recvqueue.extend(struct.pack("!H", len(env)))
self.recvqueue.extend(env)
if msg:
self.recvqueue.extend(msg)
#
# We subclass the Session class we're testing here, only
......@@ -120,7 +124,7 @@ class testSession(unittest.TestCase):
sess = MySession()
sess.sendmsg({}, {"hello": "a"})
sent = sess._socket.readsentmsg();
self.assertEqual(sent, b'\x00\x00\x00\x13\x00\x04SkanSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00\x12\x00\x02{}{"hello": "a"}')
sess.close()
self.assertRaises(SessionError, sess.sendmsg, {}, {"hello": "a"})
......@@ -147,41 +151,37 @@ class testSession(unittest.TestCase):
def test_session_recvmsg(self):
sess = MySession()
# {'to': "someone"}, {"hello": "a"}
self.recv_and_compare(sess,
b'\x00\x00\x00\x1f\x00\x10Skan\x02to(\x07someoneSkan\x05hello(\x01a',
{'to': "someone"}, {"hello": "a"})
#self.recv_and_compare(sess,
# b'\x00\x00\x00\x1f\x00\x10Skan\x02to(\x07someoneSkan\x05hello(\x01a',
# {'to': "someone"}, {"hello": "a"})
# 'malformed' messages
# shouldn't some of these raise exceptions?
self.recv_and_compare(sess,
b'\x00',
None, None)
self.recv_and_compare(sess,
b'\x00\x00\x00\x10',
None, None)
self.recv_and_compare(sess,
b'\x00\x00\x00\x02\x00\x00',
None, None)
self.recv_and_compare(sess,
b'\x00\x00\x00\x02\x00\x02',
None, None)
self.recv_and_compare(sess,
b'',
None, None)
# incomplete data field
sess._socket.addrecv(b'\x00\x00\x00\x1f\x00\x10Skan\x02to(\x07someoneSkan\x05hello(\x01')
self.assertRaises(isc.cc.message.DecodeError, sess.recvmsg)
#self.recv_and_compare(sess,
# b'\x00',
# None, None)
#self.recv_and_compare(sess,
# b'\x00\x00\x00\x10',
# None, None)
#self.recv_and_compare(sess,
# b'\x00\x00\x00\x02\x00\x00',
# None, None)
#self.recv_and_compare(sess,
# b'\x00\x00\x00\x02\x00\x02',
# None, None)
#self.recv_and_compare(sess,
# b'',
# None, None)
# need to clear
sess._socket.recvqueue = bytearray()
# 'queueing' system
# sending message {'to': 'someone', 'reply': 1}, {"hello": "a"}
#print("sending message {'to': 'someone', 'reply': 1}, {'hello': 'a'}")
# get no message without asking for a specific sequence number reply
self.assertFalse(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00(\x00\x19Skan\x02to(\x07someone\x05reply&\x011Skan\x05hello(\x01a')
sess._socket.addrecv({'to': 'someone', 'reply': 1}, {"hello": "a"})
env, msg = sess.recvmsg(False)
self.assertEqual(None, env)
self.assertTrue(sess.has_queued_msgs())
......@@ -193,7 +193,7 @@ class testSession(unittest.TestCase):
# ask for a differe sequence number reply (that doesn't exist)
# then ask for the one that is there
self.assertFalse(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00(\x00\x19Skan\x02to(\x07someone\x05reply&\x011Skan\x05hello(\x01a')
sess._socket.addrecv({'to': 'someone', 'reply': 1}, {"hello": "a"})
env, msg = sess.recvmsg(False, 2)
self.assertEqual(None, env)
self.assertEqual(None, msg)
......@@ -206,7 +206,7 @@ class testSession(unittest.TestCase):
# ask for a differe sequence number reply (that doesn't exist)
# then ask for any message
self.assertFalse(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00(\x00\x19Skan\x02to(\x07someone\x05reply&\x011Skan\x05hello(\x01a')
sess._socket.addrecv({'to': 'someone', 'reply': 1}, {"hello": "a"})
env, msg = sess.recvmsg(False, 2)
self.assertEqual(None, env)
self.assertEqual(None, msg)
......@@ -222,12 +222,12 @@ class testSession(unittest.TestCase):
# send a new message, ask for specific message (get the first)
# then ask for any message (get the second)
self.assertFalse(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00(\x00\x19Skan\x02to(\x07someone\x05reply&\x011Skan\x05hello(\x01a')
sess._socket.addrecv({'to': 'someone', 'reply': 1}, {'hello': 'a'})
env, msg = sess.recvmsg(False, 2)
self.assertEqual(None, env)
self.assertEqual(None, msg)
self.assertTrue(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00\x1f\x00\x10Skan\x02to(\x07someoneSkan\x05hello(\x01b')
sess._socket.addrecv({'to': 'someone' }, {'hello': 'b'})
env, msg = sess.recvmsg(False, 1)
self.assertEqual({'to': 'someone', 'reply': 1 }, env)
self.assertEqual({"hello": "a"}, msg)
......@@ -241,8 +241,8 @@ class testSession(unittest.TestCase):
# ask for that specific message (get the second)
# then ask for any message (get the first)
self.assertFalse(sess.has_queued_msgs())
sess._socket.addrecv(b'\x00\x00\x00\x1f\x00\x10Skan\x02to(\x07someoneSkan\x05hello(\x01b')
sess._socket.addrecv(b'\x00\x00\x00(\x00\x19Skan\x02to(\x07someone\x05reply&\x011Skan\x05hello(\x01a')
sess._socket.addrecv({'to': 'someone' }, {'hello': 'b'})
sess._socket.addrecv({'to': 'someone', 'reply': 1}, {'hello': 'a'})
env, msg = sess.recvmsg(False, 1)
self.assertEqual({'to': 'someone', 'reply': 1}, env)
self.assertEqual({"hello": "a"}, msg)
......@@ -266,29 +266,29 @@ class testSession(unittest.TestCase):
sess = MySession()
sess.group_subscribe("mygroup")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x001\x00/Skan\x05group(\x07mygroup\x04type(\tsubscribe\x08instance(\x01*')
self.assertEqual(sent, b'\x00\x00\x00<\x00:{"group": "mygroup", "type": "subscribe", "instance": "*"}')
sess.group_subscribe("mygroup")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x001\x00/Skan\x05group(\x07mygroup\x04type(\tsubscribe\x08instance(\x01*')
self.assertEqual(sent, b'\x00\x00\x00<\x00:{"group": "mygroup", "type": "subscribe", "instance": "*"}')
sess.group_subscribe("mygroup", "my_instance")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x00;\x009Skan\x05group(\x07mygroup\x04type(\tsubscribe\x08instance(\x0bmy_instance')
self.assertEqual(sent, b'\x00\x00\x00F\x00D{"group": "mygroup", "type": "subscribe", "instance": "my_instance"}')
def test_group_unsubscribe(self):
sess = MySession()
sess.group_unsubscribe("mygroup")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x003\x001Skan\x05group(\x07mygroup\x04type(\x0bunsubscribe\x08instance(\x01*')
self.assertEqual(sent, b'\x00\x00\x00>\x00<{"group": "mygroup", "type": "unsubscribe", "instance": "*"}')
sess.group_unsubscribe("mygroup")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x003\x001Skan\x05group(\x07mygroup\x04type(\x0bunsubscribe\x08instance(\x01*')
self.assertEqual(sent, b'\x00\x00\x00>\x00<{"group": "mygroup", "type": "unsubscribe", "instance": "*"}')
sess.group_unsubscribe("mygroup", "my_instance")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x00=\x00;Skan\x05group(\x07mygroup\x04type(\x0bunsubscribe\x08instance(\x0bmy_instance')
self.assertEqual(sent, b'\x00\x00\x00H\x00F{"group": "mygroup", "type": "unsubscribe", "instance": "my_instance"}')
def test_group_sendmsg(self):
sess = MySession()
......@@ -296,17 +296,17 @@ class testSession(unittest.TestCase):
sess.group_sendmsg({ 'hello': 'a' }, "my_group")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x00W\x00HSkan\x04from(\ttest_name\x03seq&\x012\x02to(\x01*\x08instance(\x01*\x05group(\x08my_group\x04type(\x04sendSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00p\x00`{"from": "test_name", "seq": 2, "to": "*", "instance": "*", "group": "my_group", "type": "send"}{"hello": "a"}')
self.assertEqual(sess._sequence, 2)
sess.group_sendmsg({ 'hello': 'a' }, "my_group", "my_instance")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x00a\x00RSkan\x04from(\ttest_name\x03seq&\x013\x02to(\x01*\x08instance(\x0bmy_instance\x05group(\x08my_group\x04type(\x04sendSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00z\x00j{"from": "test_name", "seq": 3, "to": "*", "instance": "my_instance", "group": "my_group", "type": "send"}{"hello": "a"}')
self.assertEqual(sess._sequence, 3)
sess.group_sendmsg({ 'hello': 'a' }, "your_group", "your_instance")
sent = sess._socket.readsentmsg()
self.assertEqual(sent, b'\x00\x00\x00e\x00VSkan\x04from(\ttest_name\x03seq&\x014\x02to(\x01*\x08instance(\ryour_instance\x05group(\nyour_group\x04type(\x04sendSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00~\x00n{"from": "test_name", "seq": 4, "to": "*", "instance": "your_instance", "group": "your_group", "type": "send"}{"hello": "a"}')
self.assertEqual(sess._sequence, 4)
def test_group_recvmsg(self):
......@@ -318,11 +318,11 @@ class testSession(unittest.TestCase):
sess = MySession()
sess.group_reply({ 'from': 'me', 'group': 'our_group', 'instance': 'other_instance', 'seq': 4}, {"hello": "a"})
sent = sess._socket.readsentmsg();
self.assertEqual(sent, b'\x00\x00\x00o\x00`Skan\x04from(\ttest_name\x03seq&\x012\x02to(\x02me\x08instance(\x0eother_instance\x05reply&\x014\x05group(\tour_group\x04type(\x04sendSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00\x8b\x00{{"from": "test_name", "seq": 2, "to": "me", "instance": "other_instance", "reply": 4, "group": "our_group", "type": "send"}{"hello": "a"}')
sess.group_reply({ 'from': 'me', 'group': 'our_group', 'instance': 'other_instance', 'seq': 9}, {"hello": "a"})
sent = sess._socket.readsentmsg();
self.assertEqual(sent, b'\x00\x00\x00o\x00`Skan\x04from(\ttest_name\x03seq&\x013\x02to(\x02me\x08instance(\x0eother_instance\x05reply&\x019\x05group(\tour_group\x04type(\x04sendSkan\x05hello(\x01a')
self.assertEqual(sent, b'\x00\x00\x00\x8b\x00{{"from": "test_name", "seq": 3, "to": "me", "instance": "other_instance", "reply": 9, "group": "our_group", "type": "send"}{"hello": "a"}')
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment