Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / principal / test / JU_X509Principal.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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
23 package org.onap.aaf.cadi.principal.test;
24
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
28 import org.junit.*;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31
32 import java.io.IOException;
33 import java.security.Principal;
34 import java.security.cert.CertificateEncodingException;
35 import java.security.cert.X509Certificate;
36
37 import org.onap.aaf.cadi.principal.X509Principal;
38
39 public class JU_X509Principal {
40
41     private final String name = "x509 name";
42     private final byte[] cred = "super duper secret password".getBytes();
43
44     @Mock
45     X509Certificate cert;
46
47     @Mock
48     Principal subject;
49
50     @Before
51     public void setup() throws CertificateEncodingException {
52         MockitoAnnotations.initMocks(this);
53         when(cert.getEncoded()).thenReturn(cred);
54     }
55
56     @Test
57     public void constructor1Test() throws IOException {
58         X509Principal x509 = new X509Principal(name, cert);
59         // Call twice to hit both branches
60         assertThat(x509.getAsHeader(), is("X509 " + cred));
61         assertThat(x509.getAsHeader(), is("X509 " + cred));
62         assertThat(x509.toString(), is("X509 Authentication for " + name));
63         assertTrue(x509.getCred().equals(cred));
64         assertThat(x509.getName(), is(name));
65         assertThat(x509.tag(), is("x509"));
66     }
67
68     @Test
69     public void constructor2Test() throws IOException {
70         X509Principal x509 = new X509Principal(name, cert, cred,null);
71         // Call twice to hit both branches
72         assertThat(x509.getAsHeader(), is("X509 " + cred));
73         assertThat(x509.toString(), is("X509 Authentication for " + name));
74         assertTrue(x509.getCred().equals(cred));
75         assertThat(x509.getName(), is(name));
76         assertThat(x509.tag(), is("x509"));
77     }
78
79     @Test
80     public void constructor3Test() throws IOException {
81         final String longName = "name@domain";
82         when(subject.getName()).thenReturn("OU=" + longName + ",extra");
83         when(cert.getSubjectDN()).thenReturn(subject);
84         X509Principal x509 = new X509Principal(cert, cred,null);
85         // Call twice to hit both branches
86         assertThat(x509.getAsHeader(), is("X509 " + cred));
87         assertThat(x509.toString(), is("X509 Authentication for " + longName));
88         assertTrue(x509.getCred().equals(cred));
89         assertThat(x509.getName(), is(longName));
90
91         when(subject.getName()).thenReturn(longName + ",extra");
92         when(cert.getSubjectDN()).thenReturn(subject);
93         try {
94             x509 = new X509Principal(cert, cred, null);
95             fail("Should have thrown an Exception");
96         } catch (IOException e) {
97             assertThat(e.getMessage(), is("X509 does not have Identity as CN"));
98         }
99
100         when(subject.getName()).thenReturn("OU=" + longName);
101         when(cert.getSubjectDN()).thenReturn(subject);
102         try {
103             x509 = new X509Principal(cert, cred, null);
104             fail("Should have thrown an Exception");
105         } catch (IOException e) {
106             assertThat(e.getMessage(), is("X509 does not have Identity as CN"));
107         }
108
109         when(subject.getName()).thenReturn("OU=" + name + ",exta");
110         when(cert.getSubjectDN()).thenReturn(subject);
111         try {
112             x509 = new X509Principal(cert, cred, null);
113             fail("Should have thrown an Exception");
114         } catch (IOException e) {
115             assertThat(e.getMessage(), is("X509 does not have Identity as CN"));
116         }
117
118     }
119
120     @Test
121     public void throwsTest() throws CertificateEncodingException {
122         when(cert.getEncoded()).thenThrow(new CertificateEncodingException());
123         X509Principal x509 = new X509Principal(name, cert);
124         assertThat(x509.getCred(), is(nullValue()));
125         try {
126             x509.getAsHeader();
127             fail("Should have thrown an Exception");
128         } catch (IOException e) {
129         }
130     }
131
132     @Test
133     public void getCredTest() {
134         X509Principal x509 = new X509Principal(name, cert);
135         // Call twice to hit both branches
136         assertTrue(x509.getCred().equals(cred));
137         assertTrue(x509.getCred().equals(cred));
138     }
139
140 }