|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/encrypt-query-language/go/goeql" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "xorm.io/xorm" |
| 11 | +) |
| 12 | + |
| 13 | +func proxyEngine() *xorm.Engine { |
| 14 | + |
| 15 | + proxyConnStr := "user=postgres password=postgres port=6432 host=localhost dbname=gotest sslmode=disable" |
| 16 | + proxyEngine, err := xorm.NewEngine("pgx", proxyConnStr) |
| 17 | + |
| 18 | + if err != nil { |
| 19 | + log.Fatalf("Could not connect to the database: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + return proxyEngine |
| 23 | +} |
| 24 | + |
| 25 | +func truncateDb(engine *xorm.Engine) error { |
| 26 | + query := "TRUNCATE TABLE examples" |
| 27 | + _, err := engine.Exec(query) |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("failed to truncate table: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func TestWhereQueryOnUnencryptedColumn(t *testing.T) { |
| 36 | + engine := proxyEngine() |
| 37 | + truncateDb(engine) |
| 38 | + |
| 39 | + jsonData := map[string]any{ |
| 40 | + "top": map[string]any{ |
| 41 | + "integer": float64(101), |
| 42 | + "float": 1.234, |
| 43 | + }, |
| 44 | + "bottom": "value_three", |
| 45 | + } |
| 46 | + |
| 47 | + newExample := Example{ NonEncryptedField: "sydney", EncryptedIntField: 23, EncryptedTextField: "[email protected]", EncryptedJsonbField: jsonData} |
| 48 | + |
| 49 | + _, err := engine.Insert(&newExample) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("Could not insert new example: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + var example Example |
| 55 | + text := "sydney" |
| 56 | + |
| 57 | + has, err := engine.Where("non_encrypted_field = ?", text).Get(&example) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Could not retrieve example: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + if !has { |
| 63 | + t.Errorf("Expected has to equal true, got: %v", has) |
| 64 | + } |
| 65 | + |
| 66 | + assert.Equal(t, newExample.NonEncryptedField, example.NonEncryptedField, "NonEncryptedField should match") |
| 67 | + assert.Equal(t, newExample.EncryptedIntField, example.EncryptedIntField, "EncryptedIntField should match") |
| 68 | + assert.Equal(t, newExample.EncryptedTextField, example.EncryptedTextField, "EncryptedTextField should match") |
| 69 | + assert.Equal(t, newExample.EncryptedJsonbField, example.EncryptedJsonbField, "EncryptedJsonbField should match") |
| 70 | +} |
| 71 | + |
| 72 | +func TestMatchQueryLongString(t *testing.T) { |
| 73 | + engine := proxyEngine() |
| 74 | + truncateDb(engine) |
| 75 | + |
| 76 | + jsonData := map[string]any{ |
| 77 | + "top": map[string]any{ |
| 78 | + "integer": float64(101), |
| 79 | + "float": 1.234, |
| 80 | + }, |
| 81 | + "bottom": "value_three", |
| 82 | + } |
| 83 | + |
| 84 | + examples := []Example{ |
| 85 | + { |
| 86 | + NonEncryptedField: "sydney", |
| 87 | + EncryptedIntField: 23, |
| 88 | + EncryptedTextField: "this is a long string", |
| 89 | + EncryptedJsonbField: jsonData, |
| 90 | + }, |
| 91 | + { |
| 92 | + NonEncryptedField: "melbourne", |
| 93 | + EncryptedIntField: 42, |
| 94 | + EncryptedTextField: "quick brown fox jumped", |
| 95 | + EncryptedJsonbField: jsonData, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + inserted, err := engine.Insert(&examples) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + t.Errorf("Error inserting examples: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows") |
| 106 | + |
| 107 | + query, err := goeql.SerializeQuery("this", "examples", "encrypted_text_field") |
| 108 | + if err != nil { |
| 109 | + log.Fatalf("Error marshaling encrypted_text_field query: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + var returnedExample Example |
| 113 | + has, err := engine.Where("cs_match_v1(encrypted_text_field) @> cs_match_v1(?)", query).Get(&returnedExample) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("Could not retrieve example: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + if !has { |
| 119 | + t.Errorf("Expected has to equal true, got: %v", has) |
| 120 | + } |
| 121 | + |
| 122 | + assert.Equal(t, returnedExample.EncryptedTextField, EncryptedTextField("this is a long string"), "EncryptedTextField should match") |
| 123 | +} |
| 124 | + |
| 125 | +func TestMatchQueryEmail(t *testing.T) { |
| 126 | + engine := proxyEngine() |
| 127 | + truncateDb(engine) |
| 128 | + |
| 129 | + jsonData := map[string]any{ |
| 130 | + "top": map[string]any{ |
| 131 | + "integer": float64(101), |
| 132 | + "float": 1.234, |
| 133 | + }, |
| 134 | + "bottom": "value_three", |
| 135 | + } |
| 136 | + |
| 137 | + examples := []Example{ |
| 138 | + { |
| 139 | + NonEncryptedField: "sydney", |
| 140 | + EncryptedIntField: 23, |
| 141 | + EncryptedTextField: "[email protected]", |
| 142 | + EncryptedJsonbField: jsonData, |
| 143 | + }, |
| 144 | + { |
| 145 | + NonEncryptedField: "melbourne", |
| 146 | + EncryptedIntField: 42, |
| 147 | + EncryptedTextField: "[email protected]", |
| 148 | + EncryptedJsonbField: jsonData, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + inserted, err := engine.Insert(&examples) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + t.Errorf("Error inserting examples: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows") |
| 159 | + |
| 160 | + query, err := goeql.SerializeQuery("test", "examples", "encrypted_text_field") |
| 161 | + if err != nil { |
| 162 | + log.Fatalf("Error marshaling encrypted_text_field query: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + var returnedExample Example |
| 166 | + has, err := engine.Where("cs_match_v1(encrypted_text_field) @> cs_match_v1(?)", query).Get(&returnedExample) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("Could not retrieve example: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + if !has { |
| 172 | + t.Errorf("Expected has to equal true, got: %v", has) |
| 173 | + } |
| 174 | + |
| 175 | + assert. Equal( t, returnedExample. EncryptedTextField, EncryptedTextField( "[email protected]"), "EncryptedTextField should match") |
| 176 | +} |
| 177 | + |
| 178 | +func TestJsonbQuerySimple(t *testing.T) { |
| 179 | + engine := proxyEngine() |
| 180 | + truncateDb(engine) |
| 181 | + |
| 182 | + expectedJson := map[string]any{ |
| 183 | + "top": map[string]any{ |
| 184 | + "integer": float64(101), |
| 185 | + "float": 1.234, |
| 186 | + "string": "some string", |
| 187 | + }, |
| 188 | + "bottom": "value_three", |
| 189 | + } |
| 190 | + |
| 191 | + examples := []Example{ |
| 192 | + { |
| 193 | + NonEncryptedField: "sydney", |
| 194 | + EncryptedTextField: "testing", |
| 195 | + EncryptedIntField: 42, |
| 196 | + EncryptedJsonbField: expectedJson, |
| 197 | + }, |
| 198 | + { |
| 199 | + NonEncryptedField: "melbourne", |
| 200 | + EncryptedIntField: 42, |
| 201 | + EncryptedTextField: "[email protected]", |
| 202 | + EncryptedJsonbField: generateJsonbData("first", "second", "third"), |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + inserted, err := engine.Insert(&examples) |
| 207 | + |
| 208 | + if err != nil { |
| 209 | + t.Errorf("Error inserting examples: %v", err) |
| 210 | + } |
| 211 | + |
| 212 | + assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows") |
| 213 | + |
| 214 | + // create a query |
| 215 | + jsonbQuery := map[string]any{ |
| 216 | + "top": map[string]any{ |
| 217 | + "integer": float64(101), |
| 218 | + }, |
| 219 | + } |
| 220 | + |
| 221 | + query, errTwo := goeql.SerializeQuery(jsonbQuery, "examples", "encrypted_jsonb_field") |
| 222 | + if errTwo != nil { |
| 223 | + log.Fatalf("Error marshaling encrypted_jsonb_field: %v", errTwo) |
| 224 | + } |
| 225 | + |
| 226 | + var returnedExample Example |
| 227 | + has, err := engine.Where("cs_ste_vec_v1(encrypted_jsonb_field) @> cs_ste_vec_v1(?)", query).Get(&returnedExample) |
| 228 | + if err != nil { |
| 229 | + t.Fatalf("Could not retrieve example: %v", err) |
| 230 | + } |
| 231 | + |
| 232 | + if !has { |
| 233 | + t.Errorf("Expected has to equal true, got: %v", has) |
| 234 | + } |
| 235 | + |
| 236 | + assert.Equal(t, returnedExample.EncryptedJsonbField, EncryptedJsonbField(expectedJson), "EncryptedJsonb field should match") |
| 237 | +} |
0 commit comments