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