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.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.hamcrest.CoreMatchers.nullValue;
30 import static org.mockito.Mockito.when;
31 import static org.mockito.Mockito.mock;
34 import java.io.IOException;
35 import java.util.Date;
37 import org.onap.aaf.cadi.BasicCred;
38 import org.onap.aaf.cadi.Symm;
39 import org.onap.aaf.cadi.principal.BasicPrincipal;
41 public class JU_BasicPrincipal {
44 public void Constructor1Test() throws Exception {
45 // Test that everything works when the content doesn't contain "Basic"
46 BasicPrincipal bp = new BasicPrincipal("content", "domain");
47 assertThat(bp.getName(), is("content"));
48 assertThat(bp.getCred(), is(nullValue()));
50 // Test sending a user without an implicit domain
52 String password = "password";
53 String content = name + ":" + password;
54 String domain = "exampledomain.com";
55 String encrypted = new String(Symm.base64.encode(content.getBytes()));
56 bp = new BasicPrincipal("Basic " + encrypted, domain);
57 assertThat(bp.getShortName(), is(name));
58 assertThat(bp.getName(), is(name + "@" + domain));
59 assertThat(bp.getCred(), is(password.getBytes()));
61 // Test sending a user with an implicit domain
62 String longName = name + "@" + domain + ":" + password;
63 encrypted = new String(Symm.base64.encode(longName.getBytes()));
64 bp = new BasicPrincipal("Basic " + encrypted, domain);
65 assertThat(bp.getShortName(), is(name));
66 assertThat(bp.getName(), is(name + "@" + domain));
67 assertThat(bp.getCred(), is(password.getBytes()));
69 // Check that an exception is throw if no name is given in the content
71 bp = new BasicPrincipal("Basic " + new String(Symm.base64.encode("no name".getBytes())), "");
72 fail("Should have thrown an exception");
73 } catch (IOException e) {
74 assertThat(e.getMessage(), is("Invalid Coding"));
79 public void Constructor2Test() {
81 String password = "password";
82 BasicCred bc = mock(BasicCred.class);
83 when(bc.getUser()).thenReturn(name);
84 when(bc.getCred()).thenReturn(password.getBytes());
86 BasicPrincipal bp = new BasicPrincipal(bc, "domain");
87 assertThat(bp.getName(), is(name));
88 assertThat(bp.getCred(), is(password.getBytes()));
92 public void accessorsTest() throws IOException {
94 String password = "password";
95 String content = name + ":" + password;
96 String domain = "exampledomain.com";
97 String encrypted = new String(Symm.base64.encode(content.getBytes()));
98 String bearer = "bearer";
99 long created = System.currentTimeMillis();
100 BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, domain);
101 bp.setBearer(bearer);
103 String expected = "Basic Authorization for " + name + "@" + domain + " evaluated on " + new Date(bp.created()).toString();
104 assertTrue(Math.abs(bp.created() - created) < 10);
105 assertThat(bp.toString(), is(expected));
106 assertThat(bp.tag(), is("BAth"));
107 assertThat(bp.personalName(), is(bp.getName()));
109 // This test hits the abstract class BearerPrincipal
110 assertThat(bp.getBearer(), is(bearer));
115 public void coverageTest() throws IOException {
116 String name = "User";
117 String password = "password:with:colons";
118 String content = name + ":" + password;
119 String encrypted = new String(Symm.base64.encode(content.getBytes()));
120 @SuppressWarnings("unused")
121 BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, "domain");