test_vmcp.py
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
import pybossa.vmcp as vmcp
from mock import patch
import hashlib
import M2Crypto
import base64
from default import assert_not_raises
class TestVMCP(object):
def test_myquote(self):
"""Test myquote works."""
# Valid char should be the same
err_msg = "Valid chars should not be quoted"
assert vmcp.myquote('a') == 'a', err_msg
# Non-valid
err_msg = "Non-Valid chars should be quoted"
assert vmcp.myquote('%') == '%25', err_msg
def test_calculate_buffer(self):
"""Test calculate_buffer works"""
data = {"flags": 8,
"name": "MyAwesomeVM",
"ram": 512,
"secret": "mg041na39123",
"userData": "[amiconfig]\nplugins=cernvm\n[cernvm]\nusers=user:users;password",
"vcpus": 1,
"true": True,
"false": False,
"version": "1.5"}
out = vmcp.calculate_buffer(data, 'salt')
err_msg = "Salt should be appended to the string"
assert 'salt' in out, err_msg
err_msg = "Boolean True has to be converted to 1"
assert "true=1" in out, err_msg
err_msg = "Boolean False has to be converted to 0"
assert "false=0" in out, err_msg
def test_sign(self):
"""Test sign works."""
rsa = M2Crypto.RSA.gen_key(2048, 65537)
salt = 'salt'
data = {"flags": 8,
"name": "MyAwesomeVM",
"ram": 512,
"secret": "mg041na39123",
"userData": "[amiconfig]\nplugins=cernvm\n[cernvm]\nusers=user:users;password",
"vcpus": 1,
"version": "1.5"}
strBuffer = vmcp.calculate_buffer(data, salt)
digest = hashlib.new('sha512', strBuffer).digest()
with patch('M2Crypto.RSA.load_key', return_value=rsa):
out = vmcp.sign(data, salt, 'key')
err_msg = "There should be a key named signature"
assert out.get('signature'), err_msg
err_msg = "The signature should not be empty"
assert out['signature'] is not None, err_msg
assert out['signature'] != '', err_msg
err_msg = "The signature should be the same"
signature = base64.b64decode(out['signature'])
assert rsa.verify(digest, signature, 'sha512') == 1, err_msg
# The output must be convertible into json object
import json
assert_not_raises(Exception, json.dumps, out)