diff --git a/jdk/test/sm/Utils.java b/jdk/test/sm/Utils.java index d64c6390ab..1936808936 100644 --- a/jdk/test/sm/Utils.java +++ b/jdk/test/sm/Utils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -18,7 +18,6 @@ */ import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -41,7 +40,6 @@ import java.security.spec.SM2PublicKeySpec; import java.util.Arrays; import java.util.Base64; -import java.util.Comparator; import java.util.List; /** @@ -200,16 +198,5 @@ private static ECPrivateKey ecPrivateKeyAsStr(String pkcs8KeyPEM) return (ECPrivateKey) keyFactory.generatePrivate(privateKeySpec); } - public static void deleteDirIfExists(Path dir) throws IOException { - if (!Files.exists(dir)) { - return; - } - - Files.walk(dir) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - } - private Utils() {} } diff --git a/jdk/test/sm/cert/CertPathBuilderTest.java b/jdk/test/sm/cert/CertPathBuilderTest.java index 57202a4eb0..7d3a1862b1 100644 --- a/jdk/test/sm/cert/CertPathBuilderTest.java +++ b/jdk/test/sm/cert/CertPathBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test CertPathBuilder on SM certificate. + * @compile ../Utils.java + * @run testng CertPathBuilderTest + */ + import java.nio.file.Path; import java.nio.file.Paths; import java.security.cert.CertPath; @@ -37,12 +44,6 @@ import static org.testng.Assert.assertEquals; -/* - * @test - * @summary Test CertPathBuilder on SM certificate. - * @compile ../Utils.java - * @run testng CertPathBuilderTest - */ public class CertPathBuilderTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/cert/CertPathValidatorTest.java b/jdk/test/sm/cert/CertPathValidatorTest.java index e4533223f4..26e4e479fd 100644 --- a/jdk/test/sm/cert/CertPathValidatorTest.java +++ b/jdk/test/sm/cert/CertPathValidatorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test CertPathValidator on SM certificate. + * @compile ../Utils.java + * @run testng/othervm CertPathValidatorTest + */ + import java.math.BigInteger; import java.net.InetAddress; import java.nio.file.Path; @@ -46,12 +53,6 @@ import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -/* - * @test - * @summary Test CertPathValidator on SM certificate. - * @compile ../Utils.java - * @run testng/othervm CertPathValidatorTest - */ public class CertPathValidatorTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/cert/CertStoreTest.java b/jdk/test/sm/cert/CertStoreTest.java index b13164a42c..ef78765f4c 100644 --- a/jdk/test/sm/cert/CertStoreTest.java +++ b/jdk/test/sm/cert/CertStoreTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test CertStore on SM certificate. + * @compile ../Utils.java + * @run testng CertStoreTest + */ + import java.nio.file.Path; import java.nio.file.Paths; import java.security.cert.CertStore; @@ -31,12 +38,6 @@ import static org.testng.Assert.assertEquals; -/* - * @test - * @summary Test CertStore on SM certificate. - * @compile ../Utils.java - * @run testng CertStoreTest - */ public class CertStoreTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/cert/CertificateFactoryTest.java b/jdk/test/sm/cert/CertificateFactoryTest.java index 55e4e01144..fe5eaf4463 100644 --- a/jdk/test/sm/cert/CertificateFactoryTest.java +++ b/jdk/test/sm/cert/CertificateFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test CertificateFactory on SM certificate. + * @compile ../Utils.java + * @run testng CertificateFactoryTest + */ + import java.io.ByteArrayInputStream; import java.nio.file.Path; import java.nio.file.Paths; @@ -37,12 +44,6 @@ import static org.testng.Assert.assertEquals; -/* - * @test - * @summary Test CertificateFactory on SM certificate. - * @compile ../Utils.java - * @run testng CertificateFactoryTest - */ public class CertificateFactoryTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/cert/KeyFactoryTest.java b/jdk/test/sm/cert/KeyFactoryTest.java index f9c4892582..2648cd85ee 100644 --- a/jdk/test/sm/cert/KeyFactoryTest.java +++ b/jdk/test/sm/cert/KeyFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test KeyFactory on SM certificate. + * @compile ../Utils.java + * @run testng KeyFactoryTest + */ + import java.nio.file.Path; import java.nio.file.Paths; import java.security.KeyFactory; @@ -35,12 +42,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; -/* - * @test - * @summary Test KeyFactory on SM certificate. - * @compile ../Utils.java - * @run testng KeyFactoryTest - */ public class KeyFactoryTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/cert/KeyStoreTest.java b/jdk/test/sm/cert/KeyStoreTest.java index 2305b59f65..b3ef81614e 100644 --- a/jdk/test/sm/cert/KeyStoreTest.java +++ b/jdk/test/sm/cert/KeyStoreTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test KeyStore on SM certificate. + * @compile ../Utils.java + * @run testng KeyStoreTest + */ + import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Files; @@ -33,12 +40,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; -/* - * @test - * @summary Test KeyStore on SM certificate. - * @compile ../Utils.java - * @run testng KeyStoreTest - */ public class KeyStoreTest { private static final String TEST_BASE = System.getProperty("test.src"); diff --git a/jdk/test/sm/crypto/SM2/ECKeyPairGeneratorTest.java b/jdk/test/sm/crypto/SM2/ECKeyPairGeneratorTest.java index 888a63c529..99a90094a2 100644 --- a/jdk/test/sm/crypto/SM2/ECKeyPairGeneratorTest.java +++ b/jdk/test/sm/crypto/SM2/ECKeyPairGeneratorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test EC key pair generation on SM2 curve. + * @compile ../../Utils.java + * @run testng ECKeyPairGeneratorTest + */ + import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -37,12 +44,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; -/* - * @test - * @summary Test EC key pair generation on SM2 curve. - * @compile ../../Utils.java - * @run testng ECKeyPairGeneratorTest - */ public class ECKeyPairGeneratorTest { private final static byte[] MESSAGE diff --git a/jdk/test/sm/crypto/SM2/SM2CipherTest.java b/jdk/test/sm/crypto/SM2/SM2CipherTest.java index ca7794c064..3bc7f2aed8 100644 --- a/jdk/test/sm/crypto/SM2/SM2CipherTest.java +++ b/jdk/test/sm/crypto/SM2/SM2CipherTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM2 cipher. + * @compile ../../Utils.java + * @run testng SM2CipherTest + */ + import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import java.math.BigInteger; @@ -37,12 +44,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM2 cipher. - * @compile ../../Utils.java - * @run testng SM2CipherTest - */ public class SM2CipherTest { private static final byte[] EMPTY = new byte[0]; diff --git a/jdk/test/sm/crypto/SM2/SM2KeyAgreementTest.java b/jdk/test/sm/crypto/SM2/SM2KeyAgreementTest.java index 1047320144..2d45d3cbf2 100644 --- a/jdk/test/sm/crypto/SM2/SM2KeyAgreementTest.java +++ b/jdk/test/sm/crypto/SM2/SM2KeyAgreementTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM2 key agreement. + * @compile ../../Utils.java + * @run testng SM2KeyAgreementTest + */ + import javax.crypto.KeyAgreement; import javax.crypto.SecretKey; import java.math.BigInteger; @@ -37,12 +44,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM2 key agreement. - * @compile ../../Utils.java - * @run testng SM2KeyAgreementTest - */ public class SM2KeyAgreementTest { private final static String PUB_KEY diff --git a/jdk/test/sm/crypto/SM2/SM2KeyFactoryTest.java b/jdk/test/sm/crypto/SM2/SM2KeyFactoryTest.java index 09e4cf0dd5..f1a2b433d4 100644 --- a/jdk/test/sm/crypto/SM2/SM2KeyFactoryTest.java +++ b/jdk/test/sm/crypto/SM2/SM2KeyFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM2 key factory. + * @compile ../../Utils.java + * @run testng SM2KeyFactoryTest + */ + import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPair; @@ -38,12 +45,6 @@ import static org.testng.Assert.assertNotNull; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM2 key factory. - * @compile ../../Utils.java - * @run testng SM2KeyFactoryTest - */ public class SM2KeyFactoryTest { // starts with 0x05 diff --git a/jdk/test/sm/crypto/SM2/SM2SignatureTest.java b/jdk/test/sm/crypto/SM2/SM2SignatureTest.java index 321260a07f..07165b0aac 100644 --- a/jdk/test/sm/crypto/SM2/SM2SignatureTest.java +++ b/jdk/test/sm/crypto/SM2/SM2SignatureTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM2 signature. + * @compile ../../Utils.java + * @run testng SM2SignatureTest + */ + import java.math.BigInteger; import java.nio.ByteBuffer; import java.security.InvalidKeyException; @@ -38,12 +45,6 @@ import static org.testng.Assert.assertTrue; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM2 signature. - * @compile ../../Utils.java - * @run testng SM2SignatureTest - */ public class SM2SignatureTest { private static final byte[] EMPTY = new byte[0]; diff --git a/jdk/test/sm/crypto/SM3/HmacSM3KeyGeneratorTest.java b/jdk/test/sm/crypto/SM3/HmacSM3KeyGeneratorTest.java index 45252822d2..6fa7d9a235 100644 --- a/jdk/test/sm/crypto/SM3/HmacSM3KeyGeneratorTest.java +++ b/jdk/test/sm/crypto/SM3/HmacSM3KeyGeneratorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,12 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test HmacSM3 key generator. + * @run testng HmacSM3KeyGeneratorTest + */ + import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.security.InvalidParameterException; @@ -26,11 +32,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test HmacSM3 key generator. - * @run testng HmacSM3KeyGeneratorTest - */ public class HmacSM3KeyGeneratorTest { @Test diff --git a/jdk/test/sm/crypto/SM3/HmacSM3Test.java b/jdk/test/sm/crypto/SM3/HmacSM3Test.java index f885009237..a2876fbba4 100644 --- a/jdk/test/sm/crypto/SM3/HmacSM3Test.java +++ b/jdk/test/sm/crypto/SM3/HmacSM3Test.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test HmacSM3. + * @compile ../../Utils.java + * @run testng HmacSM3Test + */ + import javax.crypto.Mac; import javax.crypto.ShortBufferException; import javax.crypto.spec.SecretKeySpec; @@ -27,12 +34,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test HmacSM3. - * @compile ../../Utils.java - * @run testng HmacSM3Test - */ public class HmacSM3Test { private static final byte[] KEY = Utils.hexToBytes("0123456789abcdef0123456789abcdef"); diff --git a/jdk/test/sm/crypto/SM3/SM3MessageDigestTest.java b/jdk/test/sm/crypto/SM3/SM3MessageDigestTest.java index bd7a6dea74..5d8610e196 100644 --- a/jdk/test/sm/crypto/SM3/SM3MessageDigestTest.java +++ b/jdk/test/sm/crypto/SM3/SM3MessageDigestTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM3 message digest. + * @compile ../../Utils.java + * @run testng SM3MessageDigestTest + */ + import java.nio.ByteBuffer; import java.security.DigestException; import java.security.MessageDigest; @@ -27,12 +34,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM3 message digest. - * @compile ../../Utils.java - * @run testng SM3MessageDigestTest - */ public class SM3MessageDigestTest { private static final byte[] MESSAGE_SHORT = Utils.hexToBytes("616263"); diff --git a/jdk/test/sm/crypto/SM4/SM4CipherTest.java b/jdk/test/sm/crypto/SM4/SM4CipherTest.java index b0205784ae..dd61a6b671 100644 --- a/jdk/test/sm/crypto/SM4/SM4CipherTest.java +++ b/jdk/test/sm/crypto/SM4/SM4CipherTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM4 cipher. + * @compile ../../Utils.java + * @run testng SM4CipherTest + */ + import javax.crypto.AEADBadTagException; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; @@ -50,12 +57,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM4 cipher. - * @compile ../../Utils.java - * @run testng SM4CipherTest - */ public class SM4CipherTest { private static final int SM4_GCM_TAG_LEN = 16; diff --git a/jdk/test/sm/crypto/SM4/SM4KeyGeneratorTest.java b/jdk/test/sm/crypto/SM4/SM4KeyGeneratorTest.java index 39656da51a..ca415575fd 100644 --- a/jdk/test/sm/crypto/SM4/SM4KeyGeneratorTest.java +++ b/jdk/test/sm/crypto/SM4/SM4KeyGeneratorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,12 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM4 key generator. + * @run testng SM4KeyGeneratorTest + */ + import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; @@ -27,11 +33,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM4 key generator. - * @run testng SM4KeyGeneratorTest - */ public class SM4KeyGeneratorTest { @Test diff --git a/jdk/test/sm/crypto/SM4/SM4ParametersTest.java b/jdk/test/sm/crypto/SM4/SM4ParametersTest.java index faa5196dfe..f7bca60219 100644 --- a/jdk/test/sm/crypto/SM4/SM4ParametersTest.java +++ b/jdk/test/sm/crypto/SM4/SM4ParametersTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,6 +17,13 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* + * @test + * @summary Test SM4 parameters. + * @compile ../../Utils.java + * @run testng SM4ParametersTest + */ + import javax.crypto.Cipher; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.IvParameterSpec; @@ -29,12 +36,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.expectThrows; -/* - * @test - * @summary Test SM4 parameters. - * @compile ../../Utils.java - * @run testng SM4ParametersTest - */ public class SM4ParametersTest { private static final int SM4_GCM_TAG_LEN = 16; diff --git a/jdk/test/sm/tool/KeyToolBinTest.java b/jdk/test/sm/tool/KeyToolBinTest.java index 4bec45bdc2..9e09d9d465 100644 --- a/jdk/test/sm/tool/KeyToolBinTest.java +++ b/jdk/test/sm/tool/KeyToolBinTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,7 +17,16 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ -import java.io.IOException; +/* + * @test + * @summary Test keytool binary on SM algorithms. + * jarsigner doesn't support these algorithms yet. + * @library /lib/testlibrary + * @compile ../Utils.java + * ../crypto/SM2/ECOperator.java + * @run testng/timeout=600 KeyToolBinTest + */ + import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Path; @@ -33,44 +42,14 @@ import jdk.testlibrary.SecurityTools; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; -/* - * @test - * @summary Test keytool binary on SM algorithms. - * jarsigner doesn't support these algorithms yet. - * @library /lib/testlibrary - * @compile ../Utils.java - * ../crypto/SM2/ECOperator.java - * @run testng KeyToolBinTest - */ public class KeyToolBinTest { - private static final Path BASE_DIR = Paths.get("SM-KeyToolBinTest"); - - private static final Path KEYSTORE = path("keystore.ks"); - - private static final Path ROOT_KEYSTORE = path("root.ks"); - private static final Path CA_KEYSTORE = path("ca.ks"); - private static final Path EE_KEYSTORE = path("ee.ks"); - private static final String PASSWORD = "testpassword"; - @BeforeMethod - public void prepare() throws IOException { - Utils.deleteDirIfExists(BASE_DIR); - Files.createDirectory(BASE_DIR); - } - - @AfterMethod - public void clean() throws IOException { - Utils.deleteDirIfExists(BASE_DIR); - } - @Test public void testGenKeyPairOnPKCS12() throws Throwable { testGenKeyPair(null, "PKCS12", null); @@ -103,9 +82,11 @@ public void testGenKeyPairWithSMOnJKS() throws Throwable { private void testGenKeyPair(String toolsUseCurveSM2, String storeType, String sigAlg) throws Throwable { - genKeyPair(KEYSTORE, toolsUseCurveSM2, storeType, "ec", sigAlg); + Path keystore = Paths.get(String.join("-", + "testGenKeyPair", storeType, toolsUseCurveSM2 + ".ks")); + genKeyPair(keystore, toolsUseCurveSM2, storeType, "ec", sigAlg); KeyStore keyStore = KeyStore.getInstance(storeType); - keyStore.load(Files.newInputStream(KEYSTORE), PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(keystore), PASSWORD.toCharArray()); ECPrivateKey privateKey = (ECPrivateKey) keyStore.getKey("ec", PASSWORD.toCharArray()); System.out.println("EC params: " + privateKey.getParams()); @@ -115,39 +96,41 @@ private void testGenKeyPair(String toolsUseCurveSM2, String storeType, @Test public void testGenSelfSignedCertOnPKCS12() throws Throwable { - testGenSelfSignedCertr(null, "PKCS12", null); + testGenSelfSignedCert(null, "PKCS12", null); } @Test public void testGenSelfSignedCertWithECOnPKCS12() throws Throwable { - testGenSelfSignedCertr("false", "PKCS12", "SHA256withECDSA"); + testGenSelfSignedCert("false", "PKCS12", "SHA256withECDSA"); } @Test public void testGenSelfSignedCertWithSMOnPKCS12() throws Throwable { - testGenSelfSignedCertr("true", "PKCS12", "SM3withSM2"); + testGenSelfSignedCert("true", "PKCS12", "SM3withSM2"); } @Test - public void testGenSelfSignedCertrOnJKS() throws Throwable { - testGenSelfSignedCertr(null, "JKS", null); + public void testGenSelfSignedCertOnJKS() throws Throwable { + testGenSelfSignedCert(null, "JKS", null); } @Test - public void testGenSelfSignedCertrWithECOnJKS() throws Throwable { - testGenSelfSignedCertr("false", "JKS", "SHA256withECDSA"); + public void testGenSelfSignedCertWithECOnJKS() throws Throwable { + testGenSelfSignedCert("false", "JKS", "SHA256withECDSA"); } @Test - public void testGenSelfSignedCertrWithSMOnJKS() throws Throwable { - testGenSelfSignedCertr("true", "JKS", "SM3withSM2"); + public void testGenSelfSignedCertWithSMOnJKS() throws Throwable { + testGenSelfSignedCert("true", "JKS", "SM3withSM2"); } - private void testGenSelfSignedCertr(String toolsUseCurveSM2, + private void testGenSelfSignedCert(String toolsUseCurveSM2, String storeType, String sigAlg) throws Throwable { + Path keystore = Paths.get(String.join("-", + "testGenSelfSignedCert", storeType, toolsUseCurveSM2 + ".ks")); String alias = curve(toolsUseCurveSM2) + "-" + sigAlg; - genKeyPair(KEYSTORE, toolsUseCurveSM2, storeType, alias, sigAlg); - outputCert(KEYSTORE, storeType, alias, path(alias + ".crt")); + genKeyPair(keystore, toolsUseCurveSM2, storeType, alias, sigAlg); + outputCert(keystore, storeType, alias, Paths.get(alias + ".crt")); } @Test @@ -182,29 +165,36 @@ public void testGenCertChainWithSMOnJKS() throws Throwable { private void genCertChain(String toolsUseCurveSM2, String storeType, String sigAlg) throws Throwable { + Path rootKeystore = Paths.get(String.join("-", + "genCertChain", storeType, toolsUseCurveSM2, "root.ks")); + Path caKeystore = Paths.get(String.join("-", + "genCertChain", storeType, toolsUseCurveSM2, "ca.ks")); + Path eeKeystore = Paths.get(String.join("-", + "genCertChain", storeType, toolsUseCurveSM2, "ee.ks")); + String suffix = curve(toolsUseCurveSM2) + "-" + sigAlg; String rootAlias = "root-" + suffix; String caAlias = "ca-" + suffix; String eeAlias = "ee-" + suffix; - Path caCSRPath = path(caAlias + ".csr"); - Path eeCSRPath = path(eeAlias + ".csr"); + Path caCSRPath = Paths.get(caAlias + ".csr"); + Path eeCSRPath = Paths.get(eeAlias + ".csr"); - genKeyPair(ROOT_KEYSTORE, toolsUseCurveSM2, storeType, rootAlias, sigAlg); - genKeyPair(CA_KEYSTORE, toolsUseCurveSM2, storeType, caAlias, sigAlg); - genKeyPair(EE_KEYSTORE, toolsUseCurveSM2, storeType, eeAlias, sigAlg); + genKeyPair(rootKeystore, toolsUseCurveSM2, storeType, rootAlias, sigAlg); + genKeyPair(caKeystore, toolsUseCurveSM2, storeType, caAlias, sigAlg); + genKeyPair(eeKeystore, toolsUseCurveSM2, storeType, eeAlias, sigAlg); - outputCert(ROOT_KEYSTORE, storeType, rootAlias, path(rootAlias + ".crt")); - genCSR(CA_KEYSTORE, toolsUseCurveSM2, storeType, caAlias, sigAlg, caCSRPath); + outputCert(rootKeystore, storeType, rootAlias, Paths.get(rootAlias + ".crt")); + genCSR(caKeystore, toolsUseCurveSM2, storeType, caAlias, sigAlg, caCSRPath); - Path caCertPath = path(caAlias + ".crt"); - genCert(ROOT_KEYSTORE, toolsUseCurveSM2, storeType, rootAlias, sigAlg, caCSRPath, caCertPath); + Path caCertPath = Paths.get(caAlias + ".crt"); + genCert(rootKeystore, toolsUseCurveSM2, storeType, rootAlias, sigAlg, caCSRPath, caCertPath); checkCert(caCertPath, expectedOrder(toolsUseCurveSM2), expectedSigAlg(toolsUseCurveSM2)); - genCSR(EE_KEYSTORE, toolsUseCurveSM2, storeType, eeAlias, sigAlg, eeCSRPath); + genCSR(eeKeystore, toolsUseCurveSM2, storeType, eeAlias, sigAlg, eeCSRPath); - Path eeCertPath = path(eeAlias + ".crt"); - genCert(CA_KEYSTORE, toolsUseCurveSM2, storeType, caAlias, sigAlg, eeCSRPath, eeCertPath); + Path eeCertPath = Paths.get(eeAlias + ".crt"); + genCert(caKeystore, toolsUseCurveSM2, storeType, caAlias, sigAlg, eeCSRPath, eeCertPath); checkCert(eeCertPath, expectedOrder(toolsUseCurveSM2), expectedSigAlg(toolsUseCurveSM2)); } @@ -390,8 +380,4 @@ private static void checkCert(Path certPath, BigInteger expectedOrder, assertEquals(expectedSigAlg, cert.getSigAlgName()); } - - private static Path path(String file) { - return BASE_DIR.resolve(Paths.get(file)); - } } diff --git a/jdk/test/sm/tool/KeyToolTest.java b/jdk/test/sm/tool/KeyToolTest.java index f7363b2006..c4b8e7e0ee 100644 --- a/jdk/test/sm/tool/KeyToolTest.java +++ b/jdk/test/sm/tool/KeyToolTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -17,34 +17,11 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ -import java.io.IOException; -import java.math.BigInteger; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.KeyStore; -import java.security.PublicKey; -import java.security.cert.X509Certificate; -import java.security.interfaces.ECPrivateKey; -import java.security.interfaces.ECPublicKey; -import java.security.spec.SM2ParameterSpec; -import java.util.ArrayList; -import java.util.List; - -import sun.security.tools.keytool.Main; - -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; - /* * @test * @summary Test keytool on SM algorithms. * @compile ../Utils.java * ../crypto/SM2/ECOperator.java - * @run testng KeyToolTest * @run testng/othervm -Duser.language=en * -Duser.country=US * -Djava.security.egd=file:/dev/./urandom @@ -60,15 +37,32 @@ * -Djdk.tools.useCurveSM2=true * KeyToolTest */ -public class KeyToolTest { - private static final Path BASE_DIR = Paths.get("SM-KeyToolTest"); +import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.KeyStore; +import java.security.PublicKey; +import java.security.cert.X509Certificate; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.ECPublicKey; +import java.security.spec.SM2ParameterSpec; +import java.util.ArrayList; +import java.util.List; + +import sun.security.tools.keytool.Main; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; - private static final Path KEYSTORE = path("keystore.ks"); +public class KeyToolTest { - private static final Path ROOT_KEYSTORE = path("root.ks"); - private static final Path CA_KEYSTORE = path("ca.ks"); - private static final Path EE_KEYSTORE = path("ee.ks"); + private static final String USE_CURVE_SM2 = System.getProperty( + "jdk.tools.useCurveSM2", "None"); + private static final Path BASE_DIR = Paths.get("UseCurveSM2-" + USE_CURVE_SM2); private static final String PASSWORD = "testpassword"; @@ -104,15 +98,9 @@ private static BigInteger expectedOrder() { ? SM2ParameterSpec.ORDER : ECOperator.SECP256R1.getOrder(); } - @BeforeMethod - public void prepare() throws IOException { - Utils.deleteDirIfExists(BASE_DIR); - Files.createDirectory(BASE_DIR); - } - - @AfterMethod - public void clean() throws IOException { - Utils.deleteDirIfExists(BASE_DIR); + @BeforeClass + public void prepare() throws Throwable { + Files.createDirectories(BASE_DIR); } @Test @@ -126,9 +114,10 @@ public void testGenKeyPairOnJKS() throws Throwable { } private void testGenKeyPair(String storeType) throws Throwable { - genKeyPair(KEYSTORE, storeType, "ec"); + Path keystore = path("testGenKeyPair-" + storeType + ".ks"); + genKeyPair(keystore, storeType, "ec"); KeyStore keyStore = KeyStore.getInstance(storeType); - keyStore.load(Files.newInputStream(KEYSTORE), PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(keystore), PASSWORD.toCharArray()); ECPrivateKey privateKey = (ECPrivateKey) keyStore.getKey("ec", PASSWORD.toCharArray()); System.out.println("EC params: " + privateKey.getParams()); @@ -137,18 +126,19 @@ private void testGenKeyPair(String storeType) throws Throwable { @Test public void testGenSelfSignedCertOnPKCS12() throws Throwable { - testGenSelfSignedCertr("PKCS12"); + testGenSelfSignedCert("PKCS12"); } @Test - public void testGenSelfSignedCertrOnJKS() throws Throwable { - testGenSelfSignedCertr("JKS"); + public void testGenSelfSignedCertOnJKS() throws Throwable { + testGenSelfSignedCert("JKS"); } - private void testGenSelfSignedCertr(String storeType) throws Throwable { + private void testGenSelfSignedCert(String storeType) throws Throwable { + Path keystore = path("testGenSelfSignedCert-" + storeType + ".ks"); String alias = CURVE + "-" + SIG_ALGO; - genKeyPair(KEYSTORE, storeType, alias); - outputCert(KEYSTORE, storeType, alias, path(alias + ".crt")); + genKeyPair(keystore, storeType, alias); + outputCert(keystore, storeType, alias, path(alias + ".crt")); } @Test @@ -162,8 +152,10 @@ public void testGenCSROnJKS() throws Throwable { } private void testGenCSR(String storeType) throws Throwable { - genKeyPair(ROOT_KEYSTORE, storeType, "ec-root"); - genCSR(ROOT_KEYSTORE, storeType, "ec-root", path("ec-sm2-root.csr")); + Path keystore = path("testGenCSR-" + storeType + ".ks"); + String alias = "ec-testGenCSR-" + storeType; + genKeyPair(keystore, storeType, alias); + genCSR(keystore, storeType, alias, path("ec-sm2-root.csr")); } @Test @@ -178,6 +170,10 @@ public void testGenCertChainOnJKS() throws Throwable { private void genCertChain(String storeType) throws Throwable { + Path rootKeystore = path("genCertChain-" + storeType + "-root.ks"); + Path caKeystore = path("genCertChain-" + storeType + "-ca.ks"); + Path eeKeystore = path("genCertChain-" + storeType + "-ee.ks"); + String suffix = CURVE + "-" + SIG_ALGO; String rootAlias = "root-" + suffix; String caAlias = "ca-" + suffix; @@ -186,21 +182,21 @@ private void genCertChain(String storeType) Path caCSRPath = path(caAlias + ".csr"); Path eeCSRPath = path(eeAlias + ".csr"); - genKeyPair(ROOT_KEYSTORE, storeType, rootAlias); - genKeyPair(CA_KEYSTORE, storeType, caAlias); - genKeyPair(EE_KEYSTORE, storeType, eeAlias); + genKeyPair(rootKeystore, storeType, rootAlias); + genKeyPair(caKeystore, storeType, caAlias); + genKeyPair(eeKeystore, storeType, eeAlias); - outputCert(ROOT_KEYSTORE, storeType, rootAlias, path(rootAlias + ".crt")); - genCSR(CA_KEYSTORE, storeType, caAlias, caCSRPath); + outputCert(rootKeystore, storeType, rootAlias, path(rootAlias + ".crt")); + genCSR(caKeystore, storeType, caAlias, caCSRPath); Path caCertPath = path(caAlias + ".crt"); - genCert(ROOT_KEYSTORE, storeType, rootAlias, caCSRPath, caCertPath); + genCert(rootKeystore, storeType, rootAlias, caCSRPath, caCertPath); checkCert(caCertPath); - genCSR(EE_KEYSTORE, storeType, eeAlias, eeCSRPath); + genCSR(eeKeystore, storeType, eeAlias, eeCSRPath); Path eeCertPath = path(eeAlias + ".crt"); - genCert(CA_KEYSTORE, storeType, caAlias, eeCSRPath, eeCertPath); + genCert(caKeystore, storeType, caAlias, eeCSRPath, eeCertPath); checkCert(eeCertPath); }