Remove unnecessary synchronization locks in service locator
[aaa.git] / aaa-authn-basic / src / test / java / org / opendaylight / aaa / basic / HttpBasicAuthTest.java
1 /*
2  * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.basic;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import java.io.UnsupportedEncodingException;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.aaa.ClaimBuilder;
25 import org.opendaylight.aaa.PasswordCredentialBuilder;
26 import org.opendaylight.aaa.api.AuthenticationException;
27 import org.opendaylight.aaa.api.Claim;
28 import org.opendaylight.aaa.api.CredentialAuth;
29
30 import com.sun.jersey.core.util.Base64;
31
32 public class HttpBasicAuthTest {
33     private static final String USERNAME = "admin";
34     private static final String PASSWORD = "admin";
35     private static final String DOMAIN = "sdn";
36     private HttpBasicAuth auth;
37
38     @SuppressWarnings("unchecked")
39     @Before
40     public void setup() {
41         auth = new HttpBasicAuth();
42         auth.credentialAuth = mock(CredentialAuth.class);
43         when(
44                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
45                         .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build())).thenReturn(
46                 new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123").build());
47         when(
48                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
49                         .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build())).thenThrow(new AuthenticationException("barf"));
50     }
51
52     @Test
53     public void testValidateOk() throws UnsupportedEncodingException {
54         String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
55         Map<String, List<String>> headers = new HashMap<>();
56         headers.put(
57                 "Authorization",
58                 Arrays.asList("Basic "
59                         + new String(Base64.encode(data.getBytes("utf-8")))));
60         Claim claim = auth.validate(headers);
61         assertNotNull(claim);
62         assertEquals(USERNAME, claim.user());
63         assertEquals("admin", claim.roles().iterator().next());
64     }
65
66     @Test(expected = AuthenticationException.class)
67     public void testValidateBadPassword() throws UnsupportedEncodingException {
68         String data = USERNAME + ":bozo:" + DOMAIN;
69         Map<String, List<String>> headers = new HashMap<>();
70         headers.put(
71                 "Authorization",
72                 Arrays.asList("Basic "
73                         + new String(Base64.encode(data.getBytes("utf-8")))));
74         auth.validate(headers);
75     }
76
77     @Test(expected = AuthenticationException.class)
78     public void testValidateBadPasswordNoDOMAIN() throws UnsupportedEncodingException {
79         String data = USERNAME + ":bozo";
80         Map<String, List<String>> headers = new HashMap<>();
81         headers.put(
82                 "Authorization",
83                 Arrays.asList("Basic "
84                         + new String(Base64.encode(data.getBytes("utf-8")))));
85         auth.validate(headers);
86     }
87
88     @Test(expected = AuthenticationException.class)
89     public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
90         // just provide the username
91         String data = USERNAME;
92         Map<String, List<String>> headers = new HashMap<>();
93         headers.put(
94                 "Authorization",
95                 Arrays.asList("Basic "
96                         + new String(Base64.encode(data.getBytes("utf-8")))));
97         auth.validate(headers);
98     }
99
100     @Test(expected = AuthenticationException.class)
101     public void testBadHeaderFormat() throws UnsupportedEncodingException {
102         // provide username:
103         String data = USERNAME + "$" + PASSWORD;
104         Map<String, List<String>> headers = new HashMap<>();
105         headers.put(
106                 "Authorization",
107                 Arrays.asList("Basic "
108                         + new String(Base64.encode(data.getBytes("utf-8")))));
109         auth.validate(headers);
110     }
111 }