DESedeKeySpecTest.java revision cec4dd4b1d33f78997603d0f89c0d0e56e64dbcd
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 Alexander Y. Kleymenov 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.crypto.tests.javax.crypto.spec; 24 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargets; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetNew; 29 30import java.lang.NullPointerException; 31import java.security.InvalidKeyException; 32import java.util.Arrays; 33 34import javax.crypto.spec.DESedeKeySpec; 35 36import junit.framework.Test; 37import junit.framework.TestCase; 38import junit.framework.TestSuite; 39 40@TestTargetClass(DESedeKeySpec.class) 41/** 42 */ 43 44public class DESedeKeySpecTest extends TestCase { 45 46 /** 47 * Constructors testing. Tests behavior of each of two constructors 48 * in the cases of: null array, short array, normal array. 49 */ 50 @TestTargets({ 51 @TestTargetNew( 52 level = TestLevel.COMPLETE, 53 notes = "Checks both constructors.", 54 method = "DESedeKeySpec", 55 args = {byte[].class} 56 ), 57 @TestTargetNew( 58 level = TestLevel.COMPLETE, 59 notes = "Checks both constructors.", 60 method = "DESedeKeySpec", 61 args = {byte[].class, int.class} 62 ) 63 }) 64 public void testDESedeKeySpec() { 65 try { 66 new DESedeKeySpec((byte []) null); 67 fail("Should raise an NullPointerException " 68 + "in case of null byte array."); 69 } catch (NullPointerException e) { 70 } catch (InvalidKeyException e) { 71 fail("Should raise an NullPointerException " 72 + "in case of null byte array."); 73 } 74 try { 75 new DESedeKeySpec(new byte [] {1, 2, 3}); 76 fail("Should raise an InvalidKeyException on a short byte array."); 77 } catch (NullPointerException e) { 78 fail("Unexpected NullPointerException was thrown."); 79 } catch (InvalidKeyException e) { 80 } 81 try { 82 new DESedeKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 83 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 84 1, 2, 3, 4}); 85 } catch (NullPointerException e) { 86 fail("Unexpected NullPointerException was thrown."); 87 } catch (InvalidKeyException e) { 88 fail("Unexpected InvalidKeyException was thrown."); 89 } 90 try { 91 new DESedeKeySpec((byte []) null, 1); 92 fail("Should raise an NullPointerException " 93 + "in case of null byte array."); 94 } catch (NullPointerException e) { 95 } catch (InvalidKeyException e) { 96 fail("Should raise an NullPointerException " 97 + "in case of null byte array."); 98 } 99 try { 100 new DESedeKeySpec(new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 101 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 102 1, 2, 3, 4}, 1); 103 fail("Should raise an InvalidKeyException on a short byte array."); 104 } catch (NullPointerException e) { 105 fail("Unexpected NullPointerException was thrown."); 106 } catch (InvalidKeyException e) { 107 } 108 try { 109 new DESedeKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 110 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 111 1, 2, 3, 4, 5}, 1); 112 } catch (NullPointerException e) { 113 fail("Unexpected NullPointerException was thrown."); 114 } catch (InvalidKeyException e) { 115 fail("Unexpected InvalidKeyException was thrown."); 116 } 117 } 118 119 /** 120 * getKey() method testing. Checks that modification of returned key 121 * does not affect the internal key. Also test check an equality of 122 * the key with the key specified in the constructor. The object under 123 * the test is created by different constructors. 124 */ 125 @TestTargetNew( 126 level = TestLevel.COMPLETE, 127 notes = "", 128 method = "getKey", 129 args = {} 130 ) 131 public void testGetKey() { 132 byte[] key = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 133 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}; 134 DESedeKeySpec ks; 135 try { 136 ks = new DESedeKeySpec(key); 137 } catch (InvalidKeyException e) { 138 fail("InvalidKeyException should not be thrown."); 139 return; 140 } 141 byte[] res = ks.getKey(); 142 assertTrue("The returned array should be equal to the specified " 143 + "in constructor.", Arrays.equals(key, res)); 144 res[0] += 1; 145 assertFalse("The modification of returned key should not affect" 146 + "the underlying key.", key[0] == res[0]); 147 148 byte[] key1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 149 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3}; 150 try { 151 ks = new DESedeKeySpec(key1, 2); 152 } catch (InvalidKeyException e) { 153 fail("InvalidKeyException should not be thrown."); 154 return; 155 } 156 res = ks.getKey(); 157 assertNotSame("The returned array should not be the same object " 158 + "as specified in a constructor.", key1, res); 159 byte[] exp = new byte[24]; 160 System.arraycopy(key1, 2, exp, 0, 24); 161 assertTrue("The returned array should be equal to the specified " 162 + "in constructor.", Arrays.equals(exp, res)); 163 } 164 165 /** 166 * isParityAdjusted(byte[] key, offset) method testing. Tests if the 167 * method throws appropriate exceptions on incorrect byte array, if 168 * it returns false on the key which is not parity adjusted, and if 169 * it returns true on parity adjusted key. 170 */ 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "", 174 method = "isParityAdjusted", 175 args = {byte[].class, int.class} 176 ) 177 public void testIsParityAdjusted() { 178 try { 179 DESedeKeySpec.isParityAdjusted(null, 1); 180 fail("Should raise an NullPointerException " 181 + "in case of null byte array."); 182 } catch (NullPointerException e) { 183 } catch (InvalidKeyException e) { 184 fail("Should raise an NullPointerException " 185 + "in case of null byte array."); 186 } 187 188 byte[] key = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 189 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 190 try { 191 DESedeKeySpec.isParityAdjusted(key, 1); 192 fail("Should raise an InvalidKeyException " 193 + "in case of short byte array."); 194 } catch (NullPointerException e) { 195 fail("Unexpected NullPointerException was thrown."); 196 } catch (InvalidKeyException e) { 197 } 198 199 byte[] key_not_pa = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 200 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}; 201 try { 202 assertFalse("Method returns true when false is expected.", 203 DESedeKeySpec.isParityAdjusted(key_not_pa, 0)); 204 } catch (NullPointerException e) { 205 fail("Unexpected NullPointerException was thrown."); 206 } catch (InvalidKeyException e) { 207 fail("Unexpected InvalidKeyException was thrown."); 208 } 209 210 byte[] key_pa = {(byte) 128, (byte) 131, (byte) 133, (byte) 134, 211 (byte) 137, (byte) 138, (byte) 140, (byte) 143, 212 (byte) 145, (byte) 146, (byte) 148, (byte) 151, 213 (byte) 152, (byte) 155, (byte) 157, (byte) 158, 214 (byte) 161, (byte) 162, (byte) 164, (byte) 167, 215 (byte) 168, (byte) 171, (byte) 173, (byte) 174}; 216 try { 217 assertTrue("Method returns false when true is expected.", 218 DESedeKeySpec.isParityAdjusted(key_pa, 0)); 219 } catch (NullPointerException e) { 220 fail("Unexpected NullPointerException was thrown."); 221 } catch (InvalidKeyException e) { 222 fail("Unexpected InvalidKeyException was thrown."); 223 } 224 } 225 226 public static Test suite() { 227 return new TestSuite(DESedeKeySpecTest.class); 228 } 229} 230 231