1 /*******************************************************************************
2  * Copyright (c) 2006, 2017 Cloudsmith Inc. and others.
3  * The code, documentation and other materials contained herein have been
4  * licensed under the Eclipse Public License - v 1.0 by the copyright holder
5  * listed above, as the Initial Contributor under such license. The text of
6  * such license is available at www.eclipse.org.
7  ******************************************************************************/
8 
9 package org.eclipse.equinox.p2.tests.metadata.repository;
10 
11 import java.net.URI;
12 import java.security.cert.Certificate;
13 import org.eclipse.core.runtime.OperationCanceledException;
14 import org.eclipse.equinox.internal.p2.repository.RepositoryPreferences;
15 import org.eclipse.equinox.p2.core.ProvisionException;
16 import org.eclipse.equinox.p2.core.UIServices;
17 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
18 
19 public class AuthTest extends ServerBasedTestCase {
20 	//	private static String UPDATE_SITE = "http://p2.piggott.ca/updateSite/";
21 	private String PRIVATE_REPO;
22 	private String NEVER_REPO;
23 	private IMetadataRepositoryManager mgr;
24 	private URI repoLoc;
25 	protected String authTestFailMessage;
26 
27 	@Override
setUp()28 	public void setUp() throws Exception {
29 		super.setUp();
30 		PRIVATE_REPO = super.getBaseURL() + "/private/mdr/composite/one";
31 		NEVER_REPO = super.getBaseURL() + "/proxy/never";
32 		mgr = getAgent().getService(IMetadataRepositoryManager.class);
33 		if (mgr == null) {
34 			throw new RuntimeException("Repository manager could not be loaded");
35 		}
36 	}
37 
setUpRepo(String repo)38 	private void setUpRepo(String repo) throws Exception {
39 		repoLoc = new URI(repo);
40 		mgr.removeRepository(repoLoc);
41 		if (mgr.contains(repoLoc))
42 			throw new RuntimeException("Error - An earlier test did not leave a clean state - could not remove repo");
43 
44 	}
45 
46 	@Override
tearDown()47 	public void tearDown() throws Exception {
48 		AllServerTests.setServiceUI(null); // cleanup hook
49 		super.tearDown();
50 		if (repoLoc != null)
51 			mgr.removeRepository(repoLoc);
52 	}
53 
testPrivateLoad()54 	public void testPrivateLoad() throws ProvisionException, Exception {
55 		AllServerTests.setServiceUI(new AladdinNotSavedService());
56 		setUpRepo(PRIVATE_REPO);
57 		try {
58 			mgr.loadRepository(repoLoc, null);
59 		} catch (OperationCanceledException e) {
60 			fail("The repository load was canceled - the UI auth service is probably not running");
61 		} catch (Exception e) {
62 			e.printStackTrace();
63 		}
64 		assertTrue("Repository should have been added", mgr.contains(repoLoc));
65 	}
66 
testNeverLoad()67 	public void testNeverLoad() throws ProvisionException, Exception {
68 		AladdinNotSavedService service;
69 		AllServerTests.setServiceUI(service = new AladdinNotSavedService());
70 		setUpRepo(NEVER_REPO);
71 		try {
72 			mgr.loadRepository(repoLoc, null);
73 		} catch (OperationCanceledException e) {
74 			fail("The repository load was canceled - the UI auth service is probably not running");
75 		} catch (ProvisionException e) {
76 			assertEquals("Repository is expected to report failed authentication", ProvisionException.REPOSITORY_FAILED_AUTHENTICATION, e.getStatus().getCode());
77 		}
78 		// note that preference includes the first attempt where user is not prompted
79 		assertEquals("There should have been N attempts", RepositoryPreferences.getLoginRetryCount() - 1, service.counter);
80 		assertFalse("Repository should not have been added", mgr.contains(repoLoc));
81 
82 	}
83 
84 	public class AladdinNotSavedService extends UIServices {
85 		public int counter = 0;
86 
87 		@Override
getUsernamePassword(String location)88 		public AuthenticationInfo getUsernamePassword(String location) {
89 			counter++;
90 			return new AuthenticationInfo("Aladdin", "open sesame", false);
91 		}
92 
93 		@Override
getUsernamePassword(String location, AuthenticationInfo previousInfo)94 		public AuthenticationInfo getUsernamePassword(String location, AuthenticationInfo previousInfo) {
95 			counter++;
96 			assertEquals("Aladdin", previousInfo.getUserName());
97 			assertEquals("open sesame", previousInfo.getPassword());
98 			assertEquals(false, previousInfo.saveResult());
99 			return previousInfo;
100 		}
101 
102 		/**
103 		 * Not used
104 		 */
105 		@Override
getTrustInfo(Certificate[][] untrustedChain, String[] unsignedDetail)106 		public TrustInfo getTrustInfo(Certificate[][] untrustedChain, String[] unsignedDetail) {
107 			return new TrustInfo(null, false, true);
108 		}
109 	}
110 }
111