<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.oak.spi.security.principal;

import java.lang.reflect.Field;
import java.security.Principal;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;

import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.spi.security.AbstractCompositeConfigurationTest;
import org.apache.jackrabbit.oak.spi.security.ConfigurationBase;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class CompositePrincipalConfigurationTest extends AbstractCompositeConfigurationTest<PrincipalConfiguration> {

    private final Root root = Mockito.mock(Root.class);

    private PrincipalConfiguration principalConfigurationMock;

    @Before
    public void before() {
        compositeConfiguration = new CompositePrincipalConfiguration();
        principalConfigurationMock = Mockito.mock(PrincipalConfiguration.class);
        Mockito.when(principalConfigurationMock.getParameters()).thenReturn(ConfigurationParameters.EMPTY);
    }

    private static void assertSize(int expected, CompositePrincipalProvider pp) throws Exception {
        Field f = CompositePrincipalProvider.class.getDeclaredField("providers");
        f.setAccessible(true);

        List<PrincipalProvider> providers = (List<PrincipalProvider>) f.get(pp);
        assertEquals(expected, providers.size());
    }

    @Test
    public void testEmptyGetPrincipalManager() {
        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testEmptyGetProvider() throws Exception {
        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertFalse(pp instanceof CompositePrincipalProvider);
        assertSame(EmptyPrincipalProvider.INSTANCE, pp);
    }

    @Test
    public void testSingleGetPrincipalManager() {
        PrincipalConfiguration testConfig = new TestPrincipalConfiguration();
        addConfiguration(testConfig);

        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testSingleGetProvider() throws Exception {
        PrincipalConfiguration testConfig = new TestPrincipalConfiguration();
        addConfiguration(testConfig);

        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);

        assertFalse(pp instanceof CompositePrincipalProvider);
        assertEquals(testConfig.getPrincipalProvider(root, NamePathMapper.DEFAULT).getClass(), pp.getClass());
    }

    @Test
    public void testMultipleGetPrincipalManager() {
        addConfiguration(principalConfigurationMock);
        addConfiguration(new TestPrincipalConfiguration());

        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testMultipleGetPrincipalProvider() throws Exception {
        addConfiguration(principalConfigurationMock);
        addConfiguration(new TestPrincipalConfiguration());

        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);

        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);
    }

    @Test
    public void testWithEmptyPrincipalProvider() throws Exception {
        addConfiguration(new TestEmptyConfiguration());
        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertSame(EmptyPrincipalProvider.INSTANCE, pp);

        addConfiguration(new TestPrincipalConfiguration());
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertFalse(pp instanceof CompositePrincipalProvider);

        addConfiguration(principalConfigurationMock);
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);

        addConfiguration(new TestEmptyConfiguration());
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);
    }


    private final class TestPrincipalConfiguration extends ConfigurationBase implements PrincipalConfiguration {

        @Nonnull
        @Override
        public PrincipalManager getPrincipalManager(Root root, NamePathMapper namePathMapper) {
            return new PrincipalManagerImpl(getPrincipalProvider(root, namePathMapper));
        }

        @Nonnull
        @Override
        public PrincipalProvider getPrincipalProvider(Root root, NamePathMapper namePathMapper) {
            return new TestPrincipalProvider();
        }

        @Nonnull
        @Override
        public String getName() {
            return PrincipalConfiguration.NAME;
        }
    }

    private static class TestPrincipalProvider implements PrincipalProvider {

        @CheckForNull
        @Override
        public Principal getPrincipal(@Nonnull String principalName) {
            return null;
        }

        @Nonnull
        @Override
        public Set<Principal> getMembershipPrincipals(@Nonnull Principal principal) {
            return ImmutableSet.of();
        }

        @Nonnull
        @Override
        public Set<? extends Principal> getPrincipals(@Nonnull String userID) {
            return ImmutableSet.of();
        }

        @Nonnull
        @Override
        public Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, int searchType) {
            return Iterators.emptyIterator();
        }

        @Nonnull
        @Override
        public Iterator<? extends Principal> findPrincipals(int searchType) {
            return Iterators.emptyIterator();
        }
    }

    private final class TestEmptyConfiguration extends ConfigurationBase implements PrincipalConfiguration {

        @Nonnull
        @Override
        public PrincipalManager getPrincipalManager(Root root, NamePathMapper namePathMapper) {
            return new PrincipalManagerImpl(getPrincipalProvider(root, namePathMapper));
        }
<commit_msg>OAK-7024: java.security.acl deprecated in Java 10, marked for removal in Java 12<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.oak.spi.security.principal;

import java.lang.reflect.Field;
import java.security.Principal;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.collect.ImmutableSet;

import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.spi.security.AbstractCompositeConfigurationTest;
import org.apache.jackrabbit.oak.spi.security.ConfigurationBase;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class CompositePrincipalConfigurationTest extends AbstractCompositeConfigurationTest<PrincipalConfiguration> {

    private final Root root = Mockito.mock(Root.class);

    private PrincipalConfiguration principalConfigurationMock;

    @Before
    public void before() {
        compositeConfiguration = new CompositePrincipalConfiguration();
        principalConfigurationMock = Mockito.mock(PrincipalConfiguration.class);
        Mockito.when(principalConfigurationMock.getParameters()).thenReturn(ConfigurationParameters.EMPTY);
    }

    private static void assertSize(int expected, CompositePrincipalProvider pp) throws Exception {
        Field f = CompositePrincipalProvider.class.getDeclaredField("providers");
        f.setAccessible(true);

        List<PrincipalProvider> providers = (List<PrincipalProvider>) f.get(pp);
        assertEquals(expected, providers.size());
    }

    @Test
    public void testEmptyGetPrincipalManager() {
        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testEmptyGetProvider() throws Exception {
        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertFalse(pp instanceof CompositePrincipalProvider);
        assertSame(EmptyPrincipalProvider.INSTANCE, pp);
    }

    @Test
    public void testSingleGetPrincipalManager() {
        PrincipalConfiguration testConfig = new TestPrincipalConfiguration();
        addConfiguration(testConfig);

        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testSingleGetProvider() throws Exception {
        PrincipalConfiguration testConfig = new TestPrincipalConfiguration();
        addConfiguration(testConfig);

        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);

        assertFalse(pp instanceof CompositePrincipalProvider);
        assertEquals(testConfig.getPrincipalProvider(root, NamePathMapper.DEFAULT).getClass(), pp.getClass());
    }

    @Test
    public void testMultipleGetPrincipalManager() {
        addConfiguration(principalConfigurationMock);
        addConfiguration(new TestPrincipalConfiguration());

        PrincipalManager pMgr = getComposite().getPrincipalManager(root, NamePathMapper.DEFAULT);
        assertTrue(pMgr instanceof PrincipalManagerImpl);
    }

    @Test
    public void testMultipleGetPrincipalProvider() throws Exception {
        addConfiguration(principalConfigurationMock);
        addConfiguration(new TestPrincipalConfiguration());

        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);

        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);
    }

    @Test
    public void testWithEmptyPrincipalProvider() throws Exception {
        addConfiguration(new TestEmptyConfiguration());
        PrincipalProvider pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertSame(EmptyPrincipalProvider.INSTANCE, pp);

        addConfiguration(new TestPrincipalConfiguration());
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertFalse(pp instanceof CompositePrincipalProvider);

        addConfiguration(principalConfigurationMock);
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);

        addConfiguration(new TestEmptyConfiguration());
        pp = getComposite().getPrincipalProvider(root, NamePathMapper.DEFAULT);
        assertTrue(pp instanceof CompositePrincipalProvider);
        assertSize(2, (CompositePrincipalProvider) pp);
    }


    private final class TestPrincipalConfiguration extends ConfigurationBase implements PrincipalConfiguration {

        @Nonnull
        @Override
        public PrincipalManager getPrincipalManager(Root root, NamePathMapper namePathMapper) {
            return new PrincipalManagerImpl(getPrincipalProvider(root, namePathMapper));
        }

        @Nonnull
        @Override
        public PrincipalProvider getPrincipalProvider(Root root, NamePathMapper namePathMapper) {
            return new TestPrincipalProvider();
        }

        @Nonnull
        @Override
        public String getName() {
            return PrincipalConfiguration.NAME;
        }
    }

    private static class TestPrincipalProvider implements PrincipalProvider {

        @CheckForNull
        @Override
        public Principal getPrincipal(@Nonnull String principalName) {
            return null;
        }

        @Nonnull
        @Override
        public Set<Principal> getMembershipPrincipals(@Nonnull Principal principal) {
            return ImmutableSet.of();
        }

        @Nonnull
        @Override
        public Set<? extends Principal> getPrincipals(@Nonnull String userID) {
            return ImmutableSet.of();
        }

        @Nonnull
        @Override
        public Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, int searchType) {
            return Collections.emptyIterator();
        }

        @Nonnull
        @Override
        public Iterator<? extends Principal> findPrincipals(int searchType) {
            return Collections.emptyIterator();
        }
    }

    private final class TestEmptyConfiguration extends ConfigurationBase implements PrincipalConfiguration {

        @Nonnull
        @Override
        public PrincipalManager getPrincipalManager(Root root, NamePathMapper namePathMapper) {
            return new PrincipalManagerImpl(getPrincipalProvider(root, namePathMapper));
        }

{'fraction_non_alphanumeric': 0.07457345517285449, 'fraction_numerical': 0.0014058406287941721, 'mean_word_length': 3.8109437442360896, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '425971', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3592, 'n_words': 935}