1   package com.atlassian.security.auth.trustedapps;
2   
3   import com.atlassian.security.auth.trustedapps.ApplicationRetriever.InvalidApplicationDetailsException;
4   
5   import org.bouncycastle.util.encoders.Base64;
6   
7   import java.io.IOException;
8   import java.io.Reader;
9   import java.io.StringReader;
10  import java.io.UnsupportedEncodingException;
11  import java.security.NoSuchAlgorithmException;
12  import java.security.NoSuchProviderException;
13  import java.security.PublicKey;
14  import java.security.spec.InvalidKeySpecException;
15  import java.util.Arrays;
16  
17  import junit.framework.TestCase;
18  
19  public class TestReaderApplicationRetriever extends TestCase
20  {
21      static final String BASE64_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCySptbugHAzWUJY3ALWhuSCPhVXnwbUBfsRExYQitBCVny4V1DcU2SAx22bH9dSM0X7NdMObF74r+Wd77QoPAtaySqFLqCeRCbFmhHgVSi+pGeCipTpueefSkz2AX8Aj+9x27tqjBsX1LtNWVLDsinEhBWN68R+iEOmf/6jGWObQIDAQAB";
22      static final byte[] PUBLIC_KEY = Base64.decode(getBytes(BASE64_PUBLIC_KEY));
23      private static final String CHARSET = TrustedApplicationUtils.Constant.CHARSET_NAME;
24  
25      static byte[] getBytes(String input)
26      {
27          try
28          {
29              return input.getBytes(CHARSET);
30          }
31          catch (UnsupportedEncodingException e)
32          {
33              throw new AssertionError(e);
34          }
35      }
36  
37      static String getString(byte[] bytes)
38      {
39          try
40          {
41              return new String(bytes, CHARSET);
42          }
43          catch (UnsupportedEncodingException e)
44          {
45              throw new AssertionError(e);
46          }
47      }
48  
49      public void testProtocolVersion0() throws Exception
50      {
51          final String cert = "appId:11112222\n" + BASE64_PUBLIC_KEY;
52          final Application app = new ReaderApplicationRetriever(new StringReader(cert), new MockEncryptionProvider()
53          {
54              public PublicKey toPublicKey(byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
55              {
56                  assertTrue(Arrays.equals(PUBLIC_KEY, encodedForm));
57                  return new MockKey();
58              }
59          }).getApplication();
60          assertEquals("appId:11112222", app.getID());
61          assertNotNull(app.getPublicKey());
62      }
63  
64      public void testProtocolVersion1() throws Exception
65      {
66          final String cert = "appId:11112222\n" + BASE64_PUBLIC_KEY + "\n" + TrustedApplicationUtils.Constant.VERSION + "\n" + TrustedApplicationUtils.Constant.MAGIC;
67          final Application app = new ReaderApplicationRetriever(new StringReader(cert), new MockEncryptionProvider()
68          {
69              public PublicKey toPublicKey(byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
70              {
71                  assertTrue(Arrays.equals(PUBLIC_KEY, encodedForm));
72                  return new MockKey();
73              }
74          }).getApplication();
75          assertEquals("appId:11112222", app.getID());
76          assertNotNull(app.getPublicKey());
77      }
78  
79      public void testProtocolVersion1BadMagic() throws Exception
80      {
81          final String cert = "appId:11112222\n" + BASE64_PUBLIC_KEY + "\n" + TrustedApplicationUtils.Constant.VERSION + "\n" + TrustedApplicationUtils.Constant.MAGIC + "123";
82          final ApplicationRetriever retriever = new ReaderApplicationRetriever(new StringReader(cert), new MockEncryptionProvider()
83          {
84              public PublicKey toPublicKey(byte[] encodedForm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
85              {
86                  assertTrue(Arrays.equals(PUBLIC_KEY, encodedForm));
87                  return new MockKey();
88              }
89          });
90          try
91          {
92              retriever.getApplication();
93              fail("Should have thrown InvalidApplicationDetailsException");
94          }
95          catch (InvalidApplicationDetailsException yay)
96          {
97          }
98      }
99  
100     public void testBadReaderThrowsCtorException() throws Exception
101     {
102         Reader reader = new Reader()
103         {
104             public void close() throws IOException
105             {
106             }
107 
108             public int read(char[] cbuf, int off, int len) throws IOException
109             {
110                 throw new IOException("bad reader");
111             }
112         };
113         try
114         {
115             new ReaderApplicationRetriever(reader, new MockEncryptionProvider());
116             fail("Should have thrown RuntimeException");
117         }
118         catch (RuntimeException yay)
119         {
120             assertEquals("java.io.IOException: bad reader", yay.getMessage());
121         }
122     }
123 }