|
1 | 1 | package org.tron.common.crypto.zksnark; |
2 | 2 |
|
3 | 3 | import java.math.BigInteger; |
| 4 | +import java.net.URL; |
| 5 | +import java.net.URLClassLoader; |
4 | 6 | import java.util.Arrays; |
5 | 7 | import org.junit.Assert; |
6 | 8 | import org.junit.Test; |
7 | 9 |
|
8 | 10 | public class FpTest { |
9 | 11 |
|
| 12 | + @Test |
| 13 | + public void testFieldInitializationWithoutCurveParameters() throws Exception { |
| 14 | + for (Class<?> field : new Class<?>[]{Fp.class, Fp2.class, Fp6.class, Fp12.class}) { |
| 15 | + assertInitializesWithoutCurveParameters(field.getName()); |
| 16 | + } |
| 17 | + } |
| 18 | + |
10 | 19 | @Test(expected = NullPointerException.class) |
11 | 20 | public void testConstructorRejectsNull() { |
12 | 21 | new Fp((BigInteger) null); |
@@ -77,4 +86,22 @@ public void testRejectsOutOfRangeCoordinates() { |
77 | 86 | Assert.assertNull(BN128Fp.create(outOfRange, two)); |
78 | 87 | Assert.assertNull(BN128Fp.create(one, outOfRange)); |
79 | 88 | } |
| 89 | + |
| 90 | + private void assertInitializesWithoutCurveParameters(String name) throws Exception { |
| 91 | + URL location = Fp.class.getProtectionDomain().getCodeSource().getLocation(); |
| 92 | + // A fresh loader exercises first use even if another test already initialized these classes. |
| 93 | + // Reject Params to detect the dependency that caused the initialization cycle. |
| 94 | + try (URLClassLoader loader = new URLClassLoader(new URL[]{location}, null) { |
| 95 | + @Override |
| 96 | + protected Class<?> loadClass(String className, boolean resolve) |
| 97 | + throws ClassNotFoundException { |
| 98 | + if (className.equals(Params.class.getName())) { |
| 99 | + throw new ClassNotFoundException("Field initialization must not load Params"); |
| 100 | + } |
| 101 | + return super.loadClass(className, resolve); |
| 102 | + } |
| 103 | + }) { |
| 104 | + Assert.assertNotNull(Class.forName(name, true, loader)); |
| 105 | + } |
| 106 | + } |
80 | 107 | } |
0 commit comments