1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.jdkim.canon;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.util.Arrays;
27  
28  public class DigestOutputStreamTest extends AbstractOutputStreamTestCase {
29  
30      private byte[] testData;
31      private byte[] expectedDigest;
32  
33      protected void setUp() throws Exception {
34          testData = new byte[4096];
35          for (int i = 0; i < testData.length; i++) {
36              testData[i] = (byte) ((i * i * 4095 + (testData.length - i) * 17) % 128);
37          }
38          MessageDigest md = MessageDigest.getInstance("sha-256");
39          md.update(testData);
40          expectedDigest = md.digest();
41      }
42  
43      public void testSingleBytes() throws NoSuchAlgorithmException, IOException {
44          DigestOutputStream dos = new DigestOutputStream(MessageDigest
45                  .getInstance("sha-256"));
46          for (int i = 0; i < testData.length; i++) {
47              dos.write(testData[i]);
48          }
49          dos.close();
50          byte[] digest = dos.getDigest();
51          assertTrue(Arrays.equals(expectedDigest, digest));
52      }
53  
54      public void testChunks() throws NoSuchAlgorithmException, IOException {
55          DigestOutputStream dos = new DigestOutputStream(MessageDigest
56                  .getInstance("sha-256"));
57          chunker(testData, dos);
58          byte[] digest = dos.getDigest();
59          assertTrue(Arrays.equals(expectedDigest, digest));
60      }
61  
62      public void testChunksAndPassthrough() throws NoSuchAlgorithmException,
63              IOException {
64          ByteArrayOutputStream bos = new ByteArrayOutputStream();
65          DigestOutputStream dos = new DigestOutputStream(MessageDigest
66                  .getInstance("sha-256"), bos);
67          chunker(testData, dos);
68          byte[] digest = dos.getDigest();
69          assertTrue(Arrays.equals(expectedDigest, digest));
70          assertTrue(Arrays.equals(testData, bos.toByteArray()));
71      }
72  }