Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 1 | # 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 | |
| 16 | import os |
Lianhao Lu | a570b0b | 2018-08-24 18:48:49 +0800 | [diff] [blame] | 17 | import subprocess |
| 18 | |
| 19 | import pytest |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 20 | |
| 21 | from vnfsdk_pkgtools.packager import utils |
Bogumil Zebek | a7308fa | 2019-02-25 11:36:37 +0100 | [diff] [blame] | 22 | from vnfsdk_pkgtools import util |
| 23 | |
| 24 | ROOT_DIR = util.get_project_root() |
| 25 | RESOURCES_DIR= os.path.join(ROOT_DIR, 'tests', 'resources', 'signature') |
| 26 | |
| 27 | MSG_FILE = os.path.join(RESOURCES_DIR, 'manifest.mf') |
| 28 | CERT_FILE = os.path.join(RESOURCES_DIR, 'test.crt') |
| 29 | KEY_FILE = os.path.join(RESOURCES_DIR, 'test.key') |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 30 | |
Lianhao Lu | 1ee29e0 | 2019-11-01 16:01:34 +0800 | [diff] [blame] | 31 | CONTENT = b"needToBeHashed" |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 32 | SHA256 = "20a480339aa4371099f9503511dcc5a8051ce3884846678ced5611ec64bbfc9c" |
| 33 | SHA512 = "dbed8672e752d51d0c7ca42050f67faf1534e58470bba96e787df5c4cf6a4f8ecf7ad45fb9307adbc5b9dec8432627d86b3eb1d3d43ee9c5e93f754ff2825320" |
| 34 | |
| 35 | def test_cal_file_hash(tmpdir): |
| 36 | p = tmpdir.join("file_to_hash.txt") |
| 37 | p.write(CONTENT) |
| 38 | assert SHA512 == utils.cal_file_hash("", str(p), 'SHA512') |
| 39 | assert SHA256 == utils.cal_file_hash(p.dirname, p.basename, 'sha256') |
Lianhao Lu | 979471f | 2018-07-26 17:50:28 +0800 | [diff] [blame] | 40 | |
| 41 | def test_cal_file_hash_remote(mocker): |
| 42 | class FakeRequest(object): |
| 43 | def __init__(self, *args): |
| 44 | self.status_code = 200 |
| 45 | self.content = CONTENT |
| 46 | mocker.patch('requests.get', new=FakeRequest) |
| 47 | assert SHA256 == utils.cal_file_hash("", "http://fake", 'sha256') |
Lianhao Lu | a570b0b | 2018-08-24 18:48:49 +0800 | [diff] [blame] | 48 | |
Lianhao Lu | a570b0b | 2018-08-24 18:48:49 +0800 | [diff] [blame] | 49 | def test_sign_verify_pairwise(): |
| 50 | cms = utils.sign(MSG_FILE, CERT_FILE, KEY_FILE) |
| 51 | # We can't examine the exact content of cms because it contains timestamp |
| 52 | assert "---BEGIN CMS---" in cms |
| 53 | assert "---END CMS---" in cms |
| 54 | utils.verify(MSG_FILE, CERT_FILE, cms, no_verify_cert=True) |
| 55 | |
| 56 | |
| 57 | def test_verify_bad(tmpdir): |
| 58 | cms = utils.sign(MSG_FILE, CERT_FILE, KEY_FILE) |
| 59 | |
| 60 | p = tmpdir.join("file_msg.txt") |
| 61 | p.write("BAD") |
| 62 | |
| 63 | with pytest.raises(subprocess.CalledProcessError): |
| 64 | utils.verify(str(p), CERT_FILE, cms, no_verify_cert=True) |
Lianhao Lu | fdb7c57 | 2019-11-28 15:12:22 +0800 | [diff] [blame^] | 65 | |
| 66 | |
| 67 | CHECK_FILE_CASES = [ |
| 68 | { |
| 69 | 'negative': False, |
| 70 | 'params': {'root': RESOURCES_DIR, |
| 71 | 'entry': 'test.key', |
| 72 | 'msg': '', |
| 73 | 'check_for_non': False, |
| 74 | 'check_dir': False, |
| 75 | } |
| 76 | }, |
| 77 | { |
| 78 | 'negative': False, |
| 79 | 'params': {'root': RESOURCES_DIR, |
| 80 | 'entry': 'non-existing-file', |
| 81 | 'msg': '', |
| 82 | 'check_for_non': True, |
| 83 | 'check_dir': False, |
| 84 | } |
| 85 | }, |
| 86 | { |
| 87 | 'negative': True, |
| 88 | 'params': {'root': RESOURCES_DIR, |
| 89 | 'entry': 'non-existing-file', |
| 90 | 'msg': '', |
| 91 | 'check_for_non': False, |
| 92 | 'check_dir': False, |
| 93 | } |
| 94 | }, |
| 95 | { |
| 96 | 'negative': False, |
| 97 | 'params': {'root': ROOT_DIR, |
| 98 | 'entry': 'tests', |
| 99 | 'msg': '', |
| 100 | 'check_for_non': False, |
| 101 | 'check_dir': True, |
| 102 | } |
| 103 | }, |
| 104 | { |
| 105 | 'negative': False, |
| 106 | 'params': {'root': ROOT_DIR, |
| 107 | 'entry': 'non-existing-dir', |
| 108 | 'msg': '', |
| 109 | 'check_for_non': True, |
| 110 | 'check_dir': True, |
| 111 | } |
| 112 | }, |
| 113 | { |
| 114 | 'negative': True, |
| 115 | 'params': {'root': ROOT_DIR, |
| 116 | 'entry': 'non-existing-dir', |
| 117 | 'msg': '', |
| 118 | 'check_for_non': False, |
| 119 | 'check_dir': True, |
| 120 | } |
| 121 | }, |
| 122 | ] |
| 123 | |
| 124 | |
| 125 | def test_check_file_dir(): |
| 126 | for case in CHECK_FILE_CASES: |
| 127 | if case['negative']: |
| 128 | with pytest.raises(ValueError): |
| 129 | utils.check_file_dir(**case['params']) |
| 130 | else: |
| 131 | utils.check_file_dir(**case['params']) |