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 | |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 16 | import os |
| 17 | import os.path |
| 18 | |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 19 | import pytest |
| 20 | |
| 21 | from vnfsdk_pkgtools.packager import manifest |
| 22 | |
| 23 | METADATA = '\n'.join(["metadata:", |
| 24 | "vnf_product_name: test", |
| 25 | "vnf_provider_id: test", |
| 26 | "vnf_package_version:1.0", |
| 27 | "vnf_release_data_time: 2017-09-15T15:00:10+08:00", |
| 28 | ]) |
| 29 | |
| 30 | METADATA_MISSING_KEY = '\n'.join(["metadata:", |
| 31 | "vnf_product_name: test", |
| 32 | "vnf_provider_id: test", |
| 33 | "vnf_package_version:1.0", |
| 34 | ]) |
| 35 | |
| 36 | METADATA_MISSING = "vnf_product_name: test" |
| 37 | |
| 38 | FILE_CONTENT = "needToBeHashed" |
| 39 | FILE_DIGEST = '\n'.join(['Source: digest', |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 40 | 'Algorithm: SHA-256', |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 41 | 'Hash: 20a480339aa4371099f9503511dcc5a8051ce3884846678ced5611ec64bbfc9c', |
| 42 | ]) |
| 43 | |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 44 | CMS = '\n'.join(['-----BEGIN CMS-----', |
| 45 | 'MIICmAYJKoZIhvcNAQcCoIICiTCCAoUCAQExDTALBglghkgBZQMEAgEwCwYJKoZI', |
| 46 | 'hvcNAQcBMYICYjCCAl4CAQEwUjBFMQswCQYDVQQGEwJQVDEPMA0GA1UECAwGTGlz', |
| 47 | 'Ym9hMQ8wDQYDVQQHDAZMaXNib2ExFDASBgNVBAoMC0V4YW1wbGUgT3JnAgkA6w7o', |
| 48 | '0SBbUUwwCwYJYIZIAWUDBAIBoIHkMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEw', |
| 49 | 'HAYJKoZIhvcNAQkFMQ8XDTE4MDgyNzAzMjY1MlowLwYJKoZIhvcNAQkEMSIEIFDv', |
| 50 | '62qcyvy9rbeUjjg0odflTyXt7GjP7xMyQe/k/joJMHkGCSqGSIb3DQEJDzFsMGow', |
| 51 | 'CwYJYIZIAWUDBAEqMAsGCWCGSAFlAwQBFjALBglghkgBZQMEAQIwCgYIKoZIhvcN', |
| 52 | 'AwcwDgYIKoZIhvcNAwICAgCAMA0GCCqGSIb3DQMCAgFAMAcGBSsOAwIHMA0GCCqG', |
| 53 | 'SIb3DQMCAgEoMA0GCSqGSIb3DQEBAQUABIIBAJzPsQ0tR9O7dXVJ7XywGLKrO/xG', |
| 54 | 'C9z7EMqxbjCX+bfkGh5b67mSWlHnN2Yox33YBV8cTz/NzHS8UW9x3CTNvt0wJ+5m', |
| 55 | 'Pcv+3w52XHu67b3LmMiJugpsyEIeB/qm1PzXPAqWAk+figwNtbhw994C6EzPQz+x', |
| 56 | 'eoS386Bie7kf/y/ac+xWiOdYYdC+SFhbko6sEJSCBzOIs1m3ufrsBukMxhxema5h', |
| 57 | 'pqE+DUlSFyilc9CQWnSLubkHmM4dZnU7qnNoTBqplDYpOYH3WSNN9Cv322JusAzt', |
| 58 | 'SzFEv182phI2C5pmjUnf7VG1WMKCH2WNtkYwMUCDcGvbHrh8n+kR8hL/BAs=', |
| 59 | '-----END CMS-----', |
| 60 | ]) |
| 61 | |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 62 | def test_metadata(tmpdir): |
| 63 | p = tmpdir.mkdir('csar').join('test.mf') |
| 64 | p.write(METADATA) |
| 65 | |
| 66 | m = manifest.Manifest(p.dirname, 'test.mf') |
| 67 | assert m.metadata['vnf_product_name'] == 'test' |
| 68 | assert m.metadata['vnf_provider_id'] == 'test' |
| 69 | assert m.metadata['vnf_package_version'] == '1.0' |
| 70 | assert m.metadata['vnf_release_data_time'] == '2017-09-15T15:00:10+08:00' |
| 71 | |
| 72 | |
| 73 | def test_metadata_missing_key(tmpdir): |
| 74 | p = tmpdir.mkdir('csar').join('test.mf') |
| 75 | p.write(METADATA_MISSING_KEY) |
| 76 | |
| 77 | with pytest.raises(manifest.ManifestException) as excinfo: |
| 78 | manifest.Manifest(p.dirname, 'test.mf') |
| 79 | excinfo.match(r"Missing metadata keys:") |
| 80 | |
| 81 | |
| 82 | def test_missing_metadata(tmpdir): |
| 83 | p = tmpdir.mkdir('csar').join('test.mf') |
| 84 | p.write(METADATA_MISSING) |
| 85 | |
| 86 | with pytest.raises(manifest.ManifestException) as excinfo: |
| 87 | manifest.Manifest(p.dirname, 'test.mf') |
| 88 | excinfo.match(r"Unknown key in line") |
| 89 | |
| 90 | def test_digest(tmpdir): |
| 91 | root = tmpdir.mkdir('csar') |
| 92 | mf = root.join('test.mf') |
| 93 | digest = root.join('digest') |
| 94 | mf.write(METADATA + '\n\n' + FILE_DIGEST) |
| 95 | digest.write(FILE_CONTENT) |
| 96 | |
| 97 | m = manifest.Manifest(mf.dirname, 'test.mf') |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 98 | assert m.digests['digest'][0] == "SHA-256" |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 99 | assert m.digests['digest'][1] == "20a480339aa4371099f9503511dcc5a8051ce3884846678ced5611ec64bbfc9c" |
| 100 | |
| 101 | def test_add_file(tmpdir): |
| 102 | root = tmpdir.mkdir('csar') |
| 103 | mf = root.join('test.mf') |
| 104 | digest = root.join('digest') |
| 105 | mf.write(METADATA) |
| 106 | digest.write(FILE_CONTENT) |
| 107 | |
| 108 | m = manifest.Manifest(mf.dirname, 'test.mf') |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 109 | m.add_file('digest', 'SHA-256') |
| 110 | assert m.digests['digest'][0] == "SHA-256" |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 111 | assert m.digests['digest'][1] == "20a480339aa4371099f9503511dcc5a8051ce3884846678ced5611ec64bbfc9c" |
| 112 | |
| 113 | def test_update_to_file(tmpdir): |
| 114 | root = tmpdir.mkdir('csar') |
| 115 | mf = root.join('test.mf') |
| 116 | digest = root.join('digest') |
| 117 | mf.write(METADATA + '\n\n' + FILE_DIGEST) |
| 118 | digest.write(FILE_CONTENT) |
| 119 | digest2 = root.join('digest2') |
| 120 | digest2.write(FILE_CONTENT) |
| 121 | |
| 122 | m1 = manifest.Manifest(mf.dirname, 'test.mf') |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 123 | m1.add_file('digest2', 'SHA-256') |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 124 | m1.signature = CMS |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 125 | m1.update_to_file() |
| 126 | m2 = manifest.Manifest(mf.dirname, 'test.mf') |
| 127 | assert m1.metadata['vnf_provider_id'] == m2.metadata['vnf_provider_id'] |
| 128 | assert m1.digests['digest'] == m2.digests['digest2'] |
Lianhao Lu | 1ee29e0 | 2019-11-01 16:01:34 +0800 | [diff] [blame] | 129 | assert len(list(m2.digests.keys())) == 2 |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 130 | assert m2.signature == CMS |
Lianhao Lu | 432bca4 | 2018-03-24 22:36:08 +0800 | [diff] [blame] | 131 | |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 132 | def test_signature(tmpdir): |
| 133 | p = tmpdir.mkdir('csar').join('test.mf') |
| 134 | p.write(METADATA + "\n\n" + CMS) |
| 135 | m = manifest.Manifest(p.dirname, 'test.mf') |
| 136 | assert m.signature == CMS |
| 137 | |
| 138 | def test_illegal_signature(tmpdir): |
| 139 | p = tmpdir.mkdir('csar').join('test.mf') |
| 140 | p.write(METADATA + "\n\n" + CMS[:-17]) |
| 141 | with pytest.raises(manifest.ManifestException) as excinfo: |
| 142 | manifest.Manifest(p.dirname, 'test.mf') |
| 143 | excinfo.match(r"Can NOT find end of sigature block") |
| 144 | |
| 145 | def test_signature_strip(tmpdir): |
| 146 | p = tmpdir.mkdir('csar').join('test.mf') |
| 147 | p.write(METADATA + "\n\n" + CMS) |
| 148 | m1 = manifest.Manifest(p.dirname, 'test.mf') |
| 149 | newfile = m1.save_to_temp_without_cms() |
| 150 | m2 = manifest.Manifest(os.path.dirname(newfile), |
| 151 | os.path.basename(newfile)) |
| 152 | assert m1.metadata == m2.metadata |
| 153 | assert m2.signature is None |
| 154 | os.unlink(newfile) |