Skip to content

Commit

Permalink
sqld: add invalid sql dump test
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Nov 26, 2024
1 parent 3c62bd7 commit 8f0f58c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
73 changes: 73 additions & 0 deletions libsql-server/tests/namespaces/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,76 @@ fn load_dump_with_attach_rejected() {

sim.run().unwrap();
}

#[test]
fn load_dump_with_invalid_sql() {
const DUMP: &str = r#"
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE test (x);
INSERT INTO test VALUES(42);
SELECT abs(-9223372036854775808)
COMMIT;"#;

let mut sim = Builder::new()
.simulation_duration(Duration::from_secs(1000))
.build();
let tmp = tempdir().unwrap();
let tmp_path = tmp.path().to_path_buf();

std::fs::write(tmp_path.join("dump.sql"), DUMP).unwrap();

make_primary(&mut sim, tmp.path().to_path_buf());

sim.client("client", async move {
let client = Client::new();

// path is not absolute is an error
let resp = client
.post(
"http://primary:9090/v1/namespaces/foo/create",
json!({ "dump_url": "file:dump.sql"}),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

// path doesn't exist is an error
let resp = client
.post(
"http://primary:9090/v1/namespaces/foo/create",
json!({ "dump_url": "file:/dump.sql"}),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

let resp = client
.post(
"http://primary:9090/v1/namespaces/foo/create",
json!({ "dump_url": format!("file:{}", tmp_path.join("dump.sql").display())}),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::BAD_REQUEST,
"{}",
resp.json::<serde_json::Value>().await.unwrap_or_default()
);

assert_snapshot!(resp.body_string().await?);

let foo =
Database::open_remote_with_connector("http://foo.primary:8080", "", TurmoilConnector)?;
let foo_conn = foo.connect()?;

let res = foo_conn.query("select count(*) from test", ()).await;
// This should error since the dump should have failed!
assert!(res.is_err());

Ok(())
});

sim.run().unwrap();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: libsql-server/tests/namespaces/dumps.rs
expression: resp.body_string().await?
snapshot_kind: text
---
{"error":"The passed dump sql is invalid: msg: near \"COMMIT\": syntax error, sql: SELECT abs(-9223372036854775808) \n COMMIT;, offset: 43"}

0 comments on commit 8f0f58c

Please sign in to comment.