001package org.eclipse.aether.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.junit.Test;
023
024import static org.hamcrest.MatcherAssert.assertThat;
025import static org.hamcrest.Matchers.is;
026import static org.junit.Assert.fail;
027
028public class StringDigestUtilTest
029{
030    @Test
031    public void sha1Simple()
032    {
033        assertThat( StringDigestUtil.sha1( null ),
034                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
035        assertThat( StringDigestUtil.sha1( "" ),
036                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
037        assertThat( StringDigestUtil.sha1( "something" ),
038                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
039        assertThat( StringDigestUtil.sha1().update( null ).digest(),
040                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
041        assertThat( StringDigestUtil.sha1().update( "" ).digest(),
042                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
043        assertThat( StringDigestUtil.sha1().update( "something" ).digest(),
044                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
045        assertThat( StringDigestUtil.sha1().update( "some" ).update( "thing" ).digest(),
046                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
047    }
048
049    @Test
050    public void sha1Manual()
051    {
052        assertThat( new StringDigestUtil( "SHA-1" ).digest(),
053                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
054        assertThat( new StringDigestUtil( "SHA-1" ).update( "" ).digest(),
055                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
056        assertThat( new StringDigestUtil( "SHA-1" ).update( "something" ).digest(),
057                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
058        assertThat( new StringDigestUtil( "SHA-1" ).update( null ).digest(),
059                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
060        assertThat( new StringDigestUtil( "SHA-1" ).update( "" ).digest(),
061                is( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ) );
062        assertThat( new StringDigestUtil( "SHA-1" ).update( "something" ).digest(),
063                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
064        assertThat( new StringDigestUtil( "SHA-1" ).update( "some" ).update( "thing" ).digest(),
065                is( "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29" ) );
066    }
067
068    @Test
069    public void md5Manual()
070    {
071        assertThat( new StringDigestUtil( "MD5" ).digest(),
072                is( "d41d8cd98f00b204e9800998ecf8427e" ) );
073        assertThat( new StringDigestUtil( "MD5" ).update( "" ).digest(),
074                is( "d41d8cd98f00b204e9800998ecf8427e" ) );
075        assertThat( new StringDigestUtil( "MD5" ).update( "something" ).digest(),
076                is( "437b930db84b8079c2dd804a71936b5f" ) );
077        assertThat( new StringDigestUtil( "MD5" ).update( null ).digest(),
078                is( "d41d8cd98f00b204e9800998ecf8427e" ) );
079        assertThat( new StringDigestUtil( "MD5" ).update( "" ).digest(),
080                is( "d41d8cd98f00b204e9800998ecf8427e" ) );
081        assertThat( new StringDigestUtil( "MD5" ).update( "something" ).digest(),
082                is( "437b930db84b8079c2dd804a71936b5f" ) );
083        assertThat( new StringDigestUtil( "MD5" ).update( "some" ).update( "thing" ).digest(),
084                is( "437b930db84b8079c2dd804a71936b5f" ) );
085    }
086
087    @Test
088    public void unsupportedAlg()
089    {
090        try
091        {
092            new StringDigestUtil( "FOO-BAR" );
093            fail( "StringDigestUtil should throw" );
094        }
095        catch ( IllegalStateException e )
096        {
097            // good
098        }
099    }
100}
101