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