65daa7b6269234fed5646d4c5972fef96c03ec6f
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / client / test / JU_AbsAuthentication.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 package org.onap.aaf.cadi.client.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import org.junit.*;
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.client.AbsAuthentication;
29 import org.onap.aaf.cadi.config.SecurityInfoC;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.net.HttpURLConnection;
35
36 public class JU_AbsAuthentication {
37     
38     private final static String ID = "id";
39     private final static String PASSWORD = "password";
40     private final static String WARNING = "Your service has 1000 consecutive bad service " +
41                                             "logins to AAF. AAF Access will be disabled after 10000\n";
42     
43     private static ByteArrayOutputStream errStream;
44     
45     @Before
46     public void setup() {
47         errStream = new ByteArrayOutputStream();
48         System.setErr(new PrintStream(errStream));
49     }
50     
51     @After
52     public void tearDown() {
53         System.setErr(System.err);
54     }
55
56     @Test
57     public void test() throws IOException, InterruptedException {
58         AuthStub stub = new AuthStub(null, null, null);
59         assertThat(stub.getID(), is(nullValue()));
60         assertThat(stub.headValue(), is(""));
61         assertThat(stub.count(), is(0));
62         
63         stub.setUser(ID);
64         assertThat(stub.getID(), is(ID));
65
66         stub = new AuthStub(null, ID, PASSWORD.getBytes());
67         assertThat(stub.getID(), is(ID));
68         assertThat(stub.headValue(), is(PASSWORD));
69         assertThat(stub.count(), is(0));
70         
71         assertThat(stub.setLastResponse(200), is(0));
72         assertThat(stub.isDenied(), is(false));
73
74         for (int i = 1; i <= 10; i++) {
75             assertThat(stub.setLastResponse(401), is(i));
76             assertThat(stub.isDenied(), is(false));
77         }
78         assertThat(stub.setLastResponse(401), is(11));
79         assertThat(stub.isDenied(), is(true));
80
81         stub.setCount(999);
82         assertThat(stub.setLastResponse(401), is(1000));
83         assertThat(errStream.toString(), is(WARNING));
84         
85         // coverage...
86         stub.setLastMiss(1);
87         assertThat(stub.isDenied(), is(false));
88     }
89     
90     private class AuthStub extends AbsAuthentication<HttpURLConnection> {
91
92         public AuthStub(SecurityInfoC<HttpURLConnection> securityInfo, String user, byte[] headValue)
93                 throws IOException { super(securityInfo, user, headValue); }
94
95         @Override public void setSecurity(HttpURLConnection client) throws CadiException { }
96         @Override public void setUser(String id) { super.setUser(id); }
97         @Override public String headValue() throws IOException { return super.headValue(); }
98         
99         public void setLastMiss(long lastMiss) { this.lastMiss = lastMiss; }
100         public void setCount(int count) { this.count = count; }
101     }
102
103 }