|
| 1 | +-- Test EQL equality operators |
| 2 | +-- Tests the = operator and eq() function for encrypted data |
| 3 | + |
| 4 | +BEGIN; |
| 5 | + |
| 6 | +-- Plan: count of tests to run |
| 7 | +SELECT plan(13); |
| 8 | + |
| 9 | +-- Setup test data |
| 10 | +SELECT lives_ok( |
| 11 | + 'SELECT create_table_with_encrypted()', |
| 12 | + 'Should create table with encrypted column' |
| 13 | +); |
| 14 | + |
| 15 | +SELECT lives_ok( |
| 16 | + 'SELECT seed_encrypted_json()', |
| 17 | + 'Should seed encrypted data' |
| 18 | +); |
| 19 | + |
| 20 | +-- Test 1: eql_v2_encrypted = eql_v2_encrypted with unique index term (HMAC) |
| 21 | +DO $$ |
| 22 | +DECLARE |
| 23 | + e eql_v2_encrypted; |
| 24 | +BEGIN |
| 25 | + e := create_encrypted_json(1, 'hm'); |
| 26 | + |
| 27 | + PERFORM results_eq( |
| 28 | + format('SELECT e FROM encrypted WHERE e = %L', e), |
| 29 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 30 | + 'eql_v2_encrypted = eql_v2_encrypted finds matching record with HMAC index' |
| 31 | + ); |
| 32 | +END; |
| 33 | +$$ LANGUAGE plpgsql; |
| 34 | + |
| 35 | +-- Test 2: eql_v2_encrypted = eql_v2_encrypted with no match |
| 36 | +DO $$ |
| 37 | +DECLARE |
| 38 | + e eql_v2_encrypted; |
| 39 | +BEGIN |
| 40 | + e := create_encrypted_json(91347, 'hm'); |
| 41 | + |
| 42 | + PERFORM is_empty( |
| 43 | + format('SELECT e FROM encrypted WHERE e = %L', e), |
| 44 | + 'eql_v2_encrypted = eql_v2_encrypted returns no result for non-matching record' |
| 45 | + ); |
| 46 | +END; |
| 47 | +$$ LANGUAGE plpgsql; |
| 48 | + |
| 49 | +-- Test 3: eql_v2.eq() function test |
| 50 | +DO $$ |
| 51 | +DECLARE |
| 52 | + e eql_v2_encrypted; |
| 53 | +BEGIN |
| 54 | + e := create_encrypted_json(1)::jsonb-'ob'; |
| 55 | + |
| 56 | + PERFORM results_eq( |
| 57 | + format('SELECT e FROM encrypted WHERE eql_v2.eq(e, %L)', e), |
| 58 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 59 | + 'eql_v2.eq() finds matching record' |
| 60 | + ); |
| 61 | +END; |
| 62 | +$$ LANGUAGE plpgsql; |
| 63 | + |
| 64 | +-- Test 4: eql_v2.eq() with no match |
| 65 | +DO $$ |
| 66 | +DECLARE |
| 67 | + e eql_v2_encrypted; |
| 68 | +BEGIN |
| 69 | + e := create_encrypted_json(91347)::jsonb-'ob'; |
| 70 | + |
| 71 | + PERFORM is_empty( |
| 72 | + format('SELECT e FROM encrypted WHERE eql_v2.eq(e, %L)', e), |
| 73 | + 'eql_v2.eq() returns no result for non-matching record' |
| 74 | + ); |
| 75 | +END; |
| 76 | +$$ LANGUAGE plpgsql; |
| 77 | + |
| 78 | +-- Test 5: eql_v2_encrypted = jsonb |
| 79 | +DO $$ |
| 80 | +DECLARE |
| 81 | + e jsonb; |
| 82 | +BEGIN |
| 83 | + e := create_encrypted_json(1)::jsonb-'ob'; |
| 84 | + |
| 85 | + PERFORM results_eq( |
| 86 | + format('SELECT e FROM encrypted WHERE e = %L::jsonb', e), |
| 87 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 88 | + 'eql_v2_encrypted = jsonb finds matching record' |
| 89 | + ); |
| 90 | +END; |
| 91 | +$$ LANGUAGE plpgsql; |
| 92 | + |
| 93 | +-- Test 6: jsonb = eql_v2_encrypted |
| 94 | +DO $$ |
| 95 | +DECLARE |
| 96 | + e jsonb; |
| 97 | +BEGIN |
| 98 | + e := create_encrypted_json(1)::jsonb-'ob'; |
| 99 | + |
| 100 | + PERFORM results_eq( |
| 101 | + format('SELECT e FROM encrypted WHERE %L::jsonb = e', e), |
| 102 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 103 | + 'jsonb = eql_v2_encrypted finds matching record' |
| 104 | + ); |
| 105 | +END; |
| 106 | +$$ LANGUAGE plpgsql; |
| 107 | + |
| 108 | +-- Test 7: Blake3 equality - eql_v2_encrypted = eql_v2_encrypted |
| 109 | +DO $$ |
| 110 | +DECLARE |
| 111 | + e eql_v2_encrypted; |
| 112 | +BEGIN |
| 113 | + e := create_encrypted_json(1, 'b3'); |
| 114 | + |
| 115 | + PERFORM results_eq( |
| 116 | + format('SELECT e FROM encrypted WHERE e = %L', e), |
| 117 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 118 | + 'Blake3: eql_v2_encrypted = eql_v2_encrypted finds matching record' |
| 119 | + ); |
| 120 | +END; |
| 121 | +$$ LANGUAGE plpgsql; |
| 122 | + |
| 123 | +-- Test 8: Blake3 equality with no match |
| 124 | +DO $$ |
| 125 | +DECLARE |
| 126 | + e eql_v2_encrypted; |
| 127 | +BEGIN |
| 128 | + e := create_encrypted_json(91347, 'b3'); |
| 129 | + |
| 130 | + PERFORM is_empty( |
| 131 | + format('SELECT e FROM encrypted WHERE e = %L', e), |
| 132 | + 'Blake3: eql_v2_encrypted = eql_v2_encrypted returns no result for non-matching record' |
| 133 | + ); |
| 134 | +END; |
| 135 | +$$ LANGUAGE plpgsql; |
| 136 | + |
| 137 | +-- Test 9: Blake3 eql_v2.eq() function |
| 138 | +DO $$ |
| 139 | +DECLARE |
| 140 | + e eql_v2_encrypted; |
| 141 | +BEGIN |
| 142 | + e := create_encrypted_json(1, 'b3'); |
| 143 | + |
| 144 | + PERFORM results_eq( |
| 145 | + format('SELECT e FROM encrypted WHERE eql_v2.eq(e, %L)', e), |
| 146 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 147 | + 'Blake3: eql_v2.eq() finds matching record' |
| 148 | + ); |
| 149 | +END; |
| 150 | +$$ LANGUAGE plpgsql; |
| 151 | + |
| 152 | +-- Test 10: Blake3 eql_v2_encrypted = jsonb |
| 153 | +DO $$ |
| 154 | +DECLARE |
| 155 | + e jsonb; |
| 156 | +BEGIN |
| 157 | + e := create_encrypted_json(1, 'b3'); |
| 158 | + |
| 159 | + PERFORM results_eq( |
| 160 | + format('SELECT e FROM encrypted WHERE e = %L::jsonb', e), |
| 161 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 162 | + 'Blake3: eql_v2_encrypted = jsonb finds matching record' |
| 163 | + ); |
| 164 | +END; |
| 165 | +$$ LANGUAGE plpgsql; |
| 166 | + |
| 167 | +-- Test 11: Blake3 jsonb = eql_v2_encrypted |
| 168 | +DO $$ |
| 169 | +DECLARE |
| 170 | + e jsonb; |
| 171 | +BEGIN |
| 172 | + e := create_encrypted_json(1, 'b3'); |
| 173 | + |
| 174 | + PERFORM results_eq( |
| 175 | + format('SELECT e FROM encrypted WHERE %L::jsonb = e', e), |
| 176 | + format('SELECT e FROM encrypted WHERE id = 1'), |
| 177 | + 'Blake3: jsonb = eql_v2_encrypted finds matching record' |
| 178 | + ); |
| 179 | +END; |
| 180 | +$$ LANGUAGE plpgsql; |
| 181 | + |
| 182 | +-- Cleanup |
| 183 | +SELECT lives_ok( |
| 184 | + 'SELECT drop_table_with_encrypted()', |
| 185 | + 'Should drop test table' |
| 186 | +); |
| 187 | + |
| 188 | +SELECT finish(); |
| 189 | +ROLLBACK; |
0 commit comments