Home | History | Annotate | Download | only in crypto
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.javax.crypto;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.util.Arrays;
     24 import javax.crypto.Cipher;
     25 import javax.crypto.CipherInputStream;
     26 import javax.crypto.SecretKey;
     27 import javax.crypto.spec.IvParameterSpec;
     28 import javax.crypto.spec.SecretKeySpec;
     29 import junit.framework.TestCase;
     30 
     31 public final class CipherInputStreamTest extends TestCase {
     32 
     33     private final byte[] keyBytes = { 127, -2, -95, -39, 35, 118, 121, -92 };
     34     private final String plainText = "abcde";
     35     private final byte[] cipherText = { 121, -124, -106, 43, -55, -67, -105, -75 };
     36     private SecretKey key;
     37 
     38     @Override protected void setUp() throws Exception {
     39         key = new SecretKeySpec(keyBytes, "DES");
     40     }
     41 
     42     public void testEncrypt() throws Exception {
     43         Cipher cipher = Cipher.getInstance("DES");
     44         cipher.init(Cipher.ENCRYPT_MODE, key);
     45         InputStream in = new CipherInputStream(
     46                 new ByteArrayInputStream(plainText.getBytes("UTF-8")), cipher);
     47         byte[] bytes = readAll(in);
     48         assertEquals(Arrays.toString(cipherText), Arrays.toString(bytes));
     49     }
     50 
     51     public void testDecrypt() throws Exception {
     52         Cipher cipher = Cipher.getInstance("DES");
     53         cipher.init(Cipher.DECRYPT_MODE, key);
     54         InputStream in = new CipherInputStream(new ByteArrayInputStream(cipherText), cipher);
     55         byte[] bytes = readAll(in);
     56         assertEquals(plainText, new String(bytes, "UTF-8"));
     57     }
     58 
     59     public void testSkip() throws Exception {
     60         Cipher cipher = Cipher.getInstance("DES");
     61         cipher.init(Cipher.DECRYPT_MODE, key);
     62         InputStream in = new CipherInputStream(new ByteArrayInputStream(cipherText), cipher);
     63         assertTrue(in.skip(5) > 0);
     64     }
     65 
     66     private byte[] readAll(InputStream in) throws IOException {
     67         ByteArrayOutputStream out = new ByteArrayOutputStream();
     68         int count;
     69         byte[] buffer = new byte[1024];
     70         while ((count = in.read(buffer)) != -1) {
     71             out.write(buffer, 0, count);
     72         }
     73         return out.toByteArray();
     74     }
     75 
     76     public void testCipherInputStream_TruncatedInput_Failure() throws Exception {
     77         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
     78         cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new byte[16], "AES"),
     79                 new IvParameterSpec(new byte[16]));
     80         InputStream is = new CipherInputStream(new ByteArrayInputStream(new byte[31]), cipher);
     81         is.read(new byte[4]);
     82         is.close();
     83     }
     84 }
     85