source: josm/trunk/test/unit/org/openstreetmap/josm/io/CertificateAmendmentTest.java@ 11921

Last change on this file since 11921 was 11921, checked in by Don-vip, 7 years ago

improve unit test coverage of utilities classes thanks to https://trajano.github.io/commons-testing

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.IOException;
5import java.net.URL;
6import java.net.URLConnection;
7
8import javax.net.ssl.SSLHandshakeException;
9
10import org.junit.Assert;
11import org.junit.Rule;
12import org.junit.Test;
13import org.openstreetmap.josm.testutils.JOSMTestRules;
14
15import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16import net.trajano.commons.testing.UtilityClassTestUtil;
17
18/**
19 * Unit tests of {@link CertificateAmendment} class.
20 */
21public class CertificateAmendmentTest {
22
23 /**
24 * Setup rule
25 */
26 @Rule
27 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
28 public JOSMTestRules test = new JOSMTestRules();
29
30 /**
31 * Tests that {@code CertificateAmendment} satisfies utility class criterias.
32 * @throws ReflectiveOperationException if an error occurs
33 */
34 @Test
35 public void testUtilityClass() throws ReflectiveOperationException {
36 UtilityClassTestUtil.assertUtilityClassWellDefined(CertificateAmendment.class);
37 }
38
39 /**
40 * Test a well-known certificate.
41 * @throws IOException in case of I/O error
42 */
43 @Test
44 public void testDefault() throws IOException {
45 // something that is neither DST nor StartSSL
46 connect("https://google.com", true);
47 }
48
49 /**
50 * Test <a href="https://letsencrypt.org">Let's Encrypt</a>.
51 * @throws IOException in case of I/O error
52 */
53 @Test
54 public void testLetsEncrypt() throws IOException {
55 // signed by letsencrypt's own ISRG root
56 // (not included yet)
57 // TODO: they switched to cross-sign CA, re-enable it if ISRG root is used again
58 // connect("https://helloworld.letsencrypt.org", false);
59 // signed by letsencrypt's cross-sign CA
60 connect("https://letsencrypt.org", true);
61 // signed by letsencrypt's cross-sign CA, requires SNI
62 connect("https://acme-v01.api.letsencrypt.org", true);
63 }
64
65 /**
66 * Test <a href="https://www.startssl.com">StartSSL</a>.
67 * @throws IOException in case of I/O error
68 */
69 @Test
70 public void testStartSSL() throws IOException {
71 // StartSSL is untrusted
72 connect("https://www.startssl.com", false);
73 }
74
75 /**
76 * Test a broken certificate.
77 * @throws IOException in case of I/O error
78 */
79 @Test
80 public void testBrokenCert() throws IOException {
81 // broken at the moment (may get fixed some day)
82 connect("https://www.pcwebshop.co.uk", false);
83 }
84
85 /**
86 * Test overpass API.
87 * @throws IOException in case of I/O error
88 */
89 @Test
90 public void testOverpass() throws IOException {
91 connect("https://overpass-api.de", true);
92 }
93
94 private static void connect(String url, boolean shouldWork) throws IOException {
95 URLConnection connection = new URL(url).openConnection();
96 try {
97 connection.connect();
98 } catch (SSLHandshakeException e) {
99 if (shouldWork) {
100 e.printStackTrace();
101 Assert.fail("Untrusted: " + url);
102 } else {
103 return;
104 }
105 }
106 if (!shouldWork) {
107 Assert.fail("Expected error: " + url);
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.