1   package com.atlassian.security.auth.trustedapps.filter;
2   
3   import com.atlassian.security.auth.trustedapps.Application;
4   import com.atlassian.security.auth.trustedapps.ApplicationRetriever;
5   import com.atlassian.security.auth.trustedapps.CurrentApplication;
6   import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
7   import com.atlassian.security.auth.trustedapps.TrustedApplication;
8   import com.atlassian.security.auth.trustedapps.TrustedApplicationUtils;
9   import com.atlassian.security.auth.trustedapps.TrustedApplicationsManager;
10  import com.atlassian.security.auth.trustedapps.filter.TrustedApplicationsFilter.CertificateServer;
11  import com.atlassian.security.auth.trustedapps.filter.TrustedApplicationsFilter.CertificateServerImpl;
12  
13  import java.io.BufferedReader;
14  import java.io.IOException;
15  import java.io.StringReader;
16  import java.io.StringWriter;
17  import java.io.Writer;
18  import java.security.PublicKey;
19  import java.util.Set;
20  
21  import junit.framework.TestCase;
22  
23  public class TestTrustedAppCertificateServerImpl extends TestCase
24  {
25  
26      public void testCertificateServer() throws Exception
27      {
28          TrustedApplicationsManager appManager = new TrustedApplicationsManager()
29          {
30              public CurrentApplication getCurrentApplication()
31              {
32                  return new CurrentApplication()
33                  {
34                      public String getID()
35                      {
36                          return "hehe-12345";
37                      }
38  
39                      public PublicKey getPublicKey()
40                      {
41                          return new PublicKey()
42                          {
43                              public String getAlgorithm()
44                              {
45                                  return "ALGY-RHYTHM";
46                              }
47  
48                              public byte[] getEncoded()
49                              {
50                                  return new byte[] { 1, 2, 3, 4 };
51                              }
52  
53                              public String getFormat()
54                              {
55                                  return "FROMAT";
56                              }
57                          };
58                      }
59  
60                      public EncryptedCertificate encode(String userName)
61                      {
62                          throw new UnsupportedOperationException();
63                      }
64                      
65                      public EncryptedCertificate encode(String userName, String urlToSign)
66                      {
67                          throw new UnsupportedOperationException();
68                      }
69                  };
70              }
71  
72              public TrustedApplication getTrustedApplication(String id)
73              {
74                  throw new UnsupportedOperationException();
75              }
76          };
77          CertificateServer server = new CertificateServerImpl(appManager);
78  
79          StringWriter writer = new StringWriter();
80          server.writeCertificate(writer);
81  
82          BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
83  
84          assertEquals("hehe-12345", reader.readLine());
85          assertEquals("AQIDBA==", reader.readLine());
86          assertEquals(TrustedApplicationUtils.Constant.VERSION.toString(), reader.readLine());
87          assertEquals(TrustedApplicationUtils.Constant.MAGIC, reader.readLine());
88          assertNull(reader.readLine());
89      }
90  
91      public void testCertificateServerWriterExceptionThrowsWrappedRuntimeEx() throws Exception
92      {
93          TrustedApplicationsManager appManager = new TrustedApplicationsManager()
94          {
95              public CurrentApplication getCurrentApplication()
96              {
97                  return new CurrentApplication()
98                  {
99                      public String getID()
100                     {
101                         return "hehe-12345";
102                     }
103 
104                     public PublicKey getPublicKey()
105                     {
106                         return new PublicKey()
107                         {
108                             public String getAlgorithm()
109                             {
110                                 return "ALGY-RHYTHM";
111                             }
112 
113                             public byte[] getEncoded()
114                             {
115                                 return new byte[] { 1, 2, 3, 4 };
116                             }
117 
118                             public String getFormat()
119                             {
120                                 return "FROMAT";
121                             }
122                         };
123                     }
124 
125                     public EncryptedCertificate encode(String userName)
126                     {
127                         throw new UnsupportedOperationException();
128                     }
129                     
130                     public EncryptedCertificate encode(String userName, String urlToSign)
131                     {
132                         throw new UnsupportedOperationException();
133                     }
134                 };
135             }
136 
137             public TrustedApplication getTrustedApplication(String id)
138             {
139                 throw new UnsupportedOperationException();
140             }
141 
142             public TrustedApplication addTrustedApplication(Application app, long certificateTimeout, Set<String> urlPatterns, Set<String> ipPatterns)
143             {
144                 throw new UnsupportedOperationException();
145             }
146 
147             public Application getApplicationCertificate(String url) throws ApplicationRetriever.RetrievalException
148             {
149                 throw new UnsupportedOperationException();
150             }
151 
152             public void deleteApplication(String id)
153             {
154                 throw new UnsupportedOperationException();
155             }
156         };
157         CertificateServer server = new CertificateServerImpl(appManager);
158 
159         final IOException ioEx = new IOException("poo");
160         Writer writer = new Writer()
161         {
162             public void write(char[] cbuf, int off, int len) throws IOException
163             {
164                 throw ioEx;
165             }
166 
167             public void close() throws IOException
168             {
169             }
170 
171             public void flush() throws IOException
172             {
173             }
174         };
175         try
176         {
177             server.writeCertificate(writer);
178             fail("RuntimeException expected");
179         }
180         catch (RuntimeException ex)
181         {
182             assertSame(ioEx, ex.getCause());
183         }
184     }
185 }