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.crypto.internal; 23 24import java.nio.ByteBuffer; 25import java.util.Arrays; 26 27import javax.crypto.ShortBufferException; 28 29import org.apache.harmony.crypto.internal.NullCipherSpi; 30import junit.framework.TestCase; 31 32/** 33 * 34 * Tests for NullCipher implementation 35 */ 36public class NullCipherSpiTest extends TestCase { 37 38 public void testEngineGetBlockSize() { 39 NullCipherSpi spi = new NullCipherSpi(); 40 assertEquals("incorrect block size", 1, spi.engineGetBlockSize()); 41 } 42 43 public void testEngineGetOutputSize() { 44 NullCipherSpi spi = new NullCipherSpi(); 45 assertEquals("incorrect output size", 100, spi.engineGetOutputSize(100)); 46 } 47 48 public void testEngineGetIV() { 49 NullCipherSpi spi = new NullCipherSpi(); 50 assertTrue("Incorrect IV", Arrays.equals(spi.engineGetIV() , new byte[8])); 51 } 52 53 /* 54 * Class under test for byte[] engineUpdate(byte[], int, int) 55 */ 56 public void testEngineUpdatebyteArrayintint() { 57 NullCipherSpi spi = new NullCipherSpi(); 58 byte[] b = {1,2,3,4,5,6,7,8,9}; 59 byte[] b1 = spi.engineUpdate(b, 3, 4); 60 for (int i = 0; i < 4; i++) { 61 assertEquals("incorrect update result", b[3+i], b1[i]); 62 } 63 } 64 65 /* 66 * Class under test for int engineUpdate(byte[], int, int, byte[], int) 67 */ 68 public void testEngineUpdatebyteArrayintintbyteArrayint() throws Exception { 69 NullCipherSpi spi = new NullCipherSpi(); 70 byte[] b = {1,2,3,4,5,6,7,8,9}; 71 byte[] b1 = new byte[10]; 72 assertEquals("incorrect update result", 4, spi.engineUpdate(b, 3, 4, b1, 5)); 73 for (int i = 0; i < 4; i++) { 74 assertEquals("incorrect update result", b[3+i], b1[5+i]); 75 } 76 } 77 78 /* 79 * Class under test for byte[] engineDoFinal(byte[], int, int) 80 */ 81 public void testEngineDoFinalbyteArrayintint() throws Exception { 82 NullCipherSpi spi = new NullCipherSpi(); 83 byte[] b = {1,2,3,4,5,6,7,8,9}; 84 byte[] b1 = null; 85 b1 = spi.engineDoFinal(b, 3, 4); 86 for (int i = 0; i < 4; i++) { 87 assertEquals("incorrect doFinal result", b[3+i], b1[i]); 88 } 89 } 90 91 /* 92 * Class under test for int engineDoFinal(byte[], int, int, byte[], int) 93 */ 94 public void testEngineDoFinalbyteArrayintintbyteArrayint() throws Exception { 95 NullCipherSpi spi = new NullCipherSpi(); 96 byte[] b = {1,2,3,4,5,6,7,8,9}; 97 byte[] b1 = new byte[10]; 98 assertEquals("incorrect doFinal result", 4, spi.engineDoFinal(b, 3, 4, b1, 5)); 99 for (int i = 0; i < 4; i++) { 100 assertEquals("incorrect doFinal result", b[3+i], b1[5+i]); 101 } 102 103 } 104 105 /* 106 * Class under test for int engineUpdate(ByteBuffer, ByteBuffer) 107 */ 108 public void testEngineUpdateByteBufferByteBuffer() throws Exception { 109 NullCipherSpi spi = new NullCipherSpi(); 110 byte[] b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 111 112 ByteBuffer inbuf = ByteBuffer.wrap(b,0,b.length); 113 ByteBuffer outbuf = ByteBuffer.allocate(6); 114 115 try { 116 spi.engineUpdate(null, outbuf); 117 fail("No expected NullPointerException"); 118 } catch (NullPointerException e) { 119 } 120 121 try { 122 spi.engineUpdate(inbuf, null); 123 fail("No expected NullPointerException"); 124 } catch (NullPointerException e) { 125 } 126 127 inbuf.get(); 128 inbuf.get(); 129 inbuf.get(); 130 inbuf.get(); 131 int result = spi.engineUpdate(inbuf, outbuf); 132 assertEquals("incorrect result", b.length - 4, result); 133 for (int i = 0; i < result; i++) { 134 assertEquals("incorrect outbuf", i + 4, outbuf.get(i)); 135 } 136 137 inbuf = ByteBuffer.wrap(b,0,b.length); 138 outbuf = ByteBuffer.allocate(5); 139 inbuf.get(); 140 inbuf.get(); 141 inbuf.get(); 142 inbuf.get(); 143 try { 144 spi.engineUpdate(inbuf, outbuf); 145 fail("No expected ShortBufferException"); 146 } catch (ShortBufferException e) { 147 } 148 } 149 150 /* 151 * Class under test for int engineDoFinal(ByteBuffer, ByteBuffer) 152 */ 153 public void testEngineDoFinalByteBufferByteBuffer() throws Exception { 154 NullCipherSpi spi = new NullCipherSpi(); 155 byte[] b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 156 157 ByteBuffer inbuf = ByteBuffer.wrap(b,0,b.length); 158 ByteBuffer outbuf = ByteBuffer.allocate(6); 159 160 try { 161 spi.engineDoFinal(null, outbuf); 162 fail("No expected NullPointerException"); 163 } catch (NullPointerException e) { 164 } 165 166 try { 167 spi.engineDoFinal(inbuf, null); 168 fail("No expected NullPointerException"); 169 } catch (NullPointerException e) { 170 } 171 172 inbuf.get(); 173 inbuf.get(); 174 inbuf.get(); 175 inbuf.get(); 176 int result = spi.engineDoFinal(inbuf, outbuf); 177 assertEquals("incorrect result", b.length - 4, result); 178 for (int i = 0; i < result; i++) { 179 assertEquals("incorrect outbuf", i + 4, outbuf.get(i)); 180 } 181 182 inbuf = ByteBuffer.wrap(b,0,b.length); 183 outbuf = ByteBuffer.allocate(5); 184 inbuf.get(); 185 inbuf.get(); 186 inbuf.get(); 187 inbuf.get(); 188 try { 189 spi.engineDoFinal(inbuf, outbuf); 190 fail("No expected ShortBufferException"); 191 } catch (ShortBufferException e) { 192 } 193 } 194 195 /* 196 * Class under test for byte[] engineWrap(Key) 197 */ 198 public void testEngineWrapKey() throws Exception { 199 NullCipherSpi spi = new NullCipherSpi(); 200 try { 201 spi.engineWrap(null); 202 fail("No expected UnsupportedOperationException"); 203 } catch (UnsupportedOperationException e) { 204 } 205 } 206 207 /* 208 * Class under test for Key engineUnwrap(byte[], String, int) 209 */ 210 public void testEngineUnwrapbyteArrayStringint() throws Exception { 211 NullCipherSpi spi = new NullCipherSpi(); 212 try { 213 spi.engineUnwrap(new byte[3], "", 10); 214 fail("No expected UnsupportedOperationException"); 215 } catch (UnsupportedOperationException e) { 216 } 217 } 218 219 /* 220 * Class under test for int engineGetKeySize(Key) 221 */ 222 public void testEngineGetKeySize() throws Exception { 223 NullCipherSpi spi = new NullCipherSpi(); 224 try { 225 spi.engineGetKeySize(null); 226 fail("No expected UnsupportedOperationException"); 227 } catch (UnsupportedOperationException e) { 228 } 229 } 230 231} 232