| 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 |
|
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 4 |
Complexity Density: 0.24 |
|
| 12 |
|
public class TestTrustedApplications extends TestCase |
| 13 |
|
{ |
| 14 |
|
private final long timeout = 200L; |
| 15 |
|
private final TestApplication app = new TestApplication("test", timeout); |
| 16 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 17 |
1
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1
PASS
|
|
| 25 |
1
|
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 |
|
|
| 33 |
1
|
app.decode(encodedCert, null); |
| 34 |
|
} |
| 35 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
1
PASS
|
|
| 36 |
1
|
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 |
|
|
| 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 |
|
|
| 54 |
|
} |
| 55 |
|
} |
| 56 |
|
|
|
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 8 |
Complexity Density: 0.67 |
|
| 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 |
|
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 64 |
3
|
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 |
|
{ |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 82 |
4
|
public void validate(final HttpServletRequest request) throws InvalidRequestException... |
| 83 |
|
{} |
| 84 |
|
}); |
| 85 |
3
|
curApp = new DefaultCurrentApplication(keyPair.getPublic(), keyPair.getPrivate(), id); |
| 86 |
|
} |
| 87 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 88 |
3
|
public EncryptedCertificate encode(final String userName)... |
| 89 |
|
{ |
| 90 |
3
|
return curApp.encode(userName); |
| 91 |
|
} |
| 92 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 93 |
3
|
public String getID()... |
| 94 |
|
{ |
| 95 |
3
|
return id; |
| 96 |
|
} |
| 97 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 98 |
0
|
public PublicKey getPublicKey()... |
| 99 |
|
{ |
| 100 |
0
|
return trustedApp.getPublicKey(); |
| 101 |
|
} |
| 102 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 103 |
5
|
public ApplicationCertificate decode(final EncryptedCertificate certificateStr, final HttpServletRequest request) throws InvalidCertificateException... |
| 104 |
|
{ |
| 105 |
5
|
return trustedApp.decode(certificateStr, request); |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
} |