Home | History | Annotate | Download | only in jsse
      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 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.util.Arrays;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Tests for <code>CertificateVerify</code> constructor and methods
     26  *
     27  */
     28 public class CertificateVerifyTest extends TestCase {
     29 
     30 	public void testCertificateVerify() throws Exception {
     31 		byte[] anonHash = new byte[0];
     32 		byte[] RSAHash = new byte[] {
     33 				1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
     34 				1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
     35 				1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
     36 				1, 2, 3, 4, 5, 6};
     37 		byte[] DSAHash  = new byte[] {
     38 				1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
     39 				1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
     40 		byte[][] signatures = new byte[][] { RSAHash, DSAHash };
     41         try {
     42             new CertificateVerify(anonHash);
     43             fail("Anonymous: No expected AlertException");
     44         } catch (AlertException e) {
     45         }
     46         try {
     47             HandshakeIODataStream in = new HandshakeIODataStream();
     48             new CertificateVerify(in, 0);
     49             fail("Anonymous: No expected AlertException");
     50         } catch (AlertException e) {
     51         }
     52 		for (int i = 0; i < signatures.length; i++) {
     53 			CertificateVerify message = new CertificateVerify(signatures[i]);
     54             assertEquals("incorrect type", Handshake.CERTIFICATE_VERIFY,
     55                     message.getType());
     56             assertTrue("incorrect CertificateVerify",
     57                     Arrays.equals(message.signedHash, signatures[i]));
     58 
     59 			HandshakeIODataStream out = new HandshakeIODataStream();
     60 			message.send(out);
     61 			byte[] encoded = out.getData(1000);
     62             assertEquals("incorrect out data length", message.length(),
     63                     encoded.length);
     64 
     65 			HandshakeIODataStream in = new HandshakeIODataStream();
     66 			in.append(encoded);
     67 			CertificateVerify message_2 = new CertificateVerify(in, message.length());
     68             assertTrue("incorrect message decoding",
     69                     Arrays.equals(message.signedHash, message_2.signedHash));
     70 
     71 			in.append(encoded);
     72 			try {
     73 				message_2 = new CertificateVerify(in, message.length() - 1);
     74 				fail("Small length: No expected AlertException");
     75 			} catch (AlertException e) {
     76 			}
     77 
     78 			in.append(encoded);
     79 			in.append(new byte[] { 1, 2, 3 });
     80 			try {
     81 				message_2 = new CertificateVerify(in, message.length() + 3);
     82 				fail("Extra bytes: No expected AlertException ");
     83 			} catch (AlertException e) {
     84 			}
     85 		}
     86 	}
     87 
     88 }
     89