You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecurityManagerMissingPermissionsTest.java 3.7KB

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