forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored some BesuCommandTests into separate test classes (hyperled…
…ger#6644) Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: amsmota <antonio.mota@citi.com>
- Loading branch information
Showing
9 changed files
with
1,341 additions
and
1,146 deletions.
There are no files selected for viewing
1,147 changes: 1 addition & 1,146 deletions
1,147
besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
besu/src/test/java/org/hyperledger/besu/cli/BesuCommandWithRequiredOptionsTest.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,62 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.cli; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class BesuCommandWithRequiredOptionsTest extends CommandTestAbstract { | ||
|
||
@Test | ||
public void presentRequiredOptionShouldPass() { | ||
parseCommandWithRequiredOption("--accept-terms-and-conditions", "true"); | ||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void missingRequiredOptionShouldFail() { | ||
parseCommandWithRequiredOption(); | ||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.startsWith( | ||
"Missing required option: '--accept-terms-and-conditions=<acceptTermsAndConditions>'"); | ||
} | ||
|
||
@Test | ||
public void havingRequiredOptionInEnvVarShouldFail() { | ||
setEnvironmentVariable("BESU_ACCEPT_TERMS_AND_CONDITIONS", "true"); | ||
parseCommandWithRequiredOption(); | ||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.startsWith( | ||
"Missing required option: '--accept-terms-and-conditions=<acceptTermsAndConditions>'"); | ||
} | ||
|
||
@Test | ||
public void havingRequiredOptionInConfigShouldFail() throws IOException { | ||
final Path toml = createTempFile("toml", "accept-terms-and-conditions=true\n"); | ||
parseCommandWithRequiredOption("--config-file", toml.toString()); | ||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.startsWith( | ||
"Missing required option: '--accept-terms-and-conditions=<acceptTermsAndConditions>'"); | ||
} | ||
} |
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
209 changes: 209 additions & 0 deletions
209
besu/src/test/java/org/hyperledger/besu/cli/HostAllowlistOptionsTest.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,209 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.cli; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class HostAllowlistOptionsTest extends CommandTestAbstract { | ||
|
||
/** test deprecated CLI option * */ | ||
@Deprecated | ||
@Test | ||
public void rpcHttpHostWhitelistAcceptsSingleArgument() { | ||
parseCommand("--host-whitelist", "a"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(1); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistAcceptsSingleArgument() { | ||
parseCommand("--host-allowlist", "a"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(1); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistAcceptsMultipleArguments() { | ||
parseCommand("--host-allowlist", "a,b"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(2); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a", "b"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("*", "localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistAcceptsDoubleComma() { | ||
parseCommand("--host-allowlist", "a,,b"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(2); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a", "b"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("*", "localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Deprecated | ||
@Test | ||
public void rpcHttpHostWhitelistAllowlistAcceptsMultipleFlags() { | ||
parseCommand("--host-whitelist=a", "--host-allowlist=b"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(2); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a", "b"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("*", "localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistAcceptsMultipleFlags() { | ||
parseCommand("--host-allowlist=a", "--host-allowlist=b"); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist().size()).isEqualTo(2); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).contains("a", "b"); | ||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()) | ||
.doesNotContain("*", "localhost"); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistStarWithAnotherHostnameMustFail() { | ||
final String[] origins = {"friend", "*"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Values '*' or 'all' can't be used with other hostnames"); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistStarWithAnotherHostnameMustFailStarFirst() { | ||
final String[] origins = {"*", "friend"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Values '*' or 'all' can't be used with other hostnames"); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistAllWithAnotherHostnameMustFail() { | ||
final String[] origins = {"friend", "all"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Values '*' or 'all' can't be used with other hostnames"); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistWithNoneMustBuildEmptyList() { | ||
final String[] origins = {"none"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture()); | ||
verify(mockRunnerBuilder).build(); | ||
|
||
assertThat(jsonRpcConfigArgumentCaptor.getValue().getHostsAllowlist()).isEmpty(); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistNoneWithAnotherDomainMustFail() { | ||
final String[] origins = {"http://domain1.com", "none"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Value 'none' can't be used with other hostnames"); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistNoneWithAnotherDomainMustFailNoneFirst() { | ||
final String[] origins = {"none", "http://domain1.com"}; | ||
parseCommand("--host-allowlist", String.join(",", origins)); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Value 'none' can't be used with other hostnames"); | ||
} | ||
|
||
@Test | ||
public void rpcHttpHostAllowlistEmptyValueFails() { | ||
parseCommand("--host-allowlist="); | ||
|
||
Mockito.verifyNoInteractions(mockRunnerBuilder); | ||
|
||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); | ||
assertThat(commandErrorOutput.toString(UTF_8)) | ||
.contains("Hostname cannot be empty string or null string."); | ||
} | ||
} |
Oops, something went wrong.