PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet.test/org/restlet/test/security/HttpBasicTestCase.java

https://bitbucket.org/haris_peco/debrief
Java | 283 lines | 187 code | 52 blank | 44 comment | 3 complexity | a63fbffe2b25782b06e7d4fb0d176a43 MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.test.security;
  31. import java.util.Arrays;
  32. import org.junit.After;
  33. import org.junit.Before;
  34. import org.restlet.Application;
  35. import org.restlet.Client;
  36. import org.restlet.Component;
  37. import org.restlet.Request;
  38. import org.restlet.Response;
  39. import org.restlet.Restlet;
  40. import org.restlet.data.ChallengeResponse;
  41. import org.restlet.data.ChallengeScheme;
  42. import org.restlet.data.MediaType;
  43. import org.restlet.data.Method;
  44. import org.restlet.data.Protocol;
  45. import org.restlet.data.Status;
  46. import org.restlet.security.ChallengeAuthenticator;
  47. import org.restlet.security.MapVerifier;
  48. import org.restlet.test.RestletTestCase;
  49. /**
  50. * Restlet unit tests for HTTP Basic authentication client/server. By default,
  51. * runs server on localhost on port {@value #DEFAULT_PORT}, which can be
  52. * overridden by setting system property {@value #RESTLET_TEST_PORT}
  53. *
  54. * @author Stian Soiland
  55. * @author Jerome Louvel
  56. */
  57. public class HttpBasicTestCase extends RestletTestCase {
  58. public class AuthenticatedRestlet extends Restlet {
  59. @Override
  60. public void handle(Request request, Response response) {
  61. response.setEntity(AUTHENTICATED_MSG, MediaType.TEXT_PLAIN);
  62. }
  63. }
  64. public class TestVerifier extends MapVerifier {
  65. public TestVerifier() {
  66. getLocalSecrets().put(SHORT_USERNAME, SHORT_PASSWORD.toCharArray());
  67. getLocalSecrets().put(LONG_USERNAME, LONG_PASSWORD.toCharArray());
  68. }
  69. @Override
  70. public boolean verify(String identifier, char[] inputSecret) {
  71. // NOTE: Allocating Strings are not really secure treatment of
  72. // passwords
  73. final String almostSecret = new String(inputSecret);
  74. System.out.println("Checking " + identifier + " " + almostSecret);
  75. try {
  76. return super.verify(identifier, inputSecret);
  77. } finally {
  78. // Clear secret from memory as soon as possible (This is better
  79. // treatment, but of course useless due to our almostSecret
  80. // copy)
  81. Arrays.fill(inputSecret, '\000');
  82. }
  83. }
  84. }
  85. public static final String WRONG_USERNAME = "wrongUser";
  86. public static final String SHORT_USERNAME = "user13";
  87. public static final String SHORT_PASSWORD = "pw15";
  88. public static final String LONG_USERNAME = "aVeryLongUsernameIsIndeedRequiredForThisTest";
  89. public static final String LONG_PASSWORD = "thisLongPasswordIsExtremelySecure";
  90. public static final String AUTHENTICATED_MSG = "You are authenticated";
  91. public static void main(String[] args) {
  92. new HttpBasicTestCase().testHTTPBasic();
  93. }
  94. private Component component;
  95. private String uri;
  96. private ChallengeAuthenticator authenticator;
  97. private MapVerifier verifier;
  98. public void guardLong() {
  99. assertTrue("Didn't authenticate short user/pwd", this.verifier.verify(
  100. LONG_USERNAME, LONG_PASSWORD.toCharArray()));
  101. }
  102. public void guardLongWrong() {
  103. assertFalse("Authenticated long username with wrong password",
  104. this.verifier.verify(LONG_USERNAME, SHORT_PASSWORD
  105. .toCharArray()));
  106. }
  107. // Test our guard.checkSecret() stand-alone
  108. public void guardShort() {
  109. assertTrue("Didn't authenticate short user/pwd", this.verifier.verify(
  110. SHORT_USERNAME, SHORT_PASSWORD.toCharArray()));
  111. }
  112. public void guardShortWrong() {
  113. assertFalse("Authenticated short username with wrong password",
  114. this.verifier.verify(SHORT_USERNAME, LONG_PASSWORD
  115. .toCharArray()));
  116. }
  117. public void guardWrongUser() {
  118. assertFalse("Authenticated wrong username", this.verifier.verify(
  119. WRONG_USERNAME, SHORT_PASSWORD.toCharArray()));
  120. }
  121. public void HTTPBasicLong() throws Exception {
  122. final Request request = new Request(Method.GET, this.uri);
  123. final Client client = new Client(Protocol.HTTP);
  124. final ChallengeResponse authentication = new ChallengeResponse(
  125. ChallengeScheme.HTTP_BASIC, LONG_USERNAME, LONG_PASSWORD);
  126. request.setChallengeResponse(authentication);
  127. final Response response = client.handle(request);
  128. assertEquals("Long username did not return 200 OK", Status.SUCCESS_OK,
  129. response.getStatus());
  130. assertEquals(AUTHENTICATED_MSG, response.getEntity().getText());
  131. client.stop();
  132. }
  133. public void HTTPBasicLongWrong() throws Exception {
  134. final Request request = new Request(Method.GET, this.uri);
  135. final Client client = new Client(Protocol.HTTP);
  136. final ChallengeResponse authentication = new ChallengeResponse(
  137. ChallengeScheme.HTTP_BASIC, LONG_USERNAME, SHORT_PASSWORD);
  138. request.setChallengeResponse(authentication);
  139. final Response response = client.handle(request);
  140. assertEquals("Long username w/wrong pw did not throw 403",
  141. Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
  142. client.stop();
  143. }
  144. // Test various HTTP Basic auth connections
  145. public void HTTPBasicNone() throws Exception {
  146. final Request request = new Request(Method.GET, this.uri);
  147. final Client client = new Client(Protocol.HTTP);
  148. final Response response = client.handle(request);
  149. assertEquals("No user did not throw 401",
  150. Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
  151. client.stop();
  152. }
  153. public void HTTPBasicShort() throws Exception {
  154. final Request request = new Request(Method.GET, this.uri);
  155. final Client client = new Client(Protocol.HTTP);
  156. final ChallengeResponse authentication = new ChallengeResponse(
  157. ChallengeScheme.HTTP_BASIC, SHORT_USERNAME, SHORT_PASSWORD);
  158. request.setChallengeResponse(authentication);
  159. final Response response = client.handle(request);
  160. assertEquals("Short username did not return 200 OK", Status.SUCCESS_OK,
  161. response.getStatus());
  162. assertEquals(AUTHENTICATED_MSG, response.getEntity().getText());
  163. client.stop();
  164. }
  165. public void HTTPBasicShortWrong() throws Exception {
  166. final Request request = new Request(Method.GET, this.uri);
  167. final Client client = new Client(Protocol.HTTP);
  168. final ChallengeResponse authentication = new ChallengeResponse(
  169. ChallengeScheme.HTTP_BASIC, SHORT_USERNAME, LONG_PASSWORD);
  170. request.setChallengeResponse(authentication);
  171. final Response response = client.handle(request);
  172. assertEquals("Short username did not throw 401",
  173. Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
  174. client.stop();
  175. }
  176. public void HTTPBasicWrongUser() throws Exception {
  177. final Request request = new Request(Method.GET, this.uri);
  178. final Client client = new Client(Protocol.HTTP);
  179. final ChallengeResponse authentication = new ChallengeResponse(
  180. ChallengeScheme.HTTP_BASIC, WRONG_USERNAME, SHORT_PASSWORD);
  181. request.setChallengeResponse(authentication);
  182. final Response response = client.handle(request);
  183. assertEquals("Wrong username did not throw 401",
  184. Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
  185. client.stop();
  186. }
  187. @Before
  188. public void makeServer() throws Exception {
  189. int port = TEST_PORT;
  190. this.component = new Component();
  191. this.component.getServers().add(Protocol.HTTP, port);
  192. this.uri = "http://localhost:" + port + "/";
  193. final Application application = new Application() {
  194. @Override
  195. public Restlet createInboundRoot() {
  196. HttpBasicTestCase.this.verifier = new TestVerifier();
  197. HttpBasicTestCase.this.authenticator = new ChallengeAuthenticator(
  198. getContext(), ChallengeScheme.HTTP_BASIC,
  199. HttpBasicTestCase.class.getSimpleName());
  200. HttpBasicTestCase.this.authenticator
  201. .setVerifier(HttpBasicTestCase.this.verifier);
  202. HttpBasicTestCase.this.authenticator
  203. .setNext(new AuthenticatedRestlet());
  204. return HttpBasicTestCase.this.authenticator;
  205. }
  206. };
  207. this.component.getDefaultHost().attach(application);
  208. this.component.start();
  209. }
  210. @After
  211. public void stopServer() throws Exception {
  212. if ((this.component != null) && this.component.isStarted()) {
  213. this.component.stop();
  214. }
  215. this.component = null;
  216. }
  217. public void testHTTPBasic() {
  218. try {
  219. makeServer();
  220. HTTPBasicWrongUser();
  221. HTTPBasicShort();
  222. HTTPBasicShortWrong();
  223. HTTPBasicNone();
  224. HTTPBasicLong();
  225. HTTPBasicLongWrong();
  226. stopServer();
  227. } catch (Exception e) {
  228. e.printStackTrace();
  229. }
  230. }
  231. }