1 package org.openecomp.sdcrests.vsp.rest.data;
3 import org.junit.Before;
5 import org.mockito.Mock;
6 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
7 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
8 import org.powermock.reflect.Whitebox;
10 import java.io.IOException;
11 import java.net.URISyntaxException;
12 import java.nio.file.Files;
13 import java.nio.file.Paths;
15 import static junit.framework.TestCase.assertTrue;
16 import static org.junit.Assert.assertFalse;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.MockitoAnnotations.initMocks;
19 import static org.powermock.api.mockito.PowerMockito.when;
21 public class PackageArchiveTest {
22 private static final String BASE_DIR = "/vspmanager.csar/";
25 SecurityManager manager;
34 public void isSignedTestCheckingWrongFile() throws IOException,
36 PackageArchive packageArchive = getArchive("notCsar.txt");
37 assertFalse("2 or 3 files expected for signed package present or signature valid for " +
38 "empty file", packageArchive.isSigned());
42 public void isSignedTestWrongPackageStructure2EmptyDirInRoot() throws IOException,
44 PackageArchive packageArchive = getArchive("signing/2-empty-directories-in-root.zip");
45 assertFalse(packageArchive.isSigned());
49 public void isSignedTestWrongPackageStructure2EmptyFilesAndEmptyDirInRoot() throws IOException,
51 PackageArchive packageArchive = getArchive("signing/2-empty-files-1-empty-directory-in-root.zip");
52 assertFalse(packageArchive.isSigned());
56 public void isSignedTestWrongPackageStructure2EmptyFilesAndDirWithContentInRoot() throws IOException,
58 PackageArchive packageArchive = getArchive("signing/2-empty-files-1-directory-with-contents-in-root.zip");
59 assertFalse(packageArchive.isSigned());
63 public void isSignedTestCorrectStructureNoSignature() throws IOException,
65 PackageArchive packageArchive = getArchive("signing/2-files-in-root.zip");
66 assertFalse(packageArchive.isSigned());
70 public void isSignedTestCorrectStructureAndSignatureExists() throws IOException,
72 PackageArchive packageArchive = getArchive("signing/csar-and-cms-in-root.zip");
73 assertTrue(packageArchive.isSigned());
77 public void isSignatureValidTestCorrectStructureAndValidSignatureExists() throws IOException,
78 URISyntaxException, SecurityManagerException {
79 PackageArchive packageArchive = getArchive("signing/signed-package.zip");
80 Whitebox.setInternalState(packageArchive, "securityManager", manager);
81 when(manager.verifySignedData(any(), any(), any())).thenReturn(true);
82 assertTrue("Signature invalid for signed package",
83 packageArchive.isSignatureValid());
86 @Test(expected = SecurityManagerException.class)
87 public void isSignatureValidTestCorrectStructureAndNotValidSignatureExists() throws IOException,
88 URISyntaxException, SecurityManagerException {
89 PackageArchive packageArchive = getArchive("signing/signed-package-tampered-data.zip");
90 Whitebox.setInternalState(packageArchive, "securityManager", manager);
91 when(manager.verifySignedData(any(), any(), any())).thenThrow(new SecurityManagerException("error!"));
92 packageArchive.isSignatureValid();
95 private PackageArchive getArchive(String path) throws URISyntaxException, IOException {
96 return new PackageArchive(Files.readAllBytes(Paths.get(
97 PackageArchiveTest.class.getResource(BASE_DIR + path).toURI())));