View Javadoc
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  
11  package org.eclipse.jgit.http.test;
12  
13  import static org.junit.Assert.fail;
14  
15  import java.io.IOException;
16  
17  import javax.servlet.http.HttpServletRequestWrapper;
18  
19  import org.eclipse.jetty.server.Request;
20  import org.eclipse.jgit.http.server.resolver.AsIsFileService;
21  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
22  import org.eclipse.jgit.lib.Repository;
23  import org.eclipse.jgit.lib.StoredConfig;
24  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
25  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  public class AsIsServiceTest extends LocalDiskRepositoryTestCase {
30  	private Repository db;
31  
32  	private AsIsFileService service;
33  
34  	@Override
35  	@Before
36  	public void setUp() throws Exception {
37  		super.setUp();
38  
39  		db = createBareRepository();
40  		service = new AsIsFileService();
41  	}
42  
43  	@Test
44  	public void testDisabledSingleton() throws ServiceNotAuthorizedException {
45  		service = AsIsFileService.DISABLED;
46  		try {
47  			service.access(new R(null, "1.2.3.4"), db);
48  			fail("Created session for anonymous user: null");
49  		} catch (ServiceNotEnabledException e) {
50  			// expected not authorized
51  		}
52  
53  		try {
54  			service.access(new R("bob", "1.2.3.4"), db);
55  			fail("Created session for user: \"bob\"");
56  		} catch (ServiceNotEnabledException e) {
57  			// expected not authorized
58  		}
59  	}
60  
61  	@Test
62  	public void testCreate_Default() throws ServiceNotEnabledException,
63  			ServiceNotAuthorizedException {
64  		service.access(new R(null, "1.2.3.4"), db);
65  		service.access(new R("bob", "1.2.3.4"), db);
66  	}
67  
68  	@Test
69  	public void testCreate_Disabled() throws ServiceNotAuthorizedException,
70  			IOException {
71  		final StoredConfig cfg = db.getConfig();
72  		cfg.setBoolean("http", null, "getanyfile", false);
73  		cfg.save();
74  
75  		try {
76  			service.access(new R(null, "1.2.3.4"), db);
77  			fail("Created session for anonymous user: null");
78  		} catch (ServiceNotEnabledException e) {
79  			// expected not authorized
80  		}
81  
82  		try {
83  			service.access(new R("bob", "1.2.3.4"), db);
84  			fail("Created session for user: \"bob\"");
85  		} catch (ServiceNotEnabledException e) {
86  			// expected not authorized
87  		}
88  	}
89  
90  	@Test
91  	public void testCreate_Enabled() throws ServiceNotEnabledException,
92  			ServiceNotAuthorizedException {
93  		db.getConfig().setBoolean("http", null, "getanyfile", true);
94  		service.access(new R(null, "1.2.3.4"), db);
95  		service.access(new R("bob", "1.2.3.4"), db);
96  	}
97  
98  	private static final class R extends HttpServletRequestWrapper {
99  		private final String user;
100 
101 		private final String host;
102 
103 		R(String user, String host) {
104 			super(new Request(null, null) /* can't pass null, sigh */);
105 			this.user = user;
106 			this.host = host;
107 		}
108 
109 		@Override
110 		public String getRemoteHost() {
111 			return host;
112 		}
113 
114 		@Override
115 		public String getRemoteUser() {
116 			return user;
117 		}
118 	}
119 }