1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19 * @author Boris V. Kuznetsov 20 */ 21 22package org.apache.harmony.security.tests.java.security; 23 24import java.nio.ByteBuffer; 25import java.security.DigestException; 26import java.security.MessageDigestSpi; 27 28import junit.framework.TestCase; 29 30/** 31 * Tests for <code>MessageDigestSpi</code> constructor and methods 32 */ 33public class MessageDigestSpiTest extends TestCase { 34 35 /** 36 * java.security.MessageDigestSpi#engineDigest(byte[], int, int) 37 */ 38 public void test_engineDigestLB$LILI() throws Exception { 39 40 final int DIGEST_LENGTH = 2; 41 42 MyMessageDigest md = new MyMessageDigest() { 43 44 public int engineGetDigestLength() { 45 return DIGEST_LENGTH; 46 } 47 48 public byte[] engineDigest() { 49 return new byte[DIGEST_LENGTH]; // return non-null value 50 } 51 }; 52 53 byte[] b = new byte[5]; 54 try { 55 // test: null output buffer 56 md.engineDigest(null, 1, DIGEST_LENGTH); 57 fail("No expected NullPointerException"); 58 } catch (NullPointerException e) { 59 } 60 try { 61 //test: len param < digest length 62 md.engineDigest(b, 1, DIGEST_LENGTH - 1); 63 fail("No expected DigestException"); 64 } catch (DigestException e) { 65 } 66 67 assertEquals("incorrect result", DIGEST_LENGTH, md 68 .engineDigest(b, 1, 3)); 69 70 // Regression for HARMONY-3045 71 md = new MyMessageDigest(); 72 try { 73 md.engineDigest(b, 0, 1); 74 fail("should throw NullPointerException"); 75 } catch (NullPointerException e) { 76 // expected 77 } 78 } 79 80 /** 81 * java.security.MessageDigestSpi#engineGetDigestLength() 82 */ 83 public void test_engineGetDigestLength() { 84 MyMessageDigest md = new MyMessageDigest(); 85 assertEquals(0, md.engineGetDigestLength()); 86 } 87 88 /** 89 * java.security.MessageDigestSpi#engineUpdate(ByteBuffer) 90 */ 91 public void test_engineUpdateLjava_nio_ByteBuffer() { 92 MyMessageDigest md = new MyMessageDigest(); 93 byte[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 94 95 ByteBuffer buf = ByteBuffer.wrap(b, 0, b.length); 96 buf.get(b); 97 int limit = buf.limit(); 98 md.engineUpdate(buf); 99 assertEquals(limit, buf.limit()); 100 assertEquals(limit, buf.position()); 101 102 buf = ByteBuffer.wrap(b, 0, b.length); 103 buf.get(); 104 buf.get(); 105 buf.get(); 106 md.engineUpdate(buf); 107 assertEquals(limit, buf.limit()); 108 assertEquals(limit, buf.position()); 109 } 110 111 /** 112 * @tests java.security.MessageDigestSpi#clone() 113 */ 114 public void test_clone() throws CloneNotSupportedException { 115 MyMessageDigest md = new MyMessageDigest(); 116 try { 117 md.clone(); 118 fail("No expected CloneNotSupportedException"); 119 } catch (CloneNotSupportedException e) { 120 } 121 122 MyMessageDigestCloneable mdc = new MyMessageDigestCloneable(); 123 assertNotSame(mdc, mdc.clone()); 124 } 125 126 private class MyMessageDigest extends MessageDigestSpi { 127 128 @Override 129 public void engineReset() { 130 } 131 132 @Override 133 public byte[] engineDigest() { 134 return null; 135 } 136 137 @Override 138 public void engineUpdate(byte arg0) { 139 } 140 141 @Override 142 public void engineUpdate(byte[] arg0, int arg1, int arg2) { 143 } 144 145 @Override 146 protected int engineDigest(byte[] buf, int offset, int len) 147 throws DigestException { 148 return super.engineDigest(buf, offset, len); 149 } 150 151 @Override 152 protected int engineGetDigestLength() { 153 return super.engineGetDigestLength(); 154 } 155 156 @Override 157 protected void engineUpdate(ByteBuffer input) { 158 super.engineUpdate(input); 159 } 160 } 161 162 private class MyMessageDigestCloneable extends MyMessageDigest implements 163 Cloneable { 164 } 165} 166