mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 10:15:52 +00:00
Unit tests for signing and verifying both valid and expired payment requests.
This commit is contained in:
parent
c6b215d79c
commit
40d60306fa
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Andreas Schildbach
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.bitcoin.protocols.payments;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
import org.bitcoin.protocols.payments.Protos;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.google.bitcoin.crypto.X509Utils;
|
||||||
|
import com.google.bitcoin.protocols.payments.PaymentProtocol.PkiVerificationData;
|
||||||
|
import com.google.bitcoin.protocols.payments.PaymentRequestException.PkiVerificationException;
|
||||||
|
|
||||||
|
public class PaymentProtocolTest {
|
||||||
|
|
||||||
|
private KeyStore caStore;
|
||||||
|
private X509Certificate caCert;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
caStore = X509Utils.loadKeyStore("JKS", "password", getClass().getResourceAsStream("test-cacerts"));
|
||||||
|
caCert = (X509Certificate) caStore.getCertificate("test-cacert");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSignAndVerifyValid() throws Exception {
|
||||||
|
Protos.PaymentRequest.Builder paymentRequest = minimalPaymentRequest().toBuilder();
|
||||||
|
|
||||||
|
// Sign
|
||||||
|
KeyStore keyStore = X509Utils
|
||||||
|
.loadKeyStore("JKS", "password", getClass().getResourceAsStream("test-valid-cert"));
|
||||||
|
PrivateKey privateKey = (PrivateKey) keyStore.getKey("test-valid", "password".toCharArray());
|
||||||
|
X509Certificate clientCert = (X509Certificate) keyStore.getCertificate("test-valid");
|
||||||
|
PaymentProtocol.signPaymentRequestPki(paymentRequest, new X509Certificate[] { clientCert }, privateKey);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
PkiVerificationData verificationData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest.build(), caStore);
|
||||||
|
assertNotNull(verificationData);
|
||||||
|
assertEquals(caCert, verificationData.rootAuthority.getTrustedCert());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = PkiVerificationException.class)
|
||||||
|
public void testSignAndVerifyExpired() throws Exception {
|
||||||
|
Protos.PaymentRequest.Builder paymentRequest = minimalPaymentRequest().toBuilder();
|
||||||
|
|
||||||
|
// Sign
|
||||||
|
KeyStore keyStore = X509Utils.loadKeyStore("JKS", "password",
|
||||||
|
getClass().getResourceAsStream("test-expired-cert"));
|
||||||
|
PrivateKey privateKey = (PrivateKey) keyStore.getKey("test-expired", "password".toCharArray());
|
||||||
|
X509Certificate clientCert = (X509Certificate) keyStore.getCertificate("test-expired");
|
||||||
|
PaymentProtocol.signPaymentRequestPki(paymentRequest, new X509Certificate[] { clientCert }, privateKey);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
PaymentProtocol.verifyPaymentRequestPki(paymentRequest.build(), caStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Protos.PaymentRequest minimalPaymentRequest() {
|
||||||
|
Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
|
||||||
|
paymentDetails.setTime(System.currentTimeMillis());
|
||||||
|
Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
|
||||||
|
paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
|
||||||
|
return paymentRequest.build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
# Create key store for CA certificate
|
||||||
|
keytool -keystore test-cacerts -importcert -file test-cacert.pem -alias test-cacert -deststorepass password
|
||||||
|
|
||||||
|
# Create key store for certificate and private key
|
||||||
|
openssl pkcs12 -export -in test-valid-cert.pem -inkey test-valid-key.pem -passin pass:password -out test-valid.p12 -passout pass:password -name test-valid
|
||||||
|
keytool -importkeystore -deststorepass password -destkeypass password -destkeystore test-valid-cert -srckeystore test-valid.p12 -srcstoretype PKCS12 -srcstorepass password -alias test-valid
|
@ -0,0 +1,54 @@
|
|||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
Proc-Type: 4,ENCRYPTED
|
||||||
|
DEK-Info: DES-EDE3-CBC,621C9B6A0E9C192A
|
||||||
|
|
||||||
|
zj5A0/qeYJ5rmvJqcNoQxaUyEonVZQgLj9Jxw76Wvc1kIMNXsT7MGq2jHaDaUMVd
|
||||||
|
LZDhRgNtirGVdfMFvsNGMB+F1SDq3Dftn/w1jKsNRwCXR958r7ciYp37ohldTEuX
|
||||||
|
0rPQfltHQHkwpCDfi7xOQa06wdSA+Ke8Sek9u77RRhT+nEWOLwJHaIyIbSQr835q
|
||||||
|
bcdW7oTFpAj7OQnEgpKMi2fU9t89N4X+F3foarfSwWGP+e8o9J7/UKKmrWGkB30Q
|
||||||
|
H3U4CbZTXP07ti+ALh86/UaBdaZdDp/pXcJK9OyRSUw693NutSRFw5bXcbobessG
|
||||||
|
lRcgJ5lrKy3F4WhyZMCQNTgr3LzymVn4DkBI3aNCI3a3hbIUi6BRyLF2zUZan5Pl
|
||||||
|
IhsTQM4HLGpuq1AfbWwPv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wRPl9r9x09z0Y
|
||||||
|
xkBUcCUZ5q+wgFepXYCB9lOBtATnFViFnTdvQuXqvLeOWDjAHBT3TDxzaB+vJMal
|
||||||
|
qD+S4x3BSO64ZIxepl4CHa1Zajp8qzVWYh1rBlTL/Uqjg3O5MuvfO0DGpRCn/6t0
|
||||||
|
CpJF/bcmRBogOvNM7zjMcDSRtIMOVgsfCqqIyXpvA5yZqocwd2o8tdiG0hKQxASV
|
||||||
|
qxvTNTXQZut/xD0enHtDaoYYTHj2c6Ayu7DZfPHvcx40mdYjGI9ZKvXcSrYkfTqc
|
||||||
|
mugXa29jah+1abRWfokV/0S9xuFTkBGPt8/Byw4oT7e8nkJGFQorObqj4gSJhCOf
|
||||||
|
/+wTqhkappSHM0mkF72mNqJLWqMPGFnUq/5x5hws3qNgDbk48TDMWKyM3UEn8IUb
|
||||||
|
zQViQNNVRxtZGTXO8Yhpqv/LgD3I9nk99xFALcBncrkrBIK121pVVM1Yf4TjsdCx
|
||||||
|
zqykQY0cghi6zVE8wkDj6f7Sh8uYe5fP1gorp0rzxyKQh1RwsI6po3GmAA12aIiX
|
||||||
|
3Z5XHE3qL7zeaiN0Mc8EkA7zVVI5Id8Wssdmf3TWRG36J99qzUWxiMsL3lKvgi9d
|
||||||
|
X5Bbxo5l+3hdC31jqrt1w2vemr4bSs5SyBQMnBL2HEsbk7PWzbxD/NdKWYecyYAz
|
||||||
|
OtFy7vAtURxYvS0IbX4uRoeAehyJ7+Ak7tulM4MG9O+w/WseBV7VS1beI9NLZg3S
|
||||||
|
tLLTc/EyLsR9us5AMjDeuIoOSN4mEtFzlq72nyVbUGELN7fXl8Hv75QYh7/SUZt4
|
||||||
|
nH+DbSxMon0SybKTywuwuSoFo/K2lwfyZvNb4R0ImlPpJobANG0knKgzKCTA+m5P
|
||||||
|
-----END RSA PRIVATE KEY-----
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user