<commit_before>package gov.nih.nci.cagrid.dorian.service.ifs;

import gov.nih.nci.cagrid.common.FaultUtil;
import gov.nih.nci.cagrid.dorian.common.Database;
import gov.nih.nci.cagrid.dorian.ifs.bean.InvalidPasswordFault;
import gov.nih.nci.cagrid.dorian.stubs.DorianInternalFault;
import gov.nih.nci.cagrid.dorian.test.Utils;
import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gridca.common.KeyUtil;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Date;

import junit.framework.TestCase;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.PKCS10CertificationRequest;


/**
 * @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 TestCredentialsManager extends TestCase {

	private static final String TABLE = "test_credentials";

	private static final String CA_USER = "CA";

	private static final String CA_PASSWORD = "<PASSWORD>";

	private Database db;

	private CredentialsManager cred;


	public void testCreateObtainCACredentials() {
		try {
			createAndStoreCA();
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testInsertObtainUserCredentials() {
		try {
			createAndStoreCA();
			createAndStoreUserCredentials("foo", "foobar");
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testDeleteUserCredentials() {
		try {
			createAndStoreCA();
			String user = "user";
			String password = "password";
			createAndStoreUserCredentials(user, password);
			cred.deleteCredentials(user);
			assertFalse(cred.hasCredentials(user));
			try {
				cred.getPrivateKey(user, password);
				assertTrue(false);
			} catch (DorianInternalFault gie) {

			}
			try {
				cred.getCertificate(user);
				assertTrue(false);
			} catch (DorianInternalFault gie) {

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


	public void testInsertObtainManyUserCredentials() {
		try {
			createAndStoreCA();
			for (int i = 0; i < 10; i++) {
				createAndStoreUserCredentials("user" + i, "password" + i);
			}
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testDeleteManyUserCredentials() {
		try {
			createAndStoreCA();
			for (int i = 0; i < 10; i++) {
				createAndStoreUserCredentials("user" + i, "password" + i);
			}

			for (int i = 0; i < 10; i++) {
				cred.deleteCredentials("user" + i);
				assertFalse(cred.hasCredentials("user" + i));
				try {
					cred.getPrivateKey("user" + i, "password" + i);
					assertTrue(false);
				} catch (DorianInternalFault gie) {

				}
				try {
					cred.getCertificate("user" + i);
					assertTrue(false);
				} catch (DorianInternalFault gie) {

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


	public void testInsertAndObtainBadPassword() {
		try {
			createAndStoreCA();
			createAndStoreUserCredentials("foo", "foobar");
			cred.getPrivateKey("foo", "foobad");
			fail("Should have thrown InvalidPasswordFault!");
		} catch (InvalidPasswordFault ipf) {
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	private void createAndStoreCA() throws Exception {
		KeyPair rootPair = KeyUtil.generateRSAKeyPair1024();
		assertNotNull(rootPair);
		String rootSub = "O=Ohio State University,OU=BMI,OU=MSCL,CN=Temp Certificate Authority";
		X509Name rootSubject = new X509Name(rootSub);
		X509Certificate root = CertUtil.generateCACertificate(rootSubject, new Date(System.currentTimeMillis()),
			new Date(System.currentTimeMillis() + 500000000), rootPair);
		assertNotNull(root);<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.cagrid.dorian.service.ifs;

import gov.nih.nci.cagrid.common.FaultUtil;
import gov.nih.nci.cagrid.dorian.common.Database;
import gov.nih.nci.cagrid.dorian.ifs.bean.InvalidPasswordFault;
import gov.nih.nci.cagrid.dorian.stubs.DorianInternalFault;
import gov.nih.nci.cagrid.dorian.test.Utils;
import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gridca.common.KeyUtil;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Date;

import junit.framework.TestCase;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.PKCS10CertificationRequest;


/**
 * @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 TestCredentialsManager extends TestCase {

	private static final String TABLE = "test_credentials";

	private static final String CA_USER = "CA";

	private static final String CA_PASSWORD = "<PASSWORD>";

	private Database db;

	private CredentialsManager cred;


	public void testCreateObtainCACredentials() {
		try {
			createAndStoreCA();
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testInsertObtainUserCredentials() {
		try {
			createAndStoreCA();
			createAndStoreUserCredentials("foo", "foobar");
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testDeleteUserCredentials() {
		try {
			createAndStoreCA();
			String user = "user";
			String password = "password";
			createAndStoreUserCredentials(user, password);
			cred.deleteCredentials(user);
			assertFalse(cred.hasCredentials(user));
			try {
				cred.getPrivateKey(user, password);
				assertTrue(false);
			} catch (DorianInternalFault gie) {

			}
			try {
				cred.getCertificate(user);
				assertTrue(false);
			} catch (DorianInternalFault gie) {

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


	public void testInsertObtainManyUserCredentials() {
		try {
			createAndStoreCA();
			for (int i = 0; i < 10; i++) {
				createAndStoreUserCredentials("user" + i, "password" + i);
			}
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}


	public void testDeleteManyUserCredentials() {
		try {
			createAndStoreCA();
			for (int i = 0; i < 10; i++) {
				createAndStoreUserCredentials("user" + i, "password" + i);
			}

			for (int i = 0; i < 10; i++) {
				cred.deleteCredentials("user" + i);
				assertFalse(cred.hasCredentials("user" + i));
				try {
					cred.getPrivateKey("user" + i, "password" + i);
					assertTrue(false);
				} catch (DorianInternalFault gie) {

				}
				try {
					cred.getCertificate("user" + i);
					assertTrue(false);
				} catch (DorianInternalFault gie) {

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

/*
	public void testInsertAndObtainBadPassword() {
		try {
			createAndStoreCA();
			createAndStoreUserCredentials("foo", "foobar");
			cred.getPrivateKey("foo", "foobad");
			fail("Should have thrown InvalidPasswordFault!");
		} catch (InvalidPasswordFault ipf) {
		} catch (Exception e) {
			FaultUtil.printFault(e);
			assertTrue(false);
		}
	}
*/

	private void createAndStoreCA() throws Exception {
		KeyPair rootPair = KeyUtil.generateRSAKeyPair1024();
		assertNotNull(rootPair);
		String rootSub = "O=Ohio State University,OU=BMI,OU=MSCL,CN=Temp Certificate Authority";
		X509Name rootSubject = new X509Name(rootSub);
		X509Certificate root = CertUtil.generateCACertificate(rootSubject, new Date(System.currentTimeMillis()),
			new Date(System.currentTimeMillis() + 500000000), rootPair);
		assertNotNull(root);
{'fraction_non_alphanumeric': 0.13355464759959143, 'fraction_numerical': 0.015066394279877425, 'mean_word_length': 4.190854870775348, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6351846', 'n_tokens_mistral': 3135, 'n_tokens_neox': 2691, 'n_words': 511}