Skip to content

Commit

Permalink
Fix handling of invalid eth1Address for setFeeRecipient. (#5795)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone authored Jun 15, 2022
1 parent e93785a commit e838281
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public static RestApi create(
.exceptionHandler(
BadRequestException.class,
(throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage()))
.exceptionHandler(
IllegalArgumentException.class,
(throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage()))
.exceptionHandler(
JsonProcessingException.class,
(throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@
import java.io.IOException;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.eth1.Eth1Address;
import tech.pegasys.teku.spec.util.DataStructureUtil;
import tech.pegasys.teku.validator.client.BeaconProposerPreparer;

public class SetFeeRecipientTest {
private final BeaconProposerPreparer beaconProposerPreparer = mock(BeaconProposerPreparer.class);
private final SetFeeRecipient handler = new SetFeeRecipient(Optional.of(beaconProposerPreparer));

private final RestApiRequest request = mock(RestApiRequest.class);
private final StubRestApiRequest request = new StubRestApiRequest(handler.getMetadata());

private final Spec spec = TestSpecFactory.createMinimalAltair();
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

@Test
void badPubkey_shouldGiveIllegalArgument() {
request.setPathParameter("pubkey", "pubkey");
assertThatThrownBy(() -> handler.handleRequest(request))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void metadata_shouldHandle400() throws JsonProcessingException {
Expand All @@ -49,6 +62,9 @@ void metadata_shouldHandle500() throws JsonProcessingException {

@Test
void shouldShareContextIfBellatrixNotEnabled() {
request.setPathParameter("pubkey", dataStructureUtil.randomPublicKey().toString());
request.setRequestBody(
new SetFeeRecipient.SetFeeRecipientBody(dataStructureUtil.randomEth1Address()));
assertThatThrownBy(
() -> {
SetFeeRecipient handler = new SetFeeRecipient(Optional.empty());
Expand All @@ -73,4 +89,13 @@ void metadata_shouldReadRequestBody() throws IOException {
new SetFeeRecipient.SetFeeRecipientBody(
Eth1Address.fromHexString("0xabcf8e0d4e9587369b2301d0790347320302cc09")));
}

@Test
void metadata_shoulThrowInvalidArgument() {
assertThatThrownBy(
() ->
getRequestBodyFromMetadata(
handler, "{\"ethaddress\":\"0xabcF8e0d4E9587369b2301d0790347320302CC09\"}"))
.isInstanceOf(IllegalArgumentException.class);
}
}

0 comments on commit e838281

Please sign in to comment.