6205692: (spec) javax.crypto.MacSpi.engineUpdate(ByteBuffer input): NPE should be specified

Reviewed-by: valeriep
This commit is contained in:
Kevin Driver 2022-08-30 20:51:34 +00:00 committed by Bradford Wetmore
parent 622be797b9
commit 6335150cea
3 changed files with 117 additions and 15 deletions

View File

@ -423,7 +423,7 @@ public class Mac implements Cloneable {
*
* @param key the key.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
*/
public final void init(Key key) throws InvalidKeyException {
@ -451,9 +451,9 @@ public class Mac implements Cloneable {
* @param key the key.
* @param params the algorithm parameters.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
* @exception InvalidAlgorithmParameterException if the given algorithm
* @throws InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this MAC.
*/
public final void init(Key key, AlgorithmParameterSpec params)
@ -476,7 +476,7 @@ public class Mac implements Cloneable {
*
* @param input the input byte to be processed.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte input) throws IllegalStateException {
@ -492,7 +492,7 @@ public class Mac implements Cloneable {
*
* @param input the array of bytes to be processed.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte[] input) throws IllegalStateException {
@ -513,7 +513,7 @@ public class Mac implements Cloneable {
* @param offset the offset in {@code input} where the input starts.
* @param len the number of bytes to process.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte[] input, int offset, int len)
@ -538,8 +538,9 @@ public class Mac implements Cloneable {
*
* @param input the ByteBuffer
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
* @throws IllegalArgumentException if {@code input} is null
* @since 1.5
*/
public final void update(ByteBuffer input) {
@ -569,7 +570,7 @@ public class Mac implements Cloneable {
*
* @return the MAC result.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final byte[] doFinal() throws IllegalStateException {
@ -603,9 +604,9 @@ public class Mac implements Cloneable {
* @param outOffset the offset in {@code output} where the MAC is
* stored
*
* @exception ShortBufferException if the given output buffer is too small
* @throws ShortBufferException if the given output buffer is too small
* to hold the result
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void doFinal(byte[] output, int outOffset)
@ -641,7 +642,7 @@ public class Mac implements Cloneable {
* @param input data in bytes
* @return the MAC result.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final byte[] doFinal(byte[] input) throws IllegalStateException
@ -678,7 +679,7 @@ public class Mac implements Cloneable {
*
* @return a clone if the provider implementation is cloneable.
*
* @exception CloneNotSupportedException if this is called on a
* @throws CloneNotSupportedException if this is called on a
* delegate that does not support {@code Cloneable}.
*/
public final Object clone() throws CloneNotSupportedException {

View File

@ -65,9 +65,9 @@ public abstract class MacSpi {
* @param key the (secret) key.
* @param params the algorithm parameters.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
* @exception InvalidAlgorithmParameterException if the given algorithm
* @throws InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this MAC.
*/
protected abstract void engineInit(Key key,
@ -101,6 +101,9 @@ public abstract class MacSpi {
* process ByteBuffers more efficiently than byte arrays.
*
* @param input the ByteBuffer
*
* @throws NullPointerException if {@code input} is null
*
* @since 1.5
*/
protected void engineUpdate(ByteBuffer input) {
@ -145,7 +148,7 @@ public abstract class MacSpi {
*
* @return a clone if the implementation is cloneable.
*
* @exception CloneNotSupportedException if this is called
* @throws CloneNotSupportedException if this is called
* on an implementation that does not support {@code Cloneable}.
*/
public Object clone() throws CloneNotSupportedException {

View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @library /test/lib
* @bug 6205692
* @summary verify MacSpi NPE on engineUpdate(ByteBuffer)
*/
import jdk.test.lib.Utils;
import javax.crypto.MacSpi;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
public class Test6205692 {
public boolean execute() throws Exception {
ByteBuffer byteBuffer = null;
MyMacSpi myMacSpi = new MyMacSpi();
Utils.runAndCheckException(() -> myMacSpi.engineUpdate(byteBuffer),
NullPointerException.class);
return true;
}
public static void main(String[] args) throws Exception {
Test6205692 test = new Test6205692();
if (test.execute()) {
System.out.println(test.getClass().getName() + ": passed!");
}
}
private static class MyMacSpi extends MacSpi {
/*
* This is the important part; the rest is blank mandatory overrides
*/
public void engineUpdate(ByteBuffer input) {
super.engineUpdate(input);
}
@Override
protected int engineGetMacLength() {
return 0;
}
@Override
protected void engineInit(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException {
}
@Override
protected void engineUpdate(byte input) {
}
@Override
protected void engineUpdate(byte[] input, int offset, int len) {
}
@Override
protected byte[] engineDoFinal() {
return new byte[0];
}
@Override
protected void engineReset() {
}
}
}