View Javadoc
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  package org.apache.commons.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.io.ByteArrayInputStream;
23  import java.util.Random;
24  
25  import org.apache.commons.codec.digest.DigestUtils;
26  import org.apache.commons.codec.digest.MessageDigestAlgorithms;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link MessageDigestInputStream}.
31   */
32  public class MessageDigestInputStreamTest {
33  
34      static byte[] generateRandomByteStream(final int pSize) {
35          final byte[] buffer = new byte[pSize];
36          final Random rnd = new Random();
37          rnd.nextBytes(buffer);
38          return buffer;
39      }
40  
41      @Test
42      public void testNoDefault() throws Exception {
43          assertThrows(IllegalStateException.class, () -> MessageDigestInputStream.builder().get());
44          assertThrows(NullPointerException.class, () -> MessageDigestInputStream.builder().setInputStream(new ByteArrayInputStream(new byte[] { 1 })).get());
45      }
46  
47      @Test
48      public void testNormalUse() throws Exception {
49          for (int i = 256; i < 8192; i *= 2) {
50              final byte[] buffer = generateRandomByteStream(i);
51              final byte[] expect = DigestUtils.sha512(buffer);
52              try (MessageDigestInputStream messageDigestInputStream = MessageDigestInputStream.builder().setMessageDigest(MessageDigestAlgorithms.SHA_512)
53                      .setInputStream(new ByteArrayInputStream(buffer)).get()) {
54                  messageDigestInputStream.consume();
55                  assertArrayEquals(expect, messageDigestInputStream.getMessageDigest().digest());
56              }
57              try (MessageDigestInputStream messageDigestInputStream = MessageDigestInputStream.builder().setByteArray(buffer)
58                      .setMessageDigest(DigestUtils.getSha512Digest()).get()) {
59                  messageDigestInputStream.consume();
60                  assertArrayEquals(expect, messageDigestInputStream.getMessageDigest().digest());
61              }
62          }
63      }
64  
65  }