View Javadoc
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  package org.eclipse.aether.util;
20  
21  import org.junit.Test;
22  
23  import static org.hamcrest.MatcherAssert.assertThat;
24  import static org.hamcrest.Matchers.is;
25  import static org.junit.Assert.fail;
26  
27  public class StringDigestUtilTest {
28      @Test
29      public void sha1Simple() {
30          assertThat(StringDigestUtil.sha1(null), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
31          assertThat(StringDigestUtil.sha1(""), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
32          assertThat(StringDigestUtil.sha1("something"), is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
33          assertThat(StringDigestUtil.sha1().update(null).digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
34          assertThat(StringDigestUtil.sha1().update("").digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
35          assertThat(
36                  StringDigestUtil.sha1().update("something").digest(), is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
37          assertThat(
38                  StringDigestUtil.sha1().update("some").update("thing").digest(),
39                  is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
40      }
41  
42      @Test
43      public void sha1Manual() {
44          assertThat(new StringDigestUtil("SHA-1").digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
45          assertThat(new StringDigestUtil("SHA-1").update("").digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
46          assertThat(
47                  new StringDigestUtil("SHA-1").update("something").digest(),
48                  is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
49          assertThat(new StringDigestUtil("SHA-1").update(null).digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
50          assertThat(new StringDigestUtil("SHA-1").update("").digest(), is("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
51          assertThat(
52                  new StringDigestUtil("SHA-1").update("something").digest(),
53                  is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
54          assertThat(
55                  new StringDigestUtil("SHA-1").update("some").update("thing").digest(),
56                  is("1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"));
57      }
58  
59      @Test
60      public void md5Manual() {
61          assertThat(new StringDigestUtil("MD5").digest(), is("d41d8cd98f00b204e9800998ecf8427e"));
62          assertThat(new StringDigestUtil("MD5").update("").digest(), is("d41d8cd98f00b204e9800998ecf8427e"));
63          assertThat(new StringDigestUtil("MD5").update("something").digest(), is("437b930db84b8079c2dd804a71936b5f"));
64          assertThat(new StringDigestUtil("MD5").update(null).digest(), is("d41d8cd98f00b204e9800998ecf8427e"));
65          assertThat(new StringDigestUtil("MD5").update("").digest(), is("d41d8cd98f00b204e9800998ecf8427e"));
66          assertThat(new StringDigestUtil("MD5").update("something").digest(), is("437b930db84b8079c2dd804a71936b5f"));
67          assertThat(
68                  new StringDigestUtil("MD5").update("some").update("thing").digest(),
69                  is("437b930db84b8079c2dd804a71936b5f"));
70      }
71  
72      @Test
73      public void unsupportedAlg() {
74          try {
75              new StringDigestUtil("FOO-BAR");
76              fail("StringDigestUtil should throw");
77          } catch (IllegalStateException e) {
78              // good
79          }
80      }
81  }