aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/security/SecurityManagerTest.java
blob: c6930157911060db1467b13acb0e7ce228639bd4 (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
package org.openecomp.sdc.vendorsoftwareproduct.security;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.io.IOException;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.ArgumentMatchers.eq;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SecurityManager.class)
public class SecurityManagerTest {
    File certDir;

    @Before
    public void setUp(){
        certDir = new File("/tmp/cert");
        certDir.mkdirs();
        PowerMockito.mockStatic(System.class);
        PowerMockito.when(System.getenv(eq("SDC_CERT_DIR"))).thenReturn(certDir.getPath());
    }

    @After
    public void tearDown(){
        certDir.delete();
    }

    @Test
    public void testGetCertificates() throws IOException {
        File origFile = new File("src/test/resources/cert/root-certificate.pem");
        File newFile = new File("/tmp/cert/root-certificate.pem");
        newFile.createNewFile();
        FileUtils.copyFile(origFile, newFile);
        SecurityManager securityManager = new SecurityManager();
        assertEquals(1, securityManager.getCertificates().size());
        newFile.delete();
        assertEquals(0, securityManager.getCertificates().size());
    }

    @Test
    public void testGetCertificatesNoDirectory() throws IOException {
        certDir.delete();
        SecurityManager securityManager = new SecurityManager();
        assertEquals(0, securityManager.getCertificates().size());
    }

    @Test(expected = SecurityManagerException.class)
    public void testGetCertificatesException() throws IOException {
        File newFile = new File("/tmp/cert/root-certificate.pem");
        newFile.createNewFile();
        SecurityManager securityManager = new SecurityManager();
        assertEquals(1, securityManager.getCertificates().size());
        newFile.delete();
        assertEquals(0, securityManager.getCertificates().size());
    }

    @Test
    public void testGetCertificatesUpdated() throws IOException {
        File origFile = new File("src/test/resources/cert/root-certificate.pem");
        File newFile = new File("/tmp/cert/root-certificate.pem");
        newFile.createNewFile();
        FileUtils.copyFile(origFile, newFile);
        SecurityManager securityManager = new SecurityManager();
        assertTrue(securityManager.getCertificates().size() == 1);
        File otherOrigFile = new File("src/test/resources/cert/package-certificate.pem");
        File otherNewFile = new File("/tmp/cert/package-certificate.pem");
        newFile.createNewFile();
        FileUtils.copyFile(otherOrigFile, otherNewFile);
        assertEquals(2, securityManager.getCertificates().size());
        otherNewFile.delete();
        assertEquals(1, securityManager.getCertificates().size());
        newFile.delete();
        assertEquals(0, securityManager.getCertificates().size());
    }
}