09331c523ec47fc09be09a40cdd303b022a502e8
[moon.git] /
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. 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.shiro.filters;
10
11 import static org.junit.Assert.*;
12
13 import org.apache.shiro.authc.AuthenticationToken;
14 import org.apache.shiro.authc.UsernamePasswordToken;
15 import org.junit.Test;
16 import org.opendaylight.aaa.shiro.filters.AuthenticationTokenUtils;
17
18 /**
19  * Tests authentication token output utilities.
20  *
21  * @author Ryan Goulding (ryandgoulding@gmail.com)
22  */
23 public class AuthenticationTokenUtilsTest {
24
25     /**
26      * A sample non-UsernamePasswordToken implementation for testing.
27      */
28     private final class NotUsernamePasswordToken implements AuthenticationToken {
29
30         @Override
31         public Object getPrincipal() {
32             return null;
33         }
34
35         @Override
36         public Object getCredentials() {
37             return null;
38         }
39     }
40
41     @Test
42     public void testIsUsernamePasswordToken() throws Exception {
43         // null test
44         final AuthenticationToken nullUsernamePasswordToken = null;
45         assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(nullUsernamePasswordToken));
46
47         // alternate implementation of AuthenticationToken
48         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
49         assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(notUsernamePasswordToken));
50
51         // positive test case
52         final AuthenticationToken positiveUsernamePasswordToken = new UsernamePasswordToken();
53         assertTrue(AuthenticationTokenUtils.isUsernamePasswordToken(positiveUsernamePasswordToken));
54
55     }
56
57     @Test
58     public void testExtractUsername() throws Exception {
59         // null test
60         final AuthenticationToken nullAuthenticationToken = null;
61         assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
62                 AuthenticationTokenUtils.extractUsername(nullAuthenticationToken));
63
64         // non-UsernamePasswordToken test
65         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
66         assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
67                 AuthenticationTokenUtils.extractUsername(notUsernamePasswordToken));
68
69         // null username test
70         final UsernamePasswordToken nullUsername = new UsernamePasswordToken();
71         nullUsername.setUsername(null);
72         assertEquals(AuthenticationTokenUtils.DEFAULT_USERNAME,
73                 AuthenticationTokenUtils.extractUsername(nullUsername));
74
75         // positive test
76         final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken();
77         final String testUsername = "testUser1";
78         positiveUsernamePasswordToken.setUsername(testUsername);
79         assertEquals(testUsername, AuthenticationTokenUtils.extractUsername(positiveUsernamePasswordToken));
80     }
81
82     @Test
83     public void testExtractHostname() throws Exception {
84         // null test
85         final AuthenticationToken nullAuthenticationToken = null;
86         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
87                 AuthenticationTokenUtils.extractHostname(nullAuthenticationToken));
88
89         // non-UsernamePasswordToken test
90         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
91         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
92                 AuthenticationTokenUtils.extractHostname(notUsernamePasswordToken));
93
94         // null hostname test
95         final UsernamePasswordToken nullHostname = new UsernamePasswordToken();
96         nullHostname.setHost(null);
97         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
98                 AuthenticationTokenUtils.extractHostname(nullHostname));
99
100         // positive test
101         final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken();
102         final String testUsername = "testHostname1";
103         positiveUsernamePasswordToken.setHost(testUsername);
104         assertEquals(testUsername, AuthenticationTokenUtils.extractHostname(positiveUsernamePasswordToken));
105     }
106
107     @Test
108     public void testGenerateUnsuccessfulAuthenticationMessage() throws Exception {
109         final UsernamePasswordToken unsuccessfulToken = new UsernamePasswordToken();
110         unsuccessfulToken.setUsername("unsuccessfulUser1");
111         unsuccessfulToken.setHost("unsuccessfulHost1");
112         assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1",
113                 AuthenticationTokenUtils.generateUnsuccessfulAuthenticationMessage(unsuccessfulToken));
114     }
115
116     @Test
117     public void testGenerateSuccessfulAuthenticationMessage() throws Exception {
118         final UsernamePasswordToken successfulToken = new UsernamePasswordToken();
119         successfulToken.setUsername("successfulUser1");
120         successfulToken.setHost("successfulHost1");
121         assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1",
122                 AuthenticationTokenUtils.generateSuccessfulAuthenticationMessage(successfulToken));
123     }
124 }