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              }
66  
67              public TrustedApplication getTrustedApplication(String id)
68              {
69                  throw new UnsupportedOperationException();
70              }
71          };
72          CertificateServer server = new CertificateServerImpl(appManager);
73  
74          StringWriter writer = new StringWriter();
75          server.writeCertificate(writer);
76  
77          BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
78  
79          assertEquals("hehe-12345", reader.readLine());
80          assertEquals("AQIDBA==", reader.readLine());
81          assertEquals(TrustedApplicationUtils.Constant.VERSION.toString(), reader.readLine());
82          assertEquals(TrustedApplicationUtils.Constant.MAGIC, reader.readLine());
83          assertNull(reader.readLine());
84      }
85  
86      public void testCertificateServerWriterExceptionThrowsWrappedRuntimeEx() throws Exception
87      {
88          TrustedApplicationsManager appManager = new TrustedApplicationsManager()
89          {
90              public CurrentApplication getCurrentApplication()
91              {
92                  return new CurrentApplication()
93                  {
94                      public String getID()
95                      {
96                          return "hehe-12345";
97                      }
98  
99                      public PublicKey getPublicKey()
100                     {
101                         return new PublicKey()
102                         {
103                             public String getAlgorithm()
104                             {
105                                 return "ALGY-RHYTHM";
106                             }
107 
108                             public byte[] getEncoded()
109                             {
110                                 return new byte[] { 1, 2, 3, 4 };
111                             }
112 
113                             public String getFormat()
114                             {
115                                 return "FROMAT";
116                             }
117                         };
118                     }
119 
120                     public EncryptedCertificate encode(String userName)
121                     {
122                         throw new UnsupportedOperationException();
123                     }
124                 };
125             }
126 
127             public TrustedApplication getTrustedApplication(String id)
128             {
129                 throw new UnsupportedOperationException();
130             }
131 
132             public TrustedApplication addTrustedApplication(Application app, long certificateTimeout, Set<String> urlPatterns, Set<String> ipPatterns)
133             {
134                 throw new UnsupportedOperationException();
135             }
136 
137             public Application getApplicationCertificate(String url) throws ApplicationRetriever.RetrievalException
138             {
139                 throw new UnsupportedOperationException();
140             }
141 
142             public void deleteApplication(String id)
143             {
144                 throw new UnsupportedOperationException();
145             }
146         };
147         CertificateServer server = new CertificateServerImpl(appManager);
148 
149         final IOException ioEx = new IOException("poo");
150         Writer writer = new Writer()
151         {
152             public void write(char[] cbuf, int off, int len) throws IOException
153             {
154                 throw ioEx;
155             }
156 
157             public void close() throws IOException
158             {
159             }
160 
161             public void flush() throws IOException
162             {
163             }
164         };
165         try
166         {
167             server.writeCertificate(writer);
168             fail("RuntimeException expected");
169         }
170         catch (RuntimeException ex)
171         {
172             assertSame(ioEx, ex.getCause());
173         }
174     }
175 }