You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsIsServiceTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.http.test;
  11. import static org.junit.Assert.fail;
  12. import java.io.IOException;
  13. import javax.servlet.http.HttpServletRequestWrapper;
  14. import org.eclipse.jetty.server.Request;
  15. import org.eclipse.jgit.http.server.resolver.AsIsFileService;
  16. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  17. import org.eclipse.jgit.lib.Repository;
  18. import org.eclipse.jgit.lib.StoredConfig;
  19. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  20. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. public class AsIsServiceTest extends LocalDiskRepositoryTestCase {
  24. private Repository db;
  25. private AsIsFileService service;
  26. @Override
  27. @Before
  28. public void setUp() throws Exception {
  29. super.setUp();
  30. db = createBareRepository();
  31. service = new AsIsFileService();
  32. }
  33. @Test
  34. public void testDisabledSingleton() throws ServiceNotAuthorizedException {
  35. service = AsIsFileService.DISABLED;
  36. try {
  37. service.access(new R(null, "1.2.3.4"), db);
  38. fail("Created session for anonymous user: null");
  39. } catch (ServiceNotEnabledException e) {
  40. // expected not authorized
  41. }
  42. try {
  43. service.access(new R("bob", "1.2.3.4"), db);
  44. fail("Created session for user: \"bob\"");
  45. } catch (ServiceNotEnabledException e) {
  46. // expected not authorized
  47. }
  48. }
  49. @Test
  50. public void testCreate_Default() throws ServiceNotEnabledException,
  51. ServiceNotAuthorizedException {
  52. service.access(new R(null, "1.2.3.4"), db);
  53. service.access(new R("bob", "1.2.3.4"), db);
  54. }
  55. @Test
  56. public void testCreate_Disabled() throws ServiceNotAuthorizedException,
  57. IOException {
  58. final StoredConfig cfg = db.getConfig();
  59. cfg.setBoolean("http", null, "getanyfile", false);
  60. cfg.save();
  61. try {
  62. service.access(new R(null, "1.2.3.4"), db);
  63. fail("Created session for anonymous user: null");
  64. } catch (ServiceNotEnabledException e) {
  65. // expected not authorized
  66. }
  67. try {
  68. service.access(new R("bob", "1.2.3.4"), db);
  69. fail("Created session for user: \"bob\"");
  70. } catch (ServiceNotEnabledException e) {
  71. // expected not authorized
  72. }
  73. }
  74. @Test
  75. public void testCreate_Enabled() throws ServiceNotEnabledException,
  76. ServiceNotAuthorizedException {
  77. db.getConfig().setBoolean("http", null, "getanyfile", true);
  78. service.access(new R(null, "1.2.3.4"), db);
  79. service.access(new R("bob", "1.2.3.4"), db);
  80. }
  81. private static final class R extends HttpServletRequestWrapper {
  82. private final String user;
  83. private final String host;
  84. R(String user, String host) {
  85. super(new Request(null, null) /* can't pass null, sigh */);
  86. this.user = user;
  87. this.host = host;
  88. }
  89. @Override
  90. public String getRemoteHost() {
  91. return host;
  92. }
  93. @Override
  94. public String getRemoteUser() {
  95. return user;
  96. }
  97. }
  98. }