-
Notifications
You must be signed in to change notification settings - Fork 45
/
FauxGcpKmsSignatureServiceTest.java
223 lines (188 loc) · 7.33 KB
/
FauxGcpKmsSignatureServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.xrpl.xrpl4j.crypto.signing.faux;
/*-
* ========================LICENSE_START=================================
* xrpl4j :: core
* %%
* Copyright (C) 2020 - 2023 XRPL Foundation and its contributors
* %%
* 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.
* =========================LICENSE_END==================================
*/
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
import com.google.common.collect.Sets;
import org.immutables.value.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
import org.xrpl.xrpl4j.crypto.keys.PrivateKeyReference;
import org.xrpl.xrpl4j.crypto.keys.PublicKey;
import org.xrpl.xrpl4j.crypto.signing.AbstractSignatureService;
import org.xrpl.xrpl4j.crypto.signing.AbstractTransactionSigner;
import org.xrpl.xrpl4j.crypto.signing.AbstractTransactionVerifier;
import org.xrpl.xrpl4j.crypto.signing.Signature;
import org.xrpl.xrpl4j.crypto.signing.SignatureService;
import org.xrpl.xrpl4j.crypto.signing.SingleSignedTransaction;
import org.xrpl.xrpl4j.model.client.channels.UnsignedClaim;
import org.xrpl.xrpl4j.model.transactions.Signer;
import org.xrpl.xrpl4j.model.transactions.Transaction;
import java.util.Optional;
import java.util.Set;
/**
* Tests the contract that a GCP KMS SignatureService might use.
*/
class FauxGcpKmsSignatureServiceTest {
@Mock
AbstractTransactionSigner<GcpKmsPrivateKeyReference> gcpKmsTransactionSignerMock;
@Mock
AbstractTransactionVerifier gcpKmsTransactionVerifierMock;
@Mock
GcpKmsPrivateKeyReference privateKeyReferenceMock;
@Mock
PublicKey publicKeyMock;
@Mock
private Signer signerMock;
@Mock
private Signature signatureMock;
@Mock
private Transaction transactionMock;
@Mock
private SingleSignedTransaction<Transaction> singleSignedTransactionMock;
private SignatureService<GcpKmsPrivateKeyReference> gcpKmsSignatureService;
@BeforeEach
void setUp() {
openMocks(this);
when(gcpKmsTransactionSignerMock.derivePublicKey(privateKeyReferenceMock)).thenReturn(publicKeyMock);
when(gcpKmsTransactionSignerMock.sign(privateKeyReferenceMock, transactionMock)).thenReturn(
singleSignedTransactionMock);
when(gcpKmsTransactionSignerMock.sign(eq(privateKeyReferenceMock), any(UnsignedClaim.class))).thenReturn(
signatureMock);
when(gcpKmsTransactionSignerMock.multiSign(privateKeyReferenceMock, transactionMock)).thenReturn(signatureMock);
this.gcpKmsSignatureService = new AbstractSignatureService<GcpKmsPrivateKeyReference>(
gcpKmsTransactionSignerMock, gcpKmsTransactionVerifierMock
) {
@Override
public PublicKey derivePublicKey(GcpKmsPrivateKeyReference privateKeyable) {
return publicKeyMock;
}
@Override
protected Signature edDsaSign(GcpKmsPrivateKeyReference privateKey, UnsignedByteArray signableTransactionBytes) {
return signatureMock;
}
@Override
protected Signature ecDsaSign(GcpKmsPrivateKeyReference privateKey, UnsignedByteArray signableTransactionBytes) {
return signatureMock;
}
@Override
protected boolean edDsaVerify(PublicKey publicKey, UnsignedByteArray transactionBytes, Signature signature) {
return true;
}
@Override
protected boolean ecDsaVerify(PublicKey publicKey, UnsignedByteArray transactionBytes, Signature signature) {
return true;
}
};
}
@Test
void derivePublicKey() {
PublicKey actual = gcpKmsSignatureService.derivePublicKey(privateKeyReferenceMock);
assertThat(actual).isEqualTo(publicKeyMock);
}
@Test
void signTransaction() {
SingleSignedTransaction<Transaction> actual = gcpKmsSignatureService.sign(privateKeyReferenceMock, transactionMock);
assertThat(actual).isEqualTo(singleSignedTransactionMock);
verify(gcpKmsTransactionSignerMock).sign(privateKeyReferenceMock, transactionMock);
}
@Test
void signClaim() {
UnsignedClaim mockUnsignedClaim = mock(UnsignedClaim.class);
Signature actual = gcpKmsSignatureService.sign(privateKeyReferenceMock, mockUnsignedClaim);
assertThat(actual).isEqualTo(signatureMock);
verify(gcpKmsTransactionSignerMock).sign(privateKeyReferenceMock, mockUnsignedClaim);
}
@Test
void multiSignTransaction() {
Signature actual = gcpKmsSignatureService.multiSign(privateKeyReferenceMock, transactionMock);
assertThat(actual).isEqualTo(signatureMock);
verify(gcpKmsTransactionSignerMock).multiSign(privateKeyReferenceMock, transactionMock);
}
@Test
void verifySingleSigned() {
gcpKmsSignatureService.verify(signerMock, transactionMock);
verify(gcpKmsTransactionVerifierMock).verify(signerMock, transactionMock);
}
@Test
void verifyMultiSignedWithoutMinSigners() {
Set<Signer> signerSet = Sets.newHashSet(signerMock);
gcpKmsSignatureService.verifyMultiSigned(signerSet, transactionMock);
verify(gcpKmsTransactionVerifierMock).verifyMultiSigned(signerSet, transactionMock, 1);
}
@Test
void verifyMultiSignedWithMinSigners() {
Set<Signer> signerSet = Sets.newHashSet(signerMock);
int minSigners = 1;
gcpKmsSignatureService.verifyMultiSigned(signerSet, transactionMock, minSigners);
verify(gcpKmsTransactionVerifierMock).verifyMultiSigned(signerSet, transactionMock, minSigners);
}
/**
* Exists only to prove out the {@link PrivateKeyReference} interface.
*/
@Value.Immutable
interface GcpKmsPrivateKeyReference extends PrivateKeyReference {
/**
* A Builder for immutables.
*
* @return A {@link ImmutableGcpKmsPrivateKeyReference.Builder}.
*/
static ImmutableGcpKmsPrivateKeyReference.Builder builder() {
return ImmutableGcpKmsPrivateKeyReference.builder();
}
/**
* The unique identifier of the platform that can decode this secret.
*
* @return A {@link String}.
*/
String platformIdentifier();
/**
* The unique identifier of the keyring that holds the the private-key used to encrypt this encoded secret.
*
* @return A {@link String}.
*/
String keyringIdentifier();
/**
* The unique identifier for the private-key used to encrypt this encoded secret.
*
* @return A {@link String}.
*/
String keyIdentifier();
/**
* The version of the encryption key used to encrypt this secret.
*
* @return A {@link String}.
*/
String keyVersion();
/**
* An optional password that will unlock this particular key.
*
* @return An {@link Optional} of type {@link String}.
*/
Optional<String> keyPassword();
}
}