<commit_before>/*******************************************************************************
 * Copyright (c) 2013 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.security;

import static org.csstudio.utility.test.HamcrestMatchers.greaterThan;
import static org.csstudio.utility.test.HamcrestMatchers.greaterThanOrEqualTo;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import java.security.Principal;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;

import org.csstudio.security.authentication.UnattendedCallbackHandler;
import org.junit.Before;
import org.junit.Test;

import com.sun.security.auth.callback.TextCallbackHandler;

/** Console (text-based) JUnit demo of various login methods
 * 
 *  <p>Does not require Plug-In test
 *  
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class LoginDemo
{
    final private static String USER_NAME = "fred";
    
    @Before
    public void setup()
    {
        System.setProperty("java.security.auth.login.config", "jaas.conf");
    }
    
    /** Should work on all Linux (Mac OS) systems */
    @Test
    public void systemDemo() throws Exception
    {
        // Use UnattendedCallbackHandler without name, password,
        // because that should not be required
        final String jaas_config =
            System.getProperty("os.name").contains("Windows")
            ? "windows"
            : "unix";
        final LoginContext login =
            new LoginContext(jaas_config, new UnattendedCallbackHandler());
        login.login();
        final Subject subject = login.getSubject();
        
        System.out.println(subject);
        final String current_user = System.getProperty("user.name");
        boolean got_user = false;
        for (Principal p : subject.getPrincipals())
            if (p.getName().equals(current_user))
                got_user = true;
        
        assertThat(subject.getPrincipals().size(), greaterThanOrEqualTo(1));
        assertThat(got_user, equalTo(true));
    }

    /** Should work as long as passwords.conf isn't changed */
    @Test<commit_msg>o.c.sec: Actually tried the Windows (NTLoginModule) login<commit_after>/*******************************************************************************
 * Copyright (c) 2013 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.security;

import static org.csstudio.utility.test.HamcrestMatchers.greaterThan;
import static org.csstudio.utility.test.HamcrestMatchers.greaterThanOrEqualTo;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import java.security.Principal;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;

import org.csstudio.security.authentication.UnattendedCallbackHandler;
import org.junit.Before;
import org.junit.Test;

import com.sun.security.auth.callback.TextCallbackHandler;

/** Console (text-based) JUnit demo of various login methods
 * 
 *  <p>Does not require Plug-In test
 *  
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class LoginDemo
{
    final private static String USER_NAME = "fred";
    
    @Before
    public void setup()
    {
        System.setProperty("java.security.auth.login.config", "jaas.conf");
    }
    
    /** Should work on all Linux (Mac OS) systems */
    @Test
    public void systemDemo() throws Exception
    {
        // Use UnattendedCallbackHandler without name, password,
        // because that should not be required
        final String jaas_config =
            System.getProperty("os.name").contains("Windows")
            ? "windows"
            : "unix";
        final LoginContext login =
            new LoginContext(jaas_config, new UnattendedCallbackHandler());
        login.login();
        final Subject subject = login.getSubject();
        
        System.out.println(subject);
        final String current_user = System.getProperty("user.name");
        boolean got_user = false;
        for (Principal p : subject.getPrincipals())
            if (p.getName().equals(current_user))
                got_user = true;
        
        assertThat(subject.getPrincipals().size(), greaterThanOrEqualTo(1));
        assertThat(got_user, equalTo(true));
        
        System.out.println("Primary user name: " +
        		SecuritySupport.getSubjectName(subject));
    }

    /** Should work as long as passwords.conf isn't changed */
    @Test
{'fraction_non_alphanumeric': 0.15648256952604778, 'fraction_numerical': 0.0035252643948296123, 'mean_word_length': 3.3389974511469838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5834168', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1332, 'n_words': 347}