/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
* @author Boris V. Kuznetsov
* @version $Revision$
*/

package org.apache.harmony.security.tests.java.security;

import java.nio.ByteBuffer;
import java.security.DigestException;
import java.security.MessageDigestSpi;

import junit.framework.TestCase;


/**
 * Tests for <code>MessageDigestSpi</code> constructor and methods
 * 
 */
public class MessageDigestSpiTest extends TestCase {

	public void testEngineGetDigestLength() {
		MyMessageDigest md = new MyMessageDigest();
		if (md.engineGetDigestLength() != 0) {
			fail("engineGetDigestLength failed");
		}
	}

	/*
	 * Class under test for void engineUpdate(ByteBuffer)
	 */
	public void testEngineUpdateByteBuffer() {
		MyMessageDigest md = new MyMessageDigest();
		byte[] b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

		ByteBuffer buf = ByteBuffer.wrap(b,0,b.length);
		buf.get(b);
		int l = buf.limit();
		md.engineUpdate(buf);
		if (buf.limit() !=l || buf.position() != l) {
			fail("Case 1. Incorrect position");
		}
		
		buf = ByteBuffer.wrap(b,0,b.length);
		buf.get();
		buf.get();
		buf.get();
		md.engineUpdate(buf);
		if (buf.limit() !=l || buf.position() != l) {
			fail("Case 2. Incorrect position");
		}
	}

    /**
     * @tests java.security.MessageDigestSpi#clone()
     */
    public void test_clone() throws CloneNotSupportedException {
        MyMessageDigest md = new MyMessageDigest();
        try {
            md.clone();
            fail("No expected CloneNotSupportedException");
        } catch (CloneNotSupportedException e) {
        }

        MyMessageDigestCloneable mdc = new MyMessageDigestCloneable();
        assertNotSame(mdc, mdc.clone());
    }

	private class MyMessageDigest extends MessageDigestSpi {
		
		public void engineReset() {}

		public byte[] engineDigest() {
			return null;
		}

		public void engineUpdate(byte arg0) {}

		public void engineUpdate(byte[] arg0, int arg1, int arg2) {}

        @Override
        protected int engineDigest(byte[] buf, int offset, int len)
                throws DigestException {
            return super.engineDigest(buf, offset, len);
        }

        @Override
        protected int engineGetDigestLength() {
            return super.engineGetDigestLength();
        }

        @Override
        protected void engineUpdate(ByteBuffer input) {
            super.engineUpdate(input);
        }
	}
    
    private class MyMessageDigestCloneable extends MyMessageDigest implements
            Cloneable {
    }
}

@@ -1064,17 +1064,16 @@
 tCase;%0A%0A
-%0A
 /**%0A * T
@@ -1139,12 +1139,8 @@
 ods%0A
- * %0A
  */%0A
@@ -1193,16 +1193,1065 @@
 Case %7B%0A%0A
+    /**%0A     * java.security.MessageDigestSpi#engineDigest(byte%5B%5D, int, int)%0A     */%0A    public void test_engineDigestLB$LILI() throws Exception %7B%0A%0A        final int DIGEST_LENGHT = 2;%0A%0A        MyMessageDigest md = new MyMessageDigest() %7B%0A%0A            public int engineGetDigestLength() %7B%0A                return DIGEST_LENGHT;%0A            %7D%0A%0A            public byte%5B%5D engineDigest() %7B%0A                return new byte%5BDIGEST_LENGHT%5D; // return non-null value%0A            %7D%0A        %7D;%0A%0A        byte%5B%5D b = new byte%5B5%5D;%0A        try %7B%0A            // test: null output buffer%0A            md.engineDigest(null, 1, DIGEST_LENGHT);%0A            fail(%22No expected NullPointerException%22);%0A        %7D catch (NullPointerException e) %7B%0A        %7D%0A        try %7B%0A            //test: len param %3C digest length%0A            md.engineDigest(b, 1, DIGEST_LENGHT - 1);%0A            fail(%22No expected DigestException%22);%0A        %7D catch (DigestException e) %7B%0A        %7D%0A%0A        assertEquals(%22incorrect result%22, DIGEST_LENGHT, md%0A                .engineDigest(b, 1, 3));%0A    %7D%0A%0A
 %09public 

Refactor test for MessageDigestSpi#engineDigest(byte[], int, int). Move it to 'api' tests