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  	@Override
73  	@Before
74  	public void setUp() throws Exception {
75  		super.setUp();
76  
77  		db = createBareRepository();
78  		factory = new DefaultUploadPackFactory();
79  	}
80  
81  	@SuppressWarnings("unchecked")
82  	@Test
83  	public void testDisabledSingleton() throws ServiceNotAuthorizedException {
84  		factory = (UploadPackFactory<HttpServletRequest>) UploadPackFactory.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_Default() throws ServiceNotEnabledException,
110 			ServiceNotAuthorizedException {
111 		UploadPack up;
112 
113 		up = factory.create(new R(null, "1.2.3.4"), db);
114 		assertNotNull("have UploadPack", up);
115 		assertSame(db, up.getRepository());
116 
117 		up = factory.create(new R("bob", "1.2.3.4"), db);
118 		assertNotNull("have UploadPack", up);
119 		assertSame(db, up.getRepository());
120 	}
121 
122 	@Test
123 	public void testCreate_Disabled() throws ServiceNotAuthorizedException,
124 			IOException {
125 		final StoredConfig cfg = db.getConfig();
126 		cfg.setBoolean("http", null, "uploadpack", false);
127 		cfg.save();
128 
129 		try {
130 			factory.create(new R(null, "localhost"), db);
131 			fail("Created session for anonymous user: null");
132 		} catch (ServiceNotEnabledException e) {
133 			// expected not authorized
134 		}
135 
136 		try {
137 			factory.create(new R("bob", "localhost"), db);
138 			fail("Created session for user: \"bob\"");
139 		} catch (ServiceNotEnabledException e) {
140 			// expected not authorized
141 		}
142 	}
143 
144 	@Test
145 	public void testCreate_Enabled() throws ServiceNotEnabledException,
146 			ServiceNotAuthorizedException {
147 		db.getConfig().setBoolean("http", null, "uploadpack", true);
148 		UploadPack up;
149 
150 		up = factory.create(new R(null, "1.2.3.4"), db);
151 		assertNotNull("have UploadPack", up);
152 		assertSame(db, up.getRepository());
153 
154 		up = factory.create(new R("bob", "1.2.3.4"), db);
155 		assertNotNull("have UploadPack", up);
156 		assertSame(db, up.getRepository());
157 	}
158 
159 	private static final class R extends HttpServletRequestWrapper {
160 		private final String user;
161 
162 		private final String host;
163 
164 		R(String user, String host) {
165 			super(new Request(null, null) /* can't pass null, sigh */);
166 			this.user = user;
167 			this.host = host;
168 		}
169 
170 		@Override
171 		public String getRemoteHost() {
172 			return host;
173 		}
174 
175 		@Override
176 		public String getRemoteUser() {
177 			return user;
178 		}
179 	}
180 }