View Javadoc
1   /*
2    * Copyright (c) 2019 Alex Jitianu <alex_jitianu@sync.ro> 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  package org.eclipse.jgit.api;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.io.StringWriter;
19  import java.nio.file.Files;
20  import java.nio.file.Path;
21  import java.security.Policy;
22  import java.util.Collections;
23  
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.PatternLayout;
26  import org.apache.log4j.WriterAppender;
27  import org.eclipse.jgit.junit.RepositoryTestCase;
28  import org.eclipse.jgit.util.FileUtils;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   * Tests that using a SecurityManager does not result in errors logged.
35   */
36  public class SecurityManagerMissingPermissionsTest extends RepositoryTestCase {
37  
38  	/**
39  	 * Collects all logging sent to the logging system.
40  	 */
41  	private final StringWriter errorOutputWriter = new StringWriter();
42  
43  	/**
44  	 * Appender to intercept all logging sent to the logging system.
45  	 */
46  	private WriterAppender appender;
47  
48  	private SecurityManager originalSecurityManager;
49  
50  	@Override
51  	@Before
52  	public void setUp() throws Exception {
53  		originalSecurityManager = System.getSecurityManager();
54  
55  		appender = new WriterAppender(
56  				new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN),
57  				errorOutputWriter);
58  
59  		Logger.getRootLogger().addAppender(appender);
60  
61  		refreshPolicyAllPermission(Policy.getPolicy());
62  		System.setSecurityManager(new SecurityManager());
63  		super.setUp();
64  	}
65  
66  	/**
67  	 * If a SecurityManager is active a lot of {@link java.io.FilePermission}
68  	 * errors are thrown and logged while initializing a repository.
69  	 *
70  	 * @throws Exception
71  	 */
72  	@Test
73  	public void testCreateNewRepos_MissingPermissions() throws Exception {
74  		File wcTree = new File(getTemporaryDirectory(),
75  				"CreateNewRepositoryTest_testCreateNewRepos");
76  
77  		File marker = new File(getTemporaryDirectory(), "marker");
78  		Files.write(marker.toPath(), Collections.singletonList("Can write"));
79  		assertTrue("Can write in test directory", marker.isFile());
80  		FileUtils.delete(marker);
81  		assertFalse("Can delete in test direcory", marker.exists());
82  
83  		Git git = Git.init().setBare(false)
84  				.setDirectory(new File(wcTree.getAbsolutePath())).call();
85  
86  		addRepoToClose(git.getRepository());
87  
88  		assertEquals("", errorOutputWriter.toString());
89  	}
90  
91  	@Override
92  	@After
93  	public void tearDown() throws Exception {
94  		System.setSecurityManager(originalSecurityManager);
95  		Logger.getRootLogger().removeAppender(appender);
96  		super.tearDown();
97  	}
98  
99  	/**
100 	 * Refresh the Java Security Policy.
101 	 *
102 	 * @param policy
103 	 *            the policy object
104 	 *
105 	 * @throws IOException
106 	 *             if the temporary file that contains the policy could not be
107 	 *             created
108 	 */
109 	private static void refreshPolicyAllPermission(Policy policy)
110 			throws IOException {
111 		// Starting with an all permissions policy.
112 		String policyString = "grant { permission java.security.AllPermission; };";
113 
114 		// Do not use TemporaryFilesFactory, it will create a dependency cycle
115 		Path policyFile = Files.createTempFile("testpolicy", ".txt");
116 
117 		try {
118 			Files.write(policyFile, Collections.singletonList(policyString));
119 			System.setProperty("java.security.policy",
120 					policyFile.toUri().toURL().toString());
121 			policy.refresh();
122 		} finally {
123 			try {
124 				Files.delete(policyFile);
125 			} catch (IOException e) {
126 				// Do not log; the test tests for no logging having occurred
127 				e.printStackTrace();
128 			}
129 		}
130 	}
131 
132 }