Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0ec2d8e40933e7f61e129d80604f40f066b59d94 (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
/*******************************************************************************
 * Copyright (c) 2008, 2018 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.tests.storage;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import javax.crypto.spec.PBEKeySpec;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.harness.FileSystemHelper;
import org.eclipse.equinox.internal.security.storage.*;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.provider.IProviderHints;
import org.junit.After;

/**
 * Temp directory is used for storage.
 */
public class StorageAbstractTest {

	final protected String defaultFileName = "secure_storage_test.equinox";

	private List<ISecurePreferences> openPreferences = new ArrayList<>(5); // <ISecurePreferences>

	protected String getModuleID() {
		return null;
	}

	@After
	public void tearDown() throws Exception {
		synchronized (openPreferences) {
			for (Iterator<ISecurePreferences> i = openPreferences.iterator(); i.hasNext();) {
				ISecurePreferences root = i.next();
				SecurePreferencesMapper.close((((SecurePreferencesWrapper) root).getContainer().getRootData()));
				URL location = ((SecurePreferencesWrapper) root).getContainer().getLocation();
				StorageUtils.delete(location);
			}
		}
	}

	protected ISecurePreferences newPreferences(URL location, Map<String, Object> options) throws IOException {
		synchronized (openPreferences) {
			ISecurePreferences result = SecurePreferencesFactory.open(location, options);
			openPreferences.add(result);
			return result;
		}
	}

	protected void closePreferences(ISecurePreferences root) {
		synchronized (openPreferences) {
			for (Iterator<ISecurePreferences> i = openPreferences.iterator(); i.hasNext();) {
				ISecurePreferences element = i.next();
				if (element.equals(root)) {
					SecurePreferencesMapper.close((((SecurePreferencesWrapper) root).getContainer().getRootData()));
					i.remove();
				}
			}
		}
	}

	protected Map<String, Object> getOptions(String defaultPassword) {
		Map<String, Object> options = new HashMap<>();

		if (defaultPassword != null) {
			PBEKeySpec password = new PBEKeySpec(defaultPassword.toCharArray());
			options.put(IProviderHints.DEFAULT_PASSWORD, password);
		}

		String requiredID = getModuleID();
		if (requiredID != null)
			options.put(IProviderHints.REQUIRED_MODULE_ID, requiredID);

		options.put(IProviderHints.PROMPT_USER, Boolean.FALSE);
		return options;
	}

	/**
	 * Might consider switching to configuration location.
	 * @throws MalformedURLException 
	 */
	@SuppressWarnings("deprecation")
	protected URL getStorageLocation() throws MalformedURLException {
		IPath tempDir = FileSystemHelper.getTempDir();
		tempDir = tempDir.append(defaultFileName);
		return tempDir.toFile().toURL();
	}
}

Back to the top