Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
zhifan huang
re6stnet
Commits
088a618d
Commit
088a618d
authored
May 05, 2022
by
zhifan huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add test_versions
parent
720f9edd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
10 deletions
+62
-10
re6st/tests/test_unit/test_registry.py
re6st/tests/test_unit/test_registry.py
+62
-10
No files found.
re6st/tests/test_unit/test_registry.py
View file @
088a618d
...
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment