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.

DefaultReceivePackFactoryTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertSame;
  14. import static org.junit.Assert.fail;
  15. import java.io.IOException;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletRequestWrapper;
  18. import org.eclipse.jetty.server.Request;
  19. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  20. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  21. import org.eclipse.jgit.lib.PersonIdent;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.lib.StoredConfig;
  24. import org.eclipse.jgit.transport.ReceivePack;
  25. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  26. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  27. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. public class DefaultReceivePackFactoryTest extends LocalDiskRepositoryTestCase {
  31. private Repository db;
  32. private ReceivePackFactory<HttpServletRequest> factory;
  33. @Override
  34. @Before
  35. public void setUp() throws Exception {
  36. super.setUp();
  37. db = createBareRepository();
  38. factory = new DefaultReceivePackFactory();
  39. }
  40. @SuppressWarnings("unchecked")
  41. @Test
  42. public void testDisabledSingleton() throws ServiceNotAuthorizedException {
  43. factory = (ReceivePackFactory<HttpServletRequest>) ReceivePackFactory.DISABLED;
  44. try {
  45. factory.create(new R(null, "localhost"), db);
  46. fail("Created session for anonymous user: null");
  47. } catch (ServiceNotEnabledException e) {
  48. // expected not authorized
  49. }
  50. try {
  51. factory.create(new R("", "localhost"), db);
  52. fail("Created session for anonymous user: \"\"");
  53. } catch (ServiceNotEnabledException e) {
  54. // expected not authorized
  55. }
  56. try {
  57. factory.create(new R("bob", "localhost"), db);
  58. fail("Created session for user: \"bob\"");
  59. } catch (ServiceNotEnabledException e) {
  60. // expected not authorized
  61. }
  62. }
  63. @Test
  64. public void testCreate_NullUser() throws ServiceNotEnabledException {
  65. try {
  66. factory.create(new R(null, "localhost"), db);
  67. fail("Created session for anonymous user: null");
  68. } catch (ServiceNotAuthorizedException e) {
  69. // expected not authorized
  70. }
  71. }
  72. @Test
  73. public void testCreate_EmptyStringUser() throws ServiceNotEnabledException {
  74. try {
  75. factory.create(new R("", "localhost"), db);
  76. fail("Created session for anonymous user: \"\"");
  77. } catch (ServiceNotAuthorizedException e) {
  78. // expected not authorized
  79. }
  80. }
  81. @Test
  82. public void testCreate_AuthUser() throws ServiceNotEnabledException,
  83. ServiceNotAuthorizedException {
  84. ReceivePack rp;
  85. rp = factory.create(new R("bob", "1.2.3.4"), db);
  86. assertNotNull("have ReceivePack", rp);
  87. assertSame(db, rp.getRepository());
  88. PersonIdent id = rp.getRefLogIdent();
  89. assertNotNull(id);
  90. assertEquals("bob", id.getName());
  91. assertEquals("bob@1.2.3.4", id.getEmailAddress());
  92. // Should have inherited off the current system, which is mocked
  93. assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
  94. assertEquals(author.getWhen(), id.getWhen());
  95. }
  96. @Test
  97. public void testCreate_Disabled() throws ServiceNotAuthorizedException,
  98. IOException {
  99. final StoredConfig cfg = db.getConfig();
  100. cfg.setBoolean("http", null, "receivepack", false);
  101. cfg.save();
  102. try {
  103. factory.create(new R(null, "localhost"), db);
  104. fail("Created session for anonymous user: null");
  105. } catch (ServiceNotEnabledException e) {
  106. // expected not authorized
  107. }
  108. try {
  109. factory.create(new R("", "localhost"), db);
  110. fail("Created session for anonymous user: \"\"");
  111. } catch (ServiceNotEnabledException e) {
  112. // expected not authorized
  113. }
  114. try {
  115. factory.create(new R("bob", "localhost"), db);
  116. fail("Created session for user: \"bob\"");
  117. } catch (ServiceNotEnabledException e) {
  118. // expected not authorized
  119. }
  120. }
  121. @Test
  122. public void testCreate_Enabled() throws ServiceNotEnabledException,
  123. ServiceNotAuthorizedException, IOException {
  124. final StoredConfig cfg = db.getConfig();
  125. cfg.setBoolean("http", null, "receivepack", true);
  126. cfg.save();
  127. ReceivePack rp;
  128. rp = factory.create(new R(null, "1.2.3.4"), db);
  129. assertNotNull("have ReceivePack", rp);
  130. assertSame(db, rp.getRepository());
  131. PersonIdent id = rp.getRefLogIdent();
  132. assertNotNull(id);
  133. assertEquals("anonymous", id.getName());
  134. assertEquals("anonymous@1.2.3.4", id.getEmailAddress());
  135. // Should have inherited off the current system, which is mocked
  136. assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
  137. assertEquals(author.getWhen(), id.getWhen());
  138. rp = factory.create(new R("bob", "1.2.3.4"), db);
  139. assertNotNull("have ReceivePack", rp);
  140. }
  141. private static final class R extends HttpServletRequestWrapper {
  142. private final String user;
  143. private final String host;
  144. R(String user, String host) {
  145. super(new Request(null, null) /* can't pass null, sigh */);
  146. this.user = user;
  147. this.host = host;
  148. }
  149. @Override
  150. public String getRemoteHost() {
  151. return host;
  152. }
  153. @Override
  154. public String getRemoteUser() {
  155. return user;
  156. }
  157. }
  158. }