package be.iminds.ilabt.jfed.lowlevel;

import be.iminds.ilabt.jfed.util.KeyUtil;
import org.testng.annotations.Test;

import java.security.cert.X509Certificate;
import java.util.List;

import static org.testng.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class AnyCredentialTest {
    @Test
    public void testCreateAutoDetect() throws CredentialException {
        AnyCredential cred = AnyCredential.createAutoDetect("test", ABAC_SPEAKSFOR_EXAMPLE_CREDENTIAL);

        assertNotNull(cred);
        assertThat(cred, instanceOf(AbacCredential.class));
    }

    @Test
    public void testIsSpeaksFor() throws CredentialException {
        AnyCredential cred = AnyCredential.createAutoDetect("test", ABAC_SPEAKSFOR_EXAMPLE_CREDENTIAL);

        assertThat(cred.isSpeaksFor(), equalTo(true));
    }

    @Test
    public void testGetSignerCertificates() throws CredentialException {
        AnyCredential cred = AnyCredential.createAutoDetect("test", ABAC_SPEAKSFOR_EXAMPLE_CREDENTIAL);

        List<X509Certificate> x509CertificateList = cred.getSignerCertificates();

        assertThat(x509CertificateList, is(notNullValue()));
        assertThat(x509CertificateList, hasSize(1));

        X509Certificate expectedCert = KeyUtil.pemToX509Certificate(SIGNER_CERT_PEM);
        assertThat(x509CertificateList.get(0), is(equalTo(expectedCert)));
    }


    private static final String ABAC_SPEAKSFOR_EXAMPLE_CREDENTIAL = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><signed-credential xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.protogeni.net/resources/credential/credential.xsd\" xsi:schemaLocation=\"http://www.protogeni.net/resources/credential/ext/policy/1 http://www.protogeni.net/resources/credential/ext/policy/1/policy.xsd\">\n" +
            "  <credential xml:id=\"_0\">\n" +
            "\n" +
            "\t<type>abac</type>\n" +
            "\t<serial/>\n" +
            "\t<owner_gid/>\n" +
            "\t<target_gid/>\n" +
            "\t<uuid/>\n" +
            "\t<expires>2015-10-09T12:04:32.309Z</expires>\n" +
            "\t<abac>\n" +
            "\t    <rt0>\n" +
            "\t\t<version>1.1</version>\n" +
            "\t\t<head>\n" +
            "   <ABACprincipal><keyid>a681ca4082c1301703fee2d62d844664675bc5f0</keyid></ABACprincipal>\n" +
            "   <role>speaks_for_a681ca4082c1301703fee2d62d844664675bc5f0</role>\n" +
            "</head>\n" +
            "<tail>\n" +
            "   <ABACprincipal><keyid>193a45bb42745506c87ad182aee2f5017a56a1cf</keyid></ABACprincipal>\n" +
            "</tail>\n" +
            "\n" +
            "\t    </rt0>\n" +
            "\t</abac>\n" +
            "\n" +
            "</credential>\n" +
            "  <signatures>\n" +
            "  <Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><Reference URI=\"#_0\"><Transforms><Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>uzjP0RfmXdSXw+XbRZ8fkuYGdMM=</DigestValue></Reference></SignedInfo><SignatureValue>fPkr2TPp+AYHu4+mz0/9fmGg+dFwsXuA60TAtAamFoTENAG8XOtqESFgWZ8cASiQBCPnxfHkz6BypxnJ9y0DntKP4mOIjMzoy6Ng3JUtjC0dCuBBWlHt96BsqZOKwmr5oTaxmCGNrmcCEyhE5W4LFFtvu0XfDxgAp1i9TAGPt7g=</SignatureValue><KeyInfo>\n" +
            "<KeyValue>\n" +
            "<RSAKeyValue>\n" +
            "<Modulus>ALY7An8l4nVLh+sCeQNT3V9xrW8QLSKnQlepD7deLUivUXnHNxSds07Ow/yPiiZq\n" +
            "fYNFslEA5F51PILPWGmsG8gzTVeWpAdaYIuz9agKyXD7rZK4ZGHcR8V+MVX0GRyh\n" +
            "SzD/OkX/eKW79YkejaLgZLrThkYi5pCom6HGKl9UVF0r\n" +
            "</Modulus>\n" +
            "<Exponent>AQAB</Exponent>\n" +
            "</RSAKeyValue></KeyValue>\n" +
            "<X509Data>\n" +
            "<X509Certificate>MIIEDDCCA3WgAwIBAgIDAOUHMA0GCSqGSIb3DQEBBAUAMIG1MQswCQYDVQQGEwJC\n" +
            "RTELMAkGA1UECBMCT1YxDjAMBgNVBAcTBUdoZW50MRgwFgYDVQQKEw9pTWluZHMg\n" +
            "LSBpbGFiLnQxHjAcBgNVBAsTFUNlcnRpZmljYXRlIEF1dGhvcml0eTEjMCEGA1UE\n" +
            "AxMaYm9zcy53YWxsMi5pbGFidC5pbWluZHMuYmUxKjAoBgkqhkiG9w0BCQEWG3Z3\n" +
            "YWxsLW9wc0BhdGxhbnRpcy51Z2VudC5iZTAeFw0xNDA5MjUxMjI5MzRaFw0xNTA5\n" +
            "MjUxMjI5MzRaMIGyMQswCQYDVQQGEwJCRTELMAkGA1UECBMCT1YxGDAWBgNVBAoT\n" +
            "D2lNaW5kcyAtIGlsYWIudDEeMBwGA1UECxMVaW1pbmRzLXdhbGwyLnd2ZGVtZWVy\n" +
            "MS0wKwYDVQQDEyRiNzU2MTljYy0xNmI5LTExZTMtOTY2YS0wMDE1MTdiZWNkYzEx\n" +
            "LTArBgkqhkiG9w0BCQEWHnd2ZGVtZWVyQHdhbGwyLmlsYWJ0LmltaW5kcy5iZTCB\n" +
            "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtjsCfyXidUuH6wJ5A1PdX3GtbxAt\n" +
            "IqdCV6kPt14tSK9Recc3FJ2zTs7D/I+KJmp9g0WyUQDkXnU8gs9YaawbyDNNV5ak\n" +
            "B1pgi7P1qArJcPutkrhkYdxHxX4xVfQZHKFLMP86Rf94pbv1iR6NouBkutOGRiLm\n" +
            "kKibocYqX1RUXSsCAwEAAaOCASkwggElMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYE\n" +
            "FKaBykCCwTAXA/7i1i2ERmRnW8XwMIGQBgNVHREEgYgwgYWGNHVybjpwdWJsaWNp\n" +
            "ZDpJRE4rd2FsbDIuaWxhYnQuaW1pbmRzLmJlK3VzZXIrd3ZkZW1lZXKBHnd2ZGVt\n" +
            "ZWVyQHdhbGwyLmlsYWJ0LmltaW5kcy5iZYYtdXJuOnV1aWQ6Yjc1NjE5Y2MtMTZi\n" +
            "OS0xMWUzLTk2NmEtMDAxNTE3YmVjZGMxMGMGCCsGAQUFBwEBBFcwVTBTBhRpg8yT\n" +
            "gKiYzKjHvbGngICqrteKG4Y7aHR0cHM6Ly93d3cud2FsbDIuaWxhYnQuaW1pbmRz\n" +
            "LmJlOjEyMzY5L3Byb3RvZ2VuaS94bWxycGMvc2EwDQYJKoZIhvcNAQEEBQADgYEA\n" +
            "mJzek8GZc5YmBcmn1RLpEFhX0cLmsrmCnQXcpHJCzH4lhUiH5O1f1mN+2X7idKbS\n" +
            "vmiQh8jKbn3jRalQOZ6kpn0XQsPnCJ4t5r7cP2p6fshHdMT11jwLkmlAR+wVlRjo\n" +
            "8jDDT8yO5vJ9Db5D3mdeeg7DQm4fl9cHunvjGpNxhhc=\n" +
            "</X509Certificate>\n" +
            "</X509Data></KeyInfo></Signature></signatures>\n" +
            "</signed-credential>";

    /**signer cert in PEM format of ABAC_SPEAKSFOR_EXAMPLE_CREDENTIAL*/
    private final String SIGNER_CERT_PEM =
            "-----BEGIN CERTIFICATE-----"+
            "MIIEDDCCA3WgAwIBAgIDAOUHMA0GCSqGSIb3DQEBBAUAMIG1MQswCQYDVQQGEwJC\n" +
            "RTELMAkGA1UECBMCT1YxDjAMBgNVBAcTBUdoZW50MRgwFgYDVQQKEw9pTWluZHMg\n" +
            "LSBpbGFiLnQxHjAcBgNVBAsTFUNlcnRpZmljYXRlIEF1dGhvcml0eTEjMCEGA1UE\n" +
            "AxMaYm9zcy53YWxsMi5pbGFidC5pbWluZHMuYmUxKjAoBgkqhkiG9w0BCQEWG3Z3\n" +
            "YWxsLW9wc0BhdGxhbnRpcy51Z2VudC5iZTAeFw0xNDA5MjUxMjI5MzRaFw0xNTA5\n" +
            "MjUxMjI5MzRaMIGyMQswCQYDVQQGEwJCRTELMAkGA1UECBMCT1YxGDAWBgNVBAoT\n" +
            "D2lNaW5kcyAtIGlsYWIudDEeMBwGA1UECxMVaW1pbmRzLXdhbGwyLnd2ZGVtZWVy\n" +
            "MS0wKwYDVQQDEyRiNzU2MTljYy0xNmI5LTExZTMtOTY2YS0wMDE1MTdiZWNkYzEx\n" +
            "LTArBgkqhkiG9w0BCQEWHnd2ZGVtZWVyQHdhbGwyLmlsYWJ0LmltaW5kcy5iZTCB\n" +
            "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtjsCfyXidUuH6wJ5A1PdX3GtbxAt\n" +
            "IqdCV6kPt14tSK9Recc3FJ2zTs7D/I+KJmp9g0WyUQDkXnU8gs9YaawbyDNNV5ak\n" +
            "B1pgi7P1qArJcPutkrhkYdxHxX4xVfQZHKFLMP86Rf94pbv1iR6NouBkutOGRiLm\n" +
            "kKibocYqX1RUXSsCAwEAAaOCASkwggElMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYE\n" +
            "FKaBykCCwTAXA/7i1i2ERmRnW8XwMIGQBgNVHREEgYgwgYWGNHVybjpwdWJsaWNp\n" +
            "ZDpJRE4rd2FsbDIuaWxhYnQuaW1pbmRzLmJlK3VzZXIrd3ZkZW1lZXKBHnd2ZGVt\n" +
            "ZWVyQHdhbGwyLmlsYWJ0LmltaW5kcy5iZYYtdXJuOnV1aWQ6Yjc1NjE5Y2MtMTZi\n" +
            "OS0xMWUzLTk2NmEtMDAxNTE3YmVjZGMxMGMGCCsGAQUFBwEBBFcwVTBTBhRpg8yT\n" +
            "gKiYzKjHvbGngICqrteKG4Y7aHR0cHM6Ly93d3cud2FsbDIuaWxhYnQuaW1pbmRz\n" +
            "LmJlOjEyMzY5L3Byb3RvZ2VuaS94bWxycGMvc2EwDQYJKoZIhvcNAQEEBQADgYEA\n" +
            "mJzek8GZc5YmBcmn1RLpEFhX0cLmsrmCnQXcpHJCzH4lhUiH5O1f1mN+2X7idKbS\n" +
            "vmiQh8jKbn3jRalQOZ6kpn0XQsPnCJ4t5r7cP2p6fshHdMT11jwLkmlAR+wVlRjo\n" +
            "8jDDT8yO5vJ9Db5D3mdeeg7DQm4fl9cHunvjGpNxhhc=\n" +
            "-----END CERTIFICATE-----";
}