<commit_before>/*
 * Copyright (c) 2013-2021 Cinchapi Inc.
 *
 * Licensed 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 com.cinchapi.concourse.security;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.cinchapi.common.io.ByteBuffers;
import com.cinchapi.concourse.server.concurrent.Threads;
import com.cinchapi.concourse.server.io.FileSystem;
import com.cinchapi.concourse.test.ConcourseBaseTest;
import com.cinchapi.concourse.test.Variables;
import com.cinchapi.concourse.thrift.AccessToken;
import com.cinchapi.concourse.time.Time;
import com.cinchapi.concourse.util.Random;
import com.cinchapi.concourse.util.TestData;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * Unit tests for {@link com.cinchapi.concourse.security.UserService}.
 *
 * @author <NAME>
 */
public class UserServiceTest extends ConcourseBaseTest {

    private String current = null;
    private UserService service = null;

    @Rule
    public TestRule watcher = new TestWatcher() {

        @Override
        protected void finished(Description desc) {
            FileSystem.deleteFile(current);
        }

        @Override
        protected void starting(Description desc) {
            current = TestData.DATA_DIR + File.separator + Time.now();
            service = UserService.create(current);

        }
    };

    @Test
    public void testDefaultAdminLogin() {
        ByteBuffer username = ByteBuffer.wrap("admin".getBytes());
        ByteBuffer password = ByteBuffer.wrap("<PASSWORD>".getBytes());
        Assert.assertTrue(service.authenticate(username, password));
    }

    @Test
    public void testChangeAdminPassword() {
        ByteBuffer username = ByteBuffer.wrap("admin".getBytes());
        ByteBuffer password = ByteBuffer.wrap("<PASSWORD>".getBytes());
        ByteBuffer newPassword = getSecurePassword();
        service.setPassword(username, newPassword);
        Assert.assertFalse(service.authenticate(username, password));
        Assert.assertTrue(service.authenticate(username, newPassword));
    }

    @Test
    public void testAddUsers() {
        Map<ByteBuffer, ByteBuffer> users = Maps.newHashMap();
        for (int i = 0; i < TestData.getScaleCount(); i++) {
            ByteBuffer username = getAcceptableUsername();
            ByteBuffer password = getSecurePassword();
            users.put(username, password);
            service.create(username, password, Role.ADMIN);
        }
        for (Entry<ByteBuffer, ByteBuffer> entry : users.entrySet()) {
            Assert.assertTrue(
                    service.authenticate(entry.getKey(), entry.getValue()));
        }
    }

    @Test
    public void testAllUsersHaveUniqueUids() {
        Set<ByteBuffer> emptySet = Sets.newHashSet();
        Set<ByteBuffer> users = (Set<ByteBuffer>) addMoreUsers(emptySet,
                service);
        Set<Short> uniqueUids = Sets.newHashSet();
        for (ByteBuffer username : users) {
            short uid = service.getUserId(username);
            Assert.assertFalse(uniqueUids.contains(uid)); // check uniqueness
            uniqueUids.add(uid);
        }
        UserService service2 = UserService.create(current); // simulate<commit_msg>fix test bug<commit_after>/*
 * Copyright (c) 2013-2021 Cinchapi Inc.
 *
 * Licensed 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 com.cinchapi.concourse.security;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.cinchapi.common.io.ByteBuffers;
import com.cinchapi.concourse.server.concurrent.Threads;
import com.cinchapi.concourse.server.io.FileSystem;
import com.cinchapi.concourse.test.ConcourseBaseTest;
import com.cinchapi.concourse.test.Variables;
import com.cinchapi.concourse.thrift.AccessToken;
import com.cinchapi.concourse.time.Time;
import com.cinchapi.concourse.util.Random;
import com.cinchapi.concourse.util.TestData;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * Unit tests for {@link com.cinchapi.concourse.security.UserService}.
 *
 * @author <NAME>
 */
public class UserServiceTest extends ConcourseBaseTest {

    private String current = null;
    private UserService service = null;

    @Rule
    public TestRule watcher = new TestWatcher() {

        @Override
        protected void finished(Description desc) {
            FileSystem.deleteFile(current);
        }

        @Override
        protected void starting(Description desc) {
            current = TestData.DATA_DIR + File.separator + Time.now();
            service = UserService.create(current);

        }
    };

    @Test
    public void testDefaultAdminLogin() {
        ByteBuffer username = ByteBuffer.wrap("admin".getBytes());
        ByteBuffer password = ByteBuffer.wrap("<PASSWORD>".getBytes());
        Assert.assertTrue(service.authenticate(username, password));
    }

    @Test
    public void testChangeAdminPassword() {
        ByteBuffer username = ByteBuffer.wrap("admin".getBytes());
        ByteBuffer password = ByteBuffer.wrap("<PASSWORD>".getBytes());
        ByteBuffer newPassword = get<PASSWORD>();
        service.setPassword(username, newPassword);
        Assert.assertFalse(service.authenticate(username, password));
        Assert.assertTrue(service.authenticate(username, newPassword));
    }

    @Test
    public void testAddUsers() {
        Map<ByteBuffer, ByteBuffer> users = Maps.newHashMap();
        for (int i = 0; i < TestData.getScaleCount(); i++) {
            ByteBuffer username = getAcceptableUsername();
            ByteBuffer password = get<PASSWORD>();
            if(users.putIfAbsent(username, password) == null) {
                service.create(username, password, Role.ADMIN);
            }
        }
        for (Entry<ByteBuffer, ByteBuffer> entry : users.entrySet()) {
            Assert.assertTrue(
                    service.authenticate(entry.getKey(), entry.getValue()));
        }
    }

    @Test
    public void testAllUsersHaveUniqueUids() {
        Set<ByteBuffer> emptySet = Sets.newHashSet();
        Set<ByteBuffer> users = (Set<ByteBuffer>) addMoreUsers(emptySet,
                service);
        Set<Short> uniqueUids = Sets.newHashSet();
        for (ByteBuffer username : users) {
            short uid = service.getUserId(username);
            Assert.assertFalse(uniqueUids.contains(uid)); // check uniqueness
            uniqueUids.add(uid);
        }
        UserService service2 = UserService.create(current); // simulate
{'fraction_non_alphanumeric': 0.10740115718418515, 'fraction_numerical': 0.0033751205400192863, 'mean_word_length': 3.243989769820972, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '5658409', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2266, 'n_words': 558}