1 package com.atlassian.security.auth.trustedapps;
2
3 import java.security.KeyPair;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.NoSuchProviderException;
6 import java.security.PrivateKey;
7 import java.security.PublicKey;
8 import java.security.spec.InvalidKeySpecException;
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import junit.framework.TestCase;
15
16 public class TestDefaultTrustedApplicationsManager extends TestCase
17 {
18 final CurrentApplication thisApp = new CurrentApplication()
19 {
20 public EncryptedCertificate encode(final String userName)
21 {
22 throw new UnsupportedOperationException();
23 }
24
25 public String getID()
26 {
27 throw new UnsupportedOperationException();
28 }
29
30 public PublicKey getPublicKey()
31 {
32 throw new UnsupportedOperationException();
33 }
34 };
35
36 public void testSimpleConstructor() throws Exception
37 {
38 final Map<String, TrustedApplication> appMap = new HashMap<String, TrustedApplication>();
39 appMap.put("test", new TrustedApplication()
40 {
41 public ApplicationCertificate decode(final EncryptedCertificate certificate, final HttpServletRequest request) throws InvalidCertificateException
42 {
43 throw new UnsupportedOperationException();
44 }
45
46 public String getID()
47 {
48 throw new UnsupportedOperationException();
49 }
50
51 public PublicKey getPublicKey()
52 {
53 throw new UnsupportedOperationException();
54 };
55
56 public RequestConditions getRequestConditions()
57 {
58 throw new UnsupportedOperationException();
59 }
60
61 public String getName()
62 {
63 return null;
64 }
65 });
66 final TrustedApplicationsManager manager = new DefaultTrustedApplicationsManager(thisApp, appMap);
67
68 assertNotNull(manager.getCurrentApplication());
69 assertSame(thisApp, manager.getCurrentApplication());
70 assertNull(manager.getTrustedApplication("something"));
71 assertNotNull(manager.getTrustedApplication("test"));
72 }
73
74 public void testSimpleConstructorNullApp() throws Exception
75 {
76 try
77 {
78 new DefaultTrustedApplicationsManager(null, new HashMap<String, TrustedApplication>());
79 fail("Should have thrown ex");
80 }
81 catch (final IllegalArgumentException yay)
82 {}
83 }
84
85 public void testSimpleConstructorNullMap() throws Exception
86 {
87
88 try
89 {
90 new DefaultTrustedApplicationsManager(thisApp, null);
91 fail("Should have thrown ex");
92 }
93 catch (final IllegalArgumentException yay)
94 {}
95 }
96
97 public void testConstructorEncryptionProvider() throws Exception
98 {
99 final MockKey publicKey = new MockKey();
100 final EncryptionProvider provider = new MockEncryptionProvider()
101 {
102 @Override
103 public KeyPair generateNewKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException
104 {
105 return new KeyPair(publicKey, new MockKey());
106 }
107
108 @Override
109 public String generateUID()
110 {
111 return "this-is-a-uid";
112 }
113 };
114
115 final TrustedApplicationsManager manager = new DefaultTrustedApplicationsManager(provider);
116 assertNotNull(manager.getCurrentApplication());
117 assertNotNull(manager.getCurrentApplication().getID());
118 assertNotNull(manager.getCurrentApplication().getPublicKey());
119 assertSame(publicKey, manager.getCurrentApplication().getPublicKey());
120 }
121
122 public void testConstructorEncryptionProviderThrowsNoSuchAlgorithmException() throws Exception
123 {
124 final EncryptionProvider provider = new MockEncryptionProvider()
125 {
126 @Override
127 public KeyPair generateNewKeyPair() throws NoSuchAlgorithmException
128 {
129 throw new NoSuchAlgorithmException("some-algorithm");
130 }
131 };
132 try
133 {
134 new DefaultTrustedApplicationsManager(provider);
135 fail("error expected");
136 }
137 catch (final Error expected)
138 {}
139 }
140
141 public void testConstructorEncryptionProviderThrowsNoSuchProviderException() throws Exception
142 {
143 final EncryptionProvider provider = new MockEncryptionProvider()
144 {
145 @Override
146 public KeyPair generateNewKeyPair() throws NoSuchProviderException
147 {
148 throw new NoSuchProviderException("some-algorithm");
149 }
150 };
151 try
152 {
153 new DefaultTrustedApplicationsManager(provider);
154 fail("error expected");
155 }
156 catch (final Error expected)
157 {}
158 }
159 }
160
161 class MockEncryptionProvider implements EncryptionProvider
162 {
163
164 public EncryptedCertificate createEncryptedCertificate(final String userName, final PrivateKey privateKey, final String appId)
165 {
166 throw new UnsupportedOperationException();
167 }
168
169 public ApplicationCertificate decodeEncryptedCertificate(final EncryptedCertificate encCert, final PublicKey publicKey, final String appId) throws InvalidCertificateException
170 {
171 throw new UnsupportedOperationException();
172 }
173
174 public KeyPair generateNewKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException
175 {
176 throw new UnsupportedOperationException();
177 }
178
179 public String generateUID()
180 {
181 throw new UnsupportedOperationException();
182 }
183
184 public Application getApplicationCertificate(final String baseUrl)
185 {
186 throw new UnsupportedOperationException();
187 }
188
189 public PrivateKey toPrivateKey(final byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
190 {
191 throw new UnsupportedOperationException();
192 }
193
194 public PublicKey toPublicKey(final byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
195 {
196 throw new UnsupportedOperationException();
197 }
198 }