855c26f34647064a04efbc2cb145281ad2b5d00f
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_ArtifactDir.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi.cm.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.security.KeyStore;
31 import java.security.KeyStoreException;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.cert.CertificateException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.junit.*;
38 import org.mockito.*;
39
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.cm.ArtifactDir;
42 import org.onap.aaf.cadi.util.Chmod;
43 import org.onap.aaf.misc.env.Trans;
44
45 import certman.v1_0.Artifacts.Artifact;
46 import certman.v1_0.CertInfo;
47
48 public class JU_ArtifactDir {
49
50         @Mock private Trans transMock;
51         @Mock private CertInfo certInfoMock;
52         @Mock private Artifact artiMock;
53         
54         private static final String dirName = "src/test/resources/artifacts";
55         private static final String nsName = "org.onap.test";
56         private static final String luggagePassword = "12345";  // That's the stupidest combination I've ever heard in my life
57
58         private List<String> issuers;
59
60         @Before
61         public void setup() {
62                 MockitoAnnotations.initMocks(this);
63                 
64                 issuers = new ArrayList<>();
65                 issuers.add("issuer1");
66                 issuers.add("issuer2");
67         }
68         
69         @AfterClass
70         public static void tearDownOnce() {
71                 cleanup();
72                 ArtifactDir.clear();
73         }
74
75         @Test
76         public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
77                 ArtifactDirStud artiDir = new ArtifactDirStud();
78
79                 try {
80                         artiDir.place(transMock, certInfoMock, artiMock, "machine");
81                         fail("Should've thrown an exception");
82                 } catch (CadiException e) {
83                         assertThat(e.getMessage(), is("File Artifacts require a path\nFile Artifacts require an AAF Namespace"));
84                 }
85                 
86                 when(artiMock.getDir()).thenReturn(dirName);
87                 try {
88                         artiDir.place(transMock, certInfoMock, artiMock, "machine");
89                         fail("Should've thrown an exception");
90                 } catch (CadiException e) {
91                         assertThat(e.getMessage(), is("File Artifacts require an AAF Namespace"));
92                 }
93                 
94                 when(artiMock.getNs()).thenReturn(nsName);
95                 when(certInfoMock.getCaIssuerDNs()).thenReturn(issuers);
96                 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
97                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
98                 
99                 File writableFile = new File(dirName + '/' + nsName + "writable.txt");
100                 artiDir.write(writableFile, Chmod.to755, "first data point", "second data point");
101                 try {
102                         artiDir.write(writableFile, Chmod.to755, (String[])null);
103                         fail("Should've thrown an exception");
104                 } catch(NullPointerException e) {
105                 }
106                 
107                 KeyStore ks = KeyStore.getInstance("pkcs12");
108                 try {
109                         artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
110                         fail("Should've thrown an exception");
111                 } catch(CadiException e) {
112                 }
113                 
114                 ks.load(null, null);
115                 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
116                 
117                 ArtifactDirStud artiDir2 = new ArtifactDirStud();
118                 artiDir2.place(transMock, certInfoMock, artiMock, "machine");
119
120                 // coverage
121                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
122
123                 ArtifactDir.clear();
124                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
125         
126         }
127
128         @Test(expected = CadiException.class)
129         public void throwsTest() throws CadiException {
130                 ArtifactDirStud artiDir = new ArtifactDirStud();
131                 when(artiMock.getDir()).thenReturn(dirName);
132                 when(artiMock.getNs()).thenReturn(nsName);
133                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
134         }
135
136         private class ArtifactDirStud extends ArtifactDir {
137                 @Override
138                 protected boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
139                         // This is only here so that we have a concrete class to test
140                         return false;
141                 }
142                 
143                 // Expose the protected methods
144
145                 public  void write(File f, Chmod c, String ... data) throws IOException {
146                         super.write(f, c, data);
147                 }
148                 public void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
149                         super.write(f, c, ks, pass);
150                 }
151         }
152
153         private static void cleanup() {
154                 File dir = new File(dirName);
155                 if (dir.exists()) {
156                         for (File f : dir.listFiles()) {
157                                 f.delete();
158                         }
159                         dir.delete();
160                 }
161         }
162
163 }