|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.activemq.openwire; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import java.io.DataOutput; |
| 22 | +import java.io.IOException; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | +import org.apache.activemq.command.CommandTypes; |
| 28 | +import org.apache.activemq.command.ExceptionResponse; |
| 29 | +import org.apache.activemq.util.ByteSequence; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.Parameterized; |
| 33 | +import org.junit.runners.Parameterized.Parameters; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test that Openwire marshalling will validate Throwable types during |
| 37 | + * unmarshalling commands that contain a Throwable |
| 38 | + */ |
| 39 | +@RunWith(Parameterized.class) |
| 40 | +public class OpenWireValidationTest { |
| 41 | + |
| 42 | + protected final int version; |
| 43 | + |
| 44 | + @Parameters(name = "version={0}") |
| 45 | + public static Collection<Object[]> data() { |
| 46 | + List<Integer> versions = List.of(1, 9, 10, 11, 12); |
| 47 | + List<Object[]> versionObjs = new ArrayList<>(); |
| 48 | + for (int i : versions) { |
| 49 | + versionObjs.add(new Object[]{i}); |
| 50 | + } |
| 51 | + |
| 52 | + // Sanity check to make sure the latest generated version is contained in the list |
| 53 | + // This will make sure that we don't forget to update this test to include |
| 54 | + // any future versions that are generated |
| 55 | + assertTrue("List of Openwire versions does not include latest version", |
| 56 | + versions.contains((int)CommandTypes.PROTOCOL_VERSION)); |
| 57 | + |
| 58 | + return versionObjs; |
| 59 | + } |
| 60 | + |
| 61 | + public OpenWireValidationTest(int version) { |
| 62 | + this.version = version; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testOpenwireThrowableValidation() throws Exception { |
| 67 | + // Create a format which will use loose encoding by default |
| 68 | + // The code for handling exception creation is shared between both |
| 69 | + // tight/loose encoding so only need to test 1 |
| 70 | + OpenWireFormat format = new OpenWireFormat(); |
| 71 | + |
| 72 | + // Override the marshaller map with a custom impl to purposely marshal a class type that is |
| 73 | + // not a Throwable for testing the unmarshaller |
| 74 | + Class<?> marshallerFactory = getMarshallerFactory(); |
| 75 | + Method createMarshallerMap = marshallerFactory.getMethod("createMarshallerMap", OpenWireFormat.class); |
| 76 | + DataStreamMarshaller[] map = (DataStreamMarshaller[]) createMarshallerMap.invoke(marshallerFactory, format); |
| 77 | + map[ExceptionResponse.DATA_STRUCTURE_TYPE] = getExceptionMarshaller(); |
| 78 | + // This will trigger updating the marshaller from the marshaller map with the right version |
| 79 | + format.setVersion(version); |
| 80 | + |
| 81 | + // Build the response and try to unmarshal which should give an IllegalArgumentExeption on unmarshall |
| 82 | + // as the test marshaller should have encoded a class type that is not a Throwable |
| 83 | + ExceptionResponse r = new ExceptionResponse(); |
| 84 | + r.setException(new Exception()); |
| 85 | + ByteSequence bss = format.marshal(r); |
| 86 | + ExceptionResponse response = (ExceptionResponse) format.unmarshal(bss); |
| 87 | + |
| 88 | + assertTrue(response.getException() instanceof IllegalArgumentException); |
| 89 | + assertTrue(response.getException().getMessage().contains("is not assignable to Throwable")); |
| 90 | + } |
| 91 | + |
| 92 | + static class NotAThrowable { |
| 93 | + private String message; |
| 94 | + |
| 95 | + public NotAThrowable(String message) { |
| 96 | + this.message = message; |
| 97 | + } |
| 98 | + |
| 99 | + public NotAThrowable() { |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private Class<?> getMarshallerFactory() throws ClassNotFoundException { |
| 104 | + return Class.forName("org.apache.activemq.openwire.v" + version + ".MarshallerFactory"); |
| 105 | + } |
| 106 | + |
| 107 | + // Create test marshallers for all non-legacy versions that will encode NotAThrowable |
| 108 | + // instead of the exception type for testing purposes |
| 109 | + protected DataStreamMarshaller getExceptionMarshaller() { |
| 110 | + switch (version) { |
| 111 | + case 12: |
| 112 | + return new org.apache.activemq.openwire.v12.ExceptionResponseMarshaller() { |
| 113 | + @Override |
| 114 | + protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, |
| 115 | + DataOutput dataOut) throws IOException { |
| 116 | + dataOut.writeBoolean(o != null); |
| 117 | + looseMarshalString(NotAThrowable.class.getName(), dataOut); |
| 118 | + looseMarshalString(o.getMessage(), dataOut); |
| 119 | + } |
| 120 | + }; |
| 121 | + case 11: |
| 122 | + return new org.apache.activemq.openwire.v11.ExceptionResponseMarshaller() { |
| 123 | + @Override |
| 124 | + protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, |
| 125 | + DataOutput dataOut) throws IOException { |
| 126 | + dataOut.writeBoolean(o != null); |
| 127 | + looseMarshalString(NotAThrowable.class.getName(), dataOut); |
| 128 | + looseMarshalString(o.getMessage(), dataOut); |
| 129 | + } |
| 130 | + }; |
| 131 | + case 10: |
| 132 | + return new org.apache.activemq.openwire.v10.ExceptionResponseMarshaller() { |
| 133 | + @Override |
| 134 | + protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, |
| 135 | + DataOutput dataOut) throws IOException { |
| 136 | + dataOut.writeBoolean(o != null); |
| 137 | + looseMarshalString(NotAThrowable.class.getName(), dataOut); |
| 138 | + looseMarshalString(o.getMessage(), dataOut); |
| 139 | + } |
| 140 | + }; |
| 141 | + case 9: |
| 142 | + return new org.apache.activemq.openwire.v9.ExceptionResponseMarshaller() { |
| 143 | + @Override |
| 144 | + protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, |
| 145 | + DataOutput dataOut) throws IOException { |
| 146 | + dataOut.writeBoolean(o != null); |
| 147 | + looseMarshalString(NotAThrowable.class.getName(), dataOut); |
| 148 | + looseMarshalString(o.getMessage(), dataOut); |
| 149 | + } |
| 150 | + }; |
| 151 | + case 1: |
| 152 | + return new org.apache.activemq.openwire.v1.ExceptionResponseMarshaller() { |
| 153 | + @Override |
| 154 | + protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, |
| 155 | + DataOutput dataOut) throws IOException { |
| 156 | + dataOut.writeBoolean(o != null); |
| 157 | + looseMarshalString(NotAThrowable.class.getName(), dataOut); |
| 158 | + looseMarshalString(o.getMessage(), dataOut); |
| 159 | + } |
| 160 | + }; |
| 161 | + default: |
| 162 | + throw new IllegalArgumentException("Unknown openwire version of " + version); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments