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

@ -35,7 +35,9 @@ dependencies {
implementation 'com.google.guava:guava:27.0.1-jre' implementation 'com.google.guava:guava:27.0.1-jre'
// Use JUnit test framework // Use JUnit test framework
testImplementation 'junit:junit:4.12' testImplementation "io.grpc:grpc-testing:${grpcVersion}"
testImplementation "junit:junit:4.12"
testImplementation "org.mockito:mockito-core:2.25.1"
// Used by GRPC generated code // Used by GRPC generated code
compile 'org.glassfish:javax.annotation:10.0-b28' compile 'org.glassfish:javax.annotation:10.0-b28'

View file

@ -48,23 +48,10 @@ public class JobServClient {
/* /*
* Constructor * 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 * Spawns a new blockingStub for network operations with the server
*/ */
public JobServClient(String host, public JobServClient(ManagedChannel channel) {
int port, this.channel = channel;
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();
blockingStub = ShellServerGrpc.newBlockingStub(this.channel); blockingStub = ShellServerGrpc.newBlockingStub(this.channel);
} }
@ -210,7 +197,7 @@ public class JobServClient {
/* /*
* main() * main()
* Client entrypoint * 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 { public static void main(String[] args) throws Exception {
@ -224,7 +211,15 @@ public class JobServClient {
JobServClient client; JobServClient client;
try { 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 // Likely bad port
} catch (NumberFormatException e) { } catch (NumberFormatException e) {

View file

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