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.assertFalse;
11 import static org.junit.Assert.assertTrue;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
18 import org.opendaylight.controller.sal.authorization.UserLevel;
20 * This test case includes tests for UserConfig and the extending class AuthorizationConfig
22 public class AuthorizationUserConfigTest {
25 public void authorizationConfigTest() {
26 AuthorizationConfig authConfig;
27 List<String> roles = new ArrayList<String>();
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());
40 public void userConfigTest() {
41 UserConfig userConfig;
42 List<String> roles = new ArrayList<String>();
44 roles.add(UserLevel.SYSTEMADMIN.toString());
45 userConfig = new UserConfig(null, "cisco", roles);
46 assertFalse(userConfig.validate().isSuccess());
50 userConfig = new UserConfig("uname", "", roles);
51 assertFalse(userConfig.validate().isSuccess());
54 roles.add(UserLevel.NETWORKOPERATOR.toString());
55 userConfig = new UserConfig("uname", "ciscocisco", roles);
56 assertTrue(userConfig.validate().isSuccess());
58 // currentPassword mismatch
59 assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
63 roles.add(UserLevel.NETWORKADMIN.toString());
64 assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
66 // Role change and same new password
68 roles.add(UserLevel.NETWORKOPERATOR.toString());
69 assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
72 // New Password = null, No change in password
73 assertTrue(userConfig.authenticate("ciscocisco").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
75 // Password changed successfully, no change in user role
76 assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
78 assertTrue(userConfig.authenticate("cisco123").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
79 assertTrue(userConfig.getRoles().get(0).equals(
80 UserLevel.NETWORKOPERATOR.toString()));
82 // Password not changed, role changed successfully
84 roles.add(UserLevel.SYSTEMADMIN.toString());
85 assertTrue(userConfig.update("cisco123", "cisco123", roles)
87 assertTrue(userConfig.authenticate("cisco123").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
88 assertTrue(userConfig.getRoles().get(0)
89 .equals(UserLevel.SYSTEMADMIN.toString()));
91 // Password and role changed successfully
92 assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
94 assertTrue(userConfig.authenticate("ciscocisco").getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
95 assertTrue(userConfig.getRoles().get(0)
96 .equals(UserLevel.SYSTEMADMIN.toString()));
98 String username = userConfig.getUser();
99 assertTrue(username.equals("uname"));
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));
109 public void userConfigPasswordTest() {
111 String regex = UserConfig.PASSWORD_REGEX;
112 String password = null;
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));
125 password = "aB3@eF#";
126 assertFalse(password.matches(regex));
129 password = "#BeCCC#CeDfDf";
130 assertFalse(password.matches(regex));
133 password = "AB8C#CC@C4";
134 assertFalse(password.matches(regex));
137 password = "ab8defg9!";
138 assertFalse(password.matches(regex));
140 // No special characters
141 password = "aBc4ef7H8";
142 assertFalse(password.matches(regex));
144 // Underscore is a special character
145 password = "Azmb_123 ";
146 assertTrue(password.matches(regex));