### Question:
DefaultSignatureValidator implements SignatureValidator { @Override public boolean validate(String signedData, String signature) { if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(signature)) { Log.e(TAG, "Purchase verification failed: missing data."); return false; } else if (!verify(signedData, signature)) { Log.w(TAG, "signature does not match data."); return false; } else { return true; } } DefaultSignatureValidator(String base64EncodedKey); @Override boolean validate(String signedData, String signature);  }### Answer:
@Test public void shouldVerifyPurchaseNullInput() throws Exception { assertThat(validator.validate(null, "")).isFalse(); }
@Test public void shouldVerifyPurchaseEmptyInput() throws Exception { assertThat(validator.validate("", "")).isFalse(); }
@Test public void shouldVerifyPurchaseNonEmptyInputEmptySignature() throws Exception { assertThat(validator.validate("{}", "")).isFalse(); }
@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(); }
@Test public void shouldValidateTheSHA1WithRSASignature() throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); KeyPair pair = keyGen.generateKeyPair(); EncodedKeySpec encoded = new X509EncodedKeySpec(pair.getPublic().getEncoded()); PublicKey encodedPublic = keyFactory.generatePublic(encoded); String encodePublicBase64 = Base64.encodeToString(encodedPublic.getEncoded(), Base64.DEFAULT); Signature sig = Signature.getInstance("SHA1WithRSA"); sig.initSign(pair.getPrivate()); String data = "some sample data"; sig.update(data.getBytes()); String signature = Base64.encodeToString(sig.sign(), Base64.DEFAULT); SignatureValidator validator = new DefaultSignatureValidator(encodePublicBase64); assertThat(validator.validate(data, signature)).isTrue(); assertThat(validator.validate(data+"extraData", signature)).isFalse(); }