1 package com.atlassian.security.auth.trustedapps;
2
3 import com.atlassian.security.auth.trustedapps.ApplicationRetriever.ApplicationNotFoundException;
4
5 import java.security.NoSuchAlgorithmException;
6 import java.security.NoSuchProviderException;
7 import java.security.PublicKey;
8 import java.security.spec.InvalidKeySpecException;
9 import java.util.Arrays;
10
11 import junit.framework.TestCase;
12
13 public class TestListApplicationRetriever extends TestCase
14 {
15 final String encoding = "UTF-8";
16 static final String BASE64_PUBLIC_KEY = TestReaderApplicationRetriever.BASE64_PUBLIC_KEY;
17 static final byte[] PUBLIC_KEY = TestReaderApplicationRetriever.PUBLIC_KEY;
18
19 public void testGetApplicationListV0() throws Exception
20 {
21 final MockKey key = new MockKey();
22 final int[] count = new int[1];
23 MockEncryptionProvider provider = new MockEncryptionProvider()
24 {
25 public PublicKey toPublicKey(byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
26 {
27 assertTrue(Arrays.equals(PUBLIC_KEY, encodedForm));
28 count[0]++;
29 return key;
30 }
31 };
32
33 Application application = construct(new String[] { "appId", BASE64_PUBLIC_KEY }, provider).getApplication();
34 assertEquals(1, count[0]);
35 assertSame(key, application.getPublicKey());
36 }
37
38 public void testGetApplicationListV1() throws Exception
39 {
40
41 final MockKey key = new MockKey();
42 final int[] count = new int[1];
43 MockEncryptionProvider provider = new MockEncryptionProvider()
44 {
45 public PublicKey toPublicKey(byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
46 {
47 assertTrue(Arrays.equals(PUBLIC_KEY, encodedForm));
48 count[0]++;
49 return key;
50 }
51 };
52
53 Application application = construct(new String[] { "appId", BASE64_PUBLIC_KEY, TrustedApplicationUtils.Constant.VERSION.toString(), TrustedApplicationUtils.Constant.MAGIC }, provider).getApplication();
54 assertEquals(1, count[0]);
55 assertSame(key, application.getPublicKey());
56 }
57
58 public void testGetApplicationListTooSmall() throws Exception
59 {
60 try
61 {
62 construct(new String[0]).getApplication();
63 fail("Should have thrown NotFoundException");
64 }
65 catch (ApplicationNotFoundException expected)
66 {
67 }
68 }
69
70 public void testConstructorAppIdParamContainNoNullsV1()
71 {
72 construct(new String[] { "applicationId", "publicKey", TrustedApplicationUtils.Constant.VERSION.toString(), TrustedApplicationUtils.Constant.MAGIC });
73 }
74
75 public void testConstructorParamsContainNullsV1() throws Exception
76 {
77 try
78 {
79 construct(new String[] { null, null, null, null });
80 fail("Should have thrown IllegalArgumentException");
81 }
82 catch (IllegalArgumentException expected)
83 {
84 }
85 }
86
87 public void testConstructorAppIdParamContainNullsV1() throws Exception
88 {
89 try
90 {
91 construct(new String[] { "applicationId", null, TrustedApplicationUtils.Constant.VERSION.toString(), TrustedApplicationUtils.Constant.MAGIC });
92 fail("Should have thrown IllegalArgumentException");
93 }
94 catch (IllegalArgumentException expected)
95 {
96 }
97 }
98
99 public void testConstructorPublicKeyParamContainNullsV1() throws Exception
100 {
101 try
102 {
103 construct(new String[] { null, "publicKey", TrustedApplicationUtils.Constant.VERSION.toString(), TrustedApplicationUtils.Constant.MAGIC });
104 fail("Should have thrown IllegalArgumentException");
105 }
106 catch (IllegalArgumentException expected)
107 {
108 }
109 }
110
111 public void testConstructorAppIdParamContainNoNullsV0()
112 {
113 construct(new String[] { "applicationId", "publicKey" });
114 }
115
116 public void testConstructorParamsContainNullsV0() throws Exception
117 {
118 try
119 {
120 construct(new String[] { null, null });
121 fail("Should have thrown IllegalArgumentException");
122 }
123 catch (IllegalArgumentException expected)
124 {
125 }
126 }
127
128 public void testConstructorAppIdParamContainNullsV0() throws Exception
129 {
130 try
131 {
132 construct(new String[] { "applicationId", null });
133 fail("Should have thrown IllegalArgumentException");
134 }
135 catch (IllegalArgumentException expected)
136 {
137 }
138 }
139
140 public void testConstructorPublicKeyParamContainNullsV0() throws Exception
141 {
142 try
143 {
144 construct(new String[] { null, "publicKey" });
145 fail("Should have thrown IllegalArgumentException");
146 }
147 catch (IllegalArgumentException expected)
148 {
149 }
150 }
151
152 private ApplicationRetriever construct(String[] input)
153 {
154 MockEncryptionProvider provider = new MockEncryptionProvider();
155 return construct(input, provider);
156 }
157
158 private ApplicationRetriever construct(String[] input, MockEncryptionProvider provider)
159 {
160 return new ListApplicationRetriever(provider, Arrays.asList(input));
161 }
162 }