|
| 1 | +//! EQL v3 and v4 installed side by side in one database (ADR-0002). |
| 2 | +//! |
| 3 | +//! The two bundles are the same SQL under two names: `eql_v3` for terms written |
| 4 | +//! by cipherstash-client, `eql_v4` for terms written by Stack Encrypt. These |
| 5 | +//! tests pin what the separate names do and do not enforce when both are |
| 6 | +//! installed: |
| 7 | +//! |
| 8 | +//! - both install without clashing, and each owns its own domains; |
| 9 | +//! - a payload of the wrong version fails the domain CHECK on insert; |
| 10 | +//! - the function form across versions (`eql_v3.eq(v3col, v4term)`) is |
| 11 | +//! ambiguous, so it errors; |
| 12 | +//! - the operator form across versions (`v3col = v4term`) does NOT error. |
| 13 | +//! With no operator defined for the exact pair, Postgres treats both domains |
| 14 | +//! as `jsonb` and uses the built-in `jsonb = jsonb`, which returns no rows. |
| 15 | +//! That is pinned here so it cannot change unnoticed. ADR-0002 defers the |
| 16 | +//! cross-version blocker operators that would make it an error; when they |
| 17 | +//! land, those assertions flip to expecting one. |
| 18 | +//! |
| 19 | +//! The payloads are real cipherstash-client ciphertexts from the |
| 20 | +//! `eql_v3_integer` fixture. A v4 payload is the same row with its envelope |
| 21 | +//! version set to 4: the two bundles share their SQL, so only the version |
| 22 | +//! decides which domain admits it. |
| 23 | +
|
| 24 | +use anyhow::Result; |
| 25 | +use sqlx::PgPool; |
| 26 | + |
| 27 | +const V4_INSTALLER: &str = "../../release/cipherstash-encrypt-v4.sql"; |
| 28 | + |
| 29 | +/// SQLSTATE `check_violation`. |
| 30 | +const CHECK_VIOLATION: &str = "23514"; |
| 31 | +/// SQLSTATE `ambiguous_function`. |
| 32 | +const AMBIGUOUS_FUNCTION: &str = "42725"; |
| 33 | + |
| 34 | +/// Install the v4 bundle into a test database that the migrations have |
| 35 | +/// already given v3. |
| 36 | +async fn install_v4(pool: &PgPool) -> Result<()> { |
| 37 | + let sql = std::fs::read_to_string(V4_INSTALLER).unwrap_or_else(|e| { |
| 38 | + panic!( |
| 39 | + "failed to read {V4_INSTALLER}: {e} — run `mise run build` \ |
| 40 | + (or, in CI, ensure the nextest-archive artifact shipped release/*.sql)" |
| 41 | + ) |
| 42 | + }); |
| 43 | + sqlx::raw_sql(&sql).execute(pool).await?; |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +/// One `integer_eq` column per version, each holding the fixture row for 42, |
| 48 | +/// written as that version's payload. |
| 49 | +async fn tables_with_one_row(pool: &PgPool) -> Result<()> { |
| 50 | + sqlx::raw_sql( |
| 51 | + "CREATE TABLE t3 (c public.eql_v3_integer_eq); |
| 52 | + CREATE TABLE t4 (c public.eql_v4_integer_eq); |
| 53 | + INSERT INTO t3 SELECT payload::public.eql_v3_integer_eq |
| 54 | + FROM fixtures.eql_v3_integer WHERE plaintext = 42; |
| 55 | + INSERT INTO t4 SELECT jsonb_set(payload, '{v}', '4')::public.eql_v4_integer_eq |
| 56 | + FROM fixtures.eql_v3_integer WHERE plaintext = 42;", |
| 57 | + ) |
| 58 | + .execute(pool) |
| 59 | + .await?; |
| 60 | + Ok(()) |
| 61 | +} |
| 62 | + |
| 63 | +/// The term-only query operand for 42 (no ciphertext `c`), as `version`. |
| 64 | +fn query_term(version: u8) -> String { |
| 65 | + format!( |
| 66 | + "(SELECT jsonb_set(payload - 'c', '{{v}}', '{version}') \ |
| 67 | + FROM fixtures.eql_v3_integer WHERE plaintext = 42)::eql_v{version}.query_integer_eq" |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +fn sqlstate(err: &sqlx::Error) -> Option<String> { |
| 72 | + match err { |
| 73 | + sqlx::Error::Database(e) => e.code().map(|c| c.into_owned()), |
| 74 | + _ => None, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))] |
| 79 | +async fn v3_and_v4_install_side_by_side(pool: PgPool) -> Result<()> { |
| 80 | + install_v4(&pool).await?; |
| 81 | + |
| 82 | + let schemas: Vec<String> = sqlx::query_scalar( |
| 83 | + "SELECT nspname::text FROM pg_namespace WHERE nspname LIKE 'eql\\_v_%' ORDER BY 1", |
| 84 | + ) |
| 85 | + .fetch_all(&pool) |
| 86 | + .await?; |
| 87 | + assert_eq!( |
| 88 | + schemas, |
| 89 | + ["eql_v3", "eql_v3_internal", "eql_v4", "eql_v4_internal"] |
| 90 | + ); |
| 91 | + |
| 92 | + let (v3, v4): (i64, i64) = sqlx::query_as( |
| 93 | + "SELECT count(*) FILTER (WHERE typname LIKE 'eql\\_v3\\_%'), |
| 94 | + count(*) FILTER (WHERE typname LIKE 'eql\\_v4\\_%') |
| 95 | + FROM pg_type WHERE typnamespace = 'public'::regnamespace AND typtype = 'd'", |
| 96 | + ) |
| 97 | + .fetch_one(&pool) |
| 98 | + .await?; |
| 99 | + assert!(v3 > 0, "v3 installed no public domains"); |
| 100 | + assert_eq!(v3, v4, "each version owns the same set of public domains"); |
| 101 | + |
| 102 | + let (a, b): (String, String) = sqlx::query_as("SELECT eql_v3.version(), eql_v4.version()") |
| 103 | + .fetch_one(&pool) |
| 104 | + .await?; |
| 105 | + assert_eq!(a, b, "both bundles come from one build"); |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))] |
| 110 | +async fn each_version_matches_its_own_terms(pool: PgPool) -> Result<()> { |
| 111 | + install_v4(&pool).await?; |
| 112 | + tables_with_one_row(&pool).await?; |
| 113 | + |
| 114 | + for (table, version) in [("t3", 3), ("t4", 4)] { |
| 115 | + let n: i64 = sqlx::query_scalar(&format!( |
| 116 | + "SELECT count(*) FROM {table} WHERE c = {}", |
| 117 | + query_term(version) |
| 118 | + )) |
| 119 | + .fetch_one(&pool) |
| 120 | + .await?; |
| 121 | + assert_eq!(n, 1, "{table} = its own version's term must match"); |
| 122 | + } |
| 123 | + Ok(()) |
| 124 | +} |
| 125 | + |
| 126 | +#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))] |
| 127 | +async fn wrong_version_insert_fails_the_domain_check(pool: PgPool) -> Result<()> { |
| 128 | + install_v4(&pool).await?; |
| 129 | + |
| 130 | + for (domain, version) in [ |
| 131 | + ("public.eql_v3_integer_eq", 4), |
| 132 | + ("public.eql_v4_integer_eq", 3), |
| 133 | + ] { |
| 134 | + let err = sqlx::query(&format!( |
| 135 | + "SELECT jsonb_set(payload, '{{v}}', '{version}')::{domain} \ |
| 136 | + FROM fixtures.eql_v3_integer WHERE plaintext = 42" |
| 137 | + )) |
| 138 | + .execute(&pool) |
| 139 | + .await |
| 140 | + .expect_err("a payload of the other version must not fit the domain"); |
| 141 | + assert_eq!( |
| 142 | + sqlstate(&err).as_deref(), |
| 143 | + Some(CHECK_VIOLATION), |
| 144 | + "a version-{version} payload cast to {domain}: {err}" |
| 145 | + ); |
| 146 | + } |
| 147 | + Ok(()) |
| 148 | +} |
| 149 | + |
| 150 | +#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))] |
| 151 | +async fn function_form_across_versions_is_ambiguous(pool: PgPool) -> Result<()> { |
| 152 | + install_v4(&pool).await?; |
| 153 | + tables_with_one_row(&pool).await?; |
| 154 | + |
| 155 | + for (table, function, term) in [("t3", "eql_v3.eq", 4), ("t4", "eql_v4.eq", 3)] { |
| 156 | + let err = sqlx::query(&format!( |
| 157 | + "SELECT count(*) FROM {table} WHERE {function}(c, {})", |
| 158 | + query_term(term) |
| 159 | + )) |
| 160 | + .execute(&pool) |
| 161 | + .await |
| 162 | + .expect_err("a cross-version function call must not resolve"); |
| 163 | + assert_eq!( |
| 164 | + sqlstate(&err).as_deref(), |
| 165 | + Some(AMBIGUOUS_FUNCTION), |
| 166 | + "{function}({table}.c, v{term} term): {err}" |
| 167 | + ); |
| 168 | + } |
| 169 | + Ok(()) |
| 170 | +} |
| 171 | + |
| 172 | +/// Pins a known gap: see the module docs. When cross-version blocker operators |
| 173 | +/// exist, these comparisons raise instead, and this test changes with them. |
| 174 | +#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))] |
| 175 | +async fn operator_across_versions_returns_no_rows(pool: PgPool) -> Result<()> { |
| 176 | + install_v4(&pool).await?; |
| 177 | + tables_with_one_row(&pool).await?; |
| 178 | + |
| 179 | + for (table, term) in [("t3", 4), ("t4", 3)] { |
| 180 | + let n: i64 = sqlx::query_scalar(&format!( |
| 181 | + "SELECT count(*) FROM {table} WHERE c = {}", |
| 182 | + query_term(term) |
| 183 | + )) |
| 184 | + .fetch_one(&pool) |
| 185 | + .await?; |
| 186 | + assert_eq!(n, 0, "{table}.c = v{term} term runs, and finds nothing"); |
| 187 | + |
| 188 | + // Why: no EQL operator takes this pair, so the planner chose the |
| 189 | + // built-in jsonb equality on the two domains' base type. |
| 190 | + let plan: Vec<String> = sqlx::query_scalar(&format!( |
| 191 | + "EXPLAIN (COSTS OFF) SELECT * FROM {table} WHERE c = {}", |
| 192 | + query_term(term) |
| 193 | + )) |
| 194 | + .fetch_all(&pool) |
| 195 | + .await?; |
| 196 | + let plan = plan.join("\n"); |
| 197 | + assert!( |
| 198 | + plan.contains("(c)::jsonb = "), |
| 199 | + "{table}.c = v{term} term should fall back to jsonb = jsonb:\n{plan}" |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + let joined: i64 = sqlx::query_scalar("SELECT count(*) FROM t3 JOIN t4 ON t3.c = t4.c") |
| 204 | + .fetch_one(&pool) |
| 205 | + .await?; |
| 206 | + assert_eq!(joined, 0, "a v3 column joined to a v4 column finds nothing"); |
| 207 | + Ok(()) |
| 208 | +} |
0 commit comments