| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.io;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.IOException;
|
|---|
| 5 | import java.net.URL;
|
|---|
| 6 | import java.net.URLConnection;
|
|---|
| 7 |
|
|---|
| 8 | import javax.net.ssl.SSLHandshakeException;
|
|---|
| 9 |
|
|---|
| 10 | import org.junit.Assert;
|
|---|
| 11 | import org.junit.BeforeClass;
|
|---|
| 12 | import org.junit.Test;
|
|---|
| 13 | import org.openstreetmap.josm.JOSMFixture;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Unit tests of {@link CertificateAmendment} class.
|
|---|
| 17 | */
|
|---|
| 18 | public class CertificateAmendmentTest {
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Setup test.
|
|---|
| 22 | * @throws IOException in case of I/O error
|
|---|
| 23 | */
|
|---|
| 24 | @BeforeClass
|
|---|
| 25 | public static void setUp() throws IOException {
|
|---|
| 26 | JOSMFixture.createUnitTestFixture().init();
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Test a well-known certificate.
|
|---|
| 31 | * @throws IOException in case of I/O error
|
|---|
| 32 | */
|
|---|
| 33 | @Test
|
|---|
| 34 | public void testDefault() throws IOException {
|
|---|
| 35 | // something that is neither DST nor StartSSL
|
|---|
| 36 | connect("https://google.com", true);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Test <a href="https://letsencrypt.org">Let's Encrypt</a>.
|
|---|
| 41 | * @throws IOException in case of I/O error
|
|---|
| 42 | */
|
|---|
| 43 | @Test
|
|---|
| 44 | public void testLetsEncrypt() throws IOException {
|
|---|
| 45 | // signed by letsencrypt
|
|---|
| 46 | connect("https://helloworld.letsencrypt.org", true);
|
|---|
| 47 | // signed by LE's cross-sign CA
|
|---|
| 48 | connect("https://letsencrypt.org", true);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Test <a href="https://www.startssl.com">StartSSL</a>.
|
|---|
| 53 | * @throws IOException in case of I/O error
|
|---|
| 54 | */
|
|---|
| 55 | @Test
|
|---|
| 56 | public void testStartSSL() throws IOException {
|
|---|
| 57 | connect("https://map.dgpsonline.eu", true);
|
|---|
| 58 | connect("https://www.startssl.com", true);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Test a broken certificate.
|
|---|
| 63 | * @throws IOException in case of I/O error
|
|---|
| 64 | */
|
|---|
| 65 | @Test
|
|---|
| 66 | public void testBrokenCert() throws IOException {
|
|---|
| 67 | // broken at the moment (may get fixed some day)
|
|---|
| 68 | connect("https://www.pcwebshop.co.uk", false);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Test overpass API.
|
|---|
| 73 | * @throws IOException in case of I/O error
|
|---|
| 74 | */
|
|---|
| 75 | @Test
|
|---|
| 76 | public void testOverpass() throws IOException {
|
|---|
| 77 | connect("https://overpass-api.de", true);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | private static void connect(String url, boolean shouldWork) throws IOException {
|
|---|
| 81 | URLConnection connection = new URL(url).openConnection();
|
|---|
| 82 | try {
|
|---|
| 83 | connection.connect();
|
|---|
| 84 | } catch (SSLHandshakeException e) {
|
|---|
| 85 | if (shouldWork) {
|
|---|
| 86 | e.printStackTrace();
|
|---|
| 87 | Assert.fail("Untrusted: " + url);
|
|---|
| 88 | } else {
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | if (!shouldWork) {
|
|---|
| 93 | Assert.fail("Expected error: " + url);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|