Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix initialization failure on unsupported platforms

  • Loading branch information...
commit 5b48029912ef2dbb215b705e204b444f9bf1bfe5 1 parent c5c2d94
@wg authored
View
2  README
@@ -46,7 +46,7 @@ Maven Artifacts
<dependency>
<groupId>com.lambdaworks</groupId>
<artifactId>scrypt</artifactId>
- <version>1.2.0</version>
+ <version>1.2.1</version>
</dependency>
Native Code Implementation
View
8 pom.xml
@@ -5,7 +5,7 @@
<groupId>com.lambdaworks</groupId>
<artifactId>scrypt</artifactId>
- <version>1.2.0</version>
+ <version>1.2.1</version>
<packaging>jar</packaging>
@@ -65,6 +65,7 @@
<executions>
<execution>
<id>sign</id>
+ <phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
@@ -75,7 +76,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>2.8</version>
+ <version>2.10</version>
<configuration>
<useFile>false</useFile>
@@ -90,6 +91,9 @@
<classesDirectory>
${project.build.directory}/${project.build.finalName}.${project.packaging}
</classesDirectory>
+ <includes>
+ <include>**/SCrypt*.java</include>
+ </includes>
</configuration>
<goals>
<goal>test</goal>
View
16 src/main/java/com/lambdaworks/jni/JarLibraryLoader.java
@@ -19,7 +19,6 @@
public class JarLibraryLoader {
private final CodeSource codeSource;
private final String libraryPath;
- private final Platform platform;
/**
* Initialize a new instance that looks for shared libraries located in the same jar
@@ -35,13 +34,10 @@ public JarLibraryLoader() {
*
* @param codeSource Code source containing shared libraries.
* @param libraryPath Path prefix of shared libraries.
- *
- * @throws UnsupportedPlatformException when the platform cannot be detected.
*/
- public JarLibraryLoader(CodeSource codeSource, String libraryPath) throws UnsupportedPlatformException {
+ public JarLibraryLoader(CodeSource codeSource, String libraryPath) {
this.codeSource = codeSource;
this.libraryPath = libraryPath;
- this.platform = Platform.detect();
}
/**
@@ -56,9 +52,10 @@ public boolean load(String name, boolean verify) {
boolean loaded = false;
try {
+ Platform platform = Platform.detect();
JarFile jar = new JarFile(codeSource.getLocation().getPath(), verify);
try {
- for (String path : libCandidates(name)) {
+ for (String path : libCandidates(platform, name)) {
JarEntry entry = jar.getJarEntry(path);
if (entry == null) continue;
@@ -72,7 +69,7 @@ public boolean load(String name, boolean verify) {
} finally {
jar.close();
}
- } catch (Exception e) {
+ } catch (Throwable e) {
loaded = false;
}
@@ -115,11 +112,12 @@ private static File extract(String name, InputStream is) throws IOException {
* Generate a list of candidate libraries for the supplied library name and suitable
* for the current platform.
*
- * @param name Library name.
+ * @param platform Current platform.
+ * @param name Library name.
*
* @return List of potential library names.
*/
- private List<String> libCandidates(String name) {
+ private List<String> libCandidates(Platform platform, String name) {
List<String> candidates = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
View
3  src/main/java/com/lambdaworks/jni/Platform.java
@@ -67,8 +67,7 @@ public static Platform detect() throws UnsupportedPlatformException {
}
}
- String msg = String.format("Unsupported architecture %s and/or OS %s", osArch, osName);
+ String msg = String.format("Unsupported platform %s %s", osArch, osName);
throw new UnsupportedPlatformException(msg);
}
-
}
View
42 src/test/java/com/lambdaworks/jni/test/AbstractPlatformDetectionTest.java
@@ -0,0 +1,42 @@
+// Copyright (C) 2011 - Will Glozer. All rights reserved.
+
+package com.lambdaworks.jni.test;
+
+import com.lambdaworks.jni.Platform;
+import org.junit.After;
+import org.junit.Before;
+
+import static java.lang.System.getProperty;
+import static java.lang.System.setProperty;
+
+public class AbstractPlatformDetectionTest {
+ private String osArch;
+ private String osName;
+
+ @Before
+ public final void saveProperties() {
+ osArch = getProperty("os.arch");
+ osName = getProperty("os.name");
+ }
+
+ @After
+ public final void restoreProperties() {
+ setProperty("os.arch", osArch);
+ setProperty("os.name", osName);
+ }
+
+ protected Platform detectArch(String arch) {
+ setProperty("os.arch", arch);
+ return Platform.detect();
+ }
+
+ protected Platform detectOs(String os) {
+ setProperty("os.name", os);
+ return Platform.detect();
+ }
+
+ protected void setPlatform(String arch, String os) {
+ setProperty("os.arch", arch);
+ setProperty("os.name", os);
+ }
+}
View
70 src/test/java/com/lambdaworks/jni/test/JarLibraryLoaderTest.java
@@ -0,0 +1,70 @@
+// Copyright (C) 2011 - Will Glozer. All rights reserved.
+
+package com.lambdaworks.jni.test;
+
+import com.lambdaworks.jni.JarLibraryLoader;
+import org.junit.Test;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.security.CodeSigner;
+import java.security.CodeSource;
+import java.security.cert.CertPath;
+import java.security.cert.CertificateFactory;
+
+import static java.lang.System.getProperty;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class JarLibraryLoaderTest extends AbstractPlatformDetectionTest {
+ @Test
+ public void loadSigned() throws Exception {
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-signed");
+ assertTrue(loader.load("libscrypt", true));
+ }
+
+ @Test
+ public void loadUnsigned() throws Exception {
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-unsigned");
+ assertTrue(loader.load("libscrypt", false));
+ }
+ @Test
+ public void loadVerifyBadSig() throws Exception {
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-badsig");
+ assertFalse(loader.load("libscrypt", true));
+ }
+
+ @Test
+ public void loadUnsupportedPlatform() throws Exception {
+ setPlatform("PA-RISC", "MPE/iX");
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-signed");
+ assertFalse(loader.load("libscrypt", true));
+ }
+
+ @Test
+ public void loadWrongPlatform() throws Exception {
+ String os = getProperty("os.name").equals("linux") ? "freebsd" : "linux";
+ setPlatform("x86_64", os);
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-signed");
+ assertFalse(loader.load("libscrypt", true));
+ }
+
+ @Test
+ public void loadInvalidEntry() throws Exception {
+ JarLibraryLoader loader = jarLibraryLoader("native-libs-invalid");
+ assertFalse(loader.load("libscrypt", true));
+ }
+
+ protected JarLibraryLoader jarLibraryLoader(String name) throws Exception {
+ URL url = getClass().getResource("/" + name + ".jar");
+ return new JarLibraryLoader(new CodeSource(url, codeSigners()), "lib");
+ }
+
+ protected CodeSigner[] codeSigners() throws Exception {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ InputStream is = getClass().getResourceAsStream("/lambdaWorks.p7");
+ CertPath certPath = cf.generateCertPath(is, "PKCS7");
+ is.close();
+ return new CodeSigner[] { new CodeSigner(certPath, null) };
+ }
+}
View
32 src/test/java/com/lambdaworks/jni/test/PlatformTest.java
@@ -0,0 +1,32 @@
+// Copyright (C) 2011 - Will Glozer. All rights reserved.
+
+package com.lambdaworks.jni.test;
+
+import com.lambdaworks.jni.Platform;
+import com.lambdaworks.jni.UnsupportedPlatformException;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class PlatformTest extends AbstractPlatformDetectionTest {
+ @Test
+ public void arch() throws Exception {
+ assertEquals(Platform.Arch.x86_64, detectArch("x86_64").arch);
+ assertEquals(Platform.Arch.x86_64, detectArch("amd64").arch);
+ assertEquals(Platform.Arch.x86, detectArch("i386").arch);
+ }
+
+ @Test
+ public void os() throws Exception {
+ assertEquals(Platform.OS.darwin, detectOs("Mac OS X").os);
+ assertEquals(Platform.OS.darwin, detectOs("Darwin").os);
+ assertEquals(Platform.OS.freebsd, detectOs("FreeBSD").os);
+ assertEquals(Platform.OS.linux, detectOs("Linux").os);
+ }
+
+ @Test(expected = UnsupportedPlatformException.class)
+ public void unsupported() throws Exception {
+ setPlatform("PA-RISC", "MPE/iX");
+ Platform.detect();
+ }
+}
View
31 src/test/resources/lambdaWorks.p7
@@ -0,0 +1,31 @@
+-----BEGIN PKCS7-----
+MIIFVQYJKoZIhvcNAQcCoIIFRjCCBUICAQExADALBgkqhkiG9w0BBwGgggUoMIIF
+JDCCBAygAwIBAgIRAKnABeJi99p8mX7TzyJcC+8wDQYJKoZIhvcNAQEFBQAwgZUx
+CzAJBgNVBAYTAlVTMQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENp
+dHkxHjAcBgNVBAoTFVRoZ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L0NQUzBCBgNVHR8EOzA5MDegNaAzhjFodHRwOi8v
+Y3JsLnVzZXJ0cnVzdC5jb20vVVROLVVTRVJGaXJzdC1PYmplY3QuY3JsMDQGCCsG
+AQUFBwEBBCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuY29tb2RvY2EuY29t
+MB8GA1UdEQQYMBaBFHdpbGxAbGFtYmRhd29ya3MuY29tMA0GCSqGSIb3DQEBBQUA
+A4IBAQA3pb4gHFzqK8mCc+Xl0JitI2KwYsyX7rzBu7/LfWlPeUMkGXK4kcp+7DAU
+jEoHAbt1ieypPcbpb6ukhuC5++9ZfKSpMVfPPxoBR3vQj2eQCZGNRZBofVfwWBTs
+GG99vOcnMxzdEjLZk24vrrvXXQriEg0A3xJ1o/TSZBtKKdF0nm8hHSaccP9qlisb
+T01P0ijVYN0b3TMfmOZjm2SGD9SFTajmBHZkPp3xHvYTtLRodqbuttphjBU/xqav
+Jh/FhG+brUBhF0bQU/NtAwpsLM5SiBPLTyoXsEj1bohXn1aRBje3q86PPTeVwwh+
+wZaQB/3EQYRMoMb32emkmN2mKy71oQAxAA==
+-----END PKCS7-----
View
BIN  src/test/resources/native-libs-badsig.jar
Binary file not shown
View
BIN  src/test/resources/native-libs-invalid.jar
Binary file not shown
View
BIN  src/test/resources/native-libs-signed.jar
Binary file not shown
View
BIN  src/test/resources/native-libs-unsigned.jar
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.