示例#1
0
    def test_sign(self):
        """Test sign works."""
        # rsa_pk = M2Crypto.RSA.gen_key(2048, 65537)
        rsa_keys = rsa.newkeys(2048, 65537)
        rsa_pk = rsa_keys[1]
        rsa_pub = rsa_keys[0]
        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)
        
        with patch('rsa.PrivateKey.load_pkcs1', return_value=rsa_pk):
            with patch('pybossa.vmcp.open', mock_open(read_data=''), create=True) as m:
                out = vmcp.sign(data, salt, 'testkey')
                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(strBuffer, signature, rsa_pub) == 1, err_msg

                # The output must be convertible into json object
                import json
                assert_not_raises(Exception, json.dumps, out)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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