Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 024e357a59a8e137f202fdfe15d263abef60fb1b (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 Cloudsmith Inc. and others.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the copyright holder
 * listed above, as the Initial Contributor under such license. The text of
 * such license is available at www.eclipse.org.
 ******************************************************************************/

package org.eclipse.equinox.p2.tests.metadata.repository;

import java.lang.reflect.Field;
import java.net.URI;
import java.security.cert.Certificate;
import java.text.MessageFormat;
import java.text.ParseException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.equinox.internal.p2.repository.Messages;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.core.UIServices;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;

/**
 * Test response to various HTTP status codes.
 */
public class HttpStatusTest extends ServerBasedTestCase {
	private IMetadataRepositoryManager mgr;
	private URI repoLoc;
	protected String authTestFailMessage;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		mgr = getAgent().getService(IMetadataRepositoryManager.class);
		if (mgr == null) {
			throw new RuntimeException("Repository manager could not be loaded");
		}
	}

	private void setUpRepo(String repo) throws Exception {
		repoLoc = new URI(repo);
		mgr.removeRepository(repoLoc);
		if (mgr.contains(repoLoc))
			throw new RuntimeException("Error - An earlier test did not leave a clean state - could not remove repo");

	}

	@Override
	public void tearDown() throws Exception {
		AllServerTests.setServiceUI(null); // cleanup hook
		super.tearDown();
		if (repoLoc != null)
			mgr.removeRepository(repoLoc);
	}

	public void testStatusCodes() throws ProvisionException, Exception {
		AllServerTests.setServiceUI(new AladdinNotSavedService());
		// http codes with expected messages
		runSequence(400, 418);
		runSequence(422, 426);
		runSequence(449, 450);
		runSequence(500, 508);
		runSequence(510, 510);
	}

	public void testUnknownStatusCodes() throws ProvisionException, Exception {
		AllServerTests.setServiceUI(new AladdinNotSavedService());
		// undefined HTTP response codes.
		runSequence(419, 421);
		runSequence(427, 448);
		runSequence(511, 601);
	}

	public void testMultipleChoiceCode() throws ProvisionException, Exception {
		AllServerTests.setServiceUI(new AladdinNotSavedService());
		// undefined HTTP response codes.
		runSequence(300, 300);
	}

	private void runSequence(int from, int to) throws Exception {
		for (int i = from; i <= to; i++) {
			setUpRepo(super.getBaseURL() + "/status/" + Integer.valueOf(i).toString());

			try {
				mgr.loadRepository(repoLoc, null);
			} catch (OperationCanceledException e) {
				fail("The repository load was canceled - the UI auth service is probably not running");
			} catch (ProvisionException e) {

				IStatus status = e.getStatus();
				String msg = e.getMessage();

				// Print for human inspection
				System.out.print(String.format("HTTP %d => %s e-message: [%s]\n", //
						i, provisionCodeToText(status.getCode()), msg));

				// assert:
				// - that HTTP code => Repository Code is correct
				// - that correct message surfaces
				//					String m = org.eclipse.equinox.internal.p2.repository.Messages.TransportErrorTranslator_400;
				// Some codes have different message
				switch (i) {
					case 401 :
						// Authentication exception -
						// Assert the ProvisionException code
						assertEquals("Expected Provision Exception code for: " + Integer.valueOf(i), //
								ProvisionException.REPOSITORY_FAILED_AUTHENTICATION, status.getCode());
						break;
					case 300 : // fall through
					case 403 : // fall through
					case 404 : // Does not use the HTTP message.
						// No need to test the message text - any error would be discovered immediately
						// in the UI anyway.
						assertEquals("Expected Provision Exception code for: " + Integer.valueOf(i), //
								ProvisionException.REPOSITORY_NOT_FOUND, status.getCode());
						break;
					case 407 : // fall through
					default :
						// All other messages should surface
						try {
							MessageFormat msgFormat = new MessageFormat(getMessageForCode(i));
							msgFormat.parse(msg);
						} catch (ParseException p) {
							fail("The expected message was not returned for the code:" + Integer.valueOf(i));
						} catch (NoSuchFieldException nsf) {
							fail("The expected message was not returned for the code:" + Integer.valueOf(i));
						}
						assertEquals("Expected Provision Exception code for: " + Integer.valueOf(i), //
								ProvisionException.REPOSITORY_FAILED_READ, status.getCode());
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			assertFalse("Repository should not have been added", mgr.contains(repoLoc));
		}
	}

	private static String getMessageForCode(int code) throws Exception {
		// use reflection on Messages class to get the string in use
		Class<Messages> c = org.eclipse.equinox.internal.p2.repository.Messages.class;
		try {
			Field field = c.getDeclaredField("TransportErrorTranslator_" + Integer.valueOf(code).toString());
			return (String) field.get(null);
		} catch (NoSuchFieldException e) {
			Field field = c.getDeclaredField("TransportErrorTranslator_UnknownErrorCode");
			return (String) field.get(null);
		}
	}

	private static String provisionCodeToText(int code) {
		String msg = "REPOSITORY_";
		switch (code) {
			case ProvisionException.REPOSITORY_EXISTS :
				return msg + "EXISTS";
			case ProvisionException.REPOSITORY_FAILED_AUTHENTICATION :
				return msg + "FAILED_AUTHENTICATION";
			case ProvisionException.REPOSITORY_FAILED_READ :
				return msg + "FAILED_READ";
			case ProvisionException.REPOSITORY_FAILED_WRITE :
				return msg + "FAILED_WRITE";
			case ProvisionException.REPOSITORY_INVALID_LOCATION :
				return msg + "INVALID_LOCATION";
			case ProvisionException.REPOSITORY_NOT_FOUND :
				return msg + "NOT_FOUND";
			case ProvisionException.REPOSITORY_READ_ONLY :
				return msg + "READ_ONLY";
			case ProvisionException.REPOSITORY_UNKNOWN_TYPE :
				return msg + "UNKNOWN_TYPE";
			default :
				return msg + String.format("<unrecognized error code: %d >", code);
		}
	}

	public class AladdinNotSavedService extends UIServices {
		public int counter = 0;

		@Override
		public AuthenticationInfo getUsernamePassword(String location) {
			counter++;
			return new AuthenticationInfo("Aladdin", "open sesame", false);
		}

		@Override
		public AuthenticationInfo getUsernamePassword(String location, AuthenticationInfo previousInfo) {
			counter++;
			assertEquals("Aladdin", previousInfo.getUserName());
			assertEquals("open sesame", previousInfo.getPassword());
			assertEquals(false, previousInfo.saveResult());
			return previousInfo;
		}

		/**
		 * Not used
		 */
		@Override
		public TrustInfo getTrustInfo(Certificate[][] untrustedChain, String[] unsignedDetail) {
			return new TrustInfo(null, false, true);
		}
	}
}

Back to the top