From 039882e2216ea2b3279b1b7aebd61635e494b6c2 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sun, 24 May 2020 22:28:19 +0200 Subject: [PATCH] Add test demonstrating crash when evaluating expression in inspector Ref: #5807 --- cli/tests/inspector5.js | 1 + cli/tests/integration_tests.rs | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 cli/tests/inspector5.js diff --git a/cli/tests/inspector5.js b/cli/tests/inspector5.js new file mode 100644 index 00000000000000..5c5b84bc6147d5 --- /dev/null +++ b/cli/tests/inspector5.js @@ -0,0 +1 @@ +setTimeout(() => {}, 60_000); diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index b3f792b4e9bf3b..d60fca9484dfb6 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2516,6 +2516,67 @@ async fn inspector_without_brk_runs_code() { assert_eq!(stdout_first_line, "hello\n"); child.kill().unwrap(); + child.wait().unwrap(); +} + +#[tokio::test] +async fn inspector_runtime_evaluate_does_not_crash() { + let script = util::tests_path().join("inspector5.js"); + let mut child = util::deno_cmd() + .arg("run") + // Warning: each inspector test should be on its own port to avoid + // conflicting with another inspector test. + .arg("--inspect=127.0.0.1:9234") + .arg(script) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let stderr = child.stderr.as_mut().unwrap(); + let ws_url = extract_ws_url_from_stderr(stderr); + let (socket, response) = tokio_tungstenite::connect_async(ws_url) + .await + .expect("Can't connect"); + assert_eq!(response.status(), 101); // Switching protocols. + + let (mut socket_tx, socket_rx) = socket.split(); + let mut socket_rx = + socket_rx.map(|msg| msg.unwrap().to_string()).filter(|msg| { + let pass = !msg.starts_with(r#"{"method":"Debugger.scriptParsed","#); + futures::future::ready(pass) + }); + + let stdout = child.stdout.as_mut().unwrap(); + let mut stdout_lines = + std::io::BufReader::new(stdout).lines().map(|r| r.unwrap()); + + use TestStep::*; + let test_steps = vec![ + WsSend(r#"{"id":1,"method":"Runtime.enable"}"#), + WsSend(r#"{"id":2,"method":"Debugger.enable"}"#), + WsRecv( + r#"{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"#, + ), + WsRecv(r#"{"id":1,"result":{}}"#), + WsRecv(r#"{"id":2,"result":{"debuggerId":"#), + WsSend(r#"{"id":3,"method":"Runtime.runIfWaitingForDebugger"}"#), + WsRecv(r#"{"id":3,"result":{}}"#), + WsSend( + r#"{"id":4,"method":"Runtime.evaluate","params":{"expression":"Deno.cwd()","objectGroup":"console","includeCommandLineAPI":true,"silent":false,"contextId":1,"returnByValue":false,"generatePreview":true,"userGesture":true,"awaitPromise":false,"replMode":true}}"#, + ), + ]; + + for step in test_steps { + match step { + StdOut(s) => assert_eq!(&stdout_lines.next().unwrap(), s), + WsRecv(s) => assert!(socket_rx.next().await.unwrap().starts_with(s)), + WsSend(s) => socket_tx.send(s.into()).await.unwrap(), + } + } + + child.kill().unwrap(); + child.wait().unwrap(); } #[test]