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.verifier;
21  
22  import java.io.IOException;
23  
24  import org.apache.bcel.verifier.tests.TestArrayAccess02Creator;
25  import org.apache.bcel.verifier.tests.TestArrayAccess03Creator;
26  import org.apache.bcel.verifier.tests.TestArrayAccess04DoubleCreator;
27  import org.apache.bcel.verifier.tests.TestArrayAccess04FloatCreator;
28  import org.apache.bcel.verifier.tests.TestArrayAccess04IntCreator;
29  import org.apache.bcel.verifier.tests.TestArrayAccess04LongCreator;
30  import org.apache.bcel.verifier.tests.TestArrayAccess04ShortCreator;
31  import org.apache.bcel.verifier.tests.TestArrayAccess04UnknownCreator;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  class VerifierArrayAccessTest extends AbstractVerifierTest {
36  
37      @Test
38      void testInvalidArrayAccess() throws IOException, ClassNotFoundException {
39          new TestArrayAccess03Creator().create();
40          assertVerifyRejected("TestArrayAccess03", "Verification of an arraystore instruction on an object must fail.");
41          new TestArrayAccess04IntCreator().create();
42          assertVerifyRejected("TestArrayAccess04Int", "Verification of an arraystore instruction of an int on an array of references must fail.");
43          new TestArrayAccess04FloatCreator().create();
44          assertVerifyRejected("TestArrayAccess04Float", "Verification of an arraystore instruction of a float on an array of references must fail.");
45          new TestArrayAccess04DoubleCreator().create();
46          assertVerifyRejected("TestArrayAccess04Double", "Verification of an arraystore instruction of a double on an array of references must fail.");
47          new TestArrayAccess04LongCreator().create();
48          assertVerifyRejected("TestArrayAccess04Long", "Verification of an arraystore instruction of a long on an array of references must fail.");
49          new TestArrayAccess04ShortCreator().create();
50          assertVerifyRejected("TestArrayAccess04Short", "Verification of an arraystore instruction of a short on an array of references must fail.");
51          final TestArrayAccess04UnknownCreator testArrayAccess04UnknownCreator = new TestArrayAccess04UnknownCreator();
52          Assertions.assertThrowsExactly(IllegalArgumentException.class, testArrayAccess04UnknownCreator::create, "Invalid type <unknown object>");
53      }
54  
55      @Test
56      void testValidArrayAccess() throws IOException, ClassNotFoundException {
57          assertVerifyOK("TestArrayAccess01", "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass.");
58          new TestArrayAccess02Creator().create();
59          assertVerifyOK("TestArrayAccess02", "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass.");
60      }
61  
62  }