< prev index next >

test/jdk/sun/security/util/misc/SetNullSigParams.java

Print this page


   1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8214096 8216039
  27  * @summary Make sure SignatureUtil works with null algorithm parameters
  28  * @modules java.base/sun.security.util
  29  */
  30 import java.security.*;
  31 import java.security.spec.AlgorithmParameterSpec;
  32 import sun.security.util.SignatureUtil;
  33 
  34 public class SetNullSigParams {
  35 
  36     public static void main(String[] args) throws Exception {
  37         Signature sig = new SpecialSigImpl();
  38         SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
  39         SignatureUtil.initSignWithParam(sig, null, null, null);
  40     }
  41 
  42     // Sample Signature impl class which simulates 3rd party provider behavior
  43     // and throws NPE when given null algorithm parameters
  44     // For max backward-compatibility, sun.security.util.SignatureUtil class
  45     // now calls setParameter() only when algorithm parameters is non-null
  46     private static class SpecialSigImpl extends Signature {
  47         SpecialSigImpl() {
  48             super("ANY");
  49         }
  50         @Override
  51         protected void engineInitVerify(PublicKey publicKey)
  52                 throws InvalidKeyException {}
  53         @Override
  54         protected void engineInitSign(PrivateKey privateKey)
  55                 throws InvalidKeyException {}
  56         @Override
  57         protected void engineUpdate(byte b) throws SignatureException {}
  58         @Override
  59         protected void engineUpdate(byte[] b, int off, int len)
   1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8214096
  27  * @summary Make sure SignatureUtil can accept null algorithm parameters
  28  * @modules java.base/sun.security.util
  29  */
  30 import java.security.*;
  31 import java.security.spec.AlgorithmParameterSpec;
  32 import sun.security.util.SignatureUtil;
  33 
  34 public class SetNullSigParams {
  35 
  36     public static void main(String[] args) throws Exception {
  37         Signature sig = new SpecialSigImpl();
  38         SignatureUtil.specialSetParameter(sig, (byte[]) null);
  39         SignatureUtil.specialSetParameter(sig, (AlgorithmParameters) null);
  40     }
  41 
  42     // Sample Signature impl class which simulates 3rd party provider behavior
  43     // and throws NPE when given null algorithm parameters
  44     // For max backward-compatibility, sun.security.util.SignatureUtil class
  45     // now calls setParameter() only when algorithm parameters is non-null
  46     private static class SpecialSigImpl extends Signature {
  47         SpecialSigImpl() {
  48             super("ANY");
  49         }
  50         @Override
  51         protected void engineInitVerify(PublicKey publicKey)
  52                 throws InvalidKeyException {}
  53         @Override
  54         protected void engineInitSign(PrivateKey privateKey)
  55                 throws InvalidKeyException {}
  56         @Override
  57         protected void engineUpdate(byte b) throws SignatureException {}
  58         @Override
  59         protected void engineUpdate(byte[] b, int off, int len)
< prev index next >