Merge "Simplify method isMutualExclusive in Subnet. Remove redundant 'if' statements."
[controller.git] / opendaylight / usermanager / api / src / test / java / org / opendaylight / controller / usermanager / AuthorizationUserConfigTest.java
1 /*
2  * Copyright (c) 2013 Cisco 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 package org.opendaylight.controller.usermanager;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
18 import org.opendaylight.controller.sal.authorization.UserLevel;
19 /*
20  * This test case includes tests for UserConfig and the extending class AuthorizationConfig
21  */
22 public class AuthorizationUserConfigTest {
23
24     @Test
25     public void authorizationConfigTest() {
26         AuthorizationConfig authConfig;
27         List<String> roles = new ArrayList<String>();
28
29         // test isValid
30         roles.add(UserLevel.SYSTEMADMIN.toString());
31         authConfig = new AuthorizationConfig(null, roles);
32         assertFalse(authConfig.validate().isSuccess());
33         authConfig = new AuthorizationConfig("admin", new ArrayList<String>());
34         assertFalse(authConfig.validate().isSuccess());
35         authConfig = new AuthorizationConfig("admin", roles);
36         assertTrue(authConfig.validate().isSuccess());
37     }
38
39     @Test
40     public void userConfigTest() {
41         UserConfig userConfig;
42         List<String> roles = new ArrayList<String>();
43
44         roles.add(UserLevel.SYSTEMADMIN.toString());
45         userConfig = new UserConfig(null, "cisco", roles);
46         assertFalse(userConfig.validate().isSuccess());
47
48         roles.clear();
49         roles.add("cisco");
50         userConfig = new UserConfig("uname", "", roles);
51         assertFalse(userConfig.validate().isSuccess());
52
53         roles.clear();
54         roles.add(UserLevel.NETWORKOPERATOR.toString());
55         userConfig = new UserConfig("uname", "ciscocisco", roles);
56         assertTrue(userConfig.validate().isSuccess());
57
58         // currentPassword mismatch
59         assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
60
61         // Role change only
62         roles.clear();
63         roles.add(UserLevel.NETWORKADMIN.toString());
64         assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
65
66         // Role change and same new password
67         roles.clear();
68         roles.add(UserLevel.NETWORKOPERATOR.toString());
69         assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
70                 .isSuccess());
71
72         // New Password = null, No change in password
73         assertTrue(userConfig.authenticate("ciscocisco").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
74
75         // Password changed successfully, no change in user role
76         assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
77                 .isSuccess());
78         assertTrue(userConfig.authenticate("cisco123").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
79         assertTrue(userConfig.getRoles().get(0).equals(
80                 UserLevel.NETWORKOPERATOR.toString()));
81
82         // Password not changed, role changed successfully
83         roles.clear();
84         roles.add(UserLevel.SYSTEMADMIN.toString());
85         assertTrue(userConfig.update("cisco123", "cisco123", roles)
86                 .isSuccess());
87         assertTrue(userConfig.authenticate("cisco123").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
88         assertTrue(userConfig.getRoles().get(0)
89                 .equals(UserLevel.SYSTEMADMIN.toString()));
90
91         // Password and role changed successfully
92         assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
93                 .isSuccess());
94         assertTrue(userConfig.authenticate("ciscocisco").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
95         assertTrue(userConfig.getRoles().get(0)
96                 .equals(UserLevel.SYSTEMADMIN.toString()));
97
98         String username = userConfig.getUser();
99         assertTrue(username.equals("uname"));
100
101         // test authenticate
102         AuthResponse authresp = userConfig.authenticate("ciscocisco");
103         assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
104         authresp = userConfig.authenticate("wrongPassword");
105         assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_REJECT_LOC));
106     }
107
108     @Test
109     public void userConfigPasswordTest() {
110
111         String regex = UserConfig.PASSWORD_REGEX;
112         String password = null;
113
114         // Good password
115         password = "aBc@eF#h9";
116         assertTrue(password.matches(regex));
117         password = "^aBc@eF#h9$88ad*o&";
118         assertTrue(password.matches(regex));
119         password = "_^aBc@\":eF#h;9$\\8|8ad*o&-(){}/,.><?+-";
120         assertTrue(password.matches(regex));
121         password = "culonE1)";
122         assertTrue(password.matches(regex));
123
124         // Too short
125         password = "aB3@eF#";
126         assertFalse(password.matches(regex));
127
128         // No number
129         password = "#BeCCC#CeDfDf";
130         assertFalse(password.matches(regex));
131
132         // No lower case
133         password = "AB8C#CC@C4";
134         assertFalse(password.matches(regex));
135
136         // No upper case
137         password = "ab8defg9!";
138         assertFalse(password.matches(regex));
139
140         // No special characters
141         password = "aBc4ef7H8";
142         assertFalse(password.matches(regex));
143
144         // Underscore is a special character
145         password = "Azmb_123 ";
146         assertTrue(password.matches(regex));
147     }
148 }