Added oparent to sdc main
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / data / PackageArchiveTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.data;
22
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;
29
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34
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;
40
41 public class PackageArchiveTest {
42     private static final String BASE_DIR = "/vspmanager.csar/";
43
44     @Mock
45     SecurityManager manager;
46
47     @Before
48     public void setUp(){
49         initMocks(this);
50     }
51
52
53     @Test
54     public void isSignedTestCheckingWrongFile() throws IOException,
55             URISyntaxException {
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());
59     }
60
61     @Test
62     public void isSignedTestWrongPackageStructure2EmptyDirInRoot() throws IOException,
63             URISyntaxException {
64         PackageArchive packageArchive = getArchive("signing/2-empty-directories-in-root.zip");
65         assertFalse(packageArchive.isSigned());
66     }
67
68     @Test
69     public void isSignedTestWrongPackageStructure2EmptyFilesAndEmptyDirInRoot() throws IOException,
70             URISyntaxException {
71         PackageArchive packageArchive = getArchive("signing/2-empty-files-1-empty-directory-in-root.zip");
72         assertFalse(packageArchive.isSigned());
73     }
74
75     @Test
76     public void isSignedTestWrongPackageStructure2EmptyFilesAndDirWithContentInRoot() throws IOException,
77             URISyntaxException {
78         PackageArchive packageArchive = getArchive("signing/2-empty-files-1-directory-with-contents-in-root.zip");
79         assertFalse(packageArchive.isSigned());
80     }
81
82     @Test
83     public void isSignedTestCorrectStructureNoSignature() throws IOException,
84             URISyntaxException {
85         PackageArchive packageArchive = getArchive("signing/2-files-in-root.zip");
86         assertFalse(packageArchive.isSigned());
87     }
88
89     @Test
90     public void isSignedTestCorrectStructureAndSignatureExists() throws IOException,
91             URISyntaxException {
92         PackageArchive packageArchive = getArchive("signing/csar-and-cms-in-root.zip");
93         assertTrue(packageArchive.isSigned());
94     }
95
96     @Test
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());
104     }
105
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();
113     }
114
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())));
118     }
119 }