Clover Coverage Report - Atlassian Trusted Apps(Aggregated)
Coverage timestamp: Tue Jun 9 2009 19:34:44 CDT
29   108   12   3.22
0   88   0.41   4.5
9     1.33  
2    
 
 
  TestTrustedApplications       Line # 12 17 4 95% 0.95
  TestTrustedApplications.TestApplication       Line # 57 12 8 77.8% 0.7777778
 
  (3)
 
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.security.PublicKey;
7   
8    import javax.servlet.http.HttpServletRequest;
9   
10    import junit.framework.TestCase;
11   
 
12    public class TestTrustedApplications extends TestCase
13    {
14    private final long timeout = 200L;
15    private final TestApplication app = new TestApplication("test", timeout);
16   
 
17  1 toggle public void testRoundTrip() throws Exception
18    {
19  1 final EncryptedCertificate encodedCert = app.encode("userX");
20  1 final ApplicationCertificate cert = app.decode(encodedCert, null);
21  1 assertEquals("userX", cert.getUserName());
22  1 assertEquals(app.getID(), cert.getApplicationID());
23    }
24   
 
25  1 toggle public void testNonExpiry() throws InvalidCertificateException
26    {
27  1 final EncryptedCertificate encodedCert = app.encode("userX");
28  1 final ApplicationCertificate cert = app.decode(encodedCert, null);
29  1 assertEquals("userX", cert.getUserName());
30  1 assertEquals(app.getID(), cert.getApplicationID());
31   
32    // decode again to make sure we can call twice
33  1 app.decode(encodedCert, null);
34    }
35   
 
36  1 toggle public void testExpiry() throws Exception
37    {
38  1 final EncryptedCertificate encodedCert = app.encode("userX");
39  1 ApplicationCertificate cert = app.decode(encodedCert, null);
40  1 assertEquals("userX", cert.getUserName());
41  1 assertEquals(app.getID(), cert.getApplicationID());
42   
43  1 Thread.sleep(timeout + 10);
44   
45    // decode it again
46  1 try
47    {
48  1 cert = app.decode(encodedCert, null);
49  0 fail("This certificate should have expired");
50    }
51    catch (final InvalidCertificateException e)
52    {
53    // expected
54    }
55    }
56   
 
57    static class TestApplication implements CurrentApplication, TrustedApplication
58    {
59    private final KeyPair keyPair;
60    private final String id;
61    private final DefaultTrustedApplication trustedApp;
62    private final DefaultCurrentApplication curApp;
63   
 
64  3 toggle public TestApplication(final String id, final long timeout)
65    {
66  3 final EncryptionProvider encryptionProvider = new BouncyCastleEncryptionProvider();
67  3 try
68    {
69  3 this.keyPair = encryptionProvider.generateNewKeyPair();
70    }
71    catch (final NoSuchAlgorithmException e)
72    {
73  0 throw new RuntimeException(e);
74    }
75    catch (final NoSuchProviderException e)
76    {
77  0 throw new RuntimeException(e);
78    }
79  3 this.id = id;
80  3 trustedApp = new DefaultTrustedApplication(encryptionProvider, keyPair.getPublic(), id, timeout, new RequestValidator()
81    {
 
82  4 toggle public void validate(final HttpServletRequest request) throws InvalidRequestException
83    {}
84    });
85  3 curApp = new DefaultCurrentApplication(keyPair.getPublic(), keyPair.getPrivate(), id);
86    }
87   
 
88  3 toggle public EncryptedCertificate encode(final String userName)
89    {
90  3 return curApp.encode(userName);
91    }
92   
 
93  3 toggle public String getID()
94    {
95  3 return id;
96    }
97   
 
98  0 toggle public PublicKey getPublicKey()
99    {
100  0 return trustedApp.getPublicKey();
101    }
102   
 
103  5 toggle public ApplicationCertificate decode(final EncryptedCertificate certificateStr, final HttpServletRequest request) throws InvalidCertificateException
104    {
105  5 return trustedApp.decode(certificateStr, request);
106    }
107    }
108    }