|
| 1 | +package demo; |
| 2 | + |
| 3 | +import com.sun.net.httpserver.HttpServer; |
| 4 | +import org.junit.Test; |
| 5 | +import org.xml.sax.SAXParseException; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.http.HttpClient; |
| 11 | +import java.net.http.HttpTimeoutException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import static org.junit.Assert.*; |
| 18 | + |
| 19 | +public class GitHubPodcastEpisodeToolTest { |
| 20 | + private static final String RSS = """ |
| 21 | + <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel><item> |
| 22 | + <title>42: GitHub & Copilot</title> |
| 23 | + <itunes:duration>00:30:00</itunes:duration> |
| 24 | + <description><![CDATA[<p>Grounded facts</p>]]></description> |
| 25 | + </item></channel></rss> |
| 26 | + """; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void readsNamespacedDuration() throws Exception { |
| 30 | + var items = GitHubPodcastEpisodeTool.parseItems(RSS.getBytes(StandardCharsets.UTF_8)); |
| 31 | + var episode = GitHubPodcastEpisodeTool.toEpisodeBrief(items.get(0)); |
| 32 | + assertEquals(Integer.valueOf(42), episode.episodeNumber()); |
| 33 | + assertEquals("42: GitHub & Copilot", episode.title()); |
| 34 | + assertEquals("00:30:00", episode.duration()); |
| 35 | + assertEquals("Grounded facts", episode.description()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void rejectsInternalAndExternalDoctypes() { |
| 40 | + for (var declaration : new String[]{ |
| 41 | + "<!DOCTYPE rss [<!ENTITY unsafe 'expanded content'>]>", |
| 42 | + "<!DOCTYPE rss [<!ENTITY unsafe SYSTEM 'file:///must-not-be-read'>]>", |
| 43 | + "<!DOCTYPE rss SYSTEM 'https://example.invalid/must-not-be-requested'>" |
| 44 | + }) { |
| 45 | + var xml = declaration + "<rss><channel><item><title>&unsafe;</title></item></channel></rss>"; |
| 46 | + assertThrows(SAXParseException.class, |
| 47 | + () -> GitHubPodcastEpisodeTool.parseItems(xml.getBytes(StandardCharsets.UTF_8))); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test(timeout = 10000) |
| 52 | + public void boundsBothHeadersAndBodyReads() throws Exception { |
| 53 | + for (var sendHeaders : new boolean[]{false, true}) { |
| 54 | + var release = new CountDownLatch(1); |
| 55 | + var headersSent = new CountDownLatch(1); |
| 56 | + var server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); |
| 57 | + server.createContext("/", exchange -> { |
| 58 | + try { |
| 59 | + if (sendHeaders) { |
| 60 | + exchange.sendResponseHeaders(200, 0); |
| 61 | + exchange.getResponseBody().flush(); |
| 62 | + headersSent.countDown(); |
| 63 | + } |
| 64 | + release.await(2, TimeUnit.SECONDS); |
| 65 | + if (!sendHeaders) { |
| 66 | + exchange.sendResponseHeaders(200, 0); |
| 67 | + } |
| 68 | + exchange.getResponseBody().write(RSS.getBytes(StandardCharsets.UTF_8)); |
| 69 | + } catch (InterruptedException exception) { |
| 70 | + Thread.currentThread().interrupt(); |
| 71 | + throw new IOException(exception); |
| 72 | + } finally { |
| 73 | + exchange.close(); |
| 74 | + } |
| 75 | + }); |
| 76 | + server.start(); |
| 77 | + try { |
| 78 | + var uri = URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/"); |
| 79 | + var start = System.nanoTime(); |
| 80 | + var failure = assertThrows(Exception.class, () -> GitHubPodcastEpisodeTool.readItems( |
| 81 | + HttpClient.newHttpClient(), uri, Duration.ofMillis(300))); |
| 82 | + assertTrue("Expected a request deadline failure", |
| 83 | + failure instanceof IOException && failure.getMessage().contains("timed out") |
| 84 | + || failure.getCause() instanceof HttpTimeoutException); |
| 85 | + assertTrue("The full RSS body must be deadline-bounded", |
| 86 | + System.nanoTime() - start < TimeUnit.MILLISECONDS.toNanos(1500)); |
| 87 | + if (sendHeaders) { |
| 88 | + assertEquals("The body-stall fixture must send headers", 0, headersSent.getCount()); |
| 89 | + } |
| 90 | + } finally { |
| 91 | + release.countDown(); |
| 92 | + server.stop(0); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments