1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.Test;
26
27 import static org.junit.Assert.*;
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 public 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("digest() should only be called once", repos[0]);
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 public 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 public 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("digest() should only be called once", proxies[0]);
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 public 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 public void testForProxy_NoProxyAuth() {
130 RemoteRepository repo = newRepo(null, newProxy(null));
131
132 String digest = AuthenticationDigest.forProxy(newSession(), repo);
133 assertEquals("", digest);
134 }
135 }