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   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.crypto;
20  
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assumptions.assumeTrue;
25  
26  import java.io.File;
27  
28  import org.junit.jupiter.api.Disabled;
29  import org.junit.jupiter.api.Test;
30  
31  
32  public class NativeCodeLoaderTest {
33  
34      @Test
35      public void test() {
36          assertTrue(NativeCodeLoader.isNativeCodeLoaded(), "Native (JNI) code loaded successfully");
37      }
38  
39      @Test
40      @Disabled("Causes crash on Ubuntu when compiled with Java 17")
41      // The following error is reported:
42      // "Corrupted channel by directly writing to native stream in forked JVM 1"
43      // Note that this appears during a subsequent test, and does not
44      // happen every time.
45      // At this point it is not known where the native stream is written.
46      public void testCanLoadIfPresent() {
47          assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
48          // This will try to reload the library, so should work
49          assertNull(NativeCodeLoader.loadLibrary());
50      }
51  
52      @Test
53      public void testNativeNotPresent() {
54          assumeTrue(!NativeCodeLoader.isNativeCodeLoaded());
55          assertNotNull(NativeCodeLoader.getLoadingError());
56      }
57  
58      @Test
59      public void testNativePresent() {
60          assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
61          assertNull(NativeCodeLoader.getLoadingError());
62      }
63  
64      @Test
65      @Disabled("Seems to cause issues with other tests on Linux; disable for now")
66      // It causes problems because the system properties are temporarily changed.
67      // However properties are only fetched once, thus the test either corrupts the settings
68      // or does not work, depending on the order of tests.
69      public void testUnSuccessfulLoad() throws Exception {
70          final String nameKey = System.getProperty(Crypto.LIB_NAME_KEY);
71          final String pathKey = System.getProperty(Crypto.LIB_PATH_KEY);
72          // An empty file should cause UnsatisfiedLinkError
73          final File empty = File.createTempFile("NativeCodeLoaderTest", "tmp");
74          try {
75              System.setProperty(Crypto.LIB_PATH_KEY, empty.getParent());
76              System.setProperty(Crypto.LIB_NAME_KEY, empty.getName());
77              final Throwable result = NativeCodeLoader.loadLibrary();
78              assertNotNull(result);
79              assertTrue(result instanceof UnsatisfiedLinkError);
80          } finally {
81              empty.delete();
82              if (nameKey != null) {
83                  System.setProperty(Crypto.LIB_NAME_KEY, nameKey);
84              }
85              if (pathKey != null) {
86                  System.setProperty(Crypto.LIB_PATH_KEY, pathKey);
87              }
88          }
89      }
90  }