View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.http.test;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNotNull;
48  import static org.junit.Assert.assertSame;
49  import static org.junit.Assert.fail;
50  
51  import java.io.IOException;
52  
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletRequestWrapper;
55  
56  import org.eclipse.jetty.server.Request;
57  import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
58  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
59  import org.eclipse.jgit.lib.PersonIdent;
60  import org.eclipse.jgit.lib.Repository;
61  import org.eclipse.jgit.lib.StoredConfig;
62  import org.eclipse.jgit.transport.ReceivePack;
63  import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
64  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
65  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
66  import org.junit.Before;
67  import org.junit.Test;
68  
69  public class DefaultReceivePackFactoryTest extends LocalDiskRepositoryTestCase {
70  	private Repository db;
71  
72  	private ReceivePackFactory<HttpServletRequest> factory;
73  
74  	@Before
75  	public void setUp() throws Exception {
76  		super.setUp();
77  
78  		db = createBareRepository();
79  		factory = new DefaultReceivePackFactory();
80  	}
81  
82  	@Test
83  	public void testDisabledSingleton() throws ServiceNotAuthorizedException {
84  		factory = (ReceivePackFactory<HttpServletRequest>) ReceivePackFactory.DISABLED;
85  
86  		try {
87  			factory.create(new R(null, "localhost"), db);
88  			fail("Created session for anonymous user: null");
89  		} catch (ServiceNotEnabledException e) {
90  			// expected not authorized
91  		}
92  
93  		try {
94  			factory.create(new R("", "localhost"), db);
95  			fail("Created session for anonymous user: \"\"");
96  		} catch (ServiceNotEnabledException e) {
97  			// expected not authorized
98  		}
99  
100 		try {
101 			factory.create(new R("bob", "localhost"), db);
102 			fail("Created session for user: \"bob\"");
103 		} catch (ServiceNotEnabledException e) {
104 			// expected not authorized
105 		}
106 	}
107 
108 	@Test
109 	public void testCreate_NullUser() throws ServiceNotEnabledException {
110 		try {
111 			factory.create(new R(null, "localhost"), db);
112 			fail("Created session for anonymous user: null");
113 		} catch (ServiceNotAuthorizedException e) {
114 			// expected not authorized
115 		}
116 	}
117 
118 	@Test
119 	public void testCreate_EmptyStringUser() throws ServiceNotEnabledException {
120 		try {
121 			factory.create(new R("", "localhost"), db);
122 			fail("Created session for anonymous user: \"\"");
123 		} catch (ServiceNotAuthorizedException e) {
124 			// expected not authorized
125 		}
126 	}
127 
128 	@Test
129 	public void testCreate_AuthUser() throws ServiceNotEnabledException,
130 			ServiceNotAuthorizedException {
131 		ReceivePack rp;
132 		rp = factory.create(new R("bob", "1.2.3.4"), db);
133 		assertNotNull("have ReceivePack", rp);
134 		assertSame(db, rp.getRepository());
135 
136 		PersonIdent id = rp.getRefLogIdent();
137 		assertNotNull(id);
138 		assertEquals("bob", id.getName());
139 		assertEquals("bob@1.2.3.4", id.getEmailAddress());
140 
141 		// Should have inherited off the current system, which is mocked
142 		assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
143 		assertEquals(author.getWhen(), id.getWhen());
144 	}
145 
146 	@Test
147 	public void testCreate_Disabled() throws ServiceNotAuthorizedException,
148 			IOException {
149 		final StoredConfig cfg = db.getConfig();
150 		cfg.setBoolean("http", null, "receivepack", false);
151 		cfg.save();
152 
153 		try {
154 			factory.create(new R(null, "localhost"), db);
155 			fail("Created session for anonymous user: null");
156 		} catch (ServiceNotEnabledException e) {
157 			// expected not authorized
158 		}
159 
160 		try {
161 			factory.create(new R("", "localhost"), db);
162 			fail("Created session for anonymous user: \"\"");
163 		} catch (ServiceNotEnabledException e) {
164 			// expected not authorized
165 		}
166 
167 		try {
168 			factory.create(new R("bob", "localhost"), db);
169 			fail("Created session for user: \"bob\"");
170 		} catch (ServiceNotEnabledException e) {
171 			// expected not authorized
172 		}
173 	}
174 
175 	@Test
176 	public void testCreate_Enabled() throws ServiceNotEnabledException,
177 			ServiceNotAuthorizedException, IOException {
178 		final StoredConfig cfg = db.getConfig();
179 		cfg.setBoolean("http", null, "receivepack", true);
180 		cfg.save();
181 
182 		ReceivePack rp;
183 
184 		rp = factory.create(new R(null, "1.2.3.4"), db);
185 		assertNotNull("have ReceivePack", rp);
186 		assertSame(db, rp.getRepository());
187 
188 		PersonIdent id = rp.getRefLogIdent();
189 		assertNotNull(id);
190 		assertEquals("anonymous", id.getName());
191 		assertEquals("anonymous@1.2.3.4", id.getEmailAddress());
192 
193 		// Should have inherited off the current system, which is mocked
194 		assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
195 		assertEquals(author.getWhen(), id.getWhen());
196 
197 		rp = factory.create(new R("bob", "1.2.3.4"), db);
198 		assertNotNull("have ReceivePack", rp);
199 	}
200 
201 	private static final class R extends HttpServletRequestWrapper {
202 		private final String user;
203 
204 		private final String host;
205 
206 		R(final String user, final String host) {
207 			super(new Request(null, null) /* can't pass null, sigh */);
208 			this.user = user;
209 			this.host = host;
210 		}
211 
212 		@Override
213 		public String getRemoteHost() {
214 			return host;
215 		}
216 
217 		@Override
218 		public String getRemoteUser() {
219 			return user;
220 		}
221 	}
222 }