Skip to content

Commit

Permalink
Tests methods don't overflow
Browse files Browse the repository at this point in the history
Testcase for #1864 and related -- hasText, data, parents could all overflow.
  • Loading branch information
jhy committed Jan 23, 2023
1 parent b5de45f commit 998f429
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/test/java/org/jsoup/nodes/ElementIT.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jsoup.nodes;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class ElementIT {
@Test
Expand Down Expand Up @@ -78,4 +78,45 @@ public void testFastReparentExistingContent() {

assertTrue(runtime <= 10000);
}

// These overflow tests take a couple seconds to run, so are in the slow tests
@Test void hasTextNoOverflow() {
// hasText() was recursive, so could overflow
Document doc = new Document("https://example.com/");
Element el = doc.body();
for (int i = 0; i <= 50000; i++) {
el = el.appendElement("p");
}
assertFalse(doc.hasText());
el.text("Hello");
assertTrue(doc.hasText());
assertEquals(el.text(), doc.text());
}

@Test void dataNoOverflow() {
// data() was recursive, so could overflow
Document doc = new Document("https://example.com/");
Element el = doc.body();
for (int i = 0; i <= 50000; i++) {
el = el.appendElement("p");
}
Element script = el.appendElement("script");
script.text("script"); // holds data nodes, so inserts as data, not text
assertFalse(script.hasText());
assertEquals("script", script.data());
assertEquals(el.data(), doc.data());
}

@Test void parentsNoOverflow() {
// parents() was recursive, so could overflow
Document doc = new Document("https://example.com/");
Element el = doc.body();
int num = 50000;
for (int i = 0; i <= num; i++) {
el = el.appendElement("p");
}
Elements parents = el.parents();
assertEquals(num+2, parents.size()); // +2 for html and body
assertEquals(doc, el.ownerDocument());
}
}

0 comments on commit 998f429

Please sign in to comment.