From 6e27d5c6943752083fbc753ab12fdf9d740c070f Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 20 May 2025 16:41:22 +1000 Subject: [PATCH 1/2] test: extract json tests into module --- .../src/jsonb/map_params.rs | 56 +++++++++++++++++++ .../cipherstash-proxy-integration/src/lib.rs | 1 + 2 files changed, 57 insertions(+) create mode 100644 packages/cipherstash-proxy-integration/src/jsonb/map_params.rs diff --git a/packages/cipherstash-proxy-integration/src/jsonb/map_params.rs b/packages/cipherstash-proxy-integration/src/jsonb/map_params.rs new file mode 100644 index 0000000..39cbc53 --- /dev/null +++ b/packages/cipherstash-proxy-integration/src/jsonb/map_params.rs @@ -0,0 +1,56 @@ +#[cfg(test)] +mod tests { + use crate::common::{connect_with_tls, id, reset_schema, trace, PROXY}; + + #[tokio::test] + async fn with_number() { + trace(); + + let client = connect_with_tls(PROXY).await; + + let id = id(); + let encrypted_jsonb = serde_json::json!({"key": 42}); + + let sql = "INSERT INTO encrypted (id, encrypted_jsonb) VALUES ($1, $2)"; + client.query(sql, &[&id, &encrypted_jsonb]).await.unwrap(); + + let sql = "SELECT id, encrypted_jsonb FROM encrypted WHERE id = $1"; + let rows = client.query(sql, &[&id]).await.unwrap(); + + assert_eq!(rows.len(), 1); + + for row in rows { + let result_id: i64 = row.get("id"); + let result: serde_json::Value = row.get("encrypted_jsonb"); + + assert_eq!(id, result_id); + assert_eq!(encrypted_jsonb, result); + } + } + + #[tokio::test] + async fn with_array() { + trace(); + + let client = connect_with_tls(PROXY).await; + + let id = id(); + let encrypted_jsonb = serde_json::json!({"a": [1, 2, 4, 42]}); + + let sql = "INSERT INTO encrypted (id, encrypted_jsonb) VALUES ($1, $2)"; + client.query(sql, &[&id, &encrypted_jsonb]).await.unwrap(); + + let sql = "SELECT id, encrypted_jsonb FROM encrypted WHERE id = $1"; + let rows = client.query(sql, &[&id]).await.unwrap(); + + assert_eq!(rows.len(), 1); + + for row in rows { + let result_id: i64 = row.get("id"); + let result: serde_json::Value = row.get("encrypted_jsonb"); + + assert_eq!(id, result_id); + assert_eq!(encrypted_jsonb, result); + } + } +} diff --git a/packages/cipherstash-proxy-integration/src/lib.rs b/packages/cipherstash-proxy-integration/src/lib.rs index dcff807..8fbb09c 100644 --- a/packages/cipherstash-proxy-integration/src/lib.rs +++ b/packages/cipherstash-proxy-integration/src/lib.rs @@ -1,6 +1,7 @@ mod common; mod empty_result; mod extended_protocol_error_messages; +mod jsonb; mod map_concat; mod map_literals; mod map_match_index; From 93c721e227a01f434e89fa25716c4052e9af1df2 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 20 May 2025 16:41:42 +1000 Subject: [PATCH 2/2] WIP --- .../src/jsonb/compare.rs | 67 +++++++++++++++++++ .../src/jsonb/mod.rs | 2 + 2 files changed, 69 insertions(+) create mode 100644 packages/cipherstash-proxy-integration/src/jsonb/compare.rs create mode 100644 packages/cipherstash-proxy-integration/src/jsonb/mod.rs diff --git a/packages/cipherstash-proxy-integration/src/jsonb/compare.rs b/packages/cipherstash-proxy-integration/src/jsonb/compare.rs new file mode 100644 index 0000000..01c688b --- /dev/null +++ b/packages/cipherstash-proxy-integration/src/jsonb/compare.rs @@ -0,0 +1,67 @@ +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use tracing::info; + + use crate::common::{clear, connect_with_tls, id, reset_schema, trace, PROXY}; + + async fn seed_jsonb_data() { + let mut map: HashMap = HashMap::new(); + + for (num, word) in (10..=100).step_by(10).zip([ + "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", + "Juliet", + ]) { + map.insert(num, word); + } + + let client = connect_with_tls(PROXY).await; + for i in (10..=100).step_by(10) { + // let key = format!() + + let id: i64 = i; + + let s = map.get(&i).unwrap(); + + let encrypted_jsonb = serde_json::json!({ + "string": s, + "number": i, + "nested": { + "number": i, + "string": s, + } + }); + + // info!("{encrypted_jsonb:?}"); + + let sql = "INSERT INTO encrypted (id, encrypted_jsonb) VALUES ($1, $2)"; + client.query(sql, &[&id, &encrypted_jsonb]).await.unwrap(); + } + } + + #[tokio::test] + async fn lt_with_jsonb_path_query() { + trace(); + + clear().await; + + seed_jsonb_data().await; + + let client = connect_with_tls(PROXY).await; + + let sql = "SELECT encrypted_jsonb FROM encrypted WHERE encrypted_jsonb->'number' < $1"; + // let sql = "SELECT encrypted_jsonb FROM encrypted WHERE eql_v1.jsonb_path_query_first(encrypted_jsonb, '$.number') < $1"; + + // let rows = client.query(sql, &[&n]).await.unwrap(); + let rows = client.query(sql, &[]).await.unwrap(); + + // assert_eq!(rows.len(), 1); + + for row in rows { + let result: String = row.get("encrypted_jsonb"); + info!("{result}"); + // assert_eq!(encrypted_text, result); + } + } +} diff --git a/packages/cipherstash-proxy-integration/src/jsonb/mod.rs b/packages/cipherstash-proxy-integration/src/jsonb/mod.rs new file mode 100644 index 0000000..97c340b --- /dev/null +++ b/packages/cipherstash-proxy-integration/src/jsonb/mod.rs @@ -0,0 +1,2 @@ +mod compare; +mod map_params;