001package org.eclipse.aether.repository;
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 static org.junit.Assert.*;
023
024import java.util.Map;
025
026import org.eclipse.aether.DefaultRepositorySystemSession;
027import org.eclipse.aether.RepositorySystemSession;
028import org.junit.Test;
029
030public class AuthenticationDigestTest
031{
032
033    private RepositorySystemSession newSession()
034    {
035        return new DefaultRepositorySystemSession();
036    }
037
038    private RemoteRepository newRepo( Authentication auth, Proxy proxy )
039    {
040        return new RemoteRepository.Builder( "test", "default", "http://localhost" ) //
041        .setAuthentication( auth ).setProxy( proxy ).build();
042    }
043
044    private Proxy newProxy( Authentication auth )
045    {
046        return new Proxy( Proxy.TYPE_HTTP, "localhost", 8080, auth );
047    }
048
049    @Test
050    public void testForRepository()
051    {
052        final RepositorySystemSession session = newSession();
053        final RemoteRepository[] repos = { null };
054
055        Authentication auth = new Authentication()
056        {
057            public void fill( AuthenticationContext context, String key, Map<String, String> data )
058            {
059                fail( "AuthenticationDigest should not call fill()" );
060            }
061
062            public void digest( AuthenticationDigest digest )
063            {
064                assertNotNull( digest );
065                assertSame( session, digest.getSession() );
066                assertNotNull( digest.getRepository() );
067                assertNull( digest.getProxy() );
068                assertNull( "digest() should only be called once", repos[0] );
069                repos[0] = digest.getRepository();
070
071                digest.update( (byte[]) null );
072                digest.update( (char[]) null );
073                digest.update( (String[]) null );
074                digest.update( null, null );
075            }
076        };
077
078        RemoteRepository repo = newRepo( auth, newProxy( null ) );
079
080        String digest = AuthenticationDigest.forRepository( session, repo );
081        assertSame( repo, repos[0] );
082        assertNotNull( digest );
083        assertTrue( digest.length() > 0 );
084    }
085
086    @Test
087    public void testForRepository_NoAuth()
088    {
089        RemoteRepository repo = newRepo( null, null );
090
091        String digest = AuthenticationDigest.forRepository( newSession(), repo );
092        assertEquals( "", digest );
093    }
094
095    @Test
096    public void testForProxy()
097    {
098        final RepositorySystemSession session = newSession();
099        final Proxy[] proxies = { null };
100
101        Authentication auth = new Authentication()
102        {
103            public void fill( AuthenticationContext context, String key, Map<String, String> data )
104            {
105                fail( "AuthenticationDigest should not call fill()" );
106            }
107
108            public void digest( AuthenticationDigest digest )
109            {
110                assertNotNull( digest );
111                assertSame( session, digest.getSession() );
112                assertNotNull( digest.getRepository() );
113                assertNotNull( digest.getProxy() );
114                assertNull( "digest() should only be called once", proxies[0] );
115                proxies[0] = digest.getProxy();
116
117                digest.update( (byte[]) null );
118                digest.update( (char[]) null );
119                digest.update( (String[]) null );
120                digest.update( null, null );
121            }
122        };
123
124        Proxy proxy = newProxy( auth );
125
126        String digest = AuthenticationDigest.forProxy( session, newRepo( null, proxy ) );
127        assertSame( proxy, proxies[0] );
128        assertNotNull( digest );
129        assertTrue( digest.length() > 0 );
130    }
131
132    @Test
133    public void testForProxy_NoProxy()
134    {
135        RemoteRepository repo = newRepo( null, null );
136
137        String digest = AuthenticationDigest.forProxy( newSession(), repo );
138        assertEquals( "", digest );
139    }
140
141    @Test
142    public void testForProxy_NoProxyAuth()
143    {
144        RemoteRepository repo = newRepo( null, newProxy( null ) );
145
146        String digest = AuthenticationDigest.forProxy( newSession(), repo );
147        assertEquals( "", digest );
148    }
149
150}