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.*;
30 import java.io.IOException;
31 import java.util.Date;
33 import org.onap.aaf.cadi.BasicCred;
34 import org.onap.aaf.cadi.Symm;
35 import org.onap.aaf.cadi.principal.BasicPrincipal;
36 import org.onap.aaf.cadi.principal.StringTagLookup;
37 import org.onap.aaf.cadi.principal.TaggedPrincipal;
38 import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup;
40 public class JU_BasicPrincipal {
43 public void Constructor1Test() throws Exception {
44 // Test that everything works when the content doesn't contain "Basic"
45 BasicPrincipal bp = new BasicPrincipal("content", "domain");
46 assertThat(bp.getName(), is("content"));
47 assertThat(bp.getCred(), is(nullValue()));
49 // Test sending a user without an implicit domain
51 String password = "password";
52 String content = name + ":" + password;
53 String domain = "exampledomain.com";
54 String encrypted = new String(Symm.base64.encode(content.getBytes()));
55 bp = new BasicPrincipal("Basic " + encrypted, domain);
56 assertThat(bp.getShortName(), is(name));
57 assertThat(bp.getName(), is(name + "@" + domain));
58 assertThat(bp.getCred(), is(password.getBytes()));
60 // Test sending a user with an implicit domain
61 String longName = name + "@" + domain + ":" + password;
62 encrypted = new String(Symm.base64.encode(longName.getBytes()));
63 bp = new BasicPrincipal("Basic " + encrypted, domain);
64 assertThat(bp.getShortName(), is(name));
65 assertThat(bp.getName(), is(name + "@" + domain));
66 assertThat(bp.getCred(), is(password.getBytes()));
68 // Check that an exception is throw if no name is given in the content
70 bp = new BasicPrincipal("Basic " + new String(Symm.base64.encode("no name".getBytes())), "");
71 fail("Should have thrown an exception");
72 } catch (IOException e) {
73 assertThat(e.getMessage(), is("Invalid Coding"));
78 public void Constructor2Test() {
80 String password = "password";
81 BasicCred bc = mock(BasicCred.class);
82 when(bc.getUser()).thenReturn(name);
83 when(bc.getCred()).thenReturn(password.getBytes());
85 BasicPrincipal bp = new BasicPrincipal(bc, "domain");
86 assertThat(bp.getName(), is(name));
87 assertThat(bp.getCred(), is(password.getBytes()));
91 public void accessorsTest() throws IOException {
93 String password = "password";
94 String content = name + ":" + password;
95 String domain = "exampledomain.com";
96 String encrypted = new String(Symm.base64.encode(content.getBytes()));
97 String bearer = "bearer";
98 long created = System.currentTimeMillis();
99 BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, domain);
100 bp.setBearer(bearer);
102 String expected = "Basic Authorization for " + name + "@" + domain + " evaluated on " + new Date(bp.created()).toString();
103 assertTrue(Math.abs(bp.created() - created) < 10);
104 assertThat(bp.toString(), is(expected));
105 assertThat(bp.tag(), is("BAth"));
106 assertThat(bp.personalName(), is(nullValue()));
108 // This test hits the abstract class BearerPrincipal
109 assertThat(bp.getBearer(), is(bearer));
114 public void coverageTest() throws IOException {
115 String name = "User";
116 String password = "password:with:colons";
117 String content = name + ":" + password;
118 String encrypted = new String(Symm.base64.encode(content.getBytes()));
119 @SuppressWarnings("unused")
120 BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, "domain");