package com.github.jberkel.payme.security;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.fest.assertions.api.Assertions.assertThat;

@RunWith(RobolectricTestRunner.class)
public class DefaultSignatureValidatorTest {
    SignatureValidator validator;

    @Before public void before() throws Exception {
        validator = new DefaultSignatureValidator(ENCODED_KEY);
    }

    @Test public void shouldVerifyPurchaseNullInput() throws Exception {
        assertThat(validator.validate(null, "")).isFalse();
    }

    @Test public void shouldVerifyPurchaseEmptyInput() throws Exception {
        assertThat(validator.validate("", "")).isTrue();
    }

    @Test public void shouldVerifyPurchaseNonEmptyInputEmptySignature() throws Exception {
        assertThat(validator.validate("{}", "")).isTrue();
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldVerifyPurchaseWithInvalidKeyAndSignatureShouldThrow() throws Exception {
        new DefaultSignatureValidator("").validate("", "signature");
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldVerifyPurchaseWithInvalidKeyShouldThrow() throws Exception {
        new DefaultSignatureValidator("INVALIDKEY").validate("", "");
    }

    @Test
    public void shouldVerifyPurchaseWithValidKeyAndInvalidSignatureShouldThrow() throws Exception {
        assertThat(validator.validate("{}", "signature")).isFalse();
    }

    private static final String ENCODED_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzoFJ+dq/PQo2u71ndt2k\n" +
            "t0XK3oGFvUPagg0QogBrp2IyBKTodFtmcb0riKtDGjZ9JKB45GIBC3RR2fuC9lOR\n" +
            "15rRjA2Tfxoig0K/VYy7K5+fkLt2yGVDd3oqBFEDSGcwYYP1LfmgI8B2WJjACu3V\n" +
            "ehEQeO/cYrr8tav6VthmqdrL9C+BL9McTMjf3FzeJOTkiGeOFCu58T/sYvSc0ESG\n" +
            "YLh4lXAIG309WvEJ0GofxM4hWnD9aHcuu+hwYblrLJ5jk9hJQJmF7isripkDOQeO\n" +
            "9UbH0kNa9o1pq05beHmGW9a1pt3vWmgBQXZQIOKZzxvmh52d0BJWBgp7NMh68MSx\n" +
            "qwIDAQAB";
}

@@ -37,16 +37,44 @@
 urity;%0A%0A
+import android.util.Base64;%0A
 import o
@@ -90,16 +90,16 @@
 Before;%0A
-
 import o
@@ -193,16 +193,271 @@
 unner;%0A%0A
+import java.security.KeyFactory;%0Aimport java.security.KeyPair;%0Aimport java.security.KeyPairGenerator;%0Aimport java.security.PublicKey;%0Aimport java.security.Signature;%0Aimport java.security.spec.EncodedKeySpec;%0Aimport java.security.spec.X509EncodedKeySpec;%0A%0A
 import s
@@ -1725,32 +1725,32 @@
 ows Exception %7B%0A
-
         assertTh
@@ -1805,24 +1805,1138 @@
 e();%0A    %7D%0A%0A
+    @Test public void shouldValidateTheSHA1WithRSASignature() throws Exception %7B%0A        // create a public/private key pair%0A        KeyFactory keyFactory   = KeyFactory.getInstance(%22RSA%22);%0A        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(%22RSA%22);%0A        KeyPair pair = keyGen.generateKeyPair();%0A        EncodedKeySpec encoded = new X509EncodedKeySpec(pair.getPublic().getEncoded());%0A        PublicKey encodedPublic = keyFactory.generatePublic(encoded);%0A        String encodePublicBase64 = Base64.encodeToString(encodedPublic.getEncoded(), Base64.DEFAULT);%0A%0A        // and sign some data with it%0A        Signature sig = Signature.getInstance(%22SHA1WithRSA%22);%0A        sig.initSign(pair.getPrivate());%0A        String data = %22some sample data%22;%0A        sig.update(data.getBytes());%0A%0A        String signature = Base64.encodeToString(sig.sign(), Base64.DEFAULT);%0A%0A        SignatureValidator validator = new DefaultSignatureValidator(encodePublicBase64);%0A        assertThat(validator.validate(data, signature)).isTrue();%0A        assertThat(validator.validate(data+%22extraData%22, signature)).isFalse();%0A    %7D%0A%0A
     private 

Test the signature validator