Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.libnetworkcache
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
Łukasz Nowak
slapos.libnetworkcache
Commits
2bd3bf5b
Commit
2bd3bf5b
authored
Jun 25, 2013
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: code cleanup
parent
35a73567
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
233 deletions
+66
-233
slapos/libnetworkcache.py
slapos/libnetworkcache.py
+2
-2
slapos/libnetworkcachetests.py
slapos/libnetworkcachetests.py
+64
-231
No files found.
slapos/libnetworkcache.py
View file @
2bd3bf5b
...
@@ -156,9 +156,9 @@ class NetworkcacheClient(object):
...
@@ -156,9 +156,9 @@ class NetworkcacheClient(object):
try
:
try
:
shacache_connection
.
request
(
'POST'
,
self
.
shacache_path
,
file_descriptor
,
shacache_connection
.
request
(
'POST'
,
self
.
shacache_path
,
file_descriptor
,
self
.
shacache_header_dict
)
self
.
shacache_header_dict
)
print
'uploade'
print
'uploade
d
'
result
=
shacache_connection
.
getresponse
()
result
=
shacache_connection
.
getresponse
()
print
'
repondu
'
print
'
answered
'
data
=
result
.
read
()
data
=
result
.
read
()
print
'read'
print
'read'
finally
:
finally
:
...
...
slapos/libnetworkcachetests.py
View file @
2bd3bf5b
...
@@ -119,6 +119,13 @@ class Server(BaseHTTPServer.HTTPServer):
...
@@ -119,6 +119,13 @@ class Server(BaseHTTPServer.HTTPServer):
__run
=
True
__run
=
True
@
classmethod
def
run
(
cls
,
*
args
,
**
kw
):
thread
=
threading
.
Thread
(
target
=
cls
(
*
args
,
**
kw
).
serve_forever
)
thread
.
daemon
=
True
thread
.
start
()
return
thread
def
serve_forever
(
self
):
def
serve_forever
(
self
):
while
self
.
__run
:
while
self
.
__run
:
self
.
handle_request
()
self
.
handle_request
()
...
@@ -127,12 +134,6 @@ class Server(BaseHTTPServer.HTTPServer):
...
@@ -127,12 +134,6 @@ class Server(BaseHTTPServer.HTTPServer):
self
.
__run
=
False
self
.
__run
=
False
def
_run_nc
(
tree
,
host
,
port
):
server_address
=
(
host
,
port
)
httpd
=
Server
(
tree
,
server_address
,
NCHandler
)
httpd
.
serve_forever
()
class
OfflineTest
(
unittest
.
TestCase
):
class
OfflineTest
(
unittest
.
TestCase
):
shacache_url
=
'http://localhost:1/shacache'
shacache_url
=
'http://localhost:1/shacache'
shadir_url
=
'http://localhost:1/shadir'
shadir_url
=
'http://localhost:1/shadir'
...
@@ -147,10 +148,9 @@ class OfflineTest(unittest.TestCase):
...
@@ -147,10 +148,9 @@ class OfflineTest(unittest.TestCase):
self
.
assertRaises
(
IOError
,
nc
.
download
,
'sha512sum'
)
self
.
assertRaises
(
IOError
,
nc
.
download
,
'sha512sum'
)
def
test_upload_offline
(
self
):
def
test_upload_offline
(
self
):
content
=
tempfile
.
TemporaryFile
()
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache_url
,
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache_url
,
self
.
shadir_url
)
self
.
shadir_url
)
self
.
assertRaises
(
IOError
,
nc
.
upload
,
content
)
self
.
assertRaises
(
IOError
,
nc
.
upload
,
tempfile
.
TemporaryFile
()
)
def
test_init_method_normal_http_url
(
self
):
def
test_init_method_normal_http_url
(
self
):
"""
"""
...
@@ -179,50 +179,23 @@ class OfflineTest(unittest.TestCase):
...
@@ -179,50 +179,23 @@ class OfflineTest(unittest.TestCase):
self
.
assertTrue
(
nc
.
shadir_host
in
self
.
shadir_url
)
self
.
assertTrue
(
nc
.
shadir_host
in
self
.
shadir_url
)
def
wait
(
host
,
port
):
addr
=
host
,
port
for
i
in
range
(
120
):
time
.
sleep
(
0.25
)
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
connect
(
addr
)
s
.
close
()
break
except
socket
.
error
,
e
:
if
e
[
0
]
not
in
(
errno
.
ECONNREFUSED
,
errno
.
ECONNRESET
):
raise
s
.
close
()
else
:
raise
class
OnlineMixin
:
class
OnlineMixin
:
def
_start_nc
(
self
):
handler
=
NCHandler
self
.
thread
=
threading
.
Thread
(
target
=
_run_nc
,
args
=
(
self
.
tree
,
self
.
host
,
self
.
port
))
self
.
thread
.
setDaemon
(
True
)
self
.
thread
.
start
()
wait
(
self
.
host
,
self
.
port
)
schema
=
'http'
schema
=
'http'
def
setUp
(
self
):
def
setUp
(
self
):
self
.
host
=
"127.0.0.1"
self
.
host
=
"127.0.0.1"
self
.
port
=
8080
self
.
port
=
8080
self
.
url
=
'%s://%s:%s/'
%
(
self
.
schema
,
self
.
host
,
self
.
port
)
self
.
url
=
'%s://%s:%s/'
%
(
self
.
schema
,
self
.
host
,
self
.
port
)
self
.
shacache
=
os
.
environ
.
get
(
'TEST_SHA_CACHE'
,
self
.
shacache
=
os
.
environ
.
get
(
'TEST_SHA_CACHE'
,
self
.
url
+
'shacache'
)
self
.
url
+
'shacache'
)
self
.
shadir
=
os
.
environ
.
get
(
'TEST_SHA_DIR'
,
self
.
url
+
'shadir'
)
self
.
shadir
=
os
.
environ
.
get
(
'TEST_SHA_DIR'
,
self
.
url
+
'shadir'
)
if
not
'TEST_SHA_CACHE'
in
os
.
environ
and
not
'TEST_SHA_DIR'
in
os
.
environ
:
if
not
'TEST_SHA_CACHE'
in
os
.
environ
and
not
'TEST_SHA_DIR'
in
os
.
environ
:
self
.
tree
=
tempfile
.
mkdtemp
()
self
.
tree
=
tempfile
.
mkdtemp
()
self
.
_start_nc
(
)
self
.
thread
=
Server
.
run
(
self
.
tree
,
(
self
.
host
,
self
.
port
),
self
.
handler
)
self
.
test_data
=
tempfile
.
TemporaryFile
()
self
.
test_data
=
tempfile
.
TemporaryFile
()
self
.
test_string
=
str
(
random
.
random
())
self
.
test_string
=
str
(
random
.
random
())
self
.
test_data
.
write
(
self
.
test_string
)
self
.
test_data
.
write
(
self
.
test_string
)
self
.
test_data
.
seek
(
0
)
self
.
test_shasum
=
hashlib
.
sha512
(
self
.
test_string
).
hexdigest
()
self
.
test_shasum
=
hashlib
.
sha512
(
self
.
test_data
.
read
()).
hexdigest
()
self
.
test_data
.
seek
(
0
)
def
tearDown
(
self
):
def
tearDown
(
self
):
if
not
'TEST_SHA_CACHE'
in
os
.
environ
and
not
'TEST_SHA_DIR'
in
os
.
environ
:
if
not
'TEST_SHA_CACHE'
in
os
.
environ
and
not
'TEST_SHA_DIR'
in
os
.
environ
:
...
@@ -235,6 +208,13 @@ class OnlineMixin:
...
@@ -235,6 +208,13 @@ class OnlineMixin:
self
.
thread
.
join
()
self
.
thread
.
join
()
shutil
.
rmtree
(
self
.
tree
)
shutil
.
rmtree
(
self
.
tree
)
def
assertDirectoryNotFound
(
self
,
msg
,
func
,
*
args
,
**
kw
):
try
:
func
(
*
args
,
**
kw
)
self
.
fail
(
"DirectoryNotFound not raised"
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
e
:
self
.
assertTrue
(
str
(
e
).
startswith
(
msg
))
key
=
"""-----BEGIN RSA PRIVATE KEY-----
key
=
"""-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDDrOO87nSiDcXOf+xGc4Iqcdjfwd0RTOxEkO9z8mPZVg2bTPwt
MIICXQIBAAKBgQDDrOO87nSiDcXOf+xGc4Iqcdjfwd0RTOxEkO9z8mPZVg2bTPwt
/GwtPgmIC4po3bJdsCpJH21ZJwfmUpaQWIApj3odDAbRXQHWhNiw9ZPMHTCmf8Zl
/GwtPgmIC4po3bJdsCpJH21ZJwfmUpaQWIApj3odDAbRXQHWhNiw9ZPMHTCmf8Zl
...
@@ -408,7 +388,6 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -408,7 +388,6 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
def
test_upload_twice_same
(
self
):
def
test_upload_twice_same
(
self
):
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
nc
.
upload
(
self
.
test_data
)
nc
.
upload
(
self
.
test_data
)
self
.
test_data
.
seek
(
0
)
nc
.
upload
(
self
.
test_data
)
nc
.
upload
(
self
.
test_data
)
def
test_download
(
self
):
def
test_download
(
self
):
...
@@ -436,18 +415,11 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -436,18 +415,11 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
result
=
signed_nc
.
select
(
key
)
result
=
signed_nc
.
select
(
key
)
self
.
assertEqual
(
result
.
read
(),
self
.
test_string
)
self
.
assertEqual
(
result
.
read
(),
self
.
test_string
)
...
@@ -456,19 +428,12 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -456,19 +428,12 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
alternate_certificate
,
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
self
.
certificate
])
(
self
.
alternate_certificate
,
self
.
certificate
))
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
result
=
signed_nc
.
select
(
key
)
result
=
signed_nc
.
select
(
key
)
self
.
assertEqual
(
result
.
read
(),
self
.
test_string
)
self
.
assertEqual
(
result
.
read
(),
self
.
test_string
)
...
@@ -477,18 +442,11 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -477,18 +442,11 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
result
=
signed_nc
.
select
(
key
)
result
=
signed_nc
.
select
(
key
)
...
@@ -498,21 +456,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -498,21 +456,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
signed_nc
.
openssl
=
'/doesnotexists'
signed_nc
.
openssl
=
'/doesnotexists'
try
:
try
:
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
self
.
fail
(
"UploadError not raised"
)
except
slapos
.
libnetworkcache
.
UploadError
,
e
:
except
slapos
.
libnetworkcache
.
UploadError
,
e
:
self
.
assertTrue
(
str
(
e
).
startswith
(
"Impossible to sign content, error"
))
self
.
assertTrue
(
str
(
e
).
startswith
(
"Impossible to sign content, error"
))
...
@@ -520,21 +472,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -520,21 +472,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
signed_nc
.
openssl
=
sys
.
executable
signed_nc
.
openssl
=
sys
.
executable
try
:
try
:
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
self
.
fail
(
"UploadError not raised"
)
except
slapos
.
libnetworkcache
.
UploadError
,
e
:
except
slapos
.
libnetworkcache
.
UploadError
,
e
:
self
.
assertTrue
(
str
(
e
).
startswith
(
"Impossible to sign content, error"
))
self
.
assertTrue
(
str
(
e
).
startswith
(
"Impossible to sign content, error"
))
...
@@ -542,67 +488,36 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -542,67 +488,36 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
assertEqual
(
True
,
signed_nc
.
upload
(
self
.
test_data
,
key
,
self
.
assertEqual
(
True
,
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
))
urlmd5
=
urlmd5
,
file_name
=
file_name
))
try
:
signed_nc
.
openssl
=
'/doesnotexists'
# disable openssl during download
self
.
assertDirectoryNotFound
(
'Could not find a trustable entry.'
,
signed_nc
.
openssl
=
'/doesnotexists'
signed_nc
.
select
,
key
)
result
=
signed_nc
.
select
(
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
# No way to download content from shadir w/o openssl
self
.
assertEqual
(
str
(
msg
),
'Could not find a trustable entry.'
)
def
test_select_signed_content_openssl_non_functional
(
self
):
def
test_select_signed_content_openssl_non_functional
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
assertEqual
(
True
,
signed_nc
.
upload
(
self
.
test_data
,
key
,
self
.
assertEqual
(
True
,
signed_nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
))
urlmd5
=
urlmd5
,
file_name
=
file_name
))
try
:
signed_nc
.
openssl
=
sys
.
executable
# disable openssl during download
self
.
assertDirectoryNotFound
(
'Could not find a trustable entry.'
,
signed_nc
.
openssl
=
sys
.
executable
signed_nc
.
select
,
key
)
result
=
signed_nc
.
select
(
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
# No way to download content from shadir w/o openssl
self
.
assertEqual
(
str
(
msg
),
'Could not find a trustable entry.'
)
def
test_select_no_entries
(
self
):
def
test_select_no_entries
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
self
.
shacache
,
self
.
shadir
)
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
...
@@ -610,104 +525,60 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -610,104 +525,60 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
f
=
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
)
f
=
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
)
# now remove the entry from shacache
# now remove the entry from shacache
open
(
f
,
'w'
).
write
(
json
.
dumps
([]))
open
(
f
,
'w'
).
write
(
json
.
dumps
([]))
try
:
self
.
assertDirectoryNotFound
(
'Could not find a trustable entry.'
,
nc
.
select
(
key
)
nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
# empty content in shadir so usual exception is raised
self
.
assertEqual
(
str
(
msg
),
'Could not find a trustable entry.'
)
def
test_select_no_json_response
(
self
):
def
test_select_no_json_response
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
self
.
shacache
,
self
.
shadir
)
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
))
file_name
=
file_name
))
f
=
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
)
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
# now remove the entry from shacache
open
(
f
,
'w'
).
write
(
'This is not a json.'
)
f
.
write
(
'This is not a json.'
)
try
:
self
.
assertDirectoryNotFound
(
'It was impossible to parse json response'
,
nc
.
select
(
key
)
nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
self
.
assertTrue
(
str
(
msg
).
startswith
(
'It was impossible to parse json '
'response'
))
def
test_select_json_no_in_json_response
(
self
):
def
test_select_json_no_in_json_response
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
self
.
shacache
,
self
.
shadir
)
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
))
file_name
=
file_name
))
f
=
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
)
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
# now remove the entry from shacache
open
(
f
,
'w'
).
write
(
json
.
dumps
([[
'This is not a json.'
,
'signature'
]]))
f
.
write
(
json
.
dumps
([[
'This is not a json.'
,
'signature'
]]))
try
:
self
.
assertDirectoryNotFound
(
nc
.
select
(
key
)
'It was impossible to parse json-in-json response'
,
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
nc
.
select
,
key
)
self
.
assertTrue
(
str
(
msg
).
startswith
(
'It was impossible to parse '
'json-in-json response'
))
def
test_select_json_in_json_no_dict
(
self
):
def
test_select_json_in_json_no_dict
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
self
.
shacache
,
self
.
shadir
)
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
self
.
assertEqual
(
True
,
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
))
file_name
=
file_name
))
f
=
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
)
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
# now remove the entry from shacache
open
(
f
,
'w'
).
write
(
json
.
dumps
([[
json
.
dumps
(
'This is a string'
),
f
.
write
(
json
.
dumps
([[
json
.
dumps
(
'This is a string'
),
'signature'
]]))
'signature'
]]))
self
.
assertDirectoryNotFound
(
try
:
'It was impossible to fetch sha512 from directory response'
,
nc
.
select
(
key
)
nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
self
.
assertTrue
(
str
(
msg
).
startswith
(
'It was impossible to fetch sha512 '
'from directory response'
))
def
test_select_signed_content_server_hacked
(
self
):
def
test_select_signed_content_server_hacked
(
self
):
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
urlmd5
=
str
(
random
.
random
())
file_name
=
'my file'
file_name
=
'my file'
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
signed_nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
self
.
shacache
,
self
.
shadir
,
key_file
.
name
,
[
self
.
certificate
])
...
@@ -725,11 +596,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -725,11 +596,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
# ...but as he has no access to key, no way to sign data..
# ...but as he has no access to key, no way to sign data..
# hacked_json[0][1] is still a good key
# hacked_json[0][1] is still a good key
open
(
f
,
'w'
).
write
(
json
.
dumps
(
hacked_json
))
open
(
f
,
'w'
).
write
(
json
.
dumps
(
hacked_json
))
try
:
self
.
assertDirectoryNotFound
(
'Could not find a trustable entry.'
,
result
=
signed_nc
.
select
(
key
)
signed_nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
# hacked content is not trusted
self
.
assertEqual
(
str
(
msg
),
'Could not find a trustable entry.'
)
def
test_select_DirectoryNotFound_too_many_for_key
(
self
):
def
test_select_DirectoryNotFound_too_many_for_key
(
self
):
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
...
@@ -739,20 +607,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -739,20 +607,15 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
test_data
=
tempfile
.
TemporaryFile
()
test_data
=
tempfile
.
TemporaryFile
()
test_string
=
str
(
random
.
random
())
test_string
=
str
(
random
.
random
())
test_data
.
write
(
test_string
)
test_data
.
write
(
test_string
)
test_data
.
seek
(
0
)
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
nc
.
upload
(
self
.
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
nc
.
upload
(
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
nc
.
upload
(
test_data
,
key
,
urlmd5
=
urlmd5
,
file_name
=
file_name
)
try
:
self
.
assertDirectoryNotFound
(
"Too many entries for a given key %r"
%
key
,
nc
.
select
(
key
)
nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
self
.
assertTrue
(
str
(
msg
).
startswith
(
"Too many entries for a given key %r"
%
key
))
def
test_DirectoryNotFound_non_trustable_entry
(
self
):
def
test_DirectoryNotFound_non_trustable_entry
(
self
):
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
)
key
=
'somekey'
+
str
(
random
.
random
())
key
=
'somekey'
+
str
(
random
.
random
())
...
@@ -766,10 +629,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -766,10 +629,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
selected
=
nc
.
select
(
key
).
read
()
selected
=
nc
.
select
(
key
).
read
()
self
.
assertEqual
(
selected
,
self
.
test_string
)
self
.
assertEqual
(
selected
,
self
.
test_string
)
# but when signature is used, networkcache will complain
# but when signature is used, networkcache will complain
try
:
self
.
assertDirectoryNotFound
(
'Could not find a trustable entry.'
,
signed_nc
.
select
(
key
)
signed_nc
.
select
,
key
)
except
slapos
.
libnetworkcache
.
DirectoryNotFound
,
msg
:
self
.
assertEqual
(
str
(
msg
),
'Could not find a trustable entry.'
)
# of course if proper key will be used to sign the content uploaded
# of course if proper key will be used to sign the content uploaded
# into shacache all will work
# into shacache all will work
...
@@ -783,12 +644,10 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -783,12 +644,10 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
key
)
key_file
.
write
(
self
.
key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
certificate_file
=
tempfile
.
NamedTemporaryFile
()
certificate_file
=
tempfile
.
NamedTemporaryFile
()
certificate_file
.
write
(
self
.
certificate
)
certificate_file
.
write
(
self
.
certificate
)
certificate_file
.
flush
()
certificate_file
.
flush
()
certificate_file
.
seek
(
0
)
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shacache
,
self
.
shadir
,
shacache_cert_file
=
certificate_file
,
shacache_key_file
=
key_file
)
shacache_cert_file
=
certificate_file
,
shacache_key_file
=
key_file
)
...
@@ -800,12 +659,10 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
...
@@ -800,12 +659,10 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
=
tempfile
.
NamedTemporaryFile
()
key_file
.
write
(
self
.
auth_key
)
key_file
.
write
(
self
.
auth_key
)
key_file
.
flush
()
key_file
.
flush
()
key_file
.
seek
(
0
)
certificate_file
=
tempfile
.
NamedTemporaryFile
()
certificate_file
=
tempfile
.
NamedTemporaryFile
()
certificate_file
.
write
(
self
.
auth_certificate
)
certificate_file
.
write
(
self
.
auth_certificate
)
certificate_file
.
flush
()
certificate_file
.
flush
()
certificate_file
.
seek
(
0
)
# simplified assertion, as no http authentication server is available
# simplified assertion, as no http authentication server is available
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shadir
,
self
.
shadir
,
nc
=
slapos
.
libnetworkcache
.
NetworkcacheClient
(
self
.
shadir
,
self
.
shadir
,
...
@@ -863,19 +720,8 @@ class OnlineTestSSLServer(OnlineMixin, unittest.TestCase):
...
@@ -863,19 +720,8 @@ class OnlineTestSSLServer(OnlineMixin, unittest.TestCase):
file_name
=
str
(
random
.
random
()),
urlmd5
=
str
(
random
.
random
()))
file_name
=
str
(
random
.
random
()),
urlmd5
=
str
(
random
.
random
()))
def
_run_nc_POST200
(
tree
,
host
,
port
):
server_address
=
(
host
,
port
)
httpd
=
Server
(
tree
,
server_address
,
NCHandlerPOST200
)
httpd
.
serve_forever
()
class
OnlineTestPOST200
(
OnlineMixin
,
unittest
.
TestCase
):
class
OnlineTestPOST200
(
OnlineMixin
,
unittest
.
TestCase
):
def
_start_nc
(
self
):
handler
=
NCHandlerPOST200
self
.
thread
=
threading
.
Thread
(
target
=
_run_nc_POST200
,
args
=
(
self
.
tree
,
self
.
host
,
self
.
port
))
self
.
thread
.
setDaemon
(
True
)
self
.
thread
.
start
()
wait
(
self
.
host
,
self
.
port
)
def
test_upload_wrong_return_code
(
self
):
def
test_upload_wrong_return_code
(
self
):
"""Check reaction on HTTP return code different then 201"""
"""Check reaction on HTTP return code different then 201"""
...
@@ -884,19 +730,8 @@ class OnlineTestPOST200(OnlineMixin, unittest.TestCase):
...
@@ -884,19 +730,8 @@ class OnlineTestPOST200(OnlineMixin, unittest.TestCase):
self
.
test_data
)
self
.
test_data
)
def
_run_nc_POSTWrongChecksum
(
tree
,
host
,
port
):
server_address
=
(
host
,
port
)
httpd
=
Server
(
tree
,
server_address
,
NCHandlerReturnWrong
)
httpd
.
serve_forever
()
class
OnlineTestWrongChecksum
(
OnlineMixin
,
unittest
.
TestCase
):
class
OnlineTestWrongChecksum
(
OnlineMixin
,
unittest
.
TestCase
):
def
_start_nc
(
self
):
handler
=
NCHandlerReturnWrong
self
.
thread
=
threading
.
Thread
(
target
=
_run_nc_POSTWrongChecksum
,
args
=
(
self
.
tree
,
self
.
host
,
self
.
port
))
self
.
thread
.
setDaemon
(
True
)
self
.
thread
.
start
()
wait
(
self
.
host
,
self
.
port
)
def
test_upload_wrong_return_sha
(
self
):
def
test_upload_wrong_return_sha
(
self
):
"""Check reaction in case of wrong sha returned"""
"""Check reaction in case of wrong sha returned"""
...
@@ -913,12 +748,9 @@ class GenerateSignatureScriptTest(unittest.TestCase):
...
@@ -913,12 +748,9 @@ class GenerateSignatureScriptTest(unittest.TestCase):
self
.
certificate
=
os
.
path
.
join
(
tempfile
.
gettempdir
(),
tempfile
.
gettempprefix
()
self
.
certificate
=
os
.
path
.
join
(
tempfile
.
gettempdir
(),
tempfile
.
gettempprefix
()
+
str
(
random
.
random
()))
+
str
(
random
.
random
()))
self
.
common_name
=
str
(
random
.
random
())
self
.
common_name
=
str
(
random
.
random
())
self
.
key_exist
=
tempfile
.
NamedTemporaryFile
(
delete
=
False
).
name
self
.
certificate_exist
=
tempfile
.
NamedTemporaryFile
(
delete
=
False
).
name
def
tearDown
(
self
):
def
tearDown
(
self
):
for
f
in
[
self
.
key
,
self
.
certificate
,
self
.
key_exist
,
for
f
in
self
.
key
,
self
.
certificate
:
self
.
certificate_exist
]:
if
os
.
path
.
lexists
(
f
):
if
os
.
path
.
lexists
(
f
):
os
.
unlink
(
f
)
os
.
unlink
(
f
)
...
@@ -934,10 +766,11 @@ class GenerateSignatureScriptTest(unittest.TestCase):
...
@@ -934,10 +766,11 @@ class GenerateSignatureScriptTest(unittest.TestCase):
self
.
assertTrue
(
' %s '
%
(
today
.
year
+
100
)
in
result
)
self
.
assertTrue
(
' %s '
%
(
today
.
year
+
100
)
in
result
)
def
test_generate_key_exists
(
self
):
def
test_generate_key_exists
(
self
):
self
.
assertRaises
(
ValueError
,
slapos
.
signature
.
generateCertificate
,
with
tempfile
.
NamedTemporaryFile
()
as
key
:
self
.
certificate
,
self
.
key_exist
,
self
.
common_name
)
self
.
assertRaises
(
ValueError
,
slapos
.
signature
.
generateCertificate
,
self
.
certificate
,
key
.
name
,
self
.
common_name
)
def
test_generate_cert_exists
(
self
):
def
test_generate_cert_exists
(
self
):
self
.
assertRaises
(
ValueError
,
slapos
.
signature
.
generateCertificate
,
with
tempfile
.
NamedTemporaryFile
()
as
cert
:
self
.
certificate_exist
,
self
.
key
,
self
.
common_name
)
self
.
assertRaises
(
ValueError
,
slapos
.
signature
.
generateCertificate
,
cert
.
name
,
self
.
key
,
self
.
common_name
)
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