2 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.basic;
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;
16 import java.io.UnsupportedEncodingException;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.List;
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;
30 import com.sun.jersey.core.util.Base64;
32 public class HttpBasicAuthTest {
33 private static final String USERNAME = "admin";
34 private static final String PASSWORD = "odl";
35 private HttpBasicAuth auth;
37 @SuppressWarnings("unchecked")
40 auth = new HttpBasicAuth();
41 auth.ca = mock(CredentialAuth.class);
43 auth.ca.authenticate(new PasswordCredentialBuilder()
44 .setUserName(USERNAME).setPassword(PASSWORD).build(),
46 new ClaimBuilder().setUser("admin").addRole("admin").build());
48 auth.ca.authenticate(new PasswordCredentialBuilder()
49 .setUserName(USERNAME).setPassword("bozo").build(),
50 null)).thenThrow(new AuthenticationException("barf"));
54 public void testValidateOk() throws UnsupportedEncodingException {
55 String data = USERNAME + ":" + PASSWORD;
56 Map<String, List<String>> headers = new HashMap<>();
59 Arrays.asList("Basic "
60 + new String(Base64.encode(data.getBytes("utf-8")))));
61 Claim claim = auth.validate(headers);
63 assertEquals(USERNAME, claim.user());
64 assertEquals("admin", claim.roles().iterator().next());
67 @Test(expected = AuthenticationException.class)
68 public void testValidateBadPassword() throws UnsupportedEncodingException {
69 String data = USERNAME + ":bozo";
70 Map<String, List<String>> headers = new HashMap<>();
73 Arrays.asList("Basic "
74 + new String(Base64.encode(data.getBytes("utf-8")))));
75 auth.validate(headers);