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.assertNotNull;
47  import static org.junit.Assert.assertSame;
48  import static org.junit.Assert.fail;
49  
50  import java.io.IOException;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletRequestWrapper;
54  
55  import org.eclipse.jetty.server.Request;
56  import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
57  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
58  import org.eclipse.jgit.lib.Repository;
59  import org.eclipse.jgit.lib.StoredConfig;
60  import org.eclipse.jgit.transport.UploadPack;
61  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
62  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
63  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
64  import org.junit.Before;
65  import org.junit.Test;
66  
67  public class DefaultUploadPackFactoryTest extends LocalDiskRepositoryTestCase {
68  	private Repository db;
69  
70  	private UploadPackFactory<HttpServletRequest> factory;
71  
72  	@Before
73  	public void setUp() throws Exception {
74  		super.setUp();
75  
76  		db = createBareRepository();
77  		factory = new DefaultUploadPackFactory();
78  	}
79  
80  	@Test
81  	public void testDisabledSingleton() throws ServiceNotAuthorizedException {
82  		factory = (UploadPackFactory<HttpServletRequest>) UploadPackFactory.DISABLED;
83  
84  		try {
85  			factory.create(new R(null, "localhost"), db);
86  			fail("Created session for anonymous user: null");
87  		} catch (ServiceNotEnabledException e) {
88  			// expected not authorized
89  		}
90  
91  		try {
92  			factory.create(new R("", "localhost"), db);
93  			fail("Created session for anonymous user: \"\"");
94  		} catch (ServiceNotEnabledException e) {
95  			// expected not authorized
96  		}
97  
98  		try {
99  			factory.create(new R("bob", "localhost"), db);
100 			fail("Created session for user: \"bob\"");
101 		} catch (ServiceNotEnabledException e) {
102 			// expected not authorized
103 		}
104 	}
105 
106 	@Test
107 	public void testCreate_Default() throws ServiceNotEnabledException,
108 			ServiceNotAuthorizedException {
109 		UploadPack up;
110 
111 		up = factory.create(new R(null, "1.2.3.4"), db);
112 		assertNotNull("have UploadPack", up);
113 		assertSame(db, up.getRepository());
114 
115 		up = factory.create(new R("bob", "1.2.3.4"), db);
116 		assertNotNull("have UploadPack", up);
117 		assertSame(db, up.getRepository());
118 	}
119 
120 	@Test
121 	public void testCreate_Disabled() throws ServiceNotAuthorizedException,
122 			IOException {
123 		final StoredConfig cfg = db.getConfig();
124 		cfg.setBoolean("http", null, "uploadpack", false);
125 		cfg.save();
126 
127 		try {
128 			factory.create(new R(null, "localhost"), db);
129 			fail("Created session for anonymous user: null");
130 		} catch (ServiceNotEnabledException e) {
131 			// expected not authorized
132 		}
133 
134 		try {
135 			factory.create(new R("bob", "localhost"), db);
136 			fail("Created session for user: \"bob\"");
137 		} catch (ServiceNotEnabledException e) {
138 			// expected not authorized
139 		}
140 	}
141 
142 	@Test
143 	public void testCreate_Enabled() throws ServiceNotEnabledException,
144 			ServiceNotAuthorizedException {
145 		db.getConfig().setBoolean("http", null, "uploadpack", true);
146 		UploadPack up;
147 
148 		up = factory.create(new R(null, "1.2.3.4"), db);
149 		assertNotNull("have UploadPack", up);
150 		assertSame(db, up.getRepository());
151 
152 		up = factory.create(new R("bob", "1.2.3.4"), db);
153 		assertNotNull("have UploadPack", up);
154 		assertSame(db, up.getRepository());
155 	}
156 
157 	private static final class R extends HttpServletRequestWrapper {
158 		private final String user;
159 
160 		private final String host;
161 
162 		R(final String user, final String host) {
163 			super(new Request(null, null) /* can't pass null, sigh */);
164 			this.user = user;
165 			this.host = host;
166 		}
167 
168 		@Override
169 		public String getRemoteHost() {
170 			return host;
171 		}
172 
173 		@Override
174 		public String getRemoteUser() {
175 			return user;
176 		}
177 	}
178 }