Repository: qpid-broker-j Updated Branches: refs/heads/master 52ee3bff0 -> 6c0c5b1ff
QPID-6933: [System Tests] Refactor ObjectMessageClassWhitelistingTest as JMS 1.1 system test Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/6c0c5b1f Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/6c0c5b1f Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/6c0c5b1f Branch: refs/heads/master Commit: 6c0c5b1ffde7232b36a0b6b1422c7826323bff11 Parents: 52ee3bf Author: Alex Rudyy <[email protected]> Authored: Wed Jan 3 16:32:05 2018 +0000 Committer: Alex Rudyy <[email protected]> Committed: Wed Jan 3 16:32:05 2018 +0000 ---------------------------------------------------------------------- .../apache/qpid/systests/ConnectionBuilder.java | 2 + .../QpidJmsClient0xConnectionBuilder.java | 14 + .../QpidJmsClientConnectionBuilder.java | 14 + .../ObjectMessageClassWhitelistingTest.java | 374 +++++++++++++++++++ .../ObjectMessageClassWhitelistingTest.java | 340 ----------------- test-profiles/Java10Excludes | 3 - 6 files changed, 404 insertions(+), 343 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/ConnectionBuilder.java ---------------------------------------------------------------------- diff --git a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/ConnectionBuilder.java b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/ConnectionBuilder.java index 085b7b1..92454e9 100644 --- a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/ConnectionBuilder.java +++ b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/ConnectionBuilder.java @@ -47,6 +47,8 @@ public interface ConnectionBuilder ConnectionBuilder setOptions(Map<String, String> options); ConnectionBuilder setPopulateJMSXUserID(boolean populateJMSXUserID); ConnectionBuilder setMessageRedelivery(final boolean redelivery); + ConnectionBuilder setDeserializationPolicyWhiteList(String whiteList); + ConnectionBuilder setDeserializationPolicyBlackList(String blackList); Connection build() throws NamingException, JMSException; ConnectionFactory buildConnectionFactory() throws NamingException; http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClient0xConnectionBuilder.java ---------------------------------------------------------------------- diff --git a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClient0xConnectionBuilder.java b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClient0xConnectionBuilder.java index 27805c9..eeb82ac 100644 --- a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClient0xConnectionBuilder.java +++ b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClient0xConnectionBuilder.java @@ -165,6 +165,20 @@ public class QpidJmsClient0xConnectionBuilder implements ConnectionBuilder } @Override + public ConnectionBuilder setDeserializationPolicyWhiteList(final String whiteList) + { + _options.put("objectMessageClassHierarchyWhiteList", whiteList); + return this; + } + + @Override + public ConnectionBuilder setDeserializationPolicyBlackList(final String blackList) + { + _options.put("objectMessageClassHierarchyBlackList", blackList); + return this; + } + + @Override public Connection build() throws JMSException, NamingException { return buildConnectionFactory().createConnection(_username, _password); http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClientConnectionBuilder.java ---------------------------------------------------------------------- diff --git a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClientConnectionBuilder.java b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClientConnectionBuilder.java index 24cdf45..2db1746 100644 --- a/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClientConnectionBuilder.java +++ b/systests/qpid-systests-jms-core/src/main/java/org/apache/qpid/systests/QpidJmsClientConnectionBuilder.java @@ -179,6 +179,20 @@ public class QpidJmsClientConnectionBuilder implements ConnectionBuilder } @Override + public ConnectionBuilder setDeserializationPolicyWhiteList(final String whiteList) + { + _options.put("jms.deserializationPolicy.whiteList", whiteList); + return this; + } + + @Override + public ConnectionBuilder setDeserializationPolicyBlackList(final String blackList) + { + _options.put("jms.deserializationPolicy.blackList", blackList); + return this; + } + + @Override public Connection build() throws NamingException, JMSException { return buildConnectionFactory().createConnection(); http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/extensions/message/ObjectMessageClassWhitelistingTest.java ---------------------------------------------------------------------- diff --git a/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/extensions/message/ObjectMessageClassWhitelistingTest.java b/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/extensions/message/ObjectMessageClassWhitelistingTest.java new file mode 100644 index 0000000..b468468 --- /dev/null +++ b/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/extensions/message/ObjectMessageClassWhitelistingTest.java @@ -0,0 +1,374 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.systests.jms_1_1.extensions.message; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.util.HashMap; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageFormatException; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Queue; +import javax.jms.Session; + +import org.junit.Test; + +import org.apache.qpid.systests.JmsTestBase; + +public class ObjectMessageClassWhitelistingTest extends JmsTestBase +{ + private static final int TEST_VALUE = 37; + + @Test + public void testObjectMessage() throws Exception + { + Queue destination = createQueue(getTestName()); + final Connection c = getConnectionBuilder().setDeserializationPolicyWhiteList("*").build(); + try + { + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer _consumer = s.createConsumer(destination); + MessageProducer _producer = s.createProducer(destination); + + sendTestObjectMessage(s, _producer); + Message receivedMessage = _consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; + Object payloadObject = receivedObjectMessage.getObject(); + assertTrue("payload is of wrong type", payloadObject instanceof HashMap); + + @SuppressWarnings("unchecked") + HashMap<String, Integer> payload = (HashMap<String, Integer>) payloadObject; + assertEquals("payload has wrong value", (Integer) TEST_VALUE, payload.get("value")); + } + finally + { + c.close(); + } + } + + @Test + public void testNotWhiteListedByConnectionUrlObjectMessage() throws Exception + { + Queue destination = createQueue(getTestName()); + final Connection c = getConnectionBuilder().setDeserializationPolicyWhiteList("org.apache.qpid").build(); + try + { + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = s.createConsumer(destination); + MessageProducer producer = s.createProducer(destination); + + sendTestObjectMessage(s, producer); + Message receivedMessage = consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; + try + { + receivedObjectMessage.getObject(); + fail("should not deserialize class"); + } + catch (MessageFormatException e) + { + // pass + } + } + finally + { + c.close(); + } + } + + @Test + public void testWhiteListedClassByConnectionUrlObjectMessage() throws Exception + { + Queue destination = createQueue(getTestName()); + final Connection c = + getConnectionBuilder().setDeserializationPolicyWhiteList("java.util.HashMap,java.lang").build(); + try + { + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = s.createConsumer(destination); + MessageProducer producer = s.createProducer(destination); + + sendTestObjectMessage(s, producer); + Message receivedMessage = consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; + + @SuppressWarnings("unchecked") + HashMap<String, Integer> object = (HashMap<String, Integer>) receivedObjectMessage.getObject(); + assertEquals("Unexpected value", (Integer) TEST_VALUE, object.get("value")); + } + finally + { + c.close(); + } + } + + @Test + public void testBlackListedClassByConnectionUrlObjectMessage() throws Exception + { + Queue destination = createQueue(getTestName()); + final Connection c = getConnectionBuilder().setDeserializationPolicyWhiteList("java") + .setDeserializationPolicyBlackList("java.lang.Integer") + .build(); + try + { + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = s.createConsumer(destination); + MessageProducer producer = s.createProducer(destination); + + sendTestObjectMessage(s, producer); + Message receivedMessage = consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; + + try + { + receivedObjectMessage.getObject(); + fail("Should not be allowed to deserialize black listed class"); + } + catch (JMSException e) + { + // pass + } + } + finally + { + c.close(); + } + } + + @Test + public void testWhiteListedAnonymousClassByConnectionUrlObjectMessage() throws Exception + { + final Connection c = + getConnectionBuilder().setDeserializationPolicyWhiteList(ObjectMessageClassWhitelistingTest.class.getCanonicalName()) + .build(); + try + { + doTestWhiteListedEnclosedClassTest(c, createAnonymousObject(TEST_VALUE)); + } + finally + { + c.close(); + } + } + + @Test + public void testBlackListedAnonymousClassByConnectionUrlObjectMessage() throws Exception + { + final Connection c = getConnectionBuilder() + .setDeserializationPolicyWhiteList(ObjectMessageClassWhitelistingTest.class.getPackage().getName()) + .setDeserializationPolicyBlackList(ObjectMessageClassWhitelistingTest.class.getCanonicalName()) + .build(); + try + { + doTestBlackListedEnclosedClassTest(c, createAnonymousObject(TEST_VALUE)); + } + finally + { + c.close(); + } + } + + @Test + public void testWhiteListedNestedClassByConnectionUrlObjectMessage() throws Exception + { + final Connection c = getConnectionBuilder() + .setDeserializationPolicyWhiteList(ObjectMessageClassWhitelistingTest.NestedClass.class.getCanonicalName()) + .build(); + try + { + doTestWhiteListedEnclosedClassTest(c, new NestedClass(TEST_VALUE)); + } + finally + { + c.close(); + } + } + + @Test + public void testBlackListedNestedClassByConnectionUrlObjectMessage() throws Exception + { + final Connection c = getConnectionBuilder() + .setDeserializationPolicyWhiteList(ObjectMessageClassWhitelistingTest.class.getCanonicalName()) + .setDeserializationPolicyBlackList(NestedClass.class.getCanonicalName()) + .build(); + try + { + doTestBlackListedEnclosedClassTest(c, new NestedClass(TEST_VALUE)); + } + finally + { + c.close(); + } + } + + private void doTestWhiteListedEnclosedClassTest(Connection c, Serializable content) throws Exception + { + Queue destination = createQueue(getTestName()); + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = s.createConsumer(destination); + MessageProducer producer = s.createProducer(destination); + + final ObjectMessage sendMessage = s.createObjectMessage(); + sendMessage.setObject(content); + producer.send(sendMessage); + + Message receivedMessage = consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + Object receivedObject = ((ObjectMessage) receivedMessage).getObject(); + assertEquals("Received object has unexpected class", content.getClass(), receivedObject.getClass()); + assertEquals("Received object has unexpected content", content, receivedObject); + } + + private void doTestBlackListedEnclosedClassTest(final Connection c, final Serializable content) throws Exception + { + Queue destination = createQueue(getTestName()); + c.start(); + Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = s.createConsumer(destination); + MessageProducer producer = s.createProducer(destination); + + final ObjectMessage sendMessage = s.createObjectMessage(); + sendMessage.setObject(content); + producer.send(sendMessage); + + Message receivedMessage = consumer.receive(getReceiveTimeout()); + assertNotNull("did not receive message within receive timeout", receivedMessage); + assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); + try + { + ((ObjectMessage) receivedMessage).getObject(); + fail("Exception not thrown"); + } + catch (MessageFormatException e) + { + // pass + } + } + + private void sendTestObjectMessage(final Session s, final MessageProducer producer) throws JMSException + { + HashMap<String, Integer> messageContent = new HashMap<>(); + messageContent.put("value", TEST_VALUE); + Message objectMessage = s.createObjectMessage(messageContent); + producer.send(objectMessage); + } + + public static Serializable createAnonymousObject(final int field) + { + return new Serializable() + { + private int _field = field; + + @Override + public int hashCode() + { + return _field; + } + + @Override + public boolean equals(final Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + final Serializable that = (Serializable) o; + + return getFieldValueByReflection(that).equals(_field); + } + + private Object getFieldValueByReflection(final Serializable that) + { + try + { + final Field f = that.getClass().getDeclaredField("_field"); + f.setAccessible(true); + return f.get(that); + } + catch (NoSuchFieldException | IllegalAccessException e) + { + throw new RuntimeException(e); + } + } + }; + } + + public static class NestedClass implements Serializable + { + private final int _field; + + public NestedClass(final int field) + { + _field = field; + } + + @Override + public boolean equals(final Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + final NestedClass that = (NestedClass) o; + + return _field == that._field; + } + + @Override + public int hashCode() + { + return _field; + } + } +} http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/systests/src/test/java/org/apache/qpid/client/message/ObjectMessageClassWhitelistingTest.java ---------------------------------------------------------------------- diff --git a/systests/src/test/java/org/apache/qpid/client/message/ObjectMessageClassWhitelistingTest.java b/systests/src/test/java/org/apache/qpid/client/message/ObjectMessageClassWhitelistingTest.java deleted file mode 100644 index 391f165..0000000 --- a/systests/src/test/java/org/apache/qpid/client/message/ObjectMessageClassWhitelistingTest.java +++ /dev/null @@ -1,340 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.client.message; - -import java.io.Serializable; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageConsumer; -import javax.jms.MessageFormatException; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Queue; -import javax.jms.Session; - -import org.apache.qpid.configuration.CommonProperties; -import org.apache.qpid.jms.ConnectionURL; -import org.apache.qpid.test.utils.QpidBrokerTestCase; - -public class ObjectMessageClassWhitelistingTest extends QpidBrokerTestCase -{ - public static final int TIMEOUT = 10000; - public static final int TEST_VALUE = 37; - private MessageConsumer _consumer; - private MessageProducer _producer; - - public void testObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, "*"); - final Connection c = getConnectionWithOptions(connectionOptions); - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - _consumer = s.createConsumer(destination); - _producer = s.createProducer(destination); - - sendTestObjectMessage(s, _producer); - Message receivedMessage = _consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; - Object payloadObject = receivedObjectMessage.getObject(); - assertTrue("payload is of wrong type", payloadObject instanceof HashMap); - HashMap<String,Integer> payload = (HashMap<String, Integer>) payloadObject; - assertEquals("payload has wrong value", (Integer)TEST_VALUE, payload.get("value")); - } - - public void testNotWhiteListedByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, "org.apache.qpid"); - final Connection c = getConnectionWithOptions(connectionOptions); - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - sendTestObjectMessage(s, producer); - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; - try - { - receivedObjectMessage.getObject(); - fail("should not deserialize class"); - } - catch (MessageFormatException e) - { - // pass - } - } - - public void testWhiteListedClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, "java.util.HashMap,java.lang"); - final Connection c = getConnectionWithOptions(connectionOptions); - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - sendTestObjectMessage(s, producer); - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; - HashMap<String, Integer> object = (HashMap<String, Integer>) receivedObjectMessage.getObject(); - assertEquals("Unexpected value", (Integer) TEST_VALUE, object.get("value")); - } - - public void testBlackListedClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, "java"); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_BLACK_LIST, "java.lang.Integer"); - final Connection c = getConnectionWithOptions(connectionOptions); - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - sendTestObjectMessage(s, producer); - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; - - try - { - receivedObjectMessage.getObject(); - fail("Should not be allowed to deserialize black listed class"); - } - catch (JMSException e) - { - // pass - } - } - - public void testNonWhiteListedBySystemPropertyObjectMessage() throws Exception - { - setSystemProperty(CommonProperties.QPID_SECURITY_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, "org.apache.qpid"); - final Connection c = getConnection(); - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - sendTestObjectMessage(s, producer); - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) receivedMessage; - try - { - receivedObjectMessage.getObject(); - fail("should not deserialize class"); - } - catch (MessageFormatException e) - { - // pass - } - } - - public void testWhiteListedAnonymousClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, ObjectMessageClassWhitelistingTest.class.getCanonicalName()); - final Connection c = getConnectionWithOptions(connectionOptions); - doTestWhiteListedEnclosedClassTest(c, createAnonymousObject(TEST_VALUE)); - } - - public void testBlackListedAnonymousClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, ObjectMessageClassWhitelistingTest.class.getPackage().getName()); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_BLACK_LIST, ObjectMessageClassWhitelistingTest.class.getCanonicalName()); - - final Connection c = getConnectionWithOptions(connectionOptions); - doTestBlackListedEnclosedClassTest(c, createAnonymousObject(TEST_VALUE)); - } - - public void testWhiteListedNestedClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, ObjectMessageClassWhitelistingTest.NestedClass.class.getCanonicalName()); - final Connection c = getConnectionWithOptions(connectionOptions); - doTestWhiteListedEnclosedClassTest(c, new NestedClass(TEST_VALUE)); - } - - public void testBlackListedNestedClassByConnectionUrlObjectMessage() throws Exception - { - Map<String, String> connectionOptions = new HashMap<>(); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_WHITE_LIST, ObjectMessageClassWhitelistingTest.class.getCanonicalName()); - connectionOptions.put(ConnectionURL.OPTIONS_OBJECT_MESSAGE_CLASS_HIERARCHY_BLACK_LIST, NestedClass.class.getCanonicalName()); - - final Connection c = getConnectionWithOptions(connectionOptions); - doTestBlackListedEnclosedClassTest(c, new NestedClass(TEST_VALUE)); - } - - private void doTestWhiteListedEnclosedClassTest(Connection c, Serializable content) throws Exception - { - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - final ObjectMessage sendMessage = s.createObjectMessage(); - sendMessage.setObject(content); - producer.send(sendMessage); - - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - Object receivedObject = ((ObjectMessage)receivedMessage).getObject(); - assertEquals("Received object has unexpected class", content.getClass(), receivedObject.getClass()); - assertEquals("Received object has unexpected content", content, receivedObject); - } - - private void doTestBlackListedEnclosedClassTest(final Connection c, final Serializable content) throws Exception - { - c.start(); - Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue destination = getTestQueue(); - MessageConsumer consumer = s.createConsumer(destination); - MessageProducer producer = s.createProducer(destination); - - final ObjectMessage sendMessage = s.createObjectMessage(); - sendMessage.setObject(content); - producer.send(sendMessage); - - Message receivedMessage = consumer.receive(TIMEOUT); - assertNotNull("did not receive message within timeout", receivedMessage); - assertTrue("message is of wrong type", receivedMessage instanceof ObjectMessage); - try - { - ((ObjectMessage)receivedMessage).getObject(); - fail("Exception not thrown"); - } - catch (MessageFormatException e) - { - // pass - } - } - - private void sendTestObjectMessage(final Session s, final MessageProducer producer) throws JMSException - { - HashMap<String, Integer> messageContent = new HashMap<>(); - messageContent.put("value", TEST_VALUE); - Message objectMessage = s.createObjectMessage(messageContent); - producer.send(objectMessage); - } - - public static Serializable createAnonymousObject(final int field) - { - return new Serializable() - { - private int _field = field; - - @Override - public int hashCode() - { - return _field; - } - - @Override - public boolean equals(final Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - final Serializable that = (Serializable) o; - - return getFieldValueByReflection(that).equals(_field); - } - - private Object getFieldValueByReflection(final Serializable that) - { - try - { - final Field f = that.getClass().getDeclaredField("_field"); - f.setAccessible(true); - return f.get(that); - } - catch (NoSuchFieldException | IllegalAccessException e) - { - throw new RuntimeException(e); - } - } - }; - } - - public static class NestedClass implements Serializable - { - private final int _field; - - public NestedClass(final int field) - { - _field = field; - } - - @Override - public boolean equals(final Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - final NestedClass that = (NestedClass) o; - - return _field == that._field; - - } - - @Override - public int hashCode() - { - return _field; - } - } -} http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6c0c5b1f/test-profiles/Java10Excludes ---------------------------------------------------------------------- diff --git a/test-profiles/Java10Excludes b/test-profiles/Java10Excludes index 00eb4c8..c7711a3 100644 --- a/test-profiles/Java10Excludes +++ b/test-profiles/Java10Excludes @@ -33,9 +33,6 @@ org.apache.qpid.server.queue.ModelTest#* // Exclude tests of connection URL failover method property org.apache.qpid.server.failover.FailoverMethodTest#* -// This test covers the client version specific mechanisms for restricting the types of Object which can be sent via an ObjectMessage -org.apache.qpid.client.message.ObjectMessageClassWhitelistingTest#* - // This test mainly covers the AMQP 0-x client's heartbeating implementation org.apache.qpid.client.HeartbeatTest#* --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
