2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdcrests.vsp.rest.data;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
27 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
28 import org.powermock.reflect.Whitebox;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
35 import static junit.framework.TestCase.assertTrue;
36 import static org.junit.Assert.assertFalse;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.MockitoAnnotations.initMocks;
39 import static org.powermock.api.mockito.PowerMockito.when;
41 public class PackageArchiveTest {
42 private static final String BASE_DIR = "/vspmanager.csar/";
45 SecurityManager manager;
54 public void isSignedTestCheckingWrongFile() throws IOException,
56 PackageArchive packageArchive = getArchive("notCsar.txt");
57 assertFalse("2 or 3 files expected for signed package present or signature valid for " +
58 "empty file", packageArchive.isSigned());
62 public void isSignedTestWrongPackageStructure2EmptyDirInRoot() throws IOException,
64 PackageArchive packageArchive = getArchive("signing/2-empty-directories-in-root.zip");
65 assertFalse(packageArchive.isSigned());
69 public void isSignedTestWrongPackageStructure2EmptyFilesAndEmptyDirInRoot() throws IOException,
71 PackageArchive packageArchive = getArchive("signing/2-empty-files-1-empty-directory-in-root.zip");
72 assertFalse(packageArchive.isSigned());
76 public void isSignedTestWrongPackageStructure2EmptyFilesAndDirWithContentInRoot() throws IOException,
78 PackageArchive packageArchive = getArchive("signing/2-empty-files-1-directory-with-contents-in-root.zip");
79 assertFalse(packageArchive.isSigned());
83 public void isSignedTestCorrectStructureNoSignature() throws IOException,
85 PackageArchive packageArchive = getArchive("signing/2-files-in-root.zip");
86 assertFalse(packageArchive.isSigned());
90 public void isSignedTestCorrectStructureAndSignatureExists() throws IOException,
92 PackageArchive packageArchive = getArchive("signing/csar-and-cms-in-root.zip");
93 assertTrue(packageArchive.isSigned());
97 public void isSignatureValidTestCorrectStructureAndValidSignatureExists() throws IOException,
98 URISyntaxException, SecurityManagerException {
99 PackageArchive packageArchive = getArchive("signing/signed-package.zip");
100 Whitebox.setInternalState(packageArchive, "securityManager", manager);
101 when(manager.verifySignedData(any(), any(), any())).thenReturn(true);
102 assertTrue("Signature invalid for signed package",
103 packageArchive.isSignatureValid());
106 @Test(expected = SecurityManagerException.class)
107 public void isSignatureValidTestCorrectStructureAndNotValidSignatureExists() throws IOException,
108 URISyntaxException, SecurityManagerException {
109 PackageArchive packageArchive = getArchive("signing/signed-package-tampered-data.zip");
110 Whitebox.setInternalState(packageArchive, "securityManager", manager);
111 when(manager.verifySignedData(any(), any(), any())).thenThrow(new SecurityManagerException("error!"));
112 packageArchive.isSignatureValid();
115 private PackageArchive getArchive(String path) throws URISyntaxException, IOException {
116 return new PackageArchive(Files.readAllBytes(Paths.get(
117 PackageArchiveTest.class.getResource(BASE_DIR + path).toURI())));