|
| 1 | +/* |
| 2 | + * Copyright 2025 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.core; |
| 18 | + |
| 19 | +import com.iexec.core.chain.ChainConfig; |
| 20 | +import jakarta.validation.ConstraintViolation; |
| 21 | +import jakarta.validation.Validation; |
| 22 | +import jakarta.validation.Validator; |
| 23 | +import jakarta.validation.ValidatorFactory; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.Set; |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +class ChainConfigTest { |
| 31 | + private static final String IEXEC_NODE_ADDRESS = "https://bellecour.iex.ec"; |
| 32 | + private static final String IEXEC_HUB_ADDRESS = "0x1a69b2eb604db8eba185df03ea4f5288dcbbd248"; |
| 33 | + private static final String POOL_ADDRESS = "poolAddress"; |
| 34 | + |
| 35 | + private Validator validator; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void setUp() { |
| 39 | + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { |
| 40 | + validator = factory.getValidator(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void chainIdMustBePositive() { |
| 46 | + final ChainConfig config = new ChainConfig( |
| 47 | + 0, // invalid chainId |
| 48 | + false, |
| 49 | + IEXEC_HUB_ADDRESS, |
| 50 | + Duration.ofMillis(100), |
| 51 | + IEXEC_NODE_ADDRESS, |
| 52 | + POOL_ADDRESS, |
| 53 | + 0L, |
| 54 | + 1.0f, |
| 55 | + 100L |
| 56 | + ); |
| 57 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 58 | + assertThat(violations) |
| 59 | + .extracting(ConstraintViolation::getMessage) |
| 60 | + .containsExactly("Chain id must be greater than 0"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void nodeAddressMustBeValidURL() { |
| 65 | + final ChainConfig config = new ChainConfig( |
| 66 | + 1, |
| 67 | + false, |
| 68 | + IEXEC_HUB_ADDRESS, |
| 69 | + Duration.ofMillis(100), |
| 70 | + "invalid-url", // invalid URL |
| 71 | + POOL_ADDRESS, |
| 72 | + 0L, |
| 73 | + 1.0f, |
| 74 | + 100L |
| 75 | + ); |
| 76 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 77 | + assertThat(violations) |
| 78 | + .extracting(ConstraintViolation::getMessage) |
| 79 | + .containsExactly("Node address must be a valid URL"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void nodeAddressMustNotBeEmpty() { |
| 84 | + final ChainConfig config = new ChainConfig( |
| 85 | + 1, |
| 86 | + false, |
| 87 | + IEXEC_HUB_ADDRESS, |
| 88 | + Duration.ofMillis(100), |
| 89 | + "", // empty nodeAddress |
| 90 | + POOL_ADDRESS, |
| 91 | + 0L, |
| 92 | + 1.0f, |
| 93 | + 100L |
| 94 | + ); |
| 95 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 96 | + assertThat(violations) |
| 97 | + .extracting(ConstraintViolation::getMessage) |
| 98 | + .containsExactly("Node address must not be empty"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void blockTimeMustBeAtLeast100ms() { |
| 103 | + final ChainConfig config = new ChainConfig( |
| 104 | + 1, |
| 105 | + false, |
| 106 | + IEXEC_HUB_ADDRESS, |
| 107 | + Duration.ofMillis(99), // less than 100ms |
| 108 | + IEXEC_NODE_ADDRESS, |
| 109 | + POOL_ADDRESS, |
| 110 | + 0L, |
| 111 | + 1.0f, |
| 112 | + 100L |
| 113 | + ); |
| 114 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 115 | + assertThat(violations) |
| 116 | + .extracting(ConstraintViolation::getMessage) |
| 117 | + .containsExactly("Block time must be greater than 100ms"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void blockTimeMustBeAtMost20Seconds() { |
| 122 | + final ChainConfig config = new ChainConfig( |
| 123 | + 1, |
| 124 | + false, |
| 125 | + IEXEC_HUB_ADDRESS, |
| 126 | + Duration.ofSeconds(21), // more than 20 seconds |
| 127 | + IEXEC_NODE_ADDRESS, |
| 128 | + POOL_ADDRESS, |
| 129 | + 0L, |
| 130 | + 1.0f, |
| 131 | + 100L |
| 132 | + ); |
| 133 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 134 | + assertThat(violations) |
| 135 | + .extracting(ConstraintViolation::getMessage) |
| 136 | + .containsExactly("Block time must be less than 20s"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void gasPriceMultiplierMustBePositive() { |
| 141 | + final ChainConfig config = new ChainConfig( |
| 142 | + 1, |
| 143 | + false, |
| 144 | + IEXEC_HUB_ADDRESS, |
| 145 | + Duration.ofMillis(100), |
| 146 | + IEXEC_NODE_ADDRESS, |
| 147 | + POOL_ADDRESS, |
| 148 | + 0L, |
| 149 | + 0.0f, // invalid multiplier |
| 150 | + 100L |
| 151 | + ); |
| 152 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 153 | + assertThat(violations) |
| 154 | + .extracting(ConstraintViolation::getMessage) |
| 155 | + .containsExactly("Gas price multiplier must be greater than 0"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void gasPriceCapMustBePositiveOrZero() { |
| 160 | + final ChainConfig config = new ChainConfig( |
| 161 | + 1, |
| 162 | + false, |
| 163 | + IEXEC_HUB_ADDRESS, |
| 164 | + Duration.ofMillis(100), |
| 165 | + IEXEC_NODE_ADDRESS, |
| 166 | + POOL_ADDRESS, |
| 167 | + 0L, |
| 168 | + 1.0f, |
| 169 | + -1L // invalid gasPriceCap |
| 170 | + ); |
| 171 | + Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 172 | + assertThat(violations) |
| 173 | + .extracting(ConstraintViolation::getMessage) |
| 174 | + .containsExactly("Gas price cap must be greater or equal to 0"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void hubAddressMustBeValidEthereumAddress() { |
| 179 | + final ChainConfig config = new ChainConfig( |
| 180 | + 1, |
| 181 | + false, |
| 182 | + "0x0", // invalid address |
| 183 | + Duration.ofMillis(100), |
| 184 | + IEXEC_NODE_ADDRESS, |
| 185 | + POOL_ADDRESS, |
| 186 | + 0L, |
| 187 | + 1.0f, |
| 188 | + 100L |
| 189 | + ); |
| 190 | + final Set<ConstraintViolation<ChainConfig>> violations = validator.validate(config); |
| 191 | + assertThat(violations) |
| 192 | + .extracting(ConstraintViolation::getMessage) |
| 193 | + .containsExactly("Hub address must be a valid non zero Ethereum address"); |
| 194 | + } |
| 195 | +} |
0 commit comments