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.repository;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.*;
28  import static org.mockito.Mockito.mock;
29  
30  public class AuthenticationContextTest {
31  
32      private RepositorySystemSession newSession() {
33          return mock(RepositorySystemSession.class);
34      }
35  
36      private RemoteRepository newRepo(Authentication auth, Proxy proxy) {
37          return new RemoteRepository.Builder("test", "default", "http://localhost") //
38                  .setAuthentication(auth)
39                  .setProxy(proxy)
40                  .build();
41      }
42  
43      private Proxy newProxy(Authentication auth) {
44          return new Proxy(Proxy.TYPE_HTTP, "localhost", 8080, auth);
45      }
46  
47      private Authentication newAuth() {
48          return new Authentication() {
49              public void fill(AuthenticationContext context, String key, Map<String, String> data) {
50                  assertNotNull(context);
51                  assertNotNull(context.getSession());
52                  assertNotNull(context.getRepository());
53                  assertNull(context.get("key"), "fill() should only be called once");
54                  context.put("key", "value");
55              }
56  
57              public void digest(AuthenticationDigest digest) {
58                  fail("AuthenticationContext should not call digest()");
59              }
60          };
61      }
62  
63      @Test
64      void testForRepository() {
65          RepositorySystemSession session = newSession();
66          RemoteRepository repo = newRepo(newAuth(), newProxy(newAuth()));
67          AuthenticationContext context = AuthenticationContext.forRepository(session, repo);
68          assertNotNull(context);
69          assertSame(session, context.getSession());
70          assertSame(repo, context.getRepository());
71          assertNull(context.getProxy());
72          assertEquals("value", context.get("key"));
73          assertEquals("value", context.get("key"));
74      }
75  
76      @Test
77      void testForRepository_NoAuth() {
78          RepositorySystemSession session = newSession();
79          RemoteRepository repo = newRepo(null, newProxy(newAuth()));
80          AuthenticationContext context = AuthenticationContext.forRepository(session, repo);
81          assertNull(context);
82      }
83  
84      @Test
85      void testForProxy() {
86          RepositorySystemSession session = newSession();
87          Proxy proxy = newProxy(newAuth());
88          RemoteRepository repo = newRepo(newAuth(), proxy);
89          AuthenticationContext context = AuthenticationContext.forProxy(session, repo);
90          assertNotNull(context);
91          assertSame(session, context.getSession());
92          assertSame(repo, context.getRepository());
93          assertSame(proxy, context.getProxy());
94          assertEquals("value", context.get("key"));
95          assertEquals("value", context.get("key"));
96      }
97  
98      @Test
99      void testForProxy_NoProxy() {
100         RepositorySystemSession session = newSession();
101         Proxy proxy = null;
102         RemoteRepository repo = newRepo(newAuth(), proxy);
103         AuthenticationContext context = AuthenticationContext.forProxy(session, repo);
104         assertNull(context);
105     }
106 
107     @Test
108     void testForProxy_NoProxyAuth() {
109         RepositorySystemSession session = newSession();
110         Proxy proxy = newProxy(null);
111         RemoteRepository repo = newRepo(newAuth(), proxy);
112         AuthenticationContext context = AuthenticationContext.forProxy(session, repo);
113         assertNull(context);
114     }
115 
116     @Test
117     void testGet_StringVsChars() {
118         AuthenticationContext context = AuthenticationContext.forRepository(newSession(), newRepo(newAuth(), null));
119         context.put("key", new char[] {'v', 'a', 'l', '1'});
120         assertEquals("val1", context.get("key"));
121         context.put("key", "val2");
122         assertArrayEquals(new char[] {'v', 'a', 'l', '2'}, context.get("key", char[].class));
123     }
124 
125     @Test
126     void testGet_StringVsFile() {
127         AuthenticationContext context = AuthenticationContext.forRepository(newSession(), newRepo(newAuth(), null));
128         context.put("key", "val1");
129         assertEquals(new File("val1"), context.get("key", File.class));
130         context.put("key", new File("val2"));
131         assertEquals("val2", context.get("key"));
132     }
133 
134     @Test
135     void testPut_EraseCharArrays() {
136         AuthenticationContext context = AuthenticationContext.forRepository(newSession(), newRepo(newAuth(), null));
137         char[] secret = {'v', 'a', 'l', 'u', 'e'};
138         context.put("key", secret);
139         context.put("key", secret.clone());
140         assertArrayEquals(new char[] {0, 0, 0, 0, 0}, secret);
141     }
142 
143     @Test
144     void testClose_EraseCharArrays() {
145         AuthenticationContext.close(null);
146 
147         AuthenticationContext context = AuthenticationContext.forRepository(newSession(), newRepo(newAuth(), null));
148         char[] secret = {'v', 'a', 'l', 'u', 'e'};
149         context.put("key", secret);
150         AuthenticationContext.close(context);
151         assertArrayEquals(new char[] {0, 0, 0, 0, 0}, secret);
152     }
153 }