6458a65d1754aef10cc4ceaadc07e4b05b074de5
[sdc.git] /
1 package org.openecomp.sdcrests.vsp.rest.data;
2
3 import org.junit.Before;
4 import org.junit.Test;
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;
9
10 import java.io.IOException;
11 import java.net.URISyntaxException;
12 import java.nio.file.Files;
13 import java.nio.file.Paths;
14
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;
20
21 public class PackageArchiveTest {
22     private static final String BASE_DIR = "/vspmanager.csar/";
23
24     @Mock
25     SecurityManager manager;
26
27     @Before
28     public void setUp(){
29         initMocks(this);
30     }
31
32
33     @Test
34     public void isSignedTestCheckingWrongFile() throws IOException,
35             URISyntaxException {
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());
39     }
40
41     @Test
42     public void isSignedTestWrongPackageStructure2EmptyDirInRoot() throws IOException,
43             URISyntaxException {
44         PackageArchive packageArchive = getArchive("signing/2-empty-directories-in-root.zip");
45         assertFalse(packageArchive.isSigned());
46     }
47
48     @Test
49     public void isSignedTestWrongPackageStructure2EmptyFilesAndEmptyDirInRoot() throws IOException,
50             URISyntaxException {
51         PackageArchive packageArchive = getArchive("signing/2-empty-files-1-empty-directory-in-root.zip");
52         assertFalse(packageArchive.isSigned());
53     }
54
55     @Test
56     public void isSignedTestWrongPackageStructure2EmptyFilesAndDirWithContentInRoot() throws IOException,
57             URISyntaxException {
58         PackageArchive packageArchive = getArchive("signing/2-empty-files-1-directory-with-contents-in-root.zip");
59         assertFalse(packageArchive.isSigned());
60     }
61
62     @Test
63     public void isSignedTestCorrectStructureNoSignature() throws IOException,
64             URISyntaxException {
65         PackageArchive packageArchive = getArchive("signing/2-files-in-root.zip");
66         assertFalse(packageArchive.isSigned());
67     }
68
69     @Test
70     public void isSignedTestCorrectStructureAndSignatureExists() throws IOException,
71             URISyntaxException {
72         PackageArchive packageArchive = getArchive("signing/csar-and-cms-in-root.zip");
73         assertTrue(packageArchive.isSigned());
74     }
75
76     @Test
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());
84     }
85
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();
93     }
94
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())));
98     }
99 }