Merge pull request #3108 from christophsturm/test-signed-witness

make sure that signed witness is immutable.
This commit is contained in:
sqrrm 2019-08-21 16:31:37 +02:00 committed by GitHub
commit ad9715a2cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 10 deletions

View File

@ -66,10 +66,10 @@ public class SignedWitness implements LazyProcessedPayload, PersistableNetworkPa
long date,
long tradeAmount) {
this.signedByArbitrator = signedByArbitrator;
this.witnessHash = witnessHash;
this.signature = signature;
this.signerPubKey = signerPubKey;
this.witnessOwnerPubKey = witnessOwnerPubKey;
this.witnessHash = witnessHash.clone();
this.signature = signature.clone();
this.signerPubKey = signerPubKey.clone();
this.witnessOwnerPubKey = witnessOwnerPubKey.clone();
this.date = date;
this.tradeAmount = tradeAmount;

View File

@ -10,22 +10,51 @@ import com.google.common.base.Charsets;
import java.time.Instant;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class SignedWitnessTest {
private ECKey arbitrator1Key;
private byte[] witnessOwner1PubKey;
private byte[] witnessHash;
private byte[] witnessHashSignature;
@Before
public void setUp() {
arbitrator1Key = new ECKey();
witnessOwner1PubKey = Sig.getPublicKeyBytes(Sig.generateKeyPair().getPublic());
witnessHash = Utils.sha256hash160(new byte[]{1});
witnessHashSignature = arbitrator1Key.signMessage(Utilities.encodeToHex(witnessHash)).getBytes(Charsets.UTF_8);
}
@Test
public void testProtoRoundTrip() {
ECKey arbitrator1Key = new ECKey();
byte[] witnessOwner1PubKey = Sig.getPublicKeyBytes(Sig.generateKeyPair().getPublic());
byte[] witnessHash = Utils.sha256hash160(new byte[]{1});
byte[] witnessHashSignature = arbitrator1Key.signMessage(Utilities.encodeToHex(witnessHash)).getBytes(Charsets.UTF_8);
SignedWitness signedWitness = new SignedWitness(true, witnessHash, witnessHashSignature, arbitrator1Key.getPubKey(), witnessOwner1PubKey, Instant.now().getEpochSecond(), 100);
assertEquals(signedWitness, SignedWitness.fromProto(signedWitness.toProtoMessage().getSignedWitness()));
}
@Test
public void isImmutable() {
byte[] signerPubkey = arbitrator1Key.getPubKey();
SignedWitness signedWitness = new SignedWitness(true, witnessHash, witnessHashSignature, signerPubkey, witnessOwner1PubKey, Instant.now().getEpochSecond(), 100);
byte[] originalWitnessHash = signedWitness.getWitnessHash().clone();
witnessHash[0] += 1;
assertArrayEquals(originalWitnessHash, signedWitness.getWitnessHash());
byte[] originalWitnessHashSignature = signedWitness.getSignature().clone();
witnessHashSignature[0] += 1;
assertArrayEquals(originalWitnessHashSignature, signedWitness.getSignature());
byte[] originalSignerPubkey = signedWitness.getSignerPubKey().clone();
signerPubkey[0] += 1;
assertArrayEquals(originalSignerPubkey, signedWitness.getSignerPubKey());
byte[] originalwitnessOwner1PubKey = signedWitness.getWitnessOwnerPubKey().clone();
witnessOwner1PubKey[0] += 1;
assertArrayEquals(originalwitnessOwner1PubKey, signedWitness.getWitnessOwnerPubKey());
}
}