|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.validator.client.loader; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.ArgumentMatchers.eq; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.ConnectException; |
| 29 | +import java.net.MalformedURLException; |
| 30 | +import java.net.URL; |
| 31 | +import java.net.UnknownHostException; |
| 32 | +import java.time.Duration; |
| 33 | +import java.util.concurrent.CompletionException; |
| 34 | +import java.util.stream.Collectors; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import tech.pegasys.teku.bls.BLSPublicKey; |
| 37 | +import tech.pegasys.teku.infrastructure.async.SafeFuture; |
| 38 | +import tech.pegasys.teku.infrastructure.async.StubAsyncRunner; |
| 39 | +import tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException; |
| 40 | +import tech.pegasys.teku.infrastructure.time.StubTimeProvider; |
| 41 | +import tech.pegasys.teku.spec.TestSpecFactory; |
| 42 | +import tech.pegasys.teku.spec.util.DataStructureUtil; |
| 43 | + |
| 44 | +class ExternalUrlKeyReaderTest { |
| 45 | + private static final Duration DELAY = Duration.ofSeconds(5); |
| 46 | + private static final String VALID_URL = "http://test:0000/api/v1/eth2/publicKeys"; |
| 47 | + private final ObjectMapper mapper = mock(ObjectMapper.class); |
| 48 | + |
| 49 | + private final StubTimeProvider timeProvider = StubTimeProvider.withTimeInMillis(0); |
| 50 | + private final StubAsyncRunner asyncRunner = new StubAsyncRunner(timeProvider); |
| 51 | + |
| 52 | + private final DataStructureUtil dataStructureUtil = |
| 53 | + new DataStructureUtil(TestSpecFactory.createDefault()); |
| 54 | + |
| 55 | + final BLSPublicKey publicKey1 = dataStructureUtil.randomPublicKey(); |
| 56 | + final BLSPublicKey publicKey2 = dataStructureUtil.randomPublicKey(); |
| 57 | + final String[] expectedKeys = new String[] {publicKey1.toHexString(), publicKey2.toHexString()}; |
| 58 | + |
| 59 | + @Test |
| 60 | + void readKeys_validUrlReturnsValidKeys() throws IOException { |
| 61 | + when(mapper.readValue(any(URL.class), eq(String[].class))).thenReturn(expectedKeys); |
| 62 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner); |
| 63 | + |
| 64 | + assertThat(reader.readKeys()).contains(publicKey1, publicKey2); |
| 65 | + verify(mapper).readValue(any(URL.class), eq(String[].class)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void readKeys_validUrlReturnsEmptyKeys() throws IOException { |
| 70 | + when(mapper.readValue(any(URL.class), eq(String[].class))).thenReturn(new String[] {}); |
| 71 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner); |
| 72 | + |
| 73 | + assertThat(reader.readKeys()).isEmpty(); |
| 74 | + verify(mapper).readValue(any(URL.class), eq(String[].class)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void readKeys_validUrlReturnsInvalidKeys() throws IOException { |
| 79 | + when(mapper.readValue(any(URL.class), eq(String[].class))) |
| 80 | + .thenReturn(new String[] {"invalid", "keys"}); |
| 81 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner); |
| 82 | + |
| 83 | + assertThatThrownBy(() -> reader.readKeys().collect(Collectors.toSet())) |
| 84 | + .isInstanceOf(IllegalArgumentException.class) |
| 85 | + .hasMessage("Invalid odd-length hex binary representation"); |
| 86 | + verify(mapper).readValue(any(URL.class), eq(String[].class)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void readKeys_malformedUrlString() { |
| 91 | + final String invalidUrl = "invalid:url"; |
| 92 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(invalidUrl, mapper, asyncRunner); |
| 93 | + |
| 94 | + assertThatThrownBy(reader::readKeys) |
| 95 | + .isInstanceOf(CompletionException.class) |
| 96 | + .hasCauseInstanceOf(InvalidConfigurationException.class) |
| 97 | + .hasMessageContaining("Failed to load public keys from invalid URL: " + invalidUrl) |
| 98 | + .hasRootCauseInstanceOf(MalformedURLException.class); |
| 99 | + verifyNoInteractions(mapper); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void readKeysWithRetry_unreachableUrlRetryUntilReachable() throws IOException { |
| 104 | + final UnknownHostException exception = new UnknownHostException("Unknown host"); |
| 105 | + when(mapper.readValue(any(URL.class), eq(String[].class))) |
| 106 | + .thenThrow(exception, exception, exception) |
| 107 | + .thenReturn(expectedKeys); |
| 108 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner); |
| 109 | + |
| 110 | + final SafeFuture<String[]> keys = reader.retry(); |
| 111 | + for (int i = 0; i < 3; i++) { |
| 112 | + assertThat(keys).isNotCompleted(); |
| 113 | + timeProvider.advanceTimeBy(DELAY); |
| 114 | + asyncRunner.executeQueuedActions(); |
| 115 | + } |
| 116 | + assertThat(keys).isCompletedWithValue(expectedKeys); |
| 117 | + verify(mapper, times(4)).readValue(any(URL.class), eq(String[].class)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void readKeysWithRetry_unreachableUrlRetryUntilMaxRetries() throws IOException { |
| 122 | + final IOException exception = new ConnectException("Connection refused"); |
| 123 | + when(mapper.readValue(any(URL.class), eq(String[].class))).thenThrow(exception); |
| 124 | + final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner); |
| 125 | + |
| 126 | + final SafeFuture<String[]> keys = reader.retry(); |
| 127 | + for (int i = 0; i < 59; i++) { |
| 128 | + assertThat(keys).isNotCompleted(); |
| 129 | + timeProvider.advanceTimeBy(DELAY); |
| 130 | + asyncRunner.executeQueuedActions(); |
| 131 | + } |
| 132 | + assertThat(keys).isCompletedExceptionally(); |
| 133 | + assertThatThrownBy(keys::join) |
| 134 | + .isInstanceOf(CompletionException.class) |
| 135 | + .hasCauseInstanceOf(InvalidConfigurationException.class) |
| 136 | + .hasRootCause(exception); |
| 137 | + verify(mapper, times(60)).readValue(any(URL.class), eq(String[].class)); |
| 138 | + } |
| 139 | +} |
0 commit comments