View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.generic;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.DataOutputStream;
27  import java.io.IOException;
28  
29  import org.apache.bcel.Const;
30  import org.apache.bcel.util.ByteSequence;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link MULTIANEWARRAY}.
35   *
36   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.10.1.9.multianewarray">JVM MULTIANEWARRAY specification</a>
37   */
38  class MULTIANEWARRAYTest {
39  
40      private static int dumpedDimensionsByte(final MULTIANEWARRAY instruction) throws IOException {
41          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
42          try (DataOutputStream dos = new DataOutputStream(bos)) {
43              instruction.dump(dos);
44          }
45          // opcode, u2 index, u1 dimensions
46          return bos.toByteArray()[3] & 0xFF;
47      }
48  
49      @Test
50      void testDimensionsRoundTrips() throws IOException {
51          assertEquals(Const.MAX_BYTE, dumpedDimensionsByte(new MULTIANEWARRAY(1, (short) Const.MAX_BYTE)));
52      }
53  
54      /**
55       * The {@code dimensions} operand of {@code multianewarray} is an unsigned byte (1-255), so a value above 127 must not be read as a negative number.
56       */
57      @Test
58      void testInitFromFileReadsDimensionsUnsigned() throws Exception {
59          // multianewarray, index 0x0005, dimensions 0xC8 (200)
60          final byte[] code = { (byte) Const.MULTIANEWARRAY, 0x00, 0x05, (byte) 0xC8 };
61          try (ByteSequence bytes = new ByteSequence(code)) {
62              final MULTIANEWARRAY instruction = (MULTIANEWARRAY) Instruction.readInstruction(bytes);
63              assertEquals(200, instruction.getDimensions());
64          }
65      }
66  
67      /**
68       * The {@code dimensions} operand of {@code multianewarray} is an unsigned byte (1-255) and {@code dump} writes it with {@code writeByte}, so the
69       * constructor must reject values above {@link Const#MAX_BYTE} instead of truncating them.
70       */
71      @Test
72      void testRejectsDimensionsAboveU1() {
73          assertEquals(Const.MAX_BYTE, new MULTIANEWARRAY(1, (short) Const.MAX_BYTE).getDimensions());
74          assertThrows(ClassGenException.class, () -> new MULTIANEWARRAY(1, (short) (Const.MAX_BYTE + 1)));
75      }
76  }