Skip to content

Commit

Permalink
add cli port validation & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beidouz committed Dec 3, 2018
1 parent 72f4354 commit acf02cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
13 changes: 11 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,17 @@ public ReturnType call(final String[] args, Cfg cfg) {
// 5. options that can be influenced by the -d and -n arguments

if (options.getPort() != null) {
// update port in the config file
cfg.toXML(new String[] {"--p2p=" + ","+ options.getPort()}, configFile);
int port_number = Integer.parseInt(options.getPort());
if (port_number < 0 || port_number > 0xFFFF) {
System.out.println(
"\nport out of range: "
+ port_number
+ ", switching to the current port configuration: "
+ "\n");
} else {
// update port in the config file
cfg.toXML(new String[] {"--p2p=" + "," + options.getPort()}, configFile);
}
// no return, allow for other parameters combined with -p
}

Expand Down
83 changes: 46 additions & 37 deletions modAionImpl/test/org/aion/zero/impl/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public class CliTest {
private static final File mainnetFork = new File(MAIN_CONFIG_PATH, forkFileName);
private static final File testnetFork = new File(TEST_CONFIG_PATH, forkFileName);

private static final String PORT_NUMBER = "12345";
private static final String DEFAULT_PORT = "30303";
private static final String TEST_PORT = "12345";
private static final String INVALID_PORT = "123450";

/** @implNote set this to true to enable printing */
private static final boolean verbose = false;
Expand Down Expand Up @@ -625,32 +627,36 @@ private Object parametersWithPort() {
// with two parameters
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, PORT_NUMBER},
new String[] {opPort, TEST_PORT, TEST_PORT},
ERROR,
expPathOnError,
expPortOnError
});
// with invalid parameter
parameters.add(
new Object[] {
new String[] {opPort, "invalid"}, ERROR, expPathOnError, expPortOnError
new String[] {opPort, INVALID_PORT}, RUN, expPathOnError, expPortOnError
});
parameters.add(
new Object[] {
new String[] {opPort, "-12345"}, RUN, expPathOnError, expPortOnError
});
// with testing port number
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER}, RUN, expectedPath, PORT_NUMBER
new String[] {opPort, TEST_PORT}, RUN, expectedPath, TEST_PORT
});
}

// port with help and version
for (String opPort : port_options) {
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, "-h"}, EXIT, expectedPath, expPortOnError
new String[] {opPort, TEST_PORT, "-h"}, EXIT, expectedPath, expPortOnError
});
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, "-v"}, EXIT, expectedPath, expPortOnError
new String[] {opPort, TEST_PORT, "-v"}, EXIT, expectedPath, expPortOnError
});
}

Expand All @@ -677,32 +683,32 @@ private Object parametersWithPort() {
// with invalid port parameter
parameters.add(
new Object[] {
new String[] {opNet, valNet, opPort, "invalid"},
ERROR,
new String[] {opNet, valNet, opPort, INVALID_PORT},
RUN,
expPathOnError,
expPortOnError
});
parameters.add(
new Object[] {
new String[] {opPort, "invalid", opNet, valNet},
ERROR,
new String[] {opPort, INVALID_PORT, opNet, valNet},
RUN,
expPathOnError,
expPortOnError
});
// with testing port number
parameters.add(
new Object[] {
new String[] {opNet, valNet, opPort, PORT_NUMBER},
new String[] {opNet, valNet, opPort, TEST_PORT},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, opNet, valNet},
new String[] {opPort, TEST_PORT, opNet, valNet},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
}
}
Expand All @@ -716,17 +722,17 @@ private Object parametersWithPort() {
for (String opPort : port_options) {
parameters.add(
new Object[] {
new String[] {opNet, valNet, opPort, PORT_NUMBER},
new String[] {opNet, valNet, opPort, TEST_PORT},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, opNet, valNet},
new String[] {opPort, TEST_PORT, opNet, valNet},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
}
}
Expand Down Expand Up @@ -756,32 +762,32 @@ private Object parametersWithPort() {
// with invalid port parameter
parameters.add(
new Object[] {
new String[] {opDir, valDir, opPort, "invalid"},
ERROR,
new String[] {opDir, valDir, opPort, INVALID_PORT},
RUN,
expectedPath,
expPortOnError
});
parameters.add(
new Object[] {
new String[] {opPort, "invalid", opDir, valDir},
ERROR,
new String[] {opPort, INVALID_PORT, opDir, valDir},
RUN,
expectedPath,
expPortOnError
});
// with testing port number
parameters.add(
new Object[] {
new String[] {opDir, valDir, opPort, PORT_NUMBER},
new String[] {opDir, valDir, opPort, TEST_PORT},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
parameters.add(
new Object[] {
new String[] {opPort, PORT_NUMBER, opDir, valDir},
new String[] {opPort, TEST_PORT, opDir, valDir},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
}
}
Expand All @@ -798,11 +804,11 @@ private Object parametersWithPort() {
parameters.add(
new Object[] {
new String[] {
opNet, valNet, opDir, valDir, opPort, PORT_NUMBER
opNet, valNet, opDir, valDir, opPort, TEST_PORT
},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
}
}
Expand All @@ -819,10 +825,10 @@ private Object parametersWithPort() {
// with relative path with subdirectories
parameters.add(
new Object[] {
new String[] {opDir, dir, opPort, PORT_NUMBER},
new String[] {opDir, dir, opPort, TEST_PORT},
RUN,
expectedPath,
PORT_NUMBER
TEST_PORT
});
}
}
Expand All @@ -835,17 +841,17 @@ private Object parametersWithPort() {
// test port number as parameter
parameters.add(
new Object[] {
new String[] {opInfo, "-p", PORT_NUMBER}, EXIT, expectedPath, PORT_NUMBER
new String[] {opInfo, "-p", TEST_PORT}, EXIT, expectedPath, TEST_PORT
});
parameters.add(
new Object[] {
new String[] {"-p", PORT_NUMBER, opInfo}, EXIT, expectedPath, PORT_NUMBER
new String[] {"-p", TEST_PORT, opInfo}, EXIT, expectedPath, TEST_PORT
});
// test invalid port parameter
parameters.add(
new Object[] {
new String[] {opInfo, "-p", "invalid"},
ERROR,
new String[] {opInfo, "-p", INVALID_PORT},
EXIT,
expPathOnError,
expPortOnError
});
Expand All @@ -858,6 +864,9 @@ private Object parametersWithPort() {
@Parameters(method = "parametersWithPort")
public void testPort(
String[] input, ReturnType expectedReturn, String expectedPath, String expectedPort) {

cfg.toXML(new String[] {"--p2p=" + "," + DEFAULT_PORT}, cfg.getInitialConfigFile());

assertThat(cli.call(input, cfg)).isEqualTo(expectedReturn);
assertThat(cfg.getBasePath()).isEqualTo(expectedPath);
assertThat(cfg.getExecConfigFile())
Expand Down Expand Up @@ -932,10 +941,10 @@ private Object parametersWithInfo() {
expected = MAIN_BASE_PATH.getAbsolutePath();
for (String op : options) {
// test port number as parameter
parameters.add(new Object[] {new String[] {op, "-p", PORT_NUMBER}, EXIT, expected});
parameters.add(new Object[] {new String[] {"-p", PORT_NUMBER, op}, EXIT, expected});
parameters.add(new Object[] {new String[] {op, "-p", TEST_PORT}, EXIT, expected});
parameters.add(new Object[] {new String[] {"-p", TEST_PORT, op}, EXIT, expected});
// invalid port parameter
parameters.add(new Object[] {new String[] {op, "-p", "invalid"}, ERROR, expOnError});
parameters.add(new Object[] {new String[] {op, "-p", INVALID_PORT}, EXIT, expOnError});
}

// with network and directory
Expand Down

0 comments on commit acf02cd

Please sign in to comment.