refactored client constructor to support class dependancy injection

This commit is contained in:
Aidan Hahn 2019-05-21 13:56:37 -07:00
parent 87681bc0e4
commit c599902ad5
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 24 additions and 23 deletions

View file

@ -48,23 +48,10 @@ public class JobServClient {
/*
* Constructor
* Creates an SslContext from cert, key, and trust store
* Creates a ManagedChannel object from SSL Parameters
* Spawns a new blockingStub for network operations with the server
*/
public JobServClient(String host,
int port,
String trustStore,
String clientCert,
String clientPrivateKey) throws SSLException {
SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(new File(trustStore));
builder.keyManager(new File(clientCert), new File(clientPrivateKey));
this.channel = NettyChannelBuilder.forAddress(host, port)
.sslContext(builder.build())
.build();
public JobServClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = ShellServerGrpc.newBlockingStub(this.channel);
}
@ -210,7 +197,7 @@ public class JobServClient {
/*
* main()
* Client entrypoint
* Parses arguments and calls the correct function
* Parses arguments, initializes client, and calls the correct functions
*/
public static void main(String[] args) throws Exception {
@ -224,7 +211,15 @@ public class JobServClient {
JobServClient client;
try {
client = new JobServClient(args[3], Integer.parseInt(args[4]), args[2], args[1], args[0]);
SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(new File(args[2]));
builder.keyManager(new File(args[1]), new File(args[0]));
ManagedChannel channel = NettyChannelBuilder.forAddress(args[3], Integer.parseInt(args[4]))
.sslContext(builder.build())
.build();
client = new JobServClient(channel);
// Likely bad port
} catch (NumberFormatException e) {

View file

@ -8,6 +8,9 @@
package JobServ;
import java.io.File;
import javax.net.ssl.SSLException;
import static org.junit.Assert.assertEquals;
import static org.mockito.AdditionalAnswers.delegatesTo;
import static org.mockito.Mockito.mock;
@ -41,7 +44,7 @@ import org.mockito.ArgumentMatchers;
*/
@RunWith(JUnit4.class)
public class JobServerAuthorizationTest {
public class JobServerAuthenticationTest {
// Authorized client key/cert/ca
private final String clientCa = "resources/client/ca.crt";
@ -60,9 +63,9 @@ public class JobServerAuthorizationTest {
// Automates (graceful) shutdown at end of tests
@Rule
public final GrpcCleanupRule grpcCleanup = newCleanupRule();
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
private final ShellServerGrpc.ShellServerImplBase = mock(ShellServerGrpc.ShellServerImplBase.class,
private final ShellServerGrpc.ShellServerImplBase serviceImpl= mock(ShellServerGrpc.ShellServerImplBase.class,
delegatesTo(new ShellServerGrpc.ShellServerImplBase() {}));
// badClient uses unauthorized certs
@ -91,7 +94,7 @@ public class JobServerAuthorizationTest {
grpcCleanup.register(InProcessServerBuilder.forName(serverName)
.sslContext(serverContextBuilder.build())
.directExecutor()
.addService(this.serviceImpl);
.addService(this.serviceImpl)
.build().start());
this.serverSslInitialized = true;
@ -177,6 +180,7 @@ public class JobServerAuthorizationTest {
public void certAuthPosTest() {
assertEquals(clientSslInitialized, true);
int result = goodClient.sendNewJobMessage("test command");
assertNotEquals(result, -2);
Boolean assertCondition = result == -2;
assertEquals(result, false);
}
}