<commit_before>package gov.nih.nci.cagrid.gums.idp;

import gov.nih.nci.cagrid.gums.bean.GUMSInternalFault;
import gov.nih.nci.cagrid.gums.ca.CertificateAuthority;
import gov.nih.nci.cagrid.gums.common.Database;
import gov.nih.nci.cagrid.gums.common.FaultUtil;
import gov.nih.nci.cagrid.gums.common.IOUtils;
import gov.nih.nci.cagrid.gums.common.ca.CertUtil;
import gov.nih.nci.cagrid.gums.common.ca.KeyUtil;
import gov.nih.nci.cagrid.gums.test.TestUtils;

import java.io.File;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

import junit.framework.TestCase;

import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.opensaml.InvalidCryptoException;
import org.opensaml.SAMLAssertion;
import org.opensaml.SAMLAttribute;
import org.opensaml.SAMLAttributeStatement;
import org.opensaml.SAMLAuthenticationStatement;
import org.opensaml.SAMLStatement;

/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class TestAssertionCredentialsManager extends TestCase {

	public static String CA_RESOURCES_DIR = "resources" + File.separator
			+ "ca-test";

	private Database db;

	private CertificateAuthority ca;

	private static String TEST_EMAIL = "<EMAIL>";
	private static String TEST_UID = "test";
	
	public void verifySAMLAssertion(SAMLAssertion saml, AssertionCredentialsManager cm)
	throws Exception{
		assertNotNull(saml);
		saml.verify(cm.getIdPCertificate(), false);
			
		try {
			// Test against a bad certificate
			saml.verify(CertUtil.loadCertificate(CA_RESOURCES_DIR
					+ "/bmi-cacert.pem"), false);
			assertTrue(false);
		} catch (InvalidCryptoException ex) {

		}
		assertEquals(cm.getIdPCertificate().getSubjectDN().toString(),saml.getIssuer());
		Iterator itr = saml.getStatements();
		int count = 0;
		boolean emailFound = false;
		boolean authFound = false;
		while(itr.hasNext()){
			count = count+1;
			SAMLStatement stmt = (SAMLStatement) itr.next();
			if(stmt instanceof SAMLAuthenticationStatement){
				if(authFound){
					assertTrue(false);
				}else{
					authFound = true;
				}
				SAMLAuthenticationStatement auth = (SAMLAuthenticationStatement)stmt;
				assertEquals(TEST_UID,auth.getSubject().getName());
				assertEquals("urn:oasis:names:tc:SAML:1.0:am:password",auth.getAuthMethod());
			}
			
			if(stmt instanceof SAMLAttributeStatement){
				if(emailFound){
					assertTrue(false);
				}else{
					emailFound = true;
				}
				SAMLAttributeStatement att = (SAMLAttributeStatement)stmt;
				assertEquals(TEST_UID,att.getSubject().getName());
				Iterator i = att.getAttributes();
				assertTrue(i.hasNext());
				SAMLAttribute a = (SAMLAttribute)i.next();
				assertEquals(AssertionCredentialsManager.EMAIL_NAMESPACE,a.getNamespace());
				assertEquals(AssertionCredentialsManager.EMAIL_NAME,a.getName());
				Iterator vals = a.getValues();
				assertTrue(vals.hasNext());
				String val = (String)vals.next();
				assertEquals(TEST_EMAIL,val);
				assertTrue(!vals.hasNext());
				assertTrue(!i.hasNext());
			}
			
		}
		
		assertEquals(2,count);
		assertTrue(authFound);
		assertTrue(emailFound);
	}
		

	public void testAutoCredentialCreation() {
		try {

			IdPConfiguration conf = getIdpConfigurationAuto();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(true, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());
			AssertionCredentialsManager cm = new AssertionCredentialsManager(
					conf, ca, db);
			X509Certificate cert = cm.getIdPCertificate();
			assertNotNull(cert);
			assertNotNull(cm.getIdPKey());
			String expectedSub = TestUtils.CA_SUBJECT_PREFIX + ",CN="
					+ AssertionCredentialsManager.CA_SUBJECT;
			assertEquals(expectedSub, cert.getSubjectDN().toString());
			SAMLAssertion saml = cm.getAuthenticationAssertion(TEST_UID,TEST_EMAIL);
			verifySAMLAssertion(saml,cm);
			String xml = IOUtils.samlAssertionToString(saml);
			SAMLAssertion saml2 = IOUtils.stringToSAMLAssertion(xml);
			verifySAMLAssertion(saml2,cm);
			
			
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		} 
	}

	public void testAutoCredentialCreationRenew() {
		try {
			IdPConfiguration conf = getIdpConfigurationAuto();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(true, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());
			AssertionCredentialsManager cm = new AssertionCredentialsManager(
					conf, ca, db);
			X509Certificate cert = cm.getIdPCertificate();
			assertNotNull(cert);
			assertNotNull(cm.getIdPKey());
			String expectedSub = TestUtils.CA_SUBJECT_PREFIX + ",CN="
					+ AssertionCredentialsManager.CA_SUBJECT;
			assertEquals(expectedSub, cert.getSubjectDN().toString());

			String subject = cert.getSubjectDN().toString();
			KeyPair pair = KeyUtil.generateRSAKeyPair1024();
			PKCS10CertificationRequest req = CertUtil
					.generateCertficateRequest(subject, pair);
			GregorianCalendar cal = new GregorianCalendar();
			Date start = cal.getTime();
			cal.add(Calendar.SECOND, 2);
			Date end = cal.getTime();

			X509Certificate shortCert = ca.requestCertificate(req, start, end);
			cm.storeCredentials(shortCert, pair.getPrivate(), conf
					.getKeyPassword());
			X509Certificate idpShortCert = cm.getIdPCertificate();
			assertEquals(shortCert,idpShortCert);
			if (cert.equals(idpShortCert)) {
				assertTrue(false);
			}

			Thread.sleep(2500);
			assertTrue(CertUtil.isExpired(idpShortCert));
			X509Certificate renewedCert = cm.getIdPCertificate();
			assertNotNull(renewedCert);

			PrivateKey renewedKey = cm.getIdPKey();
			assertNotNull(renewedKey);

			assertTrue(!CertUtil.isExpired(renewedCert));

			if (renewedCert.equals(idpShortCert)) {
				assertTrue(false);
			}

			if (renewedKey.equals(pair.getPrivate())) {
				assertTrue(false);
			}

			SAMLAssertion saml = cm.getAuthenticationAssertion(TEST_UID,TEST_EMAIL);
			verifySAMLAssertion(saml,cm);
			String xml = IOUtils.samlAssertionToString(saml);
			SAMLAssertion saml2 = IOUtils.stringToSAMLAssertion(xml);
			System.out.println(IOUtils.samlAssertionToString(saml2));
			verifySAMLAssertion(saml2,cm);

		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}

	public void testAutoCredentialCreationNoRenewal() {
		try {
			IdPConfiguration conf = getIdpConfigurationAutoNoRenew();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(false, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.cagrid.gums.idp;

import gov.nih.nci.cagrid.gums.bean.GUMSInternalFault;
import gov.nih.nci.cagrid.gums.ca.CertificateAuthority;
import gov.nih.nci.cagrid.gums.common.Database;
import gov.nih.nci.cagrid.gums.common.FaultUtil;
import gov.nih.nci.cagrid.gums.common.IOUtils;
import gov.nih.nci.cagrid.gums.common.ca.CertUtil;
import gov.nih.nci.cagrid.gums.common.ca.KeyUtil;
import gov.nih.nci.cagrid.gums.test.TestUtils;

import java.io.File;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

import junit.framework.TestCase;

import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.opensaml.InvalidCryptoException;
import org.opensaml.SAMLAssertion;
import org.opensaml.SAMLAttribute;
import org.opensaml.SAMLAttributeStatement;
import org.opensaml.SAMLAuthenticationStatement;
import org.opensaml.SAMLStatement;

/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class TestAssertionCredentialsManager extends TestCase {

	public static String CA_RESOURCES_DIR = "resources" + File.separator
			+ "ca-test";

	private Database db;

	private CertificateAuthority ca;

	private static String TEST_EMAIL = "<EMAIL>";
	private static String TEST_UID = "test";
	
	public void verifySAMLAssertion(SAMLAssertion saml, AssertionCredentialsManager cm)
	throws Exception{
		assertNotNull(saml);
		saml.verify(cm.getIdPCertificate(), false);
			
		try {
			// Test against a bad certificate
			saml.verify(CertUtil.loadCertificate(CA_RESOURCES_DIR
					+ "/bmi-cacert.pem"), false);
			assertTrue(false);
		} catch (InvalidCryptoException ex) {

		}
		assertEquals(cm.getIdPCertificate().getSubjectDN().toString(),saml.getIssuer());
		Iterator itr = saml.getStatements();
		int count = 0;
		boolean emailFound = false;
		boolean authFound = false;
		while(itr.hasNext()){
			count = count+1;
			SAMLStatement stmt = (SAMLStatement) itr.next();
			if(stmt instanceof SAMLAuthenticationStatement){
				if(authFound){
					assertTrue(false);
				}else{
					authFound = true;
				}
				SAMLAuthenticationStatement auth = (SAMLAuthenticationStatement)stmt;
				assertEquals(TEST_UID,auth.getSubject().getName());
				assertEquals("urn:oasis:names:tc:SAML:1.0:am:password",auth.getAuthMethod());
			}
			
			if(stmt instanceof SAMLAttributeStatement){
				if(emailFound){
					assertTrue(false);
				}else{
					emailFound = true;
				}
				SAMLAttributeStatement att = (SAMLAttributeStatement)stmt;
				assertEquals(TEST_UID,att.getSubject().getName());
				Iterator i = att.getAttributes();
				assertTrue(i.hasNext());
				SAMLAttribute a = (SAMLAttribute)i.next();
				assertEquals(AssertionCredentialsManager.EMAIL_NAMESPACE,a.getNamespace());
				assertEquals(AssertionCredentialsManager.EMAIL_NAME,a.getName());
				Iterator vals = a.getValues();
				assertTrue(vals.hasNext());
				String val = (String)vals.next();
				assertEquals(TEST_EMAIL,val);
				assertTrue(!vals.hasNext());
				assertTrue(!i.hasNext());
			}
			
		}
		
		assertEquals(2,count);
		assertTrue(authFound);
		assertTrue(emailFound);
	}
		

	public void testAutoCredentialCreation() {
		try {

			IdPConfiguration conf = getIdpConfigurationAuto();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(true, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());
			AssertionCredentialsManager cm = new AssertionCredentialsManager(
					conf, ca, db);
			X509Certificate cert = cm.getIdPCertificate();
			assertNotNull(cert);
			assertNotNull(cm.getIdPKey());
			String expectedSub = TestUtils.CA_SUBJECT_PREFIX + ",CN="
					+ AssertionCredentialsManager.CA_SUBJECT;
			assertEquals(expectedSub, cert.getSubjectDN().toString());
			SAMLAssertion saml = cm.getAuthenticationAssertion(TEST_UID,TEST_EMAIL);
			verifySAMLAssertion(saml,cm);
			String xml = IOUtils.samlAssertionToString(saml);
			SAMLAssertion saml2 = IOUtils.stringToSAMLAssertion(xml);
			verifySAMLAssertion(saml2,cm);
			
			
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		} 
	}

	public void testAutoCredentialCreationRenew() {
		try {
			IdPConfiguration conf = getIdpConfigurationAuto();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(true, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());
			AssertionCredentialsManager cm = new AssertionCredentialsManager(
					conf, ca, db);
			X509Certificate cert = cm.getIdPCertificate();
			assertNotNull(cert);
			assertNotNull(cm.getIdPKey());
			String expectedSub = TestUtils.CA_SUBJECT_PREFIX + ",CN="
					+ AssertionCredentialsManager.CA_SUBJECT;
			assertEquals(expectedSub, cert.getSubjectDN().toString());

			String subject = cert.getSubjectDN().toString();
			KeyPair pair = KeyUtil.generateRSAKeyPair1024();
			PKCS10CertificationRequest req = CertUtil
					.generateCertficateRequest(subject, pair);
			GregorianCalendar cal = new GregorianCalendar();
			Date start = cal.getTime();
			cal.add(Calendar.SECOND, 2);
			Date end = cal.getTime();

			X509Certificate shortCert = ca.requestCertificate(req, start, end);
			cm.storeCredentials(shortCert, pair.getPrivate(), conf
					.getKeyPassword());
			X509Certificate idpShortCert = cm.getIdPCertificate();
			assertEquals(shortCert,idpShortCert);
			if (cert.equals(idpShortCert)) {
				assertTrue(false);
			}

			Thread.sleep(2500);
			assertTrue(CertUtil.isExpired(idpShortCert));
			X509Certificate renewedCert = cm.getIdPCertificate();
			assertNotNull(renewedCert);

			PrivateKey renewedKey = cm.getIdPKey();
			assertNotNull(renewedKey);

			assertTrue(!CertUtil.isExpired(renewedCert));

			if (renewedCert.equals(idpShortCert)) {
				assertTrue(false);
			}

			if (renewedKey.equals(pair.getPrivate())) {
				assertTrue(false);
			}

			SAMLAssertion saml = cm.getAuthenticationAssertion(TEST_UID,TEST_EMAIL);
			verifySAMLAssertion(saml,cm);
			String xml = IOUtils.samlAssertionToString(saml);
			SAMLAssertion saml2 = IOUtils.stringToSAMLAssertion(xml);
			verifySAMLAssertion(saml2,cm);

		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}

	public void testAutoCredentialCreationNoRenewal() {
		try {
			IdPConfiguration conf = getIdpConfigurationAutoNoRenew();
			assertEquals(true, conf.isAutoCreateAssertingCredentials());
			assertEquals(false, conf.isAutoRenewAssertingCredentials());
			assertEquals(null, conf.getAssertingCertificate());
			assertEquals(null, conf.getAssertingKey());
{'fraction_non_alphanumeric': 0.12034465570034893, 'fraction_numerical': 0.00804671366517126, 'mean_word_length': 5.127399650959861, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '6726173', 'n_tokens_mistral': 5200, 'n_tokens_neox': 4519, 'n_words': 724}