Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a407847aa8c9abb2b2d8df0d8defa2b3693b75b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2011, 2018 SAP AG and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.console.storage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.junit.After;
import org.junit.Test;

public class SecureUserStoreTests {
	
	private static final String USER_STORE_FILE_NAME_PROPERTY = "org.eclipse.equinox.console.jaas.file";
	private static final String USER_STORE_FILE_NAME = SecureUserStoreTests.class.getName() + "_store";
	private static final String USERNAME1 = "username1";
	private static final String USERNAME2 = "username2";
	private static final String PASSWORD1 = "password1";
	private static final String PASSWORD2 = "password2";
	private static final String ROLES1 = "role1,role2";
	private static final String ROLES2 = "role3,role4";
	private static final String ROLES_TO_REMOVE = "role2";
	private static final String REMAINING_ROLES = "role1";
	
	@Test
	public void testStore() throws Exception {
		cleanUp();
		
		System.setProperty(USER_STORE_FILE_NAME_PROPERTY, USER_STORE_FILE_NAME);
		
		SecureUserStore.initStorage();
		assertTrue("Secure store file does not exist", new File(USER_STORE_FILE_NAME).exists());
		
		SecureUserStore.putUser(USERNAME1, PASSWORD1, null);
		SecureUserStore.putUser(USERNAME2, PASSWORD2, ROLES2);
		
		String[] usernames = SecureUserStore.getUserNames();
		boolean arePresent = (usernames[0].equals(USERNAME1) || usernames[0].equals(USERNAME2)) && (usernames[1].equals(USERNAME1) || usernames[1].equals(USERNAME2)) && (!usernames[0].equals(usernames[1]));
		assertTrue("Usernames not correctly saved", arePresent);
		
		String pass1 = SecureUserStore.getPassword(USERNAME1);
		String pass2 = SecureUserStore.getPassword(USERNAME2);
		assertTrue("Passwords not correctly saved", pass1.equals(PASSWORD1) && pass2.equals(PASSWORD2));
		
		boolean existsUser1 = SecureUserStore.existsUser(USERNAME1);
		boolean existsUser2 = SecureUserStore.existsUser(USERNAME2);
		assertTrue("Users should exist", existsUser1 && existsUser2);
		
		String roles = SecureUserStore.getRoles(USERNAME2);
		assertEquals("Roles for the second user are not as expected", ROLES2, roles);
		
		SecureUserStore.addRoles(USERNAME1, ROLES1);
		roles = SecureUserStore.getRoles(USERNAME1);
		boolean areRolesEqual = compareRoles(ROLES1, roles);
		assertTrue("Roles for the first user are not as expected", areRolesEqual);
		
		SecureUserStore.removeRoles(USERNAME1, ROLES_TO_REMOVE);
		roles = SecureUserStore.getRoles(USERNAME1);
		areRolesEqual = compareRoles(REMAINING_ROLES, roles);
		assertTrue("Roles for the first user are not as expected", areRolesEqual);
		
		SecureUserStore.resetPassword(USERNAME1);
		String pass = SecureUserStore.getPassword(USERNAME1);
		assertNull("Password should be null", pass);
		
		SecureUserStore.setPassword(USERNAME1, PASSWORD1);
		pass = SecureUserStore.getPassword(USERNAME1);
		assertEquals("Password should be null", PASSWORD1, pass);
		
		SecureUserStore.deleteUser(USERNAME2);
		assertFalse("User2 should not exist", SecureUserStore.existsUser(USERNAME2));
	}
	
	@After
	public void cleanUp() {
		System.setProperty(USER_STORE_FILE_NAME_PROPERTY, "");
    	File file = new File(USER_STORE_FILE_NAME);
    	if(file.exists()) {
    		file.delete();
    	}
	}
	
	private boolean compareRoles(String expectedRoles, String actualRoles) {
		Set<String> expectedRolesSet = new HashSet<>();
		for(String role : expectedRoles.split(",")) {
			expectedRolesSet.add(role);
		}
		
		Set<String> actualRolesSet = new HashSet<>();
		for(String role : actualRoles.split(",")) {
			actualRolesSet.add(role);
		}
		
		return expectedRolesSet.equals(actualRolesSet);
	}
}

Back to the top