Skip to content

Commit 50c3d9c

Browse files
committed
Testing: fix Android test discovery for skipped tests, convert BeforeClass + Assume to Rule pattern, update Android README for cert paths
1 parent 541906d commit 50c3d9c

File tree

14 files changed

+302
-96
lines changed

14 files changed

+302
-96
lines changed

IDE/Android/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,35 @@ del wolfssl
7575
mklink /D wolfssl ..\..\..\..\..\..\..\src\java\com\wolfssl\
7676
```
7777

78-
## 3. Import and Build the Example Project with Android Studio
78+
## 3. Push Certificate and KeyStore Files to Android Device
79+
80+
Several JUnit tests require access to certificate and KeyStore files. These
81+
files are located in the `examples/certs` directory and must be pushed to
82+
the Android device or emulator before running tests.
83+
84+
Start the emulator or connect your device, then use `adb push` from the root
85+
wolfcrypt-jni directory. This step can be done after starting Android Studio
86+
and compiling the project, but must be done before running the test cases.
87+
88+
```
89+
adb shell mkdir -p /data/local/tmp/examples/certs/intermediate
90+
adb shell mkdir -p /data/local/tmp/examples/certs/rsapss
91+
adb shell mkdir -p /data/local/tmp/examples/certs/crl
92+
adb push ./examples/certs/ /data/local/tmp/examples/
93+
```
94+
95+
This will push all certificate files, KeyStore files (.jks, .wks, .p12),
96+
and subdirectories (intermediate, rsapss, crl) needed by the JUnit tests.
97+
98+
If this step is skipped, tests in the following classes will be skipped due
99+
to missing certificate files:
100+
101+
- `WolfSSLKeyStoreTest`
102+
- `WolfCryptPKIXCertPathValidatorTest`
103+
- `WolfCryptPKIXRevocationCheckerTest`
104+
- `WolfSSLCertManagerOCSPTest`
105+
106+
## 4. Import and Build the Example Project with Android Studio
79107

80108
1) Open the Android Studio project by double clicking on the `Android` folder
81109
in wolfcrypt-jni/IDE/. Or, from inside Android Studio, open the `Android`

src/test/java/com/wolfssl/provider/jce/test/WolfCryptPKIXCertPathValidatorTest.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.rules.TestRule;
2828
import org.junit.rules.TestWatcher;
2929
import org.junit.runner.Description;
30+
import org.junit.runners.model.Statement;
3031
import org.junit.Test;
3132
import org.junit.BeforeClass;
3233

@@ -128,6 +129,24 @@ private static boolean isAndroid() {
128129
@Rule(order = Integer.MIN_VALUE)
129130
public TestRule testWatcher = TimedTestWatcher.create();
130131

132+
/* Rule to check if cert files are available, skips tests if not. */
133+
@Rule(order = Integer.MIN_VALUE + 1)
134+
public TestRule certFilesAvailable = new TestRule() {
135+
@Override
136+
public Statement apply(final Statement base,
137+
Description description) {
138+
return new Statement() {
139+
@Override
140+
public void evaluate() throws Throwable {
141+
File f = new File(jksCaServerRSA2048);
142+
Assume.assumeTrue("Test cert files not available: " +
143+
jksCaServerRSA2048, f.exists());
144+
base.evaluate();
145+
}
146+
};
147+
}
148+
};
149+
131150
@BeforeClass
132151
public static void testSetupAndProviderInstallation()
133152
throws Exception, NoSuchProviderException {
@@ -145,7 +164,7 @@ public static void testSetupAndProviderInstallation()
145164

146165
if (isAndroid()) {
147166
/* On Android, example certs/keys/KeyStores are on SD card */
148-
certPre = "/sdcard/";
167+
certPre = "/data/local/tmp/";
149168

150169
/* On Android, KeyStore files are .bks and type is BKS */
151170
jksExt = ".bks";
@@ -188,12 +207,6 @@ public static void testSetupAndProviderInstallation()
188207

189208
crlDer =
190209
certPre.concat("examples/certs/crl/crl.der");
191-
192-
/* Test if file exists, if not might be running on Android.
193-
* Skip tests gracefully if cert files not available. */
194-
File f = new File(jksCaServerRSA2048);
195-
Assume.assumeTrue("Test cert files not available: " + jksCaServerRSA2048,
196-
f.exists());
197210
}
198211

199212
/**

src/test/java/com/wolfssl/provider/jce/test/WolfCryptPKIXRevocationCheckerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void testSetup() throws Exception {
9292

9393
if (isAndroid()) {
9494
/* On Android, example certs are on SD card */
95-
certPre = "/sdcard/";
95+
certPre = "/data/local/tmp/";
9696
}
9797

9898
/* Set paths to example certs */

src/test/java/com/wolfssl/provider/jce/test/WolfSSLKeyStoreTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public static void testSetupAndProviderInstallation()
367367

368368
if (isAndroid()) {
369369
/* On Android, example certs/keys/KeyStores are on SD card */
370-
certPre = "/sdcard/";
370+
certPre = "/data/local/tmp/";
371371
}
372372

373373
/* Set paths to example certs/keys */
@@ -1462,6 +1462,9 @@ public void testLoadWKSasJKSFromFile()
14621462
CertificateException, InvalidKeySpecException,
14631463
UnrecoverableKeyException {
14641464

1465+
/* Skip on Android, JKS KeyStore type not available */
1466+
Assume.assumeTrue(!isAndroid());
1467+
14651468
WolfCryptProvider prov = null;
14661469
KeyStore store = null;
14671470

src/test/java/com/wolfssl/wolfcrypt/test/AesCcmTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.junit.rules.TestRule;
3838
import org.junit.rules.TestWatcher;
3939
import org.junit.runner.Description;
40+
import org.junit.runners.model.Statement;
4041

4142
import com.wolfssl.wolfcrypt.Fips;
4243
import com.wolfssl.wolfcrypt.AesCcm;
@@ -152,16 +153,32 @@ public class AesCcmTest {
152153
@Rule(order = Integer.MIN_VALUE)
153154
public TestRule testWatcher = TimedTestWatcher.create();
154155

156+
/* Rule to check if AES-CCM is available, skips tests if not.
157+
* AesCcm() constructor does not allocate native memory, so no need
158+
* to release if it throws. */
159+
@Rule(order = Integer.MIN_VALUE + 1)
160+
public TestRule aesCcmAvailable = new TestRule() {
161+
@Override
162+
public Statement apply(final Statement base,
163+
Description description) {
164+
return new Statement() {
165+
@Override
166+
public void evaluate() throws Throwable {
167+
try {
168+
new AesCcm();
169+
} catch (WolfCryptException e) {
170+
Assume.assumeTrue("AES-CCM not compiled in: " +
171+
e.getError(), false);
172+
}
173+
base.evaluate();
174+
}
175+
};
176+
}
177+
};
178+
155179
@BeforeClass
156180
public static void checkAvailability() {
157-
try {
158-
new AesCcm();
159-
System.out.println("JNI AesCcm Class");
160-
} catch (WolfCryptException e) {
161-
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
162-
System.out.println("AesCcm test skipped: " + e.getError());
163-
Assume.assumeNoException(e);
164-
}
181+
System.out.println("JNI AesCcm Class");
165182
}
166183

167184
/*

src/test/java/com/wolfssl/wolfcrypt/test/AesCmacTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.rules.TestRule;
3333
import org.junit.rules.TestWatcher;
3434
import org.junit.runner.Description;
35+
import org.junit.runners.model.Statement;
3536

3637
import java.util.Arrays;
3738
import java.util.Iterator;
@@ -53,16 +54,32 @@ public class AesCmacTest {
5354
@Rule(order = Integer.MIN_VALUE)
5455
public TestRule testWatcher = TimedTestWatcher.create();
5556

57+
/* Rule to check if AES-CMAC is available, skips tests if not.
58+
* AesCmac() constructor does not allocate native memory, so no need
59+
* to release if it throws. */
60+
@Rule(order = Integer.MIN_VALUE + 1)
61+
public TestRule aesCmacAvailable = new TestRule() {
62+
@Override
63+
public Statement apply(final Statement base,
64+
Description description) {
65+
return new Statement() {
66+
@Override
67+
public void evaluate() throws Throwable {
68+
try {
69+
new AesCmac();
70+
} catch (WolfCryptException e) {
71+
Assume.assumeTrue("AES-CMAC not compiled in: " +
72+
e.getError(), false);
73+
}
74+
base.evaluate();
75+
}
76+
};
77+
}
78+
};
79+
5680
@BeforeClass
5781
public static void checkAvailability() {
58-
try {
59-
new AesCmac();
60-
System.out.println("JNI AesCmac Class");
61-
} catch (WolfCryptException e) {
62-
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
63-
System.out.println("AesCmac test skipped: " + e.getError());
64-
Assume.assumeNoException(e);
65-
}
82+
System.out.println("JNI AesCmac Class");
6683
}
6784

6885
@Test

src/test/java/com/wolfssl/wolfcrypt/test/AesCtrTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.rules.TestRule;
3535
import org.junit.rules.TestWatcher;
3636
import org.junit.runner.Description;
37+
import org.junit.runners.model.Statement;
3738

3839
import com.wolfssl.wolfcrypt.AesCtr;
3940
import com.wolfssl.wolfcrypt.NativeStruct;
@@ -73,16 +74,32 @@ public class AesCtrTest {
7374
@Rule(order = Integer.MIN_VALUE)
7475
public TestRule testWatcher = TimedTestWatcher.create();
7576

77+
/* Rule to check if AES-CTR is available, skips tests if not.
78+
* AesCtr() constructor does not allocate native memory, so no need
79+
* to release if it throws. */
80+
@Rule(order = Integer.MIN_VALUE + 1)
81+
public TestRule aesCtrAvailable = new TestRule() {
82+
@Override
83+
public Statement apply(final Statement base,
84+
Description description) {
85+
return new Statement() {
86+
@Override
87+
public void evaluate() throws Throwable {
88+
try {
89+
new AesCtr();
90+
} catch (WolfCryptException e) {
91+
Assume.assumeTrue("AES-CTR not compiled in: " +
92+
e.getError(), false);
93+
}
94+
base.evaluate();
95+
}
96+
};
97+
}
98+
};
99+
76100
@BeforeClass
77101
public static void checkAvailability() {
78-
try {
79-
new AesCtr();
80-
System.out.println("JNI AesCtr Class");
81-
} catch (WolfCryptException e) {
82-
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
83-
System.out.println("AesCtr test skipped: " + e.getError());
84-
Assume.assumeNoException(e);
85-
}
102+
System.out.println("JNI AesCtr Class");
86103
}
87104

88105
@Test

src/test/java/com/wolfssl/wolfcrypt/test/AesEcbTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.rules.TestRule;
3535
import org.junit.rules.TestWatcher;
3636
import org.junit.runner.Description;
37+
import org.junit.runners.model.Statement;
3738

3839
import com.wolfssl.wolfcrypt.AesEcb;
3940
import com.wolfssl.wolfcrypt.NativeStruct;
@@ -69,16 +70,32 @@ public class AesEcbTest {
6970
@Rule(order = Integer.MIN_VALUE)
7071
public TestRule testWatcher = TimedTestWatcher.create();
7172

73+
/* Rule to check if AES-ECB is available, skips tests if not.
74+
* AesEcb() constructor does not allocate native memory, so no need
75+
* to release if it throws. */
76+
@Rule(order = Integer.MIN_VALUE + 1)
77+
public TestRule aesEcbAvailable = new TestRule() {
78+
@Override
79+
public Statement apply(final Statement base,
80+
Description description) {
81+
return new Statement() {
82+
@Override
83+
public void evaluate() throws Throwable {
84+
try {
85+
new AesEcb();
86+
} catch (WolfCryptException e) {
87+
Assume.assumeTrue("AES-ECB not compiled in: " +
88+
e.getError(), false);
89+
}
90+
base.evaluate();
91+
}
92+
};
93+
}
94+
};
95+
7296
@BeforeClass
7397
public static void checkAvailability() {
74-
try {
75-
new AesEcb();
76-
System.out.println("JNI AesEcb Class");
77-
} catch (WolfCryptException e) {
78-
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
79-
System.out.println("AesEcb test skipped: " + e.getError());
80-
Assume.assumeNoException(e);
81-
}
98+
System.out.println("JNI AesEcb Class");
8299
}
83100

84101
@Test

src/test/java/com/wolfssl/wolfcrypt/test/AesOfbTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.rules.TestRule;
3535
import org.junit.rules.TestWatcher;
3636
import org.junit.runner.Description;
37+
import org.junit.runners.model.Statement;
3738

3839
import com.wolfssl.wolfcrypt.AesOfb;
3940
import com.wolfssl.wolfcrypt.NativeStruct;
@@ -83,16 +84,32 @@ public class AesOfbTest {
8384
@Rule(order = Integer.MIN_VALUE)
8485
public TestRule testWatcher = TimedTestWatcher.create();
8586

87+
/* Rule to check if AES-OFB is available, skips tests if not.
88+
* AesOfb() constructor does not allocate native memory, so no need
89+
* to release if it throws. */
90+
@Rule(order = Integer.MIN_VALUE + 1)
91+
public TestRule aesOfbAvailable = new TestRule() {
92+
@Override
93+
public Statement apply(final Statement base,
94+
Description description) {
95+
return new Statement() {
96+
@Override
97+
public void evaluate() throws Throwable {
98+
try {
99+
new AesOfb();
100+
} catch (WolfCryptException e) {
101+
Assume.assumeTrue("AES-OFB not compiled in: " +
102+
e.getError(), false);
103+
}
104+
base.evaluate();
105+
}
106+
};
107+
}
108+
};
109+
86110
@BeforeClass
87111
public static void checkAvailability() {
88-
try {
89-
new AesOfb();
90-
System.out.println("JNI AesOfb Class");
91-
} catch (WolfCryptException e) {
92-
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
93-
System.out.println("AesOfb test skipped: " + e.getError());
94-
Assume.assumeNoException(e);
95-
}
112+
System.out.println("JNI AesOfb Class");
96113
}
97114

98115
@Test

0 commit comments

Comments
 (0)