Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 823207596a149b07bf7c9bc8edabcd046b85f922 (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2010, Edwin Kempin <edwin.kempin@sap.com>
 *
 * All rights reserved. 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
 *******************************************************************************/
package org.eclipse.egit.core.securestorage;

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

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.crypto.spec.PBEKeySpec;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.provider.IProviderHints;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EGitSecureStoreTest {

	ISecurePreferences secureStoreForTest;

	EGitSecureStore store;

	@Before
	public void setUp() throws Exception {
		setupNewSecureStore();
		store = new EGitSecureStore(secureStoreForTest);
	}

	@After
	public void tearDown() throws Exception {
		secureStoreForTest.flush();
	}

	@Test
	public void testPutUserAndPassword() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);

		ISecurePreferences node = secureStoreForTest.node(EGitSecureStore
				.calcNodePath(uri).toString());
		assertEquals("agitter", node.get("user", null));
		assertTrue(node.isEncrypted("password"));
		assertEquals("letmein", node.get("password", null));
	}

	@Test
	public void testGetUserAndPassword() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);

		UserPasswordCredentials storedCredentials = store.getCredentials(uri);
		assertEquals("agitter", storedCredentials.getUser());
		assertEquals("letmein", storedCredentials.getPassword());
	}

	@Test
	public void testGetUserAndPasswordUnknownURI() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");

		UserPasswordCredentials storedCredentials = store.getCredentials(uri);
		assertNull(storedCredentials);
	}

	@Test
	public void testPutUserAndPasswordURIContainingUserAndPass()
			throws Exception {
		URIish uri = new URIish(
				"http://user:pass@testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);

		ISecurePreferences node = secureStoreForTest.node(EGitSecureStore
				.calcNodePath(uri).toString());
		assertEquals("agitter", node.get("user", null));
		assertTrue(node.isEncrypted("password"));
		assertEquals("letmein", node.get("password", null));
	}

	@Test
	public void testGetUserAndPasswordURIContainingUserAndPass()
			throws Exception {
		store.putCredentials(
				new URIish("http://testRepo.example.com/testrepo"),
				new UserPasswordCredentials("agitter", "letmein"));
		UserPasswordCredentials credentials = store.getCredentials(new URIish(
				"http://agitter:letmein@testRepo.example.com/testrepo"));
		assertEquals("agitter", credentials.getUser());
		assertEquals("letmein", credentials.getPassword());
	}

	@Test
	public void testGetUserAndPasswordURIContainingOtherUserAndPass()
			throws Exception {
		store.putCredentials(
				new URIish("http://testRepo.example.com/testrepo"),
				new UserPasswordCredentials("agitter", "letmein"));
		assertNull(store.getCredentials(new URIish(
				"http://otheruser:otherpass@testRepo.example.com/testrepo")));
	}

	@Test
	public void testClearCredentials() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		store.clearCredentials(uri);
		assertEquals(null, store.getCredentials(uri));
	}

	@Test
	public void testEnsureDefaultPortHttp() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		URIish uri2 = new URIish("http://testRepo.example.com:80/testrepo");
		assertEquals(credentials.getUser(), store.getCredentials(uri2).getUser());
		assertEquals(credentials.getPassword(), store.getCredentials(uri2).getPassword());
	}

	@Test
	public void testEnsureDefaultPortHttps() throws Exception {
		URIish uri = new URIish("https://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		URIish uri2 = new URIish("https://testRepo.example.com:443/testrepo");
		assertEquals(credentials.getUser(), store.getCredentials(uri2).getUser());
		assertEquals(credentials.getPassword(), store.getCredentials(uri2).getPassword());
	}

	@Test
	public void testEnsureDefaultPortSftp() throws Exception {
		URIish uri = new URIish("sftp://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		URIish uri2 = new URIish("sftp://testRepo.example.com:22/testrepo");
		assertEquals(credentials.getUser(), store.getCredentials(uri2).getUser());
		assertEquals(credentials.getPassword(), store.getCredentials(uri2).getPassword());
	}

	@Test
	public void testEnsureDefaultPortFtp() throws Exception {
		URIish uri = new URIish("ftp://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		URIish uri2 = new URIish("ftp://testRepo.example.com:21/testrepo");
		assertEquals(credentials.getUser(), store.getCredentials(uri2).getUser());
		assertEquals(credentials.getPassword(), store.getCredentials(uri2).getPassword());
	}

	@Test
	public void testEnsureDefaultPortSsh() throws Exception {
		URIish uri = new URIish("ssh://agitter@testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		URIish uri2 = new URIish("ssh://testRepo.example.com:22/testrepo");
		assertEquals(credentials.getUser(), store.getCredentials(uri2).getUser());
		assertEquals(credentials.getPassword(), store.getCredentials(uri2).getPassword());
	}

	@Test
	public void testClearCredentialsTwice() throws Exception {
		URIish uri = new URIish("http://testRepo.example.com/testrepo");
		UserPasswordCredentials credentials = new UserPasswordCredentials(
				"agitter", "letmein");
		store.putCredentials(uri, credentials);
		store.clearCredentials(uri);
		assertEquals(null, store.getCredentials(uri));
		store.clearCredentials(uri);
		assertEquals(null, store.getCredentials(uri));
	}

	private void setupNewSecureStore() throws IOException,
			MalformedURLException {
		HashMap<String, Object> options = new HashMap<String, Object>();
		options.put(IProviderHints.DEFAULT_PASSWORD, new PBEKeySpec(
				"masterpass".toCharArray()));
		String secureStorePath = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation().append("testSecureStore").toOSString();
		File file = new File(secureStorePath);
		if (file.exists())
			FileUtils.delete(file, FileUtils.RECURSIVE | FileUtils.RETRY);
		URL url = file.toURI().toURL();
		secureStoreForTest = SecurePreferencesFactory.open(url, options);
		secureStoreForTest.node("/GIT").removeNode();
	}

}

Back to the top