/
StoragePreferencePage.java
204 lines (183 loc) · 6.45 KB
/
StoragePreferencePage.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.equinox.security.ui.storage;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.core.reference.ReferencedComposite;
import org.eclipse.reddeer.eclipse.equinox.internal.security.ui.storage.PasswordRecoveryDialog;
import org.eclipse.reddeer.eclipse.equinox.internal.security.ui.storage.ChangePasswordWizardDialog;
import org.eclipse.reddeer.eclipse.exception.EclipseLayerException;
import org.eclipse.reddeer.jface.preference.PreferencePage;
import org.eclipse.reddeer.swt.api.TableItem;
import org.eclipse.reddeer.swt.condition.ControlIsEnabled;
import org.eclipse.reddeer.swt.impl.button.*;
import org.eclipse.reddeer.swt.impl.combo.DefaultCombo;
import org.eclipse.reddeer.swt.impl.tab.DefaultTabItem;
import org.eclipse.reddeer.swt.impl.table.DefaultTable;
import org.eclipse.reddeer.swt.impl.text.LabeledText;
import org.eclipse.reddeer.swt.impl.tree.DefaultTree;
import org.eclipse.reddeer.swt.impl.tree.DefaultTreeItem;
/**
* Storage preference page represent page for secure storage in Eclipse preferences.
* Purpose of this preference page is storage handling of master password and passwords in Eclipse.
*
* @author mlabuda@redhat.com
* @author jnovak@redhat.com
*
*/
public class StoragePreferencePage extends PreferencePage {
/**
* Instantiates a new storage preference page.
*/
public StoragePreferencePage(ReferencedComposite referencedComposite) {
super(referencedComposite, "General", "Security", "Secure Storage");
}
/**
* Select Password tab in Security storage page containing general secure storage options.
*/
public StoragePreferencePage selectPasswordsTab() {
new DefaultTabItem(this, "Password").activate();
return this;
}
/**
* Select Contents tab in Storage preference page containing stored passwords with related user names/accounts.
*/
public StoragePreferencePage selectContentTab() {
new DefaultTabItem(this, "Contents").activate();
return this;
}
/**
* Select Advanced tab in Storage preference page containing decryption of stored passwords.
*/
public StoragePreferencePage selectAdvancedTab() {
new DefaultTabItem(this, "Advanced").activate();
return this;
}
/**
* Get master password providers from table shown on password tab in Storage preference page.
*
* @return list of table items representing master passwords
*/
public List<PasswordProvider> getMasterPasswordProviders() {
ArrayList<PasswordProvider> providers = new ArrayList<>();
for (TableItem item : new DefaultTable(this).getItems()) {
providers.add(new PasswordProvider(item));
}
return providers;
}
/**
* Opens master password recovery dialog.
*
* @return password recover dialog
*/
public PasswordRecoveryDialog recoverMasterPassword() {
new PushButton(this, "Recover Password...").click();
return new PasswordRecoveryDialog();
}
/**
* Opens master password change dialog.
*
* @return password change dialog
*/
public ChangePasswordWizardDialog changeMasterPassword() {
new PushButton(this, "Change Password...").click();
return new ChangePasswordWizardDialog();
}
/**
* Clears stored passwords. If there are non passwords, nothing happens.
*
* @return true if cleared passwords successfully, false otherwise
*/
public boolean clearCachedPasswords() {
try {
new WaitUntil(new ControlIsEnabled(new PushButton(this, "Clear Passwords")));
new PushButton(this, "Clear Passwords").click();
return true;
} catch(WaitTimeoutExpiredException ex) {
return false;
}
}
/**
* Finds out whether there is any password in secure storage on the end of specified path.
*
* @param pathToPassword full path in tree on Content tab of Secure Storage Preference page
* @return true if there is any password on the end of specified path, false otherwise
*/
public boolean passwordExists(String... pathToPassword) {
selectContentTab();
new DefaultTreeItem(new DefaultTree(this, 1), pathToPassword).select();
// Reactivation required
selectContentTab();
return new DefaultTable(this).getItems().size() > 0;
}
/**
* Retrieves passwords in secure storage on the end of specified path.
*
* @param pathToPasswords full path in tree on Content tab of Secure Storage Preference page
* @return stored keys and values in the Properties object
*/
public Properties getPasswordsByPath(String... pathToPasswords) {
selectContentTab();
new DefaultTreeItem(new DefaultTree(this, 1), pathToPasswords).select();
// Reactivation required
selectContentTab();
return getPasswordsFromTable(new DefaultTable(this));
}
/**
* Retrieves the secure storage location.
* @return secure storage location
*/
public String getStorageLocation() {
selectContentTab();
return new LabeledText(this, "Storage location:").getText();
}
private Properties getPasswordsFromTable(DefaultTable table) {
Properties props = new Properties();
for (TableItem item : table.getItems()) {
String id = item.getText(0);
String value = item.getText(1);
props.put(id, value);
}
return props;
}
/**
* Retrieves available encrypt algorithms.
* @return available algorithms
*/
public List<String> getAvailableEncryptionAlgorithms() {
selectAdvancedTab();
return new DefaultCombo(this).getItems();
}
/**
* Retrieves currently chosen encrypt algorithms.
* @return currently chosen encrypt algorithm name.
*/
public String getEncryptionAlgorithm() {
selectAdvancedTab();
return new DefaultCombo(this).getSelection();
}
/**
* Sets encrypt algorithm.
* @param algorithmName new algorithm name.
*/
public void setEncryptionAlgorithm(String algorithmName) {
selectAdvancedTab();
if(getAvailableEncryptionAlgorithms().contains(algorithmName)) {
new DefaultCombo(this).setSelection(algorithmName);
} else {
throw new EclipseLayerException("Algorithm " + algorithmName + " not found!");
}
}
}