<commit_before>package com.thoughtworks.acceptance;

import com.thoughtworks.acceptance.objects.Software;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.testutil.DynamicSecurityManager;

import junit.framework.TestCase;

import java.io.File;
import java.io.FilePermission;
import java.lang.reflect.ReflectPermission;
import java.security.CodeSource;
import java.security.Policy;


/**
 * Test XStream with an active SecurityManager. Note, that it is intentional, that this test is not
 * derived from AbstractAcceptanceTest to avoid loaded classes before the SecurityManager is in
 * action.
 * 
 * @author <NAME>
 */
public class SecurityManagerTest extends TestCase {

    private XStream xstream;
    private DynamicSecurityManager securityManager;
    private CodeSource defaultCodeSource;
    private File mainClasses;
    private File testClasses;
    private File libs;

    protected void setUp() throws Exception {
        super.setUp();
        System.setSecurityManager(null);
        defaultCodeSource = new CodeSource(null, null);
        mainClasses = new File(
                new File(new File(System.getProperty("user.dir"), "build"), "java"), "-");
        testClasses = new File(
                new File(new File(System.getProperty("user.dir"), "build"), "test"), "-");
        libs = new File(new File(System.getProperty("user.dir"), "lib"), "*");
        securityManager = new DynamicSecurityManager();
        Policy policy = Policy.getPolicy();
        securityManager.setPermissions(defaultCodeSource, policy.getPermissions(defaultCodeSource));
        securityManager.addPermission(
                defaultCodeSource, new RuntimePermission("setSecurityManager"));
    }

    protected void tearDown() throws Exception {
        System.setSecurityManager(null);
        super.tearDown();
    }

    public void testSerializeWithXpp3DriverAndSun14ReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                testClasses.toString(), "read"));
        securityManager.addPermission(
                defaultCodeSource, new FilePermission(libs.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessDeclaredMembers"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessClassInPackage.sun.reflect"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessClassInPackage.sun.misc"));
        securityManager
                .addPermission(defaultCodeSource, new RuntimePermission("createClassLoader"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "reflectionFactoryAccess"));
        securityManager.addPermission(defaultCodeSource, new ReflectPermission(
                "suppressAccessChecks"));
        securityManager.setReadOnly();
        System.setSecurityManager(securityManager);

        xstream = new XStream(new Sun14ReflectionProvider());

        assertBothWays();
    }

    public void testSerializeWithXpp3DriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                testClasses.toString(), "read"));
        securityManager.addPermission(
                defaultCodeSource, new FilePermission(libs.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessDeclaredMembers"));
        securityManager
                .addPermission(defaultCodeSource, new RuntimePermission("createClassLoader"));
        securityManager.addPermission(defaultCodeSource, new ReflectPermission(
                "suppressAccessChecks"));
        securityManager.setReadOnly();
        System.setSecurityManager(securityManager);

        xstream = new XStream(new PureJavaReflectionProvider());

        assertBothWays();
    }

    public void testSerializeWithDomDriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));<commit_msg>Fix for JDK 1.5 and 1.3.<commit_after>package com.thoughtworks.acceptance;

import com.thoughtworks.acceptance.objects.Software;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.testutil.DynamicSecurityManager;

import junit.framework.TestCase;

import java.io.File;
import java.io.FilePermission;
import java.lang.reflect.ReflectPermission;
import java.security.CodeSource;
import java.security.Policy;
import java.security.cert.Certificate;


/**
 * Test XStream with an active SecurityManager. Note, that it is intentional, that this test is not
 * derived from AbstractAcceptanceTest to avoid loaded classes before the SecurityManager is in
 * action.
 * 
 * @author <NAME>
 */
public class SecurityManagerTest extends TestCase {

    private XStream xstream;
    private DynamicSecurityManager securityManager;
    private CodeSource defaultCodeSource;
    private File mainClasses;
    private File testClasses;
    private File libs;

    protected void setUp() throws Exception {
        super.setUp();
        System.setSecurityManager(null);
        defaultCodeSource = new CodeSource(null, (Certificate[])null);
        mainClasses = new File(
                new File(new File(System.getProperty("user.dir"), "build"), "java"), "-");
        testClasses = new File(
                new File(new File(System.getProperty("user.dir"), "build"), "test"), "-");
        libs = new File(new File(System.getProperty("user.dir"), "lib"), "*");
        securityManager = new DynamicSecurityManager();
        Policy policy = Policy.getPolicy();
        securityManager.setPermissions(defaultCodeSource, policy.getPermissions(defaultCodeSource));
        securityManager.addPermission(
                defaultCodeSource, new RuntimePermission("setSecurityManager"));
    }

    protected void tearDown() throws Exception {
        System.setSecurityManager(null);
        super.tearDown();
    }

    public void testSerializeWithXpp3DriverAndSun14ReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                testClasses.toString(), "read"));
        securityManager.addPermission(
                defaultCodeSource, new FilePermission(libs.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessDeclaredMembers"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessClassInPackage.sun.reflect"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessClassInPackage.sun.misc"));
        securityManager
                .addPermission(defaultCodeSource, new RuntimePermission("createClassLoader"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "reflectionFactoryAccess"));
        securityManager.addPermission(defaultCodeSource, new ReflectPermission(
                "suppressAccessChecks"));
        securityManager.setReadOnly();
        System.setSecurityManager(securityManager);

        // uses implicit Sun14ReflectionProvider in JDK >= 1.4, since it has the appropriate rights
        xstream = new XStream();

        assertBothWays();
    }

    public void testSerializeWithXpp3DriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                testClasses.toString(), "read"));
        securityManager.addPermission(
                defaultCodeSource, new FilePermission(libs.toString(), "read"));
        securityManager.addPermission(defaultCodeSource, new RuntimePermission(
                "accessDeclaredMembers"));
        securityManager
                .addPermission(defaultCodeSource, new RuntimePermission("createClassLoader"));
        securityManager.addPermission(defaultCodeSource, new ReflectPermission(
                "suppressAccessChecks"));
        securityManager.setReadOnly();
        System.setSecurityManager(securityManager);

        xstream = new XStream(new PureJavaReflectionProvider());

        assertBothWays();
    }

    public void testSerializeWithDomDriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
        securityManager.addPermission(defaultCodeSource, new FilePermission(
                mainClasses.toString(), "read"));
{'fraction_non_alphanumeric': 0.08987939171473519, 'fraction_numerical': 0.002097535395909806, 'mean_word_length': 3.613449443638123, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6721574', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2198, 'n_words': 465}