Skip to content

Commit

Permalink
[PIE-1531] Allow whitespace in file paths loaded from resources direc…
Browse files Browse the repository at this point in the history
…tory (PegaSysEng#1329)

* [PIE-1531] Allow whitespace in file paths loaded from resources directory.

* replace Resources.getResource call

* Update LocalPermissioningConfigurationValidatorTest.java

* Update RpcAuthFileValidatorTest.java

* fix paths

* Update SmartContractNodePermissioningAcceptanceTestBase.java
  • Loading branch information
AbdelStark authored and notlesh committed Apr 24, 2019
1 parent 27701de commit ad7e26b
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ public PantheonNode createIbftNodeWithValidators(final String name, final String

private Optional<String> createCliqueGenesisConfig(
final Collection<? extends RunnableNode> validators) {
final String template = readGenesisFile("clique/clique.json");
final String template = readGenesisFile("/clique/clique.json");
return updateGenesisExtraData(
validators, template, CliqueExtraData::createGenesisExtraDataString);
}

private Optional<String> createIbftGenesisConfig(
final Collection<? extends RunnableNode> validators) {
final String template = readGenesisFile("ibft/ibft.json");
final String template = readGenesisFile("/ibft/ibft.json");
return updateGenesisExtraData(
validators, template, IbftExtraData::createGenesisExtraDataString);
}
Expand All @@ -292,7 +292,7 @@ private Optional<String> updateGenesisExtraData(

private String readGenesisFile(final String filepath) {
try {
final URI uri = Resources.getResource(filepath).toURI();
final URI uri = this.getClass().getResource(filepath).toURI();
return Resources.toString(uri.toURL(), Charset.defaultCharset());
} catch (final URISyntaxException | IOException e) {
throw new IllegalStateException("Unable to get test genesis config " + filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public PermissionedNodeBuilder nodesContractEnabled(final String address) {

public PermissionedNodeBuilder genesisFile(final String path) {
try {
URI uri = Resources.getResource(path).toURI();
URI uri = this.getClass().getResource(path).toURI();
this.genesisFile = Resources.toString(uri.toURL(), Charset.defaultCharset());
} catch (final URISyntaxException | IOException e) {
throw new IllegalStateException("Unable to read genesis file from: " + path, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SmartContractNodePermissioningAcceptanceTestBase extends AcceptanceTestBas
private final SmartContractNodePermissioningConditions smartContractNodePermissioningConditions;

private static final String CONTRACT_ADDRESS = "0x0000000000000000000000000000000000009999";
private static final String GENESIS_FILE = "permissioning/simple_permissioning_genesis.json";
private static final String GENESIS_FILE = "/permissioning/simple_permissioning_genesis.json";

protected final Cluster permissionedCluster;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ private GenesisConfigFile(final JsonObject config) {

public static GenesisConfigFile mainnet() {
try {
return fromConfig(Resources.toString(Resources.getResource("mainnet.json"), UTF_8));
return fromConfig(
Resources.toString(GenesisConfigFile.class.getResource("/mainnet.json"), UTF_8));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}

public static GenesisConfigFile development() {
try {
return fromConfig(Resources.toString(Resources.getResource("dev.json"), UTF_8));
return fromConfig(
Resources.toString(GenesisConfigFile.class.getResource("/dev.json"), UTF_8));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void updateWorldStateForDao(final MutableWorldState worldState) {
final JsonArray json =
new JsonArray(
Resources.toString(
Resources.getResource("daoAddresses.json"), StandardCharsets.UTF_8));
this.getClass().getResource("/daoAddresses.json"), StandardCharsets.UTF_8));
final List<Address> addresses =
IntStream.range(0, json.size())
.mapToObj(json::getString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void shouldCreateRopstenConfig() throws Exception {
MainnetProtocolSchedule.fromConfig(
GenesisConfigFile.fromConfig(
Resources.toString(
Resources.getResource("ropsten.json"), StandardCharsets.UTF_8))
this.getClass().getResource("/ropsten.json"), StandardCharsets.UTF_8))
.getConfigOptions(),
PrivacyParameters.DEFAULT);
Assertions.assertThat(sched.getByBlockNumber(0).getName()).isEqualTo("TangerineWhistle");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static BlockchainSetupUtil<Void> forTesting() {
final TemporaryFolder temp = new TemporaryFolder();
try {
temp.create();
final URL genesisFileUrl = getResourceUrl(temp, "testGenesis.json");
final URL genesisFileUrl = getResourceUrl(temp, "/testGenesis.json");
final GenesisState genesisState =
GenesisState.fromJson(
Resources.toString(genesisFileUrl, Charsets.UTF_8), protocolSchedule);
Expand All @@ -107,7 +107,7 @@ public static BlockchainSetupUtil<Void> forTesting() {
final ProtocolContext<Void> protocolContext =
new ProtocolContext<>(blockchain, worldArchive, null);

final Path blocksPath = getResourcePath(temp, "testBlockchain.blocks");
final Path blocksPath = getResourcePath(temp, "/testBlockchain.blocks");
final List<Block> blocks = new ArrayList<>();
final BlockHashFunction blockHashFunction =
ScheduleBasedBlockHashFunction.create(protocolSchedule);
Expand All @@ -128,7 +128,7 @@ public static BlockchainSetupUtil<Void> forTesting() {

private static Path getResourcePath(final TemporaryFolder temp, final String resource)
throws IOException {
final URL url = Resources.getResource(resource);
final URL url = BlockchainSetupUtil.class.getResource(resource);
final Path path =
Files.write(
temp.newFile().toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class BlockBodiesMessageTest {
public void blockBodiesRoundTrip() throws IOException {
final List<BlockBody> bodies = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class BlockHeadersMessageTest {
public void blockHeadersRoundTrip() throws IOException {
final List<BlockHeader> headers = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class GetBlockBodiesMessageTest {
public void getBlockBodiesRoundTrip() throws IOException {
final List<Hash> hashes = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class NewBlockHashesMessageTest {
public void blockHeadersRoundTrip() throws IOException {
final List<NewBlockHashesMessage.NewBlockHash> hashes = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@

public class LocalPermissioningConfigurationBuilderTest {

private static final String PERMISSIONING_CONFIG_VALID = "permissioning_config.toml";
private static final String PERMISSIONING_CONFIG_VALID = "/permissioning_config.toml";
private static final String PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY =
"permissioning_config_account_whitelist_only.toml";
"/permissioning_config_account_whitelist_only.toml";
private static final String PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY =
"permissioning_config_node_whitelist_only.toml";
"/permissioning_config_node_whitelist_only.toml";
private static final String PERMISSIONING_CONFIG_INVALID_ENODE =
"permissioning_config_invalid_enode.toml";
"/permissioning_config_invalid_enode.toml";
private static final String PERMISSIONING_CONFIG_INVALID_ACCOUNT =
"permissioning_config_invalid_account.toml";
"/permissioning_config_invalid_account.toml";
private static final String PERMISSIONING_CONFIG_EMPTY_WHITELISTS =
"permissioning_config_empty_whitelists.toml";
"/permissioning_config_empty_whitelists.toml";
private static final String PERMISSIONING_CONFIG_ABSENT_WHITELISTS =
"permissioning_config_absent_whitelists.toml";
"/permissioning_config_absent_whitelists.toml";
private static final String PERMISSIONING_CONFIG_UNRECOGNIZED_KEY =
"permissioning_config_unrecognized_key.toml";
"/permissioning_config_unrecognized_key.toml";
private static final String PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY_MULTILINE =
"permissioning_config_node_whitelist_only_multiline.toml";
"/permissioning_config_node_whitelist_only_multiline.toml";

private final String VALID_NODE_ID =
"6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0";
Expand All @@ -54,7 +54,7 @@ public void permissioningConfig() throws Exception {
final String uri = "enode://" + VALID_NODE_ID + "@192.168.0.9:4567";
final String uri2 = "enode://" + VALID_NODE_ID + "@192.169.0.9:4568";

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
Expand All @@ -71,7 +71,7 @@ public void permissioningConfig() throws Exception {
public void permissioningConfigWithOnlyNodeWhitelistSet() throws Exception {
final String uri = "enode://" + VALID_NODE_ID + "@192.168.0.9:4567";

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -85,7 +85,7 @@ public void permissioningConfigWithOnlyNodeWhitelistSet() throws Exception {

@Test
public void permissioningConfigWithOnlyAccountWhitelistSet() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -100,7 +100,7 @@ public void permissioningConfigWithOnlyAccountWhitelistSet() throws Exception {

@Test
public void permissioningConfigWithInvalidAccount() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ACCOUNT);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_INVALID_ACCOUNT);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> accountOnlyPermissioningConfig(toml));
Expand All @@ -112,7 +112,7 @@ public void permissioningConfigWithInvalidAccount() throws Exception {

@Test
public void permissioningConfigWithInvalidEnode() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ENODE);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_INVALID_ENODE);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> nodeOnlyPermissioningConfig(toml));
Expand All @@ -124,7 +124,7 @@ public void permissioningConfigWithInvalidEnode() throws Exception {

@Test
public void permissioningConfigWithEmptyWhitelistMustNotError() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_EMPTY_WHITELISTS);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_EMPTY_WHITELISTS);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
Expand All @@ -137,7 +137,7 @@ public void permissioningConfigWithEmptyWhitelistMustNotError() throws Exception

@Test
public void permissioningConfigWithAbsentWhitelistMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ABSENT_WHITELISTS);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_ABSENT_WHITELISTS);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
Expand All @@ -147,7 +147,7 @@ public void permissioningConfigWithAbsentWhitelistMustThrowException() throws Ex

@Test
public void permissioningConfigWithUnrecognizedKeyMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_UNRECOGNIZED_KEY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_UNRECOGNIZED_KEY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> accountOnlyPermissioningConfig(toml));
Expand All @@ -170,7 +170,7 @@ public void permissioningConfigWithEmptyFileMustThrowException() throws Exceptio

@Test
public void permissioningConfigFromFileMustSetFilePath() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -196,7 +196,7 @@ public void permissioningConfigFromNonexistentFileMustThrowException() {
@Test
public void permissioningConfigFromMultilineFileMustParseCorrectly() throws Exception {
final URL configFile =
Resources.getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY_MULTILINE);
this.getClass().getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY_MULTILINE);
final LocalPermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfiguration(
true, configFile.getPath(), false, configFile.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private SmartContractPermissioningController setupController(
final String resourceName, final String contractAddressString) throws IOException {
final ProtocolSchedule<Void> protocolSchedule = MainnetProtocolSchedule.create();

final String emptyContractFile = Resources.toString(Resources.getResource(resourceName), UTF_8);
final String emptyContractFile =
Resources.toString(this.getClass().getResource(resourceName), UTF_8);
final GenesisState genesisState =
GenesisState.fromConfig(GenesisConfigFile.fromConfig(emptyContractFile), protocolSchedule);

Expand All @@ -59,7 +60,7 @@ private SmartContractPermissioningController setupController(
public void testIpv4Included() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -75,7 +76,7 @@ public void testIpv4Included() throws IOException {
public void testIpv4DestinationMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -91,7 +92,7 @@ public void testIpv4DestinationMissing() throws IOException {
public void testIpv4SourceMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -107,7 +108,7 @@ public void testIpv4SourceMissing() throws IOException {
public void testIpv6Included() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -123,7 +124,7 @@ public void testIpv6Included() throws IOException {
public void testIpv6SourceMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -139,7 +140,7 @@ public void testIpv6SourceMissing() throws IOException {
public void testIpv6DestinationMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/preseededSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThat(
Expand All @@ -155,7 +156,7 @@ public void testIpv6DestinationMissing() throws IOException {
public void testPermissioningContractMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/noSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/noSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThatThrownBy(
Expand All @@ -173,7 +174,7 @@ public void testPermissioningContractMissing() throws IOException {
public void testPermissioningContractCorrupt() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/corruptSmartPermissioning.json",
"/SmartContractPermissioningControllerTest/corruptSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");

assertThatThrownBy(
Expand Down
Loading

0 comments on commit ad7e26b

Please sign in to comment.