Fix Bath config issue
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / principal / test / JU_BasicPrincipal.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
30 import java.io.IOException;
31 import java.util.Date;
32
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;
39
40 public class JU_BasicPrincipal {
41
42         @Test
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()));
48
49                 // Test sending a user without an implicit domain
50                 String name = "User";
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()));
59
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()));
67
68                 // Check that an exception is throw if no name is given in the content
69                 try {
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"));
74                 }
75         }
76
77         @Test
78         public void Constructor2Test() {
79                 String name = "User";
80                 String password = "password";
81                 BasicCred bc = mock(BasicCred.class);
82                 when(bc.getUser()).thenReturn(name);
83                 when(bc.getCred()).thenReturn(password.getBytes());
84
85                 BasicPrincipal bp = new BasicPrincipal(bc, "domain");
86                 assertThat(bp.getName(), is(name));
87                 assertThat(bp.getCred(), is(password.getBytes()));
88         }
89
90         @Test
91         public void accessorsTest() throws IOException {
92                 String name = "User";
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);
101
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(bp.getName()));
107
108                 // This test hits the abstract class BearerPrincipal
109                 assertThat(bp.getBearer(), is(bearer));
110         }
111
112
113         @Test
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");
121         }
122
123 }