Commit 088a618d authored by zhifan huang's avatar zhifan huang

add test_versions

parent 720f9edd
......@@ -14,10 +14,11 @@ from OpenSSL import crypto
from mock import Mock, patch
from re6st import registry
from re6st import ctl
from re6st.tests.tools import *
# TODO test for request_dump, requestToken, getNetworkConfig, getBoostrapPeer
# getIPV4Information, versions
# getIPV4Information
def load_config(filename="registry.json"):
......@@ -45,6 +46,11 @@ insert_cert.serial = 0
def delete_cert(cur, prefix):
cur.execute("DELETE FROM cert WHERE prefix = ?", (prefix,))
def select_side_effect(rlist, wlist, elist, timeout):
"""method used to mock select.select"""""
select_side_effect.i -= select_side_effect.i > 0
return [select_side_effect.i, wlist, None]
select_side_effect.i = 0
# TODO function for get a unique prefix
......@@ -55,6 +61,7 @@ class TestRegistryServer(unittest.TestCase):
# instance a server
cls.config = load_config()
cls.server = registry.RegistryServer(cls.config)
cls.server.prefix = format(0, "016b")
@classmethod
def tearDownClass(cls):
......@@ -68,6 +75,14 @@ class TestRegistryServer(unittest.TestCase):
self.email = ''.join(random.sample(string.ascii_lowercase, 4)) \
+ "@mail.com"
def test_increasVersion(self):
"""assume self.version is bytes type, check the result of same type"""
self.assertIsInstance(self.server.version, bytes)
self.server.increaseVersion()
self.assertIsInstance(self.server.version, bytes)
def test_recv(self):
recv = self.server.sock.recv = Mock()
recv.side_effect = [
......@@ -348,6 +363,15 @@ class TestRegistryServer(unittest.TestCase):
self.assertIsNone(get_cert(self.server.db, prefix))
def test_updateHMAC(self):
"""check hmac 5 times
the init hmac is [None, None, None],
[, 1, ] -> [1, , ] -> [1, 2, ] -> [, 2, 1] -> [, 2, ]
"""
# sometimes, server dont init the network_config, so create a empty one
if not hasattr(self.server, "network_config"):
self.server.network_config = {}
temp_config = None
def get_hmac():
return [self.server.getConfig(registry.BABEL_HMAC[i], None)
for i in range(3)]
......@@ -364,7 +388,6 @@ class TestRegistryServer(unittest.TestCase):
# step 2
self.server.updateHMAC()
self.assertEqual(get_hmac(), [key_1, None, None])
# step 3
......@@ -376,12 +399,10 @@ class TestRegistryServer(unittest.TestCase):
# step 4
self.server.updateHMAC()
self.assertEqual(get_hmac(), [None, key_2, key_1])
#setp 5
self.server.updateHMAC()
self.assertEqual(get_hmac(), [key_2, None, None])
......@@ -394,6 +415,40 @@ class TestRegistryServer(unittest.TestCase):
self.assertEqual(res, prefix2cn(prefix))
@patch("select.select")
@patch("re6st.registry.RegistryServer.recv")
@patch("re6st.registry.RegistryServer.request_dump", Mock())
@patch("re6st.registry.RegistryServer.sendto", Mock())
def test_versions(self, recv, select):
"""re6set have 1 reigstry r1, 2 node, m1, m2
recv 2 times m1, m2's packat is lost"""
self.server.ctl.neighbours = neigh = {}
for i in 1,2:
neigh[i] = (i, {format(i,"016b"):i})
recv_case = [
("0000000000000000", "v0"), # this because versions r than w, this will be overwriten by wr to None
("0000000000000000", "v0"),
("0000000000000001", "v1"),
("0000000000000001", "v1")
]
recv.side_effect = recv_case
select_side_effect.i = len(recv_case) + 1
select.side_effect = select_side_effect
res = self.server.versions()
res = json.loads(res)
expect_res = {
u"0000000000000000" : u"v0",
u"0000000000000001" : u"v1",
u"0000000000000010" : None
}
self.assertEqual(res, expect_res)
recv.assert_called_with(4)
del self.server.ctl.neighbours
@patch("select.select")
@patch("re6st.registry.RegistryServer.recv")
@patch("re6st.registry.RegistryServer.sendto", Mock())
......@@ -408,12 +463,9 @@ class TestRegistryServer(unittest.TestCase):
('0000000000000001', '2 0/16 6/16')
]
recv.side_effect = recv_case
def side_effct(rlist, wlist, elist, timeout):
# rlist is true until the len(recv_case)th call
side_effct.i -= side_effct.i > 0
return [side_effct.i, wlist, None]
side_effct.i = len(recv_case) + 1
select.side_effect = side_effct
# rlist is true until the len(recv_case)th call
select_side_effect.i = len(recv_case) + 1
select.side_effect = select_side_effect
res = self.server.topology()
......
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