Clean all unused and redundant imports in controller.
[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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
19 import org.opendaylight.controller.sal.authorization.UserLevel;
20 /*
21  * This test case includes tests for UserConfig and the extending class AuthorizationConfig
22  */
23 public class AuthorizationUserConfigTest {
24
25     @Test
26     public void authorizationConfigTest() {
27         AuthorizationConfig authConfig;
28         List<String> roles = new ArrayList<String>();
29
30         // test isValid
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());
38     }
39
40     @Test
41     public void userConfigTest() {
42         UserConfig userConfig;
43         List<String> roles = new ArrayList<String>();
44
45         roles.add(UserLevel.SYSTEMADMIN.toString());
46         userConfig = new UserConfig(null, "cisco", roles);
47         assertFalse(userConfig.validate().isSuccess());
48
49         roles.clear();
50         roles.add("cisco");
51         userConfig = new UserConfig("uname", "", roles);
52         assertFalse(userConfig.validate().isSuccess());
53
54         roles.clear();
55         roles.add(UserLevel.NETWORKOPERATOR.toString());
56         userConfig = new UserConfig("uname", "ciscocisco", roles);
57         assertTrue(userConfig.validate().isSuccess());
58
59         // currentPassword mismatch
60         assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
61
62         // Role change only
63         roles.clear();
64         roles.add(UserLevel.NETWORKADMIN.toString());
65         assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
66
67         // Role change and same new password
68         roles.clear();
69         roles.add(UserLevel.NETWORKOPERATOR.toString());
70         assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
71                 .isSuccess());
72
73         // New Password = null, No change in password
74         assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
75
76         // Password changed successfully, no change in user role
77         assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
78                 .isSuccess());
79         assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
80         assertTrue(userConfig.getRoles().get(0).equals(
81                 UserLevel.NETWORKOPERATOR.toString()));
82
83         // Password not changed, role changed successfully
84         roles.clear();
85         roles.add(UserLevel.SYSTEMADMIN.toString());
86         assertTrue(userConfig.update("cisco123", "cisco123", roles)
87                 .isSuccess());
88         assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
89         assertTrue(userConfig.getRoles().get(0)
90                 .equals(UserLevel.SYSTEMADMIN.toString()));
91
92         // Password and role changed successfully
93         assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
94                 .isSuccess());
95         assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
96         assertTrue(userConfig.getRoles().get(0)
97                 .equals(UserLevel.SYSTEMADMIN.toString()));
98
99         String username = userConfig.getUser();
100         assertTrue(username.equals("uname"));
101
102         // test authenticate
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));
107
108         // test equals()
109         roles.clear();
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);
115     }
116
117     @Test
118     public void userConfigPasswordTest() {
119
120         String regex = UserConfig.PASSWORD_REGEX;
121         String password = null;
122
123         // Good password
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));
132
133         // Too short
134         password = "aB3@eF#";
135         assertFalse(password.matches(regex));
136
137         // No number
138         password = "#BeCCC#CeDfDf";
139         assertFalse(password.matches(regex));
140
141         // No lower case
142         password = "AB8C#CC@C4";
143         assertFalse(password.matches(regex));
144
145         // No upper case
146         password = "ab8defg9!";
147         assertFalse(password.matches(regex));
148
149         // No special characters
150         password = "aBc4ef7H8";
151         assertFalse(password.matches(regex));
152
153         // Underscore is a special character
154         password = "Azmb_123 ";
155         assertTrue(password.matches(regex));
156     }
157 }