summaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/security/SecurityManagerTest.java
blob: 0ed871d47cec09230a37d1a94375585b8507ad02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorsoftwareproduct.security;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

public class SecurityManagerTest {
    private File certDir;
    private String cerDirPath = "/tmp/cert/";
    private SecurityManager securityManager;

    private File PrepareCertFiles(String origFilePath, String newFilePath) throws IOException, URISyntaxException {
        File origFile = new File(getClass().getResource(origFilePath).toURI());
        File newFile = new File(newFilePath);
        newFile.createNewFile();
        FileUtils.copyFile(origFile, newFile);
        return newFile;
    }

    private byte[] readAllBytes(String path) throws URISyntaxException, IOException {
        return Files.readAllBytes(Paths.get(getClass().getResource(path).toURI()));
    }

    @BeforeEach
    public void setUp() throws IOException {
        certDir = new File(cerDirPath);
        if (certDir.exists()) {
            tearDown();
        }
        certDir.mkdirs();
        securityManager = new SecurityManager(certDir.getPath());
    }

    @AfterEach
    public void tearDown() throws IOException {
        if (certDir.exists()) {
            FileUtils.deleteDirectory(certDir);
        }
        securityManager.cleanTrustedCertificates();
    }

    @Test
    public void testGetCertificates() throws IOException, SecurityManagerException, URISyntaxException {
        File newFile = PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "/root-certificate.pem");
        assertEquals(1, securityManager.getTrustedCertificates().size());
        newFile.delete();
        assertEquals(0, securityManager.getTrustedCertificates().size());
    }

    @Test
    public void testGetCertificatesNoDirectory() throws IOException, SecurityManagerException {
        certDir.delete();
        assertEquals(0, securityManager.getTrustedCertificates().size());
    }

    @Test
    public void testGetCertificatesException() throws IOException, SecurityManagerException {
        Assertions.assertThrows(SecurityManagerException.class, () -> {
            File newFile = new File(cerDirPath + "root-certificate.pem");
            newFile.createNewFile();
            assertEquals(1, securityManager.getTrustedCertificates().size());
            newFile.delete();
            assertEquals(0, securityManager.getTrustedCertificates().size());
        });
    }

    @Test
    public void testGetCertificatesUpdated() throws IOException, SecurityManagerException, URISyntaxException {
        File newFile = PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "root-certificate.pem");
        assertTrue(securityManager.getTrustedCertificates().size() == 1);
        File otherNewFile = PrepareCertFiles("/cert/package-certificate.pem", cerDirPath + "package-certificate.pem");
        assertEquals(2, securityManager.getTrustedCertificates().size());
        otherNewFile.delete();
        assertEquals(1, securityManager.getTrustedCertificates().size());
        newFile.delete();
        assertEquals(0, securityManager.getTrustedCertificates().size());
    }

    @Test
    public void verifySignedDataTestCertIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
        PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
        byte[] signature = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.cms");
        byte[] archive = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.csar");
        assertTrue(securityManager.verifySignedData(signature, null, archive));
    }

    @Test
    public void verifySignedDataTestCertNotIncludedIntoSignatureButExpected() throws IOException, URISyntaxException, SecurityManagerException {
        Assertions.assertThrows(SecurityManagerException.class, () -> {
            PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
            byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
            byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
            securityManager.verifySignedData(signature, null, archive);
        });

    }

    @Test
    public void verifySignedDataTestCertNotIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
        PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
        byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
        byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
        byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
        assertTrue(securityManager.verifySignedData(signature, cert, archive));
    }

    @Test
    public void verifySignedDataTestCertIntermediateNotIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
        PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
        PrepareCertFiles("/cert/signing-ca2.crt", cerDirPath + "signing-ca2.crt");
        byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
        byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
        byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4-no-intermediate.cert");
        assertTrue(securityManager.verifySignedData(signature, cert, archive));
    }

    @Test
    public void verifySignedDataTestCertWrongIntermediate() throws IOException, URISyntaxException, SecurityManagerException {
        Assertions.assertThrows(SecurityManagerException.class, () -> {
            PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
            PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
            byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
            byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
            byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4-no-intermediate.cert");
            securityManager.verifySignedData(signature, cert, archive);
        });

    }

    @Test
    public void verifySignedDataTestCertIncludedIntoSignatureWithWrongIntermediateInDirectory() throws IOException, URISyntaxException, SecurityManagerException {
        PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
        PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
        byte[] signature = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.cms");
        byte[] archive = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.csar");
        assertTrue(securityManager.verifySignedData(signature, null, archive));
    }

    @Test
    public void verifySignedDataTestCertWrongIntermediateInDirectory() throws IOException, URISyntaxException, SecurityManagerException {
        PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
        PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
        byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
        byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
        byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
        assertTrue(securityManager.verifySignedData(signature, cert, archive));
    }

    @Test
    public void verifySignedDataTestWrongCertificate() throws IOException, URISyntaxException, SecurityManagerException {
        Assertions.assertThrows(SecurityManagerException.class, () -> {
            PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "root-certificate.cert");
            byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
            byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
            byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
            securityManager.verifySignedData(signature, cert, archive);
        });

    }

    @Test
    public void verifySignedDataTestChangedArchive() throws IOException, URISyntaxException, SecurityManagerException {
        Assertions.assertThrows(SecurityManagerException.class, () -> {
            PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
            byte[] signature = readAllBytes("/cert/tampered-signed-package/dummyPnfv4.cms");
            byte[] archive = readAllBytes("/cert/tampered-signed-package/dummyPnfv4.csar");
            securityManager.verifySignedData(signature, null, archive);
        });

    }
}