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.util.Map;
22  
23  import org.eclipse.aether.DefaultRepositorySystemSession;
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.*;
28  
29  public class AuthenticationDigestTest {
30  
31      private RepositorySystemSession newSession() {
32          return new DefaultRepositorySystemSession();
33      }
34  
35      private RemoteRepository newRepo(Authentication auth, Proxy proxy) {
36          return new RemoteRepository.Builder("test", "default", "http://localhost") //
37                  .setAuthentication(auth)
38                  .setProxy(proxy)
39                  .build();
40      }
41  
42      private Proxy newProxy(Authentication auth) {
43          return new Proxy(Proxy.TYPE_HTTP, "localhost", 8080, auth);
44      }
45  
46      @Test
47      void testForRepository() {
48          final RepositorySystemSession session = newSession();
49          final RemoteRepository[] repos = {null};
50  
51          Authentication auth = new Authentication() {
52              public void fill(AuthenticationContext context, String key, Map<String, String> data) {
53                  fail("AuthenticationDigest should not call fill()");
54              }
55  
56              public void digest(AuthenticationDigest digest) {
57                  assertNotNull(digest);
58                  assertSame(session, digest.getSession());
59                  assertNotNull(digest.getRepository());
60                  assertNull(digest.getProxy());
61                  assertNull(repos[0], "digest() should only be called once");
62                  repos[0] = digest.getRepository();
63  
64                  digest.update((byte[]) null);
65                  digest.update((char[]) null);
66                  digest.update((String[]) null);
67                  digest.update(null, null);
68              }
69          };
70  
71          RemoteRepository repo = newRepo(auth, newProxy(null));
72  
73          String digest = AuthenticationDigest.forRepository(session, repo);
74          assertSame(repo, repos[0]);
75          assertNotNull(digest);
76          assertTrue(digest.length() > 0);
77      }
78  
79      @Test
80      void testForRepository_NoAuth() {
81          RemoteRepository repo = newRepo(null, null);
82  
83          String digest = AuthenticationDigest.forRepository(newSession(), repo);
84          assertEquals("", digest);
85      }
86  
87      @Test
88      void testForProxy() {
89          final RepositorySystemSession session = newSession();
90          final Proxy[] proxies = {null};
91  
92          Authentication auth = new Authentication() {
93              public void fill(AuthenticationContext context, String key, Map<String, String> data) {
94                  fail("AuthenticationDigest should not call fill()");
95              }
96  
97              public void digest(AuthenticationDigest digest) {
98                  assertNotNull(digest);
99                  assertSame(session, digest.getSession());
100                 assertNotNull(digest.getRepository());
101                 assertNotNull(digest.getProxy());
102                 assertNull(proxies[0], "digest() should only be called once");
103                 proxies[0] = digest.getProxy();
104 
105                 digest.update((byte[]) null);
106                 digest.update((char[]) null);
107                 digest.update((String[]) null);
108                 digest.update(null, null);
109             }
110         };
111 
112         Proxy proxy = newProxy(auth);
113 
114         String digest = AuthenticationDigest.forProxy(session, newRepo(null, proxy));
115         assertSame(proxy, proxies[0]);
116         assertNotNull(digest);
117         assertTrue(digest.length() > 0);
118     }
119 
120     @Test
121     void testForProxy_NoProxy() {
122         RemoteRepository repo = newRepo(null, null);
123 
124         String digest = AuthenticationDigest.forProxy(newSession(), repo);
125         assertEquals("", digest);
126     }
127 
128     @Test
129     void testForProxy_NoProxyAuth() {
130         RemoteRepository repo = newRepo(null, newProxy(null));
131 
132         String digest = AuthenticationDigest.forProxy(newSession(), repo);
133         assertEquals("", digest);
134     }
135 }