2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.usermanager;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
19 import org.opendaylight.controller.sal.authorization.UserLevel;
20 import org.opendaylight.controller.usermanager.AuthResponse;
21 import org.opendaylight.controller.usermanager.AuthorizationConfig;
22 import org.opendaylight.controller.usermanager.UserConfig;
25 * This test case includes tests for UserConfig and the extending class AuthorizationConfig
27 public class AuthorizationUserConfigTest {
30 public void authorizationConfigTest() {
31 AuthorizationConfig authConfig;
32 List<String> roles = new ArrayList<String>();
35 roles.add(UserLevel.SYSTEMADMIN.toString());
36 authConfig = new AuthorizationConfig(null, roles);
37 assertFalse(authConfig.validate().isSuccess());
38 authConfig = new AuthorizationConfig("admin", new ArrayList<String>());
39 assertFalse(authConfig.validate().isSuccess());
40 authConfig = new AuthorizationConfig("admin", roles);
41 assertTrue(authConfig.validate().isSuccess());
45 public void userConfigTest() {
46 UserConfig userConfig;
47 List<String> roles = new ArrayList<String>();
49 roles.add(UserLevel.SYSTEMADMIN.toString());
50 userConfig = new UserConfig(null, "cisco", roles);
51 assertFalse(userConfig.validate().isSuccess());
55 userConfig = new UserConfig("uname", "", roles);
56 assertFalse(userConfig.validate().isSuccess());
59 roles.add(UserLevel.NETWORKOPERATOR.toString());
60 userConfig = new UserConfig("uname", "ciscocisco", roles);
61 assertTrue(userConfig.validate().isSuccess());
63 // currentPassword mismatch
64 assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
68 roles.add(UserLevel.NETWORKADMIN.toString());
69 assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
71 // Role change and same new password
73 roles.add(UserLevel.NETWORKOPERATOR.toString());
74 assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
77 // New Password = null, No change in password
78 assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
80 // Password changed successfully, no change in user role
81 assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
83 assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
84 assertTrue(userConfig.getRoles().get(0).equals(
85 UserLevel.NETWORKOPERATOR.toString()));
87 // Password not changed, role changed successfully
89 roles.add(UserLevel.SYSTEMADMIN.toString());
90 assertTrue(userConfig.update("cisco123", "cisco123", roles)
92 assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
93 assertTrue(userConfig.getRoles().get(0)
94 .equals(UserLevel.SYSTEMADMIN.toString()));
96 // Password and role changed successfully
97 assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
99 assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
100 assertTrue(userConfig.getRoles().get(0)
101 .equals(UserLevel.SYSTEMADMIN.toString()));
103 String username = userConfig.getUser();
104 assertTrue(username.equals("uname"));
107 AuthResponse authresp = userConfig.authenticate("ciscocisco");
108 assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
109 authresp = userConfig.authenticate("wrongPassword");
110 assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_REJECT_LOC));
114 roles.add(UserLevel.NETWORKOPERATOR.toString());
115 userConfig = new UserConfig("uname", "ciscocisco", roles);
116 assertEquals(userConfig, userConfig);
117 UserConfig userConfig2 = new UserConfig("uname", "ciscocisco", roles);
118 assertEquals(userConfig, userConfig2);
122 public void userConfigPasswordTest() {
124 String regex = UserConfig.PASSWORD_REGEX;
125 String password = null;
128 password = "aBc@eF#h9";
129 assertTrue(password.matches(regex));
130 password = "^aBc@eF#h9$88ad*o&";
131 assertTrue(password.matches(regex));
132 password = "_^aBc@\":eF#h;9$\\8|8ad*o&-(){}/,.><?+-";
133 assertTrue(password.matches(regex));
134 password = "culonE1)";
135 assertTrue(password.matches(regex));
138 password = "aB3@eF#";
139 assertFalse(password.matches(regex));
142 password = "#BeCCC#CeDfDf";
143 assertFalse(password.matches(regex));
146 password = "AB8C#CC@C4";
147 assertFalse(password.matches(regex));
150 password = "ab8defg9!";
151 assertFalse(password.matches(regex));
153 // No special characters
154 password = "aBc4ef7H8";
155 assertFalse(password.matches(regex));
157 // Underscore is a special character
158 password = "Azmb_123 ";
159 assertTrue(password.matches(regex));