fix typoes, uncaught exceptions, started unit tests for ProcessManager

This commit is contained in:
Aidan Hahn 2019-05-22 23:51:37 -07:00
parent 7d90f1c87f
commit 9754f23fd8
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 178 additions and 98 deletions

View file

@ -49,7 +49,7 @@ import org.mockito.ArgumentMatchers;
public class JobServerAuthenticationTest {
private final String projectRoot = "";
// Authorized client key/cert/ca
private final String clientCa = projectRoot + "resources/client/ca.crt";
private final String clientKey = projectRoot + "resources/client/private.pem";
@ -73,65 +73,65 @@ public class JobServerAuthenticationTest {
// was setUp able to use SSL Certs
private Boolean serverSslInitialized = true;
private Boolean clientSslInitialized = true;
/*
* test constructor
* generates both clients and the server
*/
public JobServerAuthenticationTest() throws Exception {
try {
// generate SSL contexts
SslContextBuilder serverContextBuilder = SslContextBuilder.forServer(new File(serverCert),
new File(serverKey));
serverContextBuilder.trustManager(new File(clientCa));
serverContextBuilder.clientAuth(ClientAuth.REQUIRE);
this.server = new JobServServer(GrpcSslContexts.configure(serverContextBuilder).build(), 8448);
this.serverSslInitialized = true;
} catch (SSLException e) {
this.serverSslInitialized = false;
System.err.println(e.getMessage());
} catch (IOException e) {
this.serverSslInitialized = false;
System.err.println(e.getMessage());
}
try {
// generate SSL contexts
SslContextBuilder serverContextBuilder = SslContextBuilder.forServer(new File(serverCert),
new File(serverKey));
serverContextBuilder.trustManager(new File(clientCa));
serverContextBuilder.clientAuth(ClientAuth.REQUIRE);
// generate ssl for clients
this.server = new JobServServer(GrpcSslContexts.configure(serverContextBuilder).build(), 8448);
this.serverSslInitialized = true;
} catch (SSLException e) {
this.serverSslInitialized = false;
System.err.println(e.getMessage());
} catch (IOException e) {
this.serverSslInitialized = false;
System.err.println(e.getMessage());
}
// generate ssl for clients
if (this.serverSslInitialized) {
try {
SslContextBuilder goodClientBuilder = GrpcSslContexts.forClient();
goodClientBuilder.trustManager(new File(serverCa));
goodClientBuilder.keyManager(new File(clientCert), new File(clientKey));
try {
SslContextBuilder goodClientBuilder = GrpcSslContexts.forClient();
goodClientBuilder.trustManager(new File(serverCa));
goodClientBuilder.keyManager(new File(clientCert), new File(clientKey));
SslContextBuilder badClientBuilder = GrpcSslContexts.forClient();
badClientBuilder.trustManager(new File(serverCa));
badClientBuilder.keyManager(new File(badCert), new File(badKey));
ManagedChannel goodChannel = NettyChannelBuilder.forAddress("localhost", 8448)
.sslContext(goodClientBuilder.build())
.directExecutor()
.build();
SslContextBuilder badClientBuilder = GrpcSslContexts.forClient();
badClientBuilder.trustManager(new File(serverCa));
badClientBuilder.keyManager(new File(badCert), new File(badKey));
ManagedChannel goodChannel = NettyChannelBuilder.forAddress("localhost", 8448)
.sslContext(goodClientBuilder.build())
.directExecutor()
.build();
ManagedChannel badChannel = NettyChannelBuilder.forAddress("localhost", 8448)
.sslContext(badClientBuilder.build())
.directExecutor()
.build();
goodClient = new JobServClient(goodChannel);
badClient = new JobServClient(badChannel);
this.clientSslInitialized = true;
} catch (SSLException e) {
this.clientSslInitialized = false;
System.err.println(e.getMessage());
}
ManagedChannel badChannel = NettyChannelBuilder.forAddress("localhost", 8448)
.sslContext(badClientBuilder.build())
.directExecutor()
.build();
goodClient = new JobServClient(goodChannel);
badClient = new JobServClient(badChannel);
this.clientSslInitialized = true;
} catch (SSLException e) {
this.clientSslInitialized = false;
System.err.println(e.getMessage());
}
} else {
this.clientSslInitialized = false;
}
} else {
this.clientSslInitialized = false;
}
}
/*
@ -141,14 +141,14 @@ public class JobServerAuthenticationTest {
*/
@Test
public void certificateAuthenticationTest() {
assertEquals(true, serverSslInitialized);
assertEquals(true, clientSslInitialized);
assertEquals(true, serverSslInitialized);
assertEquals(true, clientSslInitialized);
int result = badClient.sendNewJobMessage("test command");
assertEquals(-2, result);
int result = badClient.sendNewJobMessage("test command");
assertEquals(-2, result);
result = goodClient.sendNewJobMessage("test command");
Boolean assertCondition = result == -2;
assertEquals(assertCondition, false);
result = goodClient.sendNewJobMessage("test command");
Boolean assertCondition = result == -2;
assertEquals(assertCondition, false);
}
}

View file

@ -0,0 +1,49 @@
/*
* ProcessManagerTest
*
* v1.0
*
* May 22, 2019
*/
package JobServ;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertEquals;
/*
* ProcessManagerTest
* Class that performs positive and negative unit tests
* of every public method in ProcessManager. This not
* only unit tests ProcessManager but also integration
* tests it with ProcessController.
*/
public class ProcessManagerTest {
ProcessManager manager;
/*
* ProcessManagerTest constructor
* initializes the process manager
*/
public ProcessManagerTest() {
manager = new ProcessManager();
}
/*
* addProcessTest()
* positive unit test for newProcess
*/
@Test
public void addProcessesTest() {
int pid1 = manager.newProcess("ping google");
assertEquals(0, pid1);
int pid2 = manager.newProcess("ping google");
assertEquals(1, pid2);
}
}