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  	@Override
75  	@Before
76  	public void setUp() throws Exception {
77  		super.setUp();
78  
79  		db = createBareRepository();
80  		factory = new DefaultReceivePackFactory();
81  	}
82  
83  	@SuppressWarnings("unchecked")
84  	@Test
85  	public void testDisabledSingleton() throws ServiceNotAuthorizedException {
86  		factory = (ReceivePackFactory<HttpServletRequest>) ReceivePackFactory.DISABLED;
87  
88  		try {
89  			factory.create(new R(null, "localhost"), db);
90  			fail("Created session for anonymous user: null");
91  		} catch (ServiceNotEnabledException e) {
92  			// expected not authorized
93  		}
94  
95  		try {
96  			factory.create(new R("", "localhost"), db);
97  			fail("Created session for anonymous user: \"\"");
98  		} catch (ServiceNotEnabledException e) {
99  			// expected not authorized
100 		}
101 
102 		try {
103 			factory.create(new R("bob", "localhost"), db);
104 			fail("Created session for user: \"bob\"");
105 		} catch (ServiceNotEnabledException e) {
106 			// expected not authorized
107 		}
108 	}
109 
110 	@Test
111 	public void testCreate_NullUser() throws ServiceNotEnabledException {
112 		try {
113 			factory.create(new R(null, "localhost"), db);
114 			fail("Created session for anonymous user: null");
115 		} catch (ServiceNotAuthorizedException e) {
116 			// expected not authorized
117 		}
118 	}
119 
120 	@Test
121 	public void testCreate_EmptyStringUser() throws ServiceNotEnabledException {
122 		try {
123 			factory.create(new R("", "localhost"), db);
124 			fail("Created session for anonymous user: \"\"");
125 		} catch (ServiceNotAuthorizedException e) {
126 			// expected not authorized
127 		}
128 	}
129 
130 	@Test
131 	public void testCreate_AuthUser() throws ServiceNotEnabledException,
132 			ServiceNotAuthorizedException {
133 		ReceivePack rp;
134 		rp = factory.create(new R("bob", "1.2.3.4"), db);
135 		assertNotNull("have ReceivePack", rp);
136 		assertSame(db, rp.getRepository());
137 
138 		PersonIdent id = rp.getRefLogIdent();
139 		assertNotNull(id);
140 		assertEquals("bob", id.getName());
141 		assertEquals("bob@1.2.3.4", id.getEmailAddress());
142 
143 		// Should have inherited off the current system, which is mocked
144 		assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
145 		assertEquals(author.getWhen(), id.getWhen());
146 	}
147 
148 	@Test
149 	public void testCreate_Disabled() throws ServiceNotAuthorizedException,
150 			IOException {
151 		final StoredConfig cfg = db.getConfig();
152 		cfg.setBoolean("http", null, "receivepack", false);
153 		cfg.save();
154 
155 		try {
156 			factory.create(new R(null, "localhost"), db);
157 			fail("Created session for anonymous user: null");
158 		} catch (ServiceNotEnabledException e) {
159 			// expected not authorized
160 		}
161 
162 		try {
163 			factory.create(new R("", "localhost"), db);
164 			fail("Created session for anonymous user: \"\"");
165 		} catch (ServiceNotEnabledException e) {
166 			// expected not authorized
167 		}
168 
169 		try {
170 			factory.create(new R("bob", "localhost"), db);
171 			fail("Created session for user: \"bob\"");
172 		} catch (ServiceNotEnabledException e) {
173 			// expected not authorized
174 		}
175 	}
176 
177 	@Test
178 	public void testCreate_Enabled() throws ServiceNotEnabledException,
179 			ServiceNotAuthorizedException, IOException {
180 		final StoredConfig cfg = db.getConfig();
181 		cfg.setBoolean("http", null, "receivepack", true);
182 		cfg.save();
183 
184 		ReceivePack rp;
185 
186 		rp = factory.create(new R(null, "1.2.3.4"), db);
187 		assertNotNull("have ReceivePack", rp);
188 		assertSame(db, rp.getRepository());
189 
190 		PersonIdent id = rp.getRefLogIdent();
191 		assertNotNull(id);
192 		assertEquals("anonymous", id.getName());
193 		assertEquals("anonymous@1.2.3.4", id.getEmailAddress());
194 
195 		// Should have inherited off the current system, which is mocked
196 		assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
197 		assertEquals(author.getWhen(), id.getWhen());
198 
199 		rp = factory.create(new R("bob", "1.2.3.4"), db);
200 		assertNotNull("have ReceivePack", rp);
201 	}
202 
203 	private static final class R extends HttpServletRequestWrapper {
204 		private final String user;
205 
206 		private final String host;
207 
208 		R(String user, String host) {
209 			super(new Request(null, null) /* can't pass null, sigh */);
210 			this.user = user;
211 			this.host = host;
212 		}
213 
214 		@Override
215 		public String getRemoteHost() {
216 			return host;
217 		}
218 
219 		@Override
220 		public String getRemoteUser() {
221 			return user;
222 		}
223 	}
224 }