<commit_before>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.xpack.core.XPackClientPlugin;
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition.FieldGrantExcludeGroup;
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege;
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges.ManageApplicationPrivileges;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static java.util.Collections.emptySet;
import static org.hamcrest.Matchers.equalTo;

public class GetUserPrivilegesResponseTests extends ESTestCase {

    public void testSerialization() throws IOException {
        final GetUserPrivilegesResponse original = randomResponse();

        final BytesStreamOutput out = new BytesStreamOutput();
        original.writeTo(out);

        final GetUserPrivilegesResponse copy = new GetUserPrivilegesResponse();
        final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables());
        StreamInput in = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(out.bytes())), registry);
        copy.readFrom(in);

        assertThat(copy.getClusterPrivileges(), equalTo(original.getClusterPrivileges()));
        assertThat(copy.getConditionalClusterPrivileges(), equalTo(original.getConditionalClusterPrivileges()));
        assertThat(sorted(copy.getIndexPrivileges()), equalTo(sorted(original.getIndexPrivileges())));
        assertThat(copy.getApplicationPrivileges(), equalTo(original.getApplicationPrivileges()));
        assertThat(copy.getRunAs(), equalTo(original.getRunAs()));
    }

    public void testEqualsAndHashCode() throws IOException {
        final GetUserPrivilegesResponse response = randomResponse();
        final EqualsHashCodeTestUtils.CopyFunction<GetUserPrivilegesResponse> copy = original -> new GetUserPrivilegesResponse(
            original.getClusterPrivileges(),
            original.getConditionalClusterPrivileges(),
            original.getIndexPrivileges(),
            original.getApplicationPrivileges(),
            original.getRunAs()
        );
        final EqualsHashCodeTestUtils.MutateFunction<GetUserPrivilegesResponse> mutate =
            new EqualsHashCodeTestUtils.MutateFunction<GetUserPrivilegesResponse>() {
                @Override
                public GetUserPrivilegesResponse mutate(GetUserPrivilegesResponse original) {
                    final int random = randomIntBetween(1, 0b11111);
                    final Set<String> cluster = maybeMutate(random, 1, original.getClusterPrivileges(), () -> randomAlphaOfLength(5));
                    final Set<ConditionalClusterPrivilege> conditionalCluster = maybeMutate(random, 2,
                        original.getConditionalClusterPrivileges(), () -> new ManageApplicationPrivileges(randomStringSet(3)));
                    final Set<GetUserPrivilegesResponse.Indices> index = maybeMutate(random, 3, original.getIndexPrivileges(),
                        () -> new GetUserPrivilegesResponse.Indices(randomStringSet(1), randomStringSet(1), emptySet(), emptySet()));
                    final Set<ApplicationResourcePrivileges> application = maybeMutate(random, 4, original.getApplicationPrivileges(),
                        () -> ApplicationResourcePrivileges.builder().resources(generateRandomStringArray(3, 3, false, false))
                            .application(randomAlphaOfLength(5)).privileges(generateRandomStringArray(3, 5, false, false)).build());
                    final Set<String> runAs = maybeMutate(random, 5, original.getRunAs(), () -> randomAlphaOfLength(8));
                    return new GetUserPrivilegesResponse(cluster, conditionalCluster, index, application, runAs);
                }

                private <T> Set<T> maybeMutate(int random, int index, Set<T> original, Supplier<T> supplier) {
                    if ((random & (1 << index)) == 0) {
                        return original;
                    }
                    if (original.isEmpty()) {
                        return Collections.singleton(supplier.get());
                    } else {
                        return emptySet();
                    }
                }
            };
        EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, copy, mutate);
    }

    private GetUserPrivilegesResponse randomResponse() {
        final Set<String> cluster = randomStringSet(5);
        final Set<ConditionalClusterPrivilege> conditionalCluster = Sets.newHashSet(randomArray(3, ConditionalClusterPrivilege[]::new,
            () -> new ManageApplicationPrivileges(
                randomStringSet(3)<commit_msg>[TEST] Fix bit indexing in test mutator<commit_after>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.xpack.core.XPackClientPlugin;
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition.FieldGrantExcludeGroup;
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege;
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges.ManageApplicationPrivileges;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static java.util.Collections.emptySet;
import static org.hamcrest.Matchers.equalTo;

public class GetUserPrivilegesResponseTests extends ESTestCase {

    public void testSerialization() throws IOException {
        final GetUserPrivilegesResponse original = randomResponse();

        final BytesStreamOutput out = new BytesStreamOutput();
        original.writeTo(out);

        final GetUserPrivilegesResponse copy = new GetUserPrivilegesResponse();
        final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables());
        StreamInput in = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(out.bytes())), registry);
        copy.readFrom(in);

        assertThat(copy.getClusterPrivileges(), equalTo(original.getClusterPrivileges()));
        assertThat(copy.getConditionalClusterPrivileges(), equalTo(original.getConditionalClusterPrivileges()));
        assertThat(sorted(copy.getIndexPrivileges()), equalTo(sorted(original.getIndexPrivileges())));
        assertThat(copy.getApplicationPrivileges(), equalTo(original.getApplicationPrivileges()));
        assertThat(copy.getRunAs(), equalTo(original.getRunAs()));
    }

    public void testEqualsAndHashCode() throws IOException {
        final GetUserPrivilegesResponse response = randomResponse();
        final EqualsHashCodeTestUtils.CopyFunction<GetUserPrivilegesResponse> copy = original -> new GetUserPrivilegesResponse(
            original.getClusterPrivileges(),
            original.getConditionalClusterPrivileges(),
            original.getIndexPrivileges(),
            original.getApplicationPrivileges(),
            original.getRunAs()
        );
        final EqualsHashCodeTestUtils.MutateFunction<GetUserPrivilegesResponse> mutate =
            new EqualsHashCodeTestUtils.MutateFunction<GetUserPrivilegesResponse>() {
                @Override
                public GetUserPrivilegesResponse mutate(GetUserPrivilegesResponse original) {
                    final int random = randomIntBetween(1, 0b11111);
                    final Set<String> cluster = maybeMutate(random, 0, original.getClusterPrivileges(), () -> randomAlphaOfLength(5));
                    final Set<ConditionalClusterPrivilege> conditionalCluster = maybeMutate(random, 1,
                        original.getConditionalClusterPrivileges(), () -> new ManageApplicationPrivileges(randomStringSet(3)));
                    final Set<GetUserPrivilegesResponse.Indices> index = maybeMutate(random, 2, original.getIndexPrivileges(),
                        () -> new GetUserPrivilegesResponse.Indices(randomStringSet(1), randomStringSet(1), emptySet(), emptySet()));
                    final Set<ApplicationResourcePrivileges> application = maybeMutate(random, 3, original.getApplicationPrivileges(),
                        () -> ApplicationResourcePrivileges.builder().resources(generateRandomStringArray(3, 3, false, false))
                            .application(randomAlphaOfLength(5)).privileges(generateRandomStringArray(3, 5, false, false)).build());
                    final Set<String> runAs = maybeMutate(random, 4, original.getRunAs(), () -> randomAlphaOfLength(8));
                    return new GetUserPrivilegesResponse(cluster, conditionalCluster, index, application, runAs);
                }

                private <T> Set<T> maybeMutate(int random, int index, Set<T> original, Supplier<T> supplier) {
                    if ((random & (1 << index)) == 0) {
                        return original;
                    }
                    if (original.isEmpty()) {
                        return Collections.singleton(supplier.get());
                    } else {
                        return emptySet();
                    }
                }
            };
        EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, copy, mutate);
    }

    private GetUserPrivilegesResponse randomResponse() {
        final Set<String> cluster = randomStringSet(5);
        final Set<ConditionalClusterPrivilege> conditionalCluster = Sets.newHashSet(randomArray(3, ConditionalClusterPrivilege[]::new,
            () -> new ManageApplicationPrivileges(
                randomStringSet(3)
{'fraction_non_alphanumeric': 0.0926381373995737, 'fraction_numerical': 0.004426955238563699, 'mean_word_length': 3.654330408241129, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3322644', 'n_tokens_mistral': 3131, 'n_tokens_neox': 3091, 'n_words': 563}