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