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.util.HashMap;
7   import java.util.Map;
8   
9   import org.junit.Test;
10  import org.mockito.invocation.InvocationOnMock;
11  import org.mockito.stubbing.Answer;
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertSame;
16  import static org.junit.Assert.fail;
17  import static org.mockito.Mockito.mock;
18  import static org.mockito.Mockito.when;
19  
20  public class TestDefaultTrustedApplicationsManager
21  {
22      final CurrentApplication thisApp = mock(CurrentApplication.class);
23  
24      @Test
25      public void testSimpleConstructor() throws Exception
26      {
27          final Map<String, TrustedApplication> appMap = new HashMap<String, TrustedApplication>();
28          appMap.put("test", mock(TrustedApplication.class));
29          final TrustedApplicationsManager manager = new DefaultTrustedApplicationsManager(thisApp, appMap);
30  
31          assertNotNull(manager.getCurrentApplication());
32          assertSame(thisApp, manager.getCurrentApplication());
33          assertNull(manager.getTrustedApplication("something"));
34          assertNotNull(manager.getTrustedApplication("test"));
35      }
36  
37      @Test
38      public void testSimpleConstructorNullApp() throws Exception
39      {
40          try
41          {
42              new DefaultTrustedApplicationsManager(null, new HashMap<String, TrustedApplication>());
43              fail("Should have thrown ex");
44          }
45          catch (final IllegalArgumentException yay)
46          {}
47      }
48  
49      @Test
50      public void testSimpleConstructorNullMap() throws Exception
51      {
52  
53          try
54          {
55              new DefaultTrustedApplicationsManager(thisApp, null);
56              fail("Should have thrown ex");
57          }
58          catch (final IllegalArgumentException yay)
59          {}
60      }
61  
62      @Test
63      public void testConstructorEncryptionProvider() throws Exception
64      {
65          final MockKey publicKey = new MockKey();
66          final EncryptionProvider provider = mock(EncryptionProvider.class);
67          when(provider.generateNewKeyPair()).thenAnswer(new Answer<KeyPair>(){
68              public KeyPair answer(InvocationOnMock invocation) throws Throwable
69              {
70                  return new KeyPair(publicKey, new MockKey());
71              }
72          });
73          when(provider.generateUID()).thenReturn("this-is-a-uid");
74  
75          final TrustedApplicationsManager manager = new DefaultTrustedApplicationsManager(provider);
76          assertNotNull(manager.getCurrentApplication());
77          assertNotNull(manager.getCurrentApplication().getID());
78          assertNotNull(manager.getCurrentApplication().getPublicKey());
79          assertSame(publicKey, manager.getCurrentApplication().getPublicKey());
80      }
81  
82      @Test(expected = AssertionError.class)
83      public void testConstructorEncryptionProviderThrowsNoSuchAlgorithmException() throws Exception
84      {
85          final EncryptionProvider provider = mock(EncryptionProvider.class);
86          when(provider.generateNewKeyPair()).thenThrow(new NoSuchAlgorithmException("some-algorithm"));
87          new DefaultTrustedApplicationsManager(provider);
88      }
89  
90      @Test(expected = AssertionError.class)
91      public void testConstructorEncryptionProviderThrowsNoSuchProviderException() throws Exception
92      {
93          final EncryptionProvider provider = mock(EncryptionProvider.class);
94          when(provider.generateNewKeyPair()).thenThrow(new NoSuchProviderException("some-algorithm"));
95          new DefaultTrustedApplicationsManager(provider);
96      }
97  }