|
| 1 | +-- Test corrupted payload handling |
| 2 | +-- Verifies that cloudsync_payload_apply rejects corrupted payloads |
| 3 | +-- without crashing or corrupting state. |
| 4 | + |
| 5 | +\set testid '41' |
| 6 | +\ir helper_test_init.sql |
| 7 | + |
| 8 | +\connect postgres |
| 9 | +\ir helper_psql_conn_setup.sql |
| 10 | +DROP DATABASE IF EXISTS cloudsync_test_41_src; |
| 11 | +DROP DATABASE IF EXISTS cloudsync_test_41_dst; |
| 12 | +CREATE DATABASE cloudsync_test_41_src; |
| 13 | +CREATE DATABASE cloudsync_test_41_dst; |
| 14 | + |
| 15 | +-- Setup source database with data |
| 16 | +\connect cloudsync_test_41_src |
| 17 | +\ir helper_psql_conn_setup.sql |
| 18 | +CREATE EXTENSION IF NOT EXISTS cloudsync; |
| 19 | +CREATE TABLE test_tbl (id TEXT PRIMARY KEY, val TEXT); |
| 20 | +SELECT cloudsync_init('test_tbl', 'CLS', true) AS _init_src \gset |
| 21 | +INSERT INTO test_tbl VALUES ('id1', 'value1'); |
| 22 | +INSERT INTO test_tbl VALUES ('id2', 'value2'); |
| 23 | + |
| 24 | +-- Get a valid payload |
| 25 | +SELECT encode(cloudsync_payload_encode(tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq), 'hex') AS valid_payload_hex |
| 26 | +FROM cloudsync_changes |
| 27 | +WHERE site_id = cloudsync_siteid() \gset |
| 28 | + |
| 29 | +-- Setup destination database |
| 30 | +\connect cloudsync_test_41_dst |
| 31 | +\ir helper_psql_conn_setup.sql |
| 32 | +CREATE EXTENSION IF NOT EXISTS cloudsync; |
| 33 | +CREATE TABLE test_tbl (id TEXT PRIMARY KEY, val TEXT); |
| 34 | +SELECT cloudsync_init('test_tbl', 'CLS', true) AS _init_dst \gset |
| 35 | + |
| 36 | +-- Record initial state |
| 37 | +SELECT COUNT(*) AS initial_count FROM test_tbl \gset |
| 38 | + |
| 39 | +-- Test 1: Empty blob (zero bytes) |
| 40 | +DO $$ |
| 41 | +BEGIN |
| 42 | + PERFORM cloudsync_payload_apply(''::bytea); |
| 43 | + -- If it returns without error with 0 rows, that's also acceptable |
| 44 | +EXCEPTION WHEN OTHERS THEN |
| 45 | + -- Expected: error on empty payload |
| 46 | + NULL; |
| 47 | +END $$; |
| 48 | + |
| 49 | +SELECT COUNT(*) AS count_after_empty FROM test_tbl \gset |
| 50 | +SELECT (:count_after_empty::int = :initial_count::int) AS empty_blob_ok \gset |
| 51 | +\if :empty_blob_ok |
| 52 | +\echo [PASS] (:testid) Empty blob rejected - table unchanged |
| 53 | +\else |
| 54 | +\echo [FAIL] (:testid) Empty blob corrupted table state |
| 55 | +SELECT (:fail::int + 1) AS fail \gset |
| 56 | +\endif |
| 57 | + |
| 58 | +-- Test 2: Random garbage bytes |
| 59 | +DO $$ |
| 60 | +BEGIN |
| 61 | + PERFORM cloudsync_payload_apply(decode('deadbeefcafebabe0102030405060708', 'hex')); |
| 62 | +EXCEPTION WHEN OTHERS THEN |
| 63 | + -- Expected: error on garbage payload |
| 64 | + NULL; |
| 65 | +END $$; |
| 66 | + |
| 67 | +SELECT COUNT(*) AS count_after_garbage FROM test_tbl \gset |
| 68 | +SELECT (:count_after_garbage::int = :initial_count::int) AS garbage_ok \gset |
| 69 | +\if :garbage_ok |
| 70 | +\echo [PASS] (:testid) Garbage bytes rejected - table unchanged |
| 71 | +\else |
| 72 | +\echo [FAIL] (:testid) Garbage bytes corrupted table state |
| 73 | +SELECT (:fail::int + 1) AS fail \gset |
| 74 | +\endif |
| 75 | + |
| 76 | +-- Test 3: Truncated payload (first 10 bytes of valid payload) |
| 77 | +-- Build truncated hex at top level using psql variable interpolation |
| 78 | +SELECT substr(:'valid_payload_hex', 1, 20) AS truncated_hex \gset |
| 79 | +SELECT cloudsync_payload_apply(decode(:'truncated_hex', 'hex')) AS _apply_truncated \gset |
| 80 | +-- If the above errors, psql continues (ON_ERROR_STOP is off) |
| 81 | + |
| 82 | +SELECT COUNT(*) AS count_after_truncated FROM test_tbl \gset |
| 83 | +SELECT (:count_after_truncated::int = :initial_count::int) AS truncated_ok \gset |
| 84 | +\if :truncated_ok |
| 85 | +\echo [PASS] (:testid) Truncated payload rejected - table unchanged |
| 86 | +\else |
| 87 | +\echo [FAIL] (:testid) Truncated payload corrupted table state |
| 88 | +SELECT (:fail::int + 1) AS fail \gset |
| 89 | +\endif |
| 90 | + |
| 91 | +-- Test 4: Valid payload with flipped byte in the middle |
| 92 | +-- Compute corrupted payload at top level: flip one byte via XOR with FF |
| 93 | +SELECT |
| 94 | + substr(:'valid_payload_hex', 1, length(:'valid_payload_hex') / 2 - 1) |
| 95 | + || lpad(to_hex(get_byte(decode(substr(:'valid_payload_hex', length(:'valid_payload_hex') / 2, 2), 'hex'), 0) # 255), 2, '0') |
| 96 | + || substr(:'valid_payload_hex', length(:'valid_payload_hex') / 2 + 2) |
| 97 | + AS corrupted_hex \gset |
| 98 | +SELECT cloudsync_payload_apply(decode(:'corrupted_hex', 'hex')) AS _apply_corrupted \gset |
| 99 | +-- If the above errors, psql continues (ON_ERROR_STOP is off) |
| 100 | + |
| 101 | +SELECT COUNT(*) AS count_after_flipped FROM test_tbl \gset |
| 102 | +SELECT (:count_after_flipped::int = :initial_count::int) AS flipped_ok \gset |
| 103 | +\if :flipped_ok |
| 104 | +\echo [PASS] (:testid) Flipped-byte payload rejected - table unchanged |
| 105 | +\else |
| 106 | +\echo [FAIL] (:testid) Flipped-byte payload corrupted table state |
| 107 | +SELECT (:fail::int + 1) AS fail \gset |
| 108 | +\endif |
| 109 | + |
| 110 | +-- Test 5: Now apply the VALID payload to confirm it still works |
| 111 | +SELECT cloudsync_payload_apply(decode(:'valid_payload_hex', 'hex')) AS valid_apply \gset |
| 112 | +SELECT COUNT(*) AS count_after_valid FROM test_tbl \gset |
| 113 | +SELECT (:count_after_valid::int = 2) AS valid_ok \gset |
| 114 | +\if :valid_ok |
| 115 | +\echo [PASS] (:testid) Valid payload applied successfully after corrupted attempts |
| 116 | +\else |
| 117 | +\echo [FAIL] (:testid) Valid payload failed after corrupted attempts - count: :count_after_valid |
| 118 | +SELECT (:fail::int + 1) AS fail \gset |
| 119 | +\endif |
| 120 | + |
| 121 | +-- Cleanup |
| 122 | +\ir helper_test_cleanup.sql |
| 123 | +\if :should_cleanup |
| 124 | +DROP DATABASE IF EXISTS cloudsync_test_41_src; |
| 125 | +DROP DATABASE IF EXISTS cloudsync_test_41_dst; |
| 126 | +\endif |
0 commit comments