e67e096fb642e1fe7b01a081c5f71a70ece55d78
[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.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;
32 import org.junit.*;
33
34 import java.io.IOException;
35 import java.util.Date;
36
37 import org.onap.aaf.cadi.BasicCred;
38 import org.onap.aaf.cadi.Symm;
39 import org.onap.aaf.cadi.principal.BasicPrincipal;
40
41 public class JU_BasicPrincipal {
42
43     @Test
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()));
49
50         // Test sending a user without an implicit domain
51         String name = "User";
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()));
60
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()));
68
69         // Check that an exception is throw if no name is given in the content
70         try {
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"));
75         }
76     }
77
78     @Test
79     public void Constructor2Test() {
80         String name = "User";
81         String password = "password";
82         BasicCred bc = mock(BasicCred.class);
83         when(bc.getUser()).thenReturn(name);
84         when(bc.getCred()).thenReturn(password.getBytes());
85
86         BasicPrincipal bp = new BasicPrincipal(bc, "domain");
87         assertThat(bp.getName(), is(name));
88         assertThat(bp.getCred(), is(password.getBytes()));
89     }
90
91     @Test
92     public void accessorsTest() throws IOException {
93         String name = "User";
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);
102
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()));
108
109         // This test hits the abstract class BearerPrincipal
110         assertThat(bp.getBearer(), is(bearer));
111     }
112
113
114     @Test
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");
122     }
123
124 }