1 package org.eclipse.aether.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.util.Map;
26
27 import org.eclipse.aether.DefaultRepositorySystemSession;
28 import org.eclipse.aether.RepositorySystemSession;
29 import org.junit.Test;
30
31 public class AuthenticationContextTest
32 {
33
34 private RepositorySystemSession newSession()
35 {
36 return new DefaultRepositorySystemSession();
37 }
38
39 private RemoteRepository newRepo( Authentication auth, Proxy proxy )
40 {
41 return new RemoteRepository.Builder( "test", "default", "http://localhost" ) //
42 .setAuthentication( auth ).setProxy( proxy ).build();
43 }
44
45 private Proxy newProxy( Authentication auth )
46 {
47 return new Proxy( Proxy.TYPE_HTTP, "localhost", 8080, auth );
48 }
49
50 private Authentication newAuth()
51 {
52 return new Authentication()
53 {
54 public void fill( AuthenticationContext context, String key, Map<String, String> data )
55 {
56 assertNotNull( context );
57 assertNotNull( context.getSession() );
58 assertNotNull( context.getRepository() );
59 assertNull( "fill() should only be called once", context.get( "key" ) );
60 context.put( "key", "value" );
61 }
62
63 public void digest( AuthenticationDigest digest )
64 {
65 fail( "AuthenticationContext should not call digest()" );
66 }
67 };
68 }
69
70 @Test
71 public void testForRepository()
72 {
73 RepositorySystemSession session = newSession();
74 RemoteRepository repo = newRepo( newAuth(), newProxy( newAuth() ) );
75 AuthenticationContext context = AuthenticationContext.forRepository( session, repo );
76 assertNotNull( context );
77 assertSame( session, context.getSession() );
78 assertSame( repo, context.getRepository() );
79 assertNull( context.getProxy() );
80 assertEquals( "value", context.get( "key" ) );
81 assertEquals( "value", context.get( "key" ) );
82 }
83
84 @Test
85 public void testForRepository_NoAuth()
86 {
87 RepositorySystemSession session = newSession();
88 RemoteRepository repo = newRepo( null, newProxy( newAuth() ) );
89 AuthenticationContext context = AuthenticationContext.forRepository( session, repo );
90 assertNull( context );
91 }
92
93 @Test
94 public void testForProxy()
95 {
96 RepositorySystemSession session = newSession();
97 Proxy proxy = newProxy( newAuth() );
98 RemoteRepository repo = newRepo( newAuth(), proxy );
99 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 }