|
| 1 | +namespace AngleSharp.Js.Tests |
| 2 | +{ |
| 3 | + using AngleSharp.Dom; |
| 4 | + using Jint.Runtime; |
| 5 | + using NUnit.Framework; |
| 6 | + using System; |
| 7 | + using System.Threading.Tasks; |
| 8 | + |
| 9 | + public class StackGuardTests |
| 10 | + { |
| 11 | + // A recursion that never ends. Jint implements no tail calls, so this really does |
| 12 | + // grow the call stack. Without a stack guard it exhausts the native one, and the |
| 13 | + // resulting StackOverflowException takes the whole process down - which is why the |
| 14 | + // tests below cannot assert anything weaker than "we got here at all". |
| 15 | + private const String RunawayRecursion = "function boom() { return boom(); } boom();"; |
| 16 | + |
| 17 | + // Deeper than a 1 MB stack holds, so the engine has to keep going on a fresh one |
| 18 | + // instead of reporting the depth as an error. |
| 19 | + private const String DeepRecursion = "function depth(n) { return n === 0 ? 0 : 1 + depth(n - 1); } depth(1500);"; |
| 20 | + |
| 21 | + [Test] |
| 22 | + public async Task RunawayRecursionInPageScriptDoesNotEscapeOpenAsync() |
| 23 | + { |
| 24 | + var config = Configuration.Default |
| 25 | + .WithJs() |
| 26 | + .WithEventLoop(); |
| 27 | + |
| 28 | + var content = $"<!doctype html><script>{RunawayRecursion}</script>"; |
| 29 | + var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(content)); |
| 30 | + |
| 31 | + Assert.IsNotNull(document); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public async Task RunawayRecursionReportsMaximumCallStackSizeExceeded() |
| 36 | + { |
| 37 | + // A small limit keeps the failure quick and independent of how much native |
| 38 | + // stack the test runner happens to have left. |
| 39 | + var config = Configuration.Default |
| 40 | + .WithJs(new JsScriptingOptions { MaxCallStackDepth = 200 }) |
| 41 | + .WithEventLoop(); |
| 42 | + |
| 43 | + var document = await BrowsingContext.New(config).OpenNewAsync(); |
| 44 | + var error = Assert.Throws<JavaScriptException>(() => document.ExecuteScript(RunawayRecursion)); |
| 45 | + |
| 46 | + Assert.AreEqual("Maximum call stack size exceeded", error.Message); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public async Task DeepButFiniteRecursionStillSucceeds() |
| 51 | + { |
| 52 | + var config = Configuration.Default |
| 53 | + .WithJs() |
| 54 | + .WithEventLoop(); |
| 55 | + |
| 56 | + var document = await BrowsingContext.New(config).OpenNewAsync(); |
| 57 | + var result = document.ExecuteScript(DeepRecursion); |
| 58 | + |
| 59 | + Assert.AreEqual(1500.0, result); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public async Task EditingTheOptionsAfterwardsLeavesTheServiceAlone() |
| 64 | + { |
| 65 | + var options = new JsScriptingOptions(); |
| 66 | + var config = Configuration.Default |
| 67 | + .WithJs(options) |
| 68 | + .WithEventLoop(); |
| 69 | + |
| 70 | + // The engine is only built once a document asks for it, so an edit landing |
| 71 | + // in between must not be the one deciding how that document behaves. |
| 72 | + options.MaxCallStackDepth = 200; |
| 73 | + |
| 74 | + var document = await BrowsingContext.New(config).OpenNewAsync(); |
| 75 | + var result = document.ExecuteScript(DeepRecursion); |
| 76 | + |
| 77 | + Assert.AreEqual(1500.0, result); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments