Skip to content

Commit

Permalink
Update QA service for DS [ECR-3586]: (#1224)
Browse files Browse the repository at this point in the history
1. Added an ability to set key pair in TransactionMessage.Builder, so that the builder with a _set_ key pair can be passed around. #signedWith(keys) is provided to just set the keys (then can be followed by any other setters and completed with #build); #sign — as a shortcut to builder.signedWith(keys).build().

2. Added a Testkit#port method returning the TCP port on which the server providing service transport listens

3. Added a system-time provider for cases when the tests don’t need to control the current time.

4. QA service:
    1. Removed conversion of transactions into JSON, as it is no longer supported by the core.
    2. Removed conversion of transactions into RawTransactions
    3. Removed usages of RequiresNativeLibrary as it is no longer used in filtering
    4. Removed operations submitting a self-signed transaction except the two enabling Node testing. The rest of transactions shall be submitted through the usual channel.
    5. Moved transaction decoding tests to the transaction converter.

5. Add BOM to build scripts
  • Loading branch information
dmitry-timofeev authored Nov 18, 2019
1 parent f20fe67 commit bd55453
Show file tree
Hide file tree
Showing 51 changed files with 1,104 additions and 1,523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.exonum.binding.common.message;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.exonum.binding.common.crypto.CryptoFunction;
import com.exonum.binding.common.crypto.CryptoFunctions;
Expand Down Expand Up @@ -110,9 +111,12 @@ static TransactionMessage fromBytes(byte[] bytes) {
* Builder for the binary transaction message.
*/
class Builder {
private static CryptoFunction DEFAULT_CRYPTO_FUNCTION = CryptoFunctions.ed25519();
private Integer serviceId;
private Integer transactionId;
private ByteString payload;
private KeyPair keys;
private CryptoFunction cryptoFunction;

/**
* Sets service identifier to the transaction message.
Expand Down Expand Up @@ -160,16 +164,51 @@ public Builder payload(ByteString payload) {
}

/**
* Signs the message, creating a new signed binary transaction message.
* Sets the Ed25519 key pair to use to sign the message.
*
* @param keys a key pair with a private and public keys. The public key is included
* in the message as an author key of the message. The private key is used for signing
* the message, but not included in it
*/
public Builder signedWith(KeyPair keys) {
return signedWith(keys, DEFAULT_CRYPTO_FUNCTION);
}

/**
* Sets the key pair and the crypto function to use to sign the message.
*
* @param keys key pair with private and public keys. Public key is used as an author key of the
* message and private key is used for signing the message.
* @param keys a key pair with a private and public keys. The public key is included
* in the message as an author key of the message. The private key is used for signing
* the message, but not included in it
* @param crypto a cryptographic function to use
*/
public Builder signedWith(KeyPair keys, CryptoFunction crypto) {
this.keys = checkNotNull(keys);
this.cryptoFunction = checkNotNull(crypto);
return this;
}

/**
* Signs the message with the given Ed25519 keys, creating a new signed binary
* transaction message. A shorthand for {@code signedWith(keys).build()}.
*
* @param keys a key pair to {@linkplain #signedWith(KeyPair) sign} the message
* @return a new signed binary transaction message
* @throws IllegalStateException if any field weren't set
* @throws IllegalArgumentException if the public key has wrong size
*/
public TransactionMessage sign(KeyPair keys) {
return signedWith(keys).build();
}

/**
* Signs the message, creating a new signed binary transaction message.
*
* @return a new signed binary transaction message
* @throws IllegalStateException if serviceId or transactionId or payload weren't set
* @throws IllegalArgumentException if public key has wrong size
* @throws IllegalStateException if any field weren't set
* @throws IllegalArgumentException if the public key has wrong size
*/
public TransactionMessage sign(KeyPair keys, CryptoFunction crypto) {
public TransactionMessage build() {
checkRequiredFieldsSet();
PublicKey authorPublicKey = keys.getPublicKey();
checkArgument(authorPublicKey.size() == Ed25519.PUBLIC_KEY_BYTES,
Expand All @@ -187,7 +226,7 @@ public TransactionMessage sign(KeyPair keys, CryptoFunction crypto) {
.build()
.toByteArray();

byte[] signature = crypto.signMessage(exonumMessage, keys.getPrivateKey());
byte[] signature = cryptoFunction.signMessage(exonumMessage, keys.getPrivateKey());

Consensus.SignedMessage signedMessage = Consensus.SignedMessage.newBuilder()
.setAuthor(Types.PublicKey.newBuilder()
Expand Down Expand Up @@ -219,6 +258,7 @@ private void checkRequiredFieldsSet() {
undefinedFields =
transactionId == null ? undefinedFields + " transactionId" : undefinedFields;
undefinedFields = payload == null ? undefinedFields + " payload" : undefinedFields;
undefinedFields = keys == null ? undefinedFields + " keys" : undefinedFields;
if (!undefinedFields.isEmpty()) {
throw new IllegalStateException(
"Following field(s) are required but weren't set: " + undefinedFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void messageBuilderTest() {
.serviceId(SERVICE_ID)
.transactionId(TRANSACTION_ID)
.payload(payload)
.sign(keys, cryptoFunction);
.signedWith(keys, cryptoFunction)
.build();

// Check the message has correct attributes
assertThat(message.getServiceId(), is(SERVICE_ID));
Expand All @@ -82,7 +83,7 @@ void invalidKeyLengthTest() {
KeyPair keys = KeyPair.createKeyPair(Bytes.bytes(0x00, 0x01), publicKeyBytes);

Exception e = assertThrows(IllegalArgumentException.class,
() -> messageBuilder.sign(keys, CRYPTO));
() -> messageBuilder.sign(keys));

assertThat(e.getMessage(), allOf(
containsString(String.valueOf(publicKeyBytes.length)),
Expand All @@ -105,7 +106,7 @@ void payloadFromByteBufferWithCustomPositionAndLimitTest() {
.serviceId(SERVICE_ID)
.transactionId(TRANSACTION_ID)
.payload(payloadBuffer)
.sign(CRYPTO.generateKeyPair(), CRYPTO);
.sign(CRYPTO.generateKeyPair());

assertThat(message.getPayload().toByteArray(), is(payload));
}
Expand All @@ -123,25 +124,31 @@ private static List<Executable> notProperlyFilledMessagesSource() {
return ImmutableList.of(
() -> TransactionMessage.builder()
.serviceId(SERVICE_ID)
.sign(keyPair, CRYPTO),
.signedWith(keyPair)
.build(),
() -> TransactionMessage.builder()
.serviceId(SERVICE_ID)
.transactionId(TRANSACTION_ID)
.sign(keyPair, CRYPTO),
.signedWith(keyPair)
.build(),
() -> TransactionMessage.builder()
.transactionId(TRANSACTION_ID)
.sign(keyPair, CRYPTO),
.signedWith(keyPair)
.build(),
() -> TransactionMessage.builder()
.serviceId(SERVICE_ID)
.payload(payload)
.sign(keyPair, CRYPTO),
.signedWith(keyPair)
.build(),
() -> TransactionMessage.builder()
.transactionId(TRANSACTION_ID)
.payload(payload)
.sign(keyPair, CRYPTO),
.signedWith(keyPair)
.build(),
() -> TransactionMessage.builder()
.payload(payload)
.sign(keyPair, CRYPTO)
.signedWith(keyPair)
.build()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void roundTrip() {
.serviceId((short) 1)
.transactionId((short) 2)
.payload(payload)
.sign(keys, cryptoFunction);
.signedWith(keys, cryptoFunction)
.build();

roundTripTest(message, serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static TransactionMessage aMessage() {
.serviceId(1)
.transactionId(2)
.payload(bytes())
.sign(keyPair, cryptoFunction);
.sign(keyPair);
}

private static class Wrapper<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ private static List<TransactionMessage> source() {
.serviceId(Short.MIN_VALUE)
.transactionId(Short.MAX_VALUE)
.payload(bytes())
.sign(keys, ed25519()),
.sign(keys),
TransactionMessage.builder()
.serviceId((short) 0)
.transactionId((short) 127)
.payload(bytes(0x00, 0x01, 0x02))
.sign(keys, ed25519())
.sign(keys)
);
}

Expand Down
4 changes: 2 additions & 2 deletions exonum-java-binding/core/rust/src/testkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use exonum::{
use exonum_testkit::{TestKit, TestKitBuilder};
use exonum_time::{time_provider::TimeProvider, TimeServiceFactory};
use jni::{
objects::{JObject, JValue},
objects::{JClass, JObject, JValue},
sys::{jboolean, jbyteArray, jobjectArray, jshort},
Executor, JNIEnv,
};
Expand Down Expand Up @@ -100,7 +100,7 @@ pub extern "system" fn Java_com_exonum_binding_testkit_TestKit_nativeCreateTestK
#[no_mangle]
pub extern "system" fn Java_com_exonum_binding_testkit_TestKit_nativeFreeTestKit(
env: JNIEnv,
_: JObject,
_: JClass,
handle: Handle,
) {
drop_handle::<TestKit>(&env, handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void logApiMountEvent(ServiceWrapper service, String serviceApiPath, Rou
String serviceName = service.getName();
int port = server.getActualPort().orElse(0);
// Currently the API is mounted on *all* interfaces, see VertxServer#start
logger.info("Service {} API is mounted at :{}{}", serviceName, port, serviceApiPath);
logger.info("Service {} API is mounted at <host>::{}{}", serviceName, port, serviceApiPath);

// Log the full path to one of the service endpoint
serviceRoutes.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.exonum.binding.cryptocurrency.transactions;

import com.exonum.binding.common.crypto.CryptoFunction;
import com.exonum.binding.common.crypto.CryptoFunctions;
import com.exonum.binding.common.crypto.KeyPair;
import com.exonum.binding.common.crypto.PublicKey;
import com.exonum.binding.common.message.TransactionMessage;
Expand All @@ -28,8 +26,6 @@
*/
public final class TransactionUtils {

private static final CryptoFunction CRYPTO_FUNCTION = CryptoFunctions.ed25519();

static final long DEFAULT_INITIAL_BALANCE = 100L;

/**
Expand All @@ -42,7 +38,7 @@ static TransactionMessage newCreateWalletTransaction(
.payload(newCreateWalletTxPayload(initialBalance))
.serviceId(serviceId)
.transactionId(CreateWalletTx.ID)
.sign(ownerKeyPair, CRYPTO_FUNCTION);
.sign(ownerKeyPair);
}

/**
Expand All @@ -65,7 +61,7 @@ static TransactionMessage newTransferTransaction(
.payload(newTransferTxPayload(seed, receiverKey, sum))
.serviceId(serviceId)
.transactionId(TransferTx.ID)
.sign(ownerKeyPair, CRYPTO_FUNCTION);
.sign(ownerKeyPair);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ private static TransactionMessage createTestTransactionMessage(String key, Strin
.setKey(key)
.setValue(value)
.build())
.sign(KEY_PAIR, CRYPTO_FUNCTION);
.sign(KEY_PAIR);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions exonum-java-binding/launcher-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto-generated sources for proto messages
exonum_java_runtime_plugin/proto/
2 changes: 2 additions & 0 deletions exonum-java-binding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@
<exclude>**/DeployArguments*</exclude>
<exclude>com/exonum/core/messages/*</exclude>
<exclude>com/exonum/binding/fakeservice/Transactions*</exclude>
<exclude>com/exonum/binding/qaservice/Config*</exclude>
<exclude>com/exonum/binding/qaservice/transactions/TxMessageProtos*</exclude>
</excludes>
</configuration>
<executions>
Expand Down
41 changes: 39 additions & 2 deletions exonum-java-binding/qa-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<properties>
<checkstyle.configLocation>${project.parent.basedir}/../checkstyle.xml</checkstyle.configLocation>
<ejb-core.nativeLibPath>${project.parent.basedir}/core/rust/target/debug</ejb-core.nativeLibPath>
<!-- Artifact name, used to identify the artifact during deploy -->
<artifactName>${project.groupId}:${project.artifactId}:${project.version}</artifactName>
<!-- Artifact JAR name (w/o extension) -->
<artifactFileName>${project.artifactId}-${project.version}-artifact</artifactFileName>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -181,12 +185,45 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Exclude integration tests as they require a `package`d artifact
and some special configuration. -->
<excludedGroups>integration</excludedGroups>
<argLine>
${jacoco.args}
${java.vm.assertionFlag}
</argLine>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- Use tag-based filtering: include all the *Test.java files that have 'integration'
tag. -->
<includes>
<include>**/*Test.java</include>
</includes>
<groups>integration</groups>
<systemPropertyVariables>
<it.artifactsDir>${project.build.directory}</it.artifactsDir>
<it.artifactFilename>${artifactFileName}.jar</it.artifactFilename>
<it.artifactName>${artifactName}</it.artifactName>
</systemPropertyVariables>
<argLine>
${jacoco.it.args}
-Djava.library.path=${ejb-core.nativeLibPath}
${java.vm.assertionFlag}
</argLine>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
Expand All @@ -205,14 +242,14 @@
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-${project.version}-artifact</finalName>
<finalName>${artifactFileName}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<!-- Do not use the produced artifact jar file as a main jar
i.e. do not install it in the local repo -->
<attach>false</attach>
<archive>
<manifestEntries>
<Plugin-Id>${project.groupId}:${project.artifactId}:${project.version}</Plugin-Id>
<Plugin-Id>${artifactName}</Plugin-Id>
<Plugin-Version>${project.version}</Plugin-Version>
<Plugin-Provider>${project.groupId}</Plugin-Provider>
</manifestEntries>
Expand Down
Loading

0 comments on commit bd55453

Please sign in to comment.