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    *      https://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  package org.apache.commons.codec.cli;
19  
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.InputStream;
26  import java.io.PrintStream;
27  import java.nio.charset.StandardCharsets;
28  
29  import org.apache.commons.lang3.JavaVersion;
30  import org.apache.commons.lang3.SystemUtils;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link Digest}.
35   */
36  class DigestTest {
37  
38      @Test
39      void testAllAlgorithmsUseTheSameStandardInput() throws Exception {
40          final InputStream originalIn = System.in;
41          final PrintStream originalOut = System.out;
42          final ByteArrayOutputStream captured = new ByteArrayOutputStream();
43          try {
44              System.setIn(new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)));
45              System.setOut(new PrintStream(captured, true, StandardCharsets.UTF_8.name()));
46              Digest.main(new String[] { "ALL" });
47          } finally {
48              System.setIn(originalIn);
49              System.setOut(originalOut);
50          }
51          final String output = captured.toString(StandardCharsets.UTF_8.name());
52          assertTrue(output.contains("MD2 da853b0d3f88d99b30283a69e6ded6bb"), output);
53          assertTrue(output.contains("MD5 900150983cd24fb0d6963f7d28e17f72"), output);
54          assertTrue(output.contains("SHA-1 a9993e364706816aba3e25717850c26c9cd0d89d"), output);
55          assertTrue(output.contains("SHA-224 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"), output);
56          assertTrue(output.contains("SHA-256 ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"), output);
57          assertTrue(output.contains("SHA-384 cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"), output);
58          assertTrue(output.contains(
59                  "SHA-512 ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"),
60                  output);
61          assertTrue(output.contains("SHA-512/224 4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa"), output);
62          assertTrue(output.contains("SHA-512/256 53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23"), output);
63          if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) {
64              assertTrue(output.contains("SHA3-224 e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf"), output);
65              assertTrue(output.contains("SHA3-256 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"), output);
66              assertTrue(output.contains("SHA3-384 ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25"), output);
67              assertTrue(output.contains(
68                      "SHA3-512 b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"),
69                      output);
70          }
71      }
72  
73      /**
74       * Tests if empty arguments are handled correctly.
75       */
76      @Test
77      void testEmptyArguments() {
78          assertThrows(IllegalArgumentException.class, () -> Digest.main(new String[0]));
79      }
80  
81      /**
82       * Tests if null arguments are handled correctly.
83       */
84      @Test
85      void testNullArguments() {
86          assertThrows(NullPointerException.class, () -> Digest.main(null));
87      }
88  }