1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.principal.test;
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
32 import java.io.IOException;
33 import java.security.Principal;
34 import java.security.cert.CertificateEncodingException;
35 import java.security.cert.X509Certificate;
37 import org.onap.aaf.cadi.principal.X509Principal;
39 public class JU_X509Principal {
41 private final String name = "x509 name";
42 private final byte[] cred = "super duper secret password".getBytes();
51 public void setup() throws CertificateEncodingException {
52 MockitoAnnotations.initMocks(this);
53 when(cert.getEncoded()).thenReturn(cred);
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"));
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"));
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));
91 when(subject.getName()).thenReturn(longName + ",extra");
92 when(cert.getSubjectDN()).thenReturn(subject);
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"));
100 when(subject.getName()).thenReturn("OU=" + longName);
101 when(cert.getSubjectDN()).thenReturn(subject);
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"));
109 when(subject.getName()).thenReturn("OU=" + name + ",exta");
110 when(cert.getSubjectDN()).thenReturn(subject);
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"));
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()));
127 fail("Should have thrown an Exception");
128 } catch (IOException e) {
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));