-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Secp wallet bulkload acceptance test
- Loading branch information
1 parent
a55dd5a
commit 18000d7
Showing
5 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
acceptance-tests/src/test/java/tech/pegasys/web3signer/dsl/utils/HealthCheckResultUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2023 ConsenSys AG. | ||
* | ||
* 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 tech.pegasys.web3signer.dsl.utils; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Splitter; | ||
|
||
public class HealthCheckResultUtil { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
/* | ||
Healthcheck Json looks like: | ||
{ | ||
"status": "UP", | ||
"checks": [ | ||
{ | ||
"id": "default-check", | ||
"status": "UP" | ||
}, | ||
{ | ||
"id": "keys-check", | ||
"status": "UP", | ||
"checks": [ | ||
{ | ||
"id": "wallet-bulk-loading", | ||
"status": "UP", | ||
"data": { | ||
"keys-loaded": 4, | ||
"error-count": 0 | ||
} | ||
}, | ||
{ | ||
"id": "config-files-loading", | ||
"status": "UP", | ||
"data": { | ||
"keys-loaded": 4, | ||
"error-count": 0 | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"outcome": "UP" | ||
} | ||
*/ | ||
public static int getHealthCheckKeysCheckData( | ||
String healthCheckJsonBody, final String healthCheckKeyName, final String dataKey) | ||
throws JsonProcessingException { | ||
final List<String> keyCheckIds = Splitter.on('/').splitToList(healthCheckKeyName); | ||
int dataKeyValue = -1; | ||
final JsonNode jsonNode = OBJECT_MAPPER.readTree(healthCheckJsonBody); | ||
for (JsonNode checksNode : jsonNode.get("checks")) { | ||
// id = keys-check | ||
if (checksNode.get("id").asText().equals(keyCheckIds.get(0))) { | ||
for (JsonNode keyChecksNode : checksNode.get("checks")) { | ||
// id = See HealthCheckNames.java | ||
if (keyChecksNode.get("id").asText().equals(keyCheckIds.get(1))) { | ||
dataKeyValue = keyChecksNode.get("data").get(dataKey).asInt(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return dataKeyValue; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...test/java/tech/pegasys/web3signer/tests/bulkloading/SecpWalletBulkloadAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2023 ConsenSys AG. | ||
* | ||
* 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 tech.pegasys.web3signer.tests.bulkloading; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.web3j.crypto.WalletUtils.generateWalletFile; | ||
import static tech.pegasys.web3signer.core.config.HealthCheckNames.KEYS_CHECK_V3_WALLET_BULK_LOADING; | ||
import static tech.pegasys.web3signer.dsl.utils.HealthCheckResultUtil.getHealthCheckKeysCheckData; | ||
|
||
import tech.pegasys.web3signer.dsl.signer.SignerConfigurationBuilder; | ||
import tech.pegasys.web3signer.dsl.utils.DefaultKeystoresParameters; | ||
import tech.pegasys.web3signer.signing.KeyType; | ||
import tech.pegasys.web3signer.signing.config.KeystoresParameters; | ||
import tech.pegasys.web3signer.signing.secp256k1.EthPublicKeyUtils; | ||
import tech.pegasys.web3signer.signing.util.IdentifierUtils; | ||
import tech.pegasys.web3signer.tests.AcceptanceTestBase; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.GeneralSecurityException; | ||
import java.security.interfaces.ECPublicKey; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import io.restassured.http.ContentType; | ||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.web3j.crypto.CipherException; | ||
import org.web3j.crypto.ECKeyPair; | ||
import org.web3j.crypto.Keys; | ||
|
||
public class SecpWalletBulkloadAcceptanceTest extends AcceptanceTestBase { | ||
@TempDir private static Path walletsDir; | ||
@TempDir private static Path walletsPasswordDir; | ||
|
||
private static List<String> publicKeys; | ||
|
||
@BeforeAll | ||
static void initV3Wallets() throws IOException, GeneralSecurityException, CipherException { | ||
publicKeys = new ArrayList<>(); | ||
for (int i = 0; i < 4; i++) { | ||
final ECKeyPair ecKeyPair = Keys.createEcKeyPair(); | ||
final ECPublicKey ecPublicKey = EthPublicKeyUtils.createPublicKey(ecKeyPair.getPublicKey()); | ||
final String publicKeyHex = | ||
IdentifierUtils.normaliseIdentifier(EthPublicKeyUtils.toHexString(ecPublicKey)); | ||
publicKeys.add(publicKeyHex); | ||
|
||
// generate v3 wallet | ||
final boolean useFullScrypt = false; | ||
final String fileName = | ||
generateWalletFile("test123", ecKeyPair, walletsDir.toFile(), useFullScrypt); | ||
|
||
// write corresponding password | ||
final Path passwordFile = | ||
walletsPasswordDir.resolve(fileName.substring(0, fileName.lastIndexOf(".json")) + ".txt"); | ||
Files.writeString(passwordFile, "test123"); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("buildWalletParameters") | ||
void walletFilesAreBulkloaded(final KeystoresParameters walletBulkloadParameters) | ||
throws Exception { | ||
final SignerConfigurationBuilder configBuilder = | ||
new SignerConfigurationBuilder() | ||
.withMode("eth1") | ||
.withWalletBulkloadParameters(walletBulkloadParameters); | ||
|
||
startSigner(configBuilder.build()); | ||
|
||
final Response response = signer.callApiPublicKeys(KeyType.SECP256K1); | ||
response | ||
.then() | ||
.statusCode(200) | ||
.contentType(ContentType.JSON) | ||
.body("", containsInAnyOrder(publicKeys.toArray(String[]::new))); | ||
|
||
final Response healthcheckResponse = signer.healthcheck(); | ||
healthcheckResponse | ||
.then() | ||
.statusCode(200) | ||
.contentType(ContentType.JSON) | ||
.body("status", equalTo("UP")); | ||
|
||
final String jsonBody = healthcheckResponse.body().asString(); | ||
final int keysLoaded = | ||
getHealthCheckKeysCheckData(jsonBody, KEYS_CHECK_V3_WALLET_BULK_LOADING, "keys-loaded"); | ||
assertThat(keysLoaded).isEqualTo(publicKeys.size()); | ||
} | ||
|
||
private static Stream<KeystoresParameters> buildWalletParameters() { | ||
// build wallet bulkloading parameters, one with password dir, other with password file | ||
final KeystoresParameters withPasswordDir = | ||
new DefaultKeystoresParameters(walletsDir, walletsPasswordDir, null); | ||
|
||
try (final Stream<Path> passwordFiles = Files.list(walletsPasswordDir)) { | ||
final Path passwordFile = passwordFiles.findAny().orElseThrow(); | ||
final KeystoresParameters withPasswordFile = | ||
new DefaultKeystoresParameters(walletsDir, null, passwordFile); | ||
return Stream.of(withPasswordDir, withPasswordFile); | ||
} catch (final IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |