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.io.File;
025import java.util.Map;
026
027import org.eclipse.aether.DefaultRepositorySystemSession;
028import org.eclipse.aether.RepositorySystemSession;
029import org.junit.Test;
030
031public class AuthenticationContextTest
032{
033
034    private RepositorySystemSession newSession()
035    {
036        return new DefaultRepositorySystemSession();
037    }
038
039    private RemoteRepository newRepo( Authentication auth, Proxy proxy )
040    {
041        return new RemoteRepository.Builder( "test", "default", "http://localhost" ) //
042        .setAuthentication( auth ).setProxy( proxy ).build();
043    }
044
045    private Proxy newProxy( Authentication auth )
046    {
047        return new Proxy( Proxy.TYPE_HTTP, "localhost", 8080, auth );
048    }
049
050    private Authentication newAuth()
051    {
052        return new Authentication()
053        {
054            public void fill( AuthenticationContext context, String key, Map<String, String> data )
055            {
056                assertNotNull( context );
057                assertNotNull( context.getSession() );
058                assertNotNull( context.getRepository() );
059                assertNull( "fill() should only be called once", context.get( "key" ) );
060                context.put( "key", "value" );
061            }
062
063            public void digest( AuthenticationDigest digest )
064            {
065                fail( "AuthenticationContext should not call digest()" );
066            }
067        };
068    }
069
070    @Test
071    public void testForRepository()
072    {
073        RepositorySystemSession session = newSession();
074        RemoteRepository repo = newRepo( newAuth(), newProxy( newAuth() ) );
075        AuthenticationContext context = AuthenticationContext.forRepository( session, repo );
076        assertNotNull( context );
077        assertSame( session, context.getSession() );
078        assertSame( repo, context.getRepository() );
079        assertNull( context.getProxy() );
080        assertEquals( "value", context.get( "key" ) );
081        assertEquals( "value", context.get( "key" ) );
082    }
083
084    @Test
085    public void testForRepository_NoAuth()
086    {
087        RepositorySystemSession session = newSession();
088        RemoteRepository repo = newRepo( null, newProxy( newAuth() ) );
089        AuthenticationContext context = AuthenticationContext.forRepository( session, repo );
090        assertNull( context );
091    }
092
093    @Test
094    public void testForProxy()
095    {
096        RepositorySystemSession session = newSession();
097        Proxy proxy = newProxy( newAuth() );
098        RemoteRepository repo = newRepo( newAuth(), proxy );
099        AuthenticationContext context = AuthenticationContext.forProxy( session, repo );
100        assertNotNull( context );
101        assertSame( session, context.getSession() );
102        assertSame( repo, context.getRepository() );
103        assertSame( proxy, context.getProxy() );
104        assertEquals( "value", context.get( "key" ) );
105        assertEquals( "value", context.get( "key" ) );
106    }
107
108    @Test
109    public void testForProxy_NoProxy()
110    {
111        RepositorySystemSession session = newSession();
112        Proxy proxy = null;
113        RemoteRepository repo = newRepo( newAuth(), proxy );
114        AuthenticationContext context = AuthenticationContext.forProxy( session, repo );
115        assertNull( context );
116    }
117
118    @Test
119    public void testForProxy_NoProxyAuth()
120    {
121        RepositorySystemSession session = newSession();
122        Proxy proxy = newProxy( null );
123        RemoteRepository repo = newRepo( newAuth(), proxy );
124        AuthenticationContext context = AuthenticationContext.forProxy( session, repo );
125        assertNull( context );
126    }
127
128    @Test
129    public void testGet_StringVsChars()
130    {
131        AuthenticationContext context = AuthenticationContext.forRepository( newSession(), newRepo( newAuth(), null ) );
132        context.put( "key", new char[] { 'v', 'a', 'l', '1' } );
133        assertEquals( "val1", context.get( "key" ) );
134        context.put( "key", "val2" );
135        assertArrayEquals( new char[] { 'v', 'a', 'l', '2' }, context.get( "key", char[].class ) );
136    }
137
138    @Test
139    public void testGet_StringVsFile()
140    {
141        AuthenticationContext context = AuthenticationContext.forRepository( newSession(), newRepo( newAuth(), null ) );
142        context.put( "key", "val1" );
143        assertEquals( new File( "val1" ), context.get( "key", File.class ) );
144        context.put( "key", new File( "val2" ) );
145        assertEquals( "val2", context.get( "key" ) );
146    }
147
148    @Test
149    public void testPut_EraseCharArrays()
150    {
151        AuthenticationContext context = AuthenticationContext.forRepository( newSession(), newRepo( newAuth(), null ) );
152        char[] secret = { 'v', 'a', 'l', 'u', 'e' };
153        context.put( "key", secret );
154        context.put( "key", secret.clone() );
155        assertArrayEquals( new char[] { 0, 0, 0, 0, 0 }, secret );
156    }
157
158    @Test
159    public void testClose_EraseCharArrays()
160    {
161        AuthenticationContext.close( null );
162
163        AuthenticationContext context = AuthenticationContext.forRepository( newSession(), newRepo( newAuth(), null ) );
164        char[] secret = { 'v', 'a', 'l', 'u', 'e' };
165        context.put( "key", secret );
166        AuthenticationContext.close( context );
167        assertArrayEquals( new char[] { 0, 0, 0, 0, 0 }, secret );
168    }
169
170}