blob: 3124ea5e9aed4fda23f4399a2066903c2527305a [file] [log] [blame]
Lianhao Lu432bca42018-03-24 22:36:08 +08001# Copyright (c) 2018 Intel Corp. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14#
15
16import os
Lianhao Lua570b0b2018-08-24 18:48:49 +080017import subprocess
18
19import pytest
Lianhao Lu432bca42018-03-24 22:36:08 +080020
21from vnfsdk_pkgtools.packager import utils
22
23CONTENT = "needToBeHashed"
24SHA256 = "20a480339aa4371099f9503511dcc5a8051ce3884846678ced5611ec64bbfc9c"
25SHA512 = "dbed8672e752d51d0c7ca42050f67faf1534e58470bba96e787df5c4cf6a4f8ecf7ad45fb9307adbc5b9dec8432627d86b3eb1d3d43ee9c5e93f754ff2825320"
26
27def test_cal_file_hash(tmpdir):
28 p = tmpdir.join("file_to_hash.txt")
29 p.write(CONTENT)
30 assert SHA512 == utils.cal_file_hash("", str(p), 'SHA512')
31 assert SHA256 == utils.cal_file_hash(p.dirname, p.basename, 'sha256')
Lianhao Lu979471f2018-07-26 17:50:28 +080032
33def test_cal_file_hash_remote(mocker):
34 class FakeRequest(object):
35 def __init__(self, *args):
36 self.status_code = 200
37 self.content = CONTENT
38 mocker.patch('requests.get', new=FakeRequest)
39 assert SHA256 == utils.cal_file_hash("", "http://fake", 'sha256')
Lianhao Lua570b0b2018-08-24 18:48:49 +080040
41
42MSG_FILE = "tests/resources/signature/manifest.mf"
43CERT_FILE = "tests/resources/signature/test.crt"
44KEY_FILE = "tests/resources/signature/test.key"
45
46def test_sign_verify_pairwise():
47 cms = utils.sign(MSG_FILE, CERT_FILE, KEY_FILE)
48 # We can't examine the exact content of cms because it contains timestamp
49 assert "---BEGIN CMS---" in cms
50 assert "---END CMS---" in cms
51 utils.verify(MSG_FILE, CERT_FILE, cms, no_verify_cert=True)
52
53
54def test_verify_bad(tmpdir):
55 cms = utils.sign(MSG_FILE, CERT_FILE, KEY_FILE)
56
57 p = tmpdir.join("file_msg.txt")
58 p.write("BAD")
59
60 with pytest.raises(subprocess.CalledProcessError):
61 utils.verify(str(p), CERT_FILE, cms, no_verify_cert=True)
62