Skip to content

Commit

Permalink
Issue #84: Run two test suites with anonymous and authenticated agents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mk23 committed Jan 7, 2016
1 parent ac16a0b commit 23ae762
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 32 deletions.
47 changes: 42 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,48 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=${jmxproxy.test.port}</argLine>
</configuration>
<executions>
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>true</skipTests>
</configuration>
</execution>
<execution>
<id>anonymous</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=${jmxproxy.test.port}
</argLine>
</configuration>
</execution>
<execution>
<id>authenticated</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.access.file=scripts/server/access.txt
-Dcom.sun.management.jmxremote.password.file=scripts/server/passwd.txt
-Dcom.sun.management.jmxremote.port=${jmxproxy.test.port}
</argLine>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.github.mk23.jmxproxy.core.tests;

import com.github.mk23.jmxproxy.JMXProxyConfiguration.JMXProxyApplicationConfiguration;
import com.github.mk23.jmxproxy.jmx.ConnectionCredentials;
import com.github.mk23.jmxproxy.jmx.ConnectionManager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.lang.management.ManagementFactory;
Expand All @@ -27,10 +31,13 @@
public class AttributeTest {
private final ObjectMapper om = new ObjectMapper();

private final String passwdFile = System.getProperty("com.sun.management.jmxremote.password.file");

private final String validHost = "localhost:" + System.getProperty("com.sun.management.jmxremote.port");
private final String validMBean = "AttributeTest:type=test";

private final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());
private final ConnectionCredentials validAuth;

public interface AttributeTestJMXMBean {
Object getNullValue();
Expand Down Expand Up @@ -116,6 +123,13 @@ public String getMultiJsonStringValue() {
}

public AttributeTest() throws Exception {
if (passwdFile != null) {
String[] creds = new BufferedReader(new FileReader(new File(passwdFile))).readLine().split("\\s+");
validAuth = new ConnectionCredentials(creds[0], creds[1]);
} else {
validAuth = null;
}

try {
ManagementFactory.getPlatformMBeanServer().registerMBean(
new AttributeTestJMX(), new ObjectName(validMBean)
Expand All @@ -137,7 +151,7 @@ private String jsonFixture(String filename) throws IOException {
public void checkNull() throws Exception {
final String attributeKey = "NullValue";
final String expectedResult = new String("null");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check null serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -146,7 +160,7 @@ public void checkNull() throws Exception {
public void checkBoolean() throws Exception {
final String attributeKey = "BooleanValue";
final String expectedResult = new String("true");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check boolean serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -155,7 +169,7 @@ public void checkBoolean() throws Exception {
public void checkBoxedBooleanArray() throws Exception {
final String attributeKey = "BoxedBooleanArrayValue";
final String expectedResult = jsonFixture("fixtures/boxed_boolean_array.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check boolean array serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -164,7 +178,7 @@ public void checkBoxedBooleanArray() throws Exception {
public void checkInt() throws Exception {
final String attributeKey = "IntValue";
final String expectedResult = new String("1");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check int serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -173,7 +187,7 @@ public void checkInt() throws Exception {
public void checkBoxedIntegerArray() throws Exception {
final String attributeKey = "BoxedIntegerArrayValue";
final String expectedResult = jsonFixture("fixtures/boxed_integer_array.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check integer array serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -182,7 +196,7 @@ public void checkBoxedIntegerArray() throws Exception {
public void checkDouble() throws Exception {
final String attributeKey = "DoubleValue";
final String expectedResult = new String("1.23");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check double serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -191,7 +205,7 @@ public void checkDouble() throws Exception {
public void checkBoxedDoubleArray() throws Exception {
final String attributeKey = "BoxedDoubleArrayValue";
final String expectedResult = jsonFixture("fixtures/boxed_double_array.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check double array serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -200,7 +214,7 @@ public void checkBoxedDoubleArray() throws Exception {
public void checkListInteger() throws Exception {
final String attributeKey = "ListIntegerValue";
final String expectedResult = jsonFixture("fixtures/list_integer.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check integer list serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -209,7 +223,7 @@ public void checkListInteger() throws Exception {
public void checkListListInteger() throws Exception {
final String attributeKey = "ListListIntegerValue";
final String expectedResult = jsonFixture("fixtures/list_list_integer.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check list of integer lists serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -218,7 +232,7 @@ public void checkListListInteger() throws Exception {
public void checkString() throws Exception {
final String attributeKey = "StringValue";
final String expectedResult = new String("\"val\"");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check string serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -227,7 +241,7 @@ public void checkString() throws Exception {
public void checkStringArray() throws Exception {
final String attributeKey = "StringArrayValue";
final String expectedResult = jsonFixture("fixtures/list_string.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check string array serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -236,7 +250,7 @@ public void checkStringArray() throws Exception {
public void checkListString() throws Exception {
final String attributeKey = "ListStringValue";
final String expectedResult = jsonFixture("fixtures/list_string.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check string list serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -245,7 +259,7 @@ public void checkListString() throws Exception {
public void checkListListString() throws Exception {
final String attributeKey = "ListListStringValue";
final String expectedResult = jsonFixture("fixtures/list_list_string.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check list of string lists serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -255,7 +269,7 @@ public void checkListListString() throws Exception {
public void checkSingleJsonString() throws Exception {
final String attributeKey = "SingleJsonStringValue";
final String expectedResult = new String("\"val\"");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));
assertThat("check single json string serialization", acquiredResult, is(expectedResult));
}
Expand All @@ -265,7 +279,7 @@ public void checkSingleJsonString() throws Exception {
public void checkMultiJsonString() throws Exception {
final String attributeKey = "MultiJsonStringValue";
final String expectedResult = jsonFixture("fixtures/multi_json_string.json");
final String acquiredResult = asJson(manager.getHost(validHost).getMBean(validMBean).getAttribute(attributeKey));
final String acquiredResult = asJson(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(attributeKey));

assertThat("check multiple json strings serialization", acquiredResult, is(expectedResult));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.github.mk23.jmxproxy.JMXProxyConfiguration.JMXProxyApplicationConfiguration;
import com.github.mk23.jmxproxy.jmx.ConnectionManager;
import com.github.mk23.jmxproxy.jmx.ConnectionCredentials;

import io.dropwizard.util.Duration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.lang.management.ManagementFactory;

import java.util.Arrays;
Expand All @@ -20,6 +25,8 @@
import static org.junit.Assert.assertTrue;

public class ConnectionManagerTest {
private final String passwdFile = System.getProperty("com.sun.management.jmxremote.password.file");

private final String validHost = "localhost:" + System.getProperty("com.sun.management.jmxremote.port");
private final String invalidHost = "localhost:0";

Expand All @@ -30,13 +37,22 @@ public class ConnectionManagerTest {
private final String validAttribute = "Name";
private final String invalidAttribute = "InvalidAttribute";

private final ConnectionCredentials validAuth;

public interface ConnectionManagerTestJMXMBean {
}

public class ConnectionManagerTestJMX implements ConnectionManagerTestJMXMBean {
}

public ConnectionManagerTest() throws Exception {
if (passwdFile != null) {
String[] creds = new BufferedReader(new FileReader(new File(passwdFile))).readLine().split("\\s+");
validAuth = new ConnectionCredentials(creds[0], creds[1]);
} else {
validAuth = null;
}

try {
ManagementFactory.getPlatformMBeanServer().registerMBean(
new ConnectionManagerTestJMX(), new ObjectName(localMBean)
Expand All @@ -50,14 +66,14 @@ public ConnectionManagerTest() throws Exception {
public void checkValidHost() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNotNull(manager.getHost(validHost));
assertNotNull(manager.getHost(validHost, validAuth));
}

@Test
public void checkInvalidHost() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNull(manager.getHost(invalidHost));
assertNull(manager.getHost(invalidHost, validAuth));
}

@Test
Expand All @@ -67,7 +83,7 @@ public void checkValidHostWhitelist() throws Exception {

final ConnectionManager manager = new ConnectionManager(serviceConfig);

assertNotNull(manager.getHost(validHost));
assertNotNull(manager.getHost(validHost, validAuth));
}

@Test(expected=WebApplicationException.class)
Expand All @@ -77,66 +93,66 @@ public void checkInvalidHostWhitelist() throws Exception {

final ConnectionManager manager = new ConnectionManager(serviceConfig);

manager.getHost(invalidHost);
manager.getHost(invalidHost, validAuth);
}

/* MBean tests */
@Test
public void checkValidHostMBeans() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertTrue(manager.getHost(validHost).getMBeans().contains(validMBean));
assertTrue(manager.getHost(validHost, validAuth).getMBeans().contains(validMBean));
}

@Test
public void checkValidHostValidMBean() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNotNull(manager.getHost(validHost).getMBean(validMBean));
assertNotNull(manager.getHost(validHost, validAuth).getMBean(validMBean));
}

@Test
public void checkValidHostInvalidMBean() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNull(manager.getHost(validHost).getMBean(invalidMBean));
assertNull(manager.getHost(validHost, validAuth).getMBean(invalidMBean));
}

/* Attribute tests */
@Test
public void checkValidHostValidMBeanAttributes() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertTrue(manager.getHost(validHost).getMBean(validMBean).getAttributes().contains(validAttribute));
assertTrue(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttributes().contains(validAttribute));
}

@Test
public void checkValidHostValidMBeanValidAttribute() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNotNull(manager.getHost(validHost).getMBean(validMBean).getAttribute(validAttribute));
assertNotNull(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(validAttribute));
}

@Test
public void checkValidHostValidMBeanInvalidAttribute() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration());

assertNull(manager.getHost(validHost).getMBean(validMBean).getAttribute(invalidAttribute));
assertNull(manager.getHost(validHost, validAuth).getMBean(validMBean).getAttribute(invalidAttribute));
}

/* Custom MBean tests */
@Test
public void checkValidHostRemovedMBean() throws Exception {
final ConnectionManager manager = new ConnectionManager(new JMXProxyApplicationConfiguration().setCacheDuration(Duration.seconds(3)));

assertNotNull(manager.getHost(validHost).getMBean(localMBean));
assertNotNull(manager.getHost(validHost, validAuth).getMBean(localMBean));

ManagementFactory.getPlatformMBeanServer().unregisterMBean(
new ObjectName(localMBean)
);

java.lang.Thread.sleep(Duration.seconds(5).toMilliseconds());

assertNull(manager.getHost(validHost).getMBean(localMBean));
assertNull(manager.getHost(validHost, validAuth).getMBean(localMBean));
}
}

0 comments on commit 23ae762

Please sign in to comment.