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;
21 * This test case includes tests for UserConfig and the extending class AuthorizationConfig
23 public class AuthorizationUserConfigTest {
26 public void authorizationConfigTest() {
27 AuthorizationConfig authConfig;
28 List<String> roles = new ArrayList<String>();
31 roles.add(UserLevel.SYSTEMADMIN.toString());
32 authConfig = new AuthorizationConfig(null, roles);
33 assertFalse(authConfig.validate().isSuccess());
34 authConfig = new AuthorizationConfig("admin", new ArrayList<String>());
35 assertFalse(authConfig.validate().isSuccess());
36 authConfig = new AuthorizationConfig("admin", roles);
37 assertTrue(authConfig.validate().isSuccess());
41 public void userConfigTest() {
42 UserConfig userConfig;
43 List<String> roles = new ArrayList<String>();
45 roles.add(UserLevel.SYSTEMADMIN.toString());
46 userConfig = new UserConfig(null, "cisco", roles);
47 assertFalse(userConfig.validate().isSuccess());
51 userConfig = new UserConfig("uname", "", roles);
52 assertFalse(userConfig.validate().isSuccess());
55 roles.add(UserLevel.NETWORKOPERATOR.toString());
56 userConfig = new UserConfig("uname", "ciscocisco", roles);
57 assertTrue(userConfig.validate().isSuccess());
59 // currentPassword mismatch
60 assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
64 roles.add(UserLevel.NETWORKADMIN.toString());
65 assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
67 // Role change and same new password
69 roles.add(UserLevel.NETWORKOPERATOR.toString());
70 assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
73 // New Password = null, No change in password
74 assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
76 // Password changed successfully, no change in user role
77 assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
79 assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
80 assertTrue(userConfig.getRoles().get(0).equals(
81 UserLevel.NETWORKOPERATOR.toString()));
83 // Password not changed, role changed successfully
85 roles.add(UserLevel.SYSTEMADMIN.toString());
86 assertTrue(userConfig.update("cisco123", "cisco123", roles)
88 assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
89 assertTrue(userConfig.getRoles().get(0)
90 .equals(UserLevel.SYSTEMADMIN.toString()));
92 // Password and role changed successfully
93 assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
95 assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
96 assertTrue(userConfig.getRoles().get(0)
97 .equals(UserLevel.SYSTEMADMIN.toString()));
99 String username = userConfig.getUser();
100 assertTrue(username.equals("uname"));
103 AuthResponse authresp = userConfig.authenticate("ciscocisco");
104 assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
105 authresp = userConfig.authenticate("wrongPassword");
106 assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_REJECT_LOC));
110 roles.add(UserLevel.NETWORKOPERATOR.toString());
111 userConfig = new UserConfig("uname", "ciscocisco", roles);
112 assertEquals(userConfig, userConfig);
113 UserConfig userConfig2 = new UserConfig("uname", "ciscocisco", roles);
114 assertEquals(userConfig, userConfig2);
118 public void userConfigPasswordTest() {
120 String regex = UserConfig.PASSWORD_REGEX;
121 String password = null;
124 password = "aBc@eF#h9";
125 assertTrue(password.matches(regex));
126 password = "^aBc@eF#h9$88ad*o&";
127 assertTrue(password.matches(regex));
128 password = "_^aBc@\":eF#h;9$\\8|8ad*o&-(){}/,.><?+-";
129 assertTrue(password.matches(regex));
130 password = "culonE1)";
131 assertTrue(password.matches(regex));
134 password = "aB3@eF#";
135 assertFalse(password.matches(regex));
138 password = "#BeCCC#CeDfDf";
139 assertFalse(password.matches(regex));
142 password = "AB8C#CC@C4";
143 assertFalse(password.matches(regex));
146 password = "ab8defg9!";
147 assertFalse(password.matches(regex));
149 // No special characters
150 password = "aBc4ef7H8";
151 assertFalse(password.matches(regex));
153 // Underscore is a special character
154 password = "Azmb_123 ";
155 assertTrue(password.matches(regex));