1
2
3
4
5
6
7
8
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
35
36 public class SecurityManagerMissingPermissionsTest extends RepositoryTestCase {
37
38
39
40
41 private final StringWriter errorOutputWriter = new StringWriter();
42
43
44
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
68
69
70
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
101
102
103
104
105
106
107
108
109 private static void refreshPolicyAllPermission(Policy policy)
110 throws IOException {
111
112 String policyString = "grant { permission java.security.AllPermission; };";
113
114
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
127 e.printStackTrace();
128 }
129 }
130 }
131
132 }