Skip to content

Commit 1d3663b

Browse files
committed
more tests
1 parent e1bf8a2 commit 1d3663b

File tree

4 files changed

+300
-11
lines changed

4 files changed

+300
-11
lines changed

languages/go/xorm/.envrc.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export CS_CLIENT_ID=
88
export CS_CLIENT_KEY=
99

1010
# Used by Proxy
11-
export CS_ENCRYPTION__CLIENT_ID=
12-
export CS_ENCRYPTION__CLIENT_KEY=
11+
export CS_ENCRYPTION__CLIENT_ID=$CS_CLIENT_ID
12+
export CS_ENCRYPTION__CLIENT_KEY=$CS_CLIENT_KEY
1313
export CS_AUDIT__ENABLED=false
1414
export CS_DATABASE__PORT=5432
1515
export CS_DATABASE__USERNAME=postgres

languages/go/xorm/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ Run examples:
7373
```shell
7474
./run.sh examples
7575
```
76+
77+
Run tests:
78+
79+
```shell
80+
./run.sh tests
81+
```

languages/go/xorm/e2e_test.go

Lines changed: 276 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ func TestWhereQueryOnUnencryptedColumn(t *testing.T) {
6363
t.Errorf("Expected has to equal true, got: %v", has)
6464
}
6565

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")
66+
assert.Equal(t, newExample.NonEncryptedField, example.NonEncryptedField, "NonEncryptedField does not match")
67+
assert.Equal(t, newExample.EncryptedIntField, example.EncryptedIntField, "EncryptedIntField does not match")
68+
assert.Equal(t, newExample.EncryptedTextField, example.EncryptedTextField, "EncryptedTextField does not match")
69+
assert.Equal(t, newExample.EncryptedJsonbField, example.EncryptedJsonbField, "EncryptedJsonbField does not match")
7070
}
7171

7272
func TestMatchQueryLongString(t *testing.T) {
@@ -235,3 +235,275 @@ func TestJsonbQuerySimple(t *testing.T) {
235235

236236
assert.Equal(t, returnedExample.EncryptedJsonbField, EncryptedJsonbField(expectedJson), "EncryptedJsonb field should match")
237237
}
238+
239+
func TestJsonbQueryNested(t *testing.T) {
240+
engine := proxyEngine()
241+
truncateDb(engine)
242+
243+
expectedJson := map[string]any{
244+
"top": map[string]any{
245+
"integer": float64(101),
246+
"float": 1.234,
247+
"string": "some string",
248+
"nested_one": map[string]any{
249+
"nested_two": "hello world",
250+
},
251+
},
252+
"bottom": "value_three",
253+
}
254+
255+
examples := []Example{
256+
{
257+
NonEncryptedField: "sydney",
258+
EncryptedTextField: "testing",
259+
EncryptedIntField: 42,
260+
EncryptedJsonbField: expectedJson,
261+
},
262+
{
263+
NonEncryptedField: "melbourne",
264+
EncryptedIntField: 42,
265+
EncryptedTextField: "[email protected]",
266+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
267+
},
268+
}
269+
270+
inserted, err := engine.Insert(&examples)
271+
272+
if err != nil {
273+
t.Errorf("Error inserting examples: %v", err)
274+
}
275+
276+
assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows")
277+
278+
// create a query
279+
jsonbQuery := map[string]any{
280+
"top": map[string]any{
281+
"nested_one": map[string]any{
282+
"nested_two": "hello world",
283+
},
284+
},
285+
}
286+
287+
query, errTwo := goeql.SerializeQuery(jsonbQuery, "examples", "encrypted_jsonb_field")
288+
if errTwo != nil {
289+
log.Fatalf("Error marshaling encrypted_jsonb_field: %v", errTwo)
290+
}
291+
292+
var returnedExample Example
293+
has, err := engine.Where("cs_ste_vec_v1(encrypted_jsonb_field) @> cs_ste_vec_v1(?)", query).Get(&returnedExample)
294+
if err != nil {
295+
t.Fatalf("Could not retrieve example: %v", err)
296+
}
297+
298+
if !has {
299+
t.Errorf("Expected has to equal true, got: %v", has)
300+
}
301+
302+
assert.Equal(t, returnedExample.EncryptedJsonbField, EncryptedJsonbField(expectedJson), "EncryptedJsonb field should match")
303+
}
304+
305+
func TestOreStringRangeQuery(t *testing.T) {
306+
engine := proxyEngine()
307+
truncateDb(engine)
308+
expected := EncryptedTextField("whale")
309+
310+
examples := []Example{
311+
{
312+
NonEncryptedField: "sydney",
313+
EncryptedTextField: expected,
314+
EncryptedIntField: 42,
315+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
316+
},
317+
{
318+
NonEncryptedField: "melbourne",
319+
EncryptedIntField: 42,
320+
EncryptedTextField: "apple",
321+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
322+
},
323+
}
324+
325+
inserted, err := engine.Insert(&examples)
326+
327+
if err != nil {
328+
t.Errorf("Error inserting examples: %v", err)
329+
}
330+
331+
assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows")
332+
333+
// Query
334+
query, errQuery := goeql.SerializeQuery("tree", "examples", "encrypted_text_field")
335+
if errQuery != nil {
336+
log.Fatalf("err: %v", errQuery)
337+
}
338+
339+
var returnedExample Example
340+
has, err := engine.Where("cs_ore_64_8_v1(encrypted_text_field) > cs_ore_64_8_v1(?)", query).Get(&returnedExample)
341+
if err != nil {
342+
t.Fatalf("Could not retrieve example: %v", err)
343+
}
344+
345+
if !has {
346+
t.Errorf("Expected has to equal true, got: %v", has)
347+
}
348+
349+
assert.Equal(t, returnedExample.EncryptedTextField, expected, "EncryptedText field should match")
350+
}
351+
352+
func TestOreIntRangeQuery(t *testing.T) {
353+
engine := proxyEngine()
354+
truncateDb(engine)
355+
expected := EncryptedIntField(42)
356+
357+
examples := []Example{
358+
{
359+
NonEncryptedField: "sydney",
360+
EncryptedTextField: "whale",
361+
EncryptedIntField: expected,
362+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
363+
},
364+
{
365+
NonEncryptedField: "melbourne",
366+
EncryptedIntField: 23,
367+
EncryptedTextField: "apple",
368+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
369+
},
370+
}
371+
372+
inserted, err := engine.Insert(&examples)
373+
374+
if err != nil {
375+
t.Errorf("Error inserting examples: %v", err)
376+
}
377+
378+
assert.Equal(t, int64(2), inserted, "Expected to insert 2 rows")
379+
380+
// Query
381+
query, errQuery := goeql.SerializeQuery(32, "examples", "encrypted_int_field")
382+
if errQuery != nil {
383+
log.Fatalf("err: %v", errQuery)
384+
}
385+
386+
var returnedExample Example
387+
has, err := engine.Where("cs_ore_64_8_v1(encrypted_int_field) > cs_ore_64_8_v1(?)", query).Get(&returnedExample)
388+
if err != nil {
389+
t.Fatalf("Could not retrieve example: %v", err)
390+
}
391+
392+
if !has {
393+
t.Errorf("Expected has to equal true, got: %v", has)
394+
}
395+
396+
assert.Equal(t, returnedExample.EncryptedIntField, expected, "EncryptedInt field should match")
397+
}
398+
399+
func TestOreBoolRangeQuery(t *testing.T) {
400+
engine := proxyEngine()
401+
truncateDb(engine)
402+
expected := EncryptedBoolField(true)
403+
404+
examples := []Example{
405+
{
406+
NonEncryptedField: "sydney",
407+
EncryptedTextField: "whale",
408+
EncryptedIntField: 42,
409+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
410+
EncryptedBoolField: false,
411+
},
412+
{
413+
NonEncryptedField: "melbourne",
414+
EncryptedIntField: 23,
415+
EncryptedTextField: "pineapple",
416+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
417+
EncryptedBoolField: expected,
418+
},
419+
{
420+
NonEncryptedField: "launceston",
421+
EncryptedIntField: 23,
422+
EncryptedTextField: "apple",
423+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
424+
EncryptedBoolField: false,
425+
},
426+
}
427+
428+
inserted, err := engine.Insert(&examples)
429+
430+
if err != nil {
431+
t.Errorf("Error inserting examples: %v", err)
432+
}
433+
434+
assert.Equal(t, int64(3), inserted, "Expected to insert 3 rows")
435+
436+
// Query
437+
query, errQuery := goeql.SerializeQuery(false, "examples", "encrypted_bool_field")
438+
if errQuery != nil {
439+
log.Fatalf("err: %v", errQuery)
440+
}
441+
442+
var returnedExample Example
443+
has, err := engine.Where("cs_ore_64_8_v1(encrypted_bool_field) > cs_ore_64_8_v1(?)", query).Get(&returnedExample)
444+
if err != nil {
445+
t.Fatalf("Could not retrieve example: %v", err)
446+
}
447+
448+
if !has {
449+
t.Errorf("Expected has to equal true, got: %v", has)
450+
}
451+
452+
assert.Equal(t, returnedExample.EncryptedBoolField, expected, "EncryptedBool field should match")
453+
}
454+
455+
func TestUniqueStringQuery(t *testing.T) {
456+
engine := proxyEngine()
457+
truncateDb(engine)
458+
expected := EncryptedTextField("testing two")
459+
460+
examples := []Example{
461+
{
462+
NonEncryptedField: "sydney",
463+
EncryptedTextField: "whale",
464+
EncryptedIntField: 42,
465+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
466+
EncryptedBoolField: false,
467+
},
468+
{
469+
NonEncryptedField: "melbourne",
470+
EncryptedIntField: 23,
471+
EncryptedTextField: expected,
472+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
473+
EncryptedBoolField: true,
474+
},
475+
{
476+
NonEncryptedField: "launceston",
477+
EncryptedIntField: 23,
478+
EncryptedTextField: "apple",
479+
EncryptedJsonbField: generateJsonbData("first", "second", "third"),
480+
EncryptedBoolField: false,
481+
},
482+
}
483+
484+
inserted, err := engine.Insert(&examples)
485+
486+
if err != nil {
487+
t.Errorf("Error inserting examples: %v", err)
488+
}
489+
490+
assert.Equal(t, int64(3), inserted, "Expected to insert 3 rows")
491+
492+
// Query
493+
query, errQuery := goeql.SerializeQuery("testing two", "examples", "encrypted_text_field")
494+
if errQuery != nil {
495+
log.Fatalf("err: %v", errQuery)
496+
}
497+
498+
var returnedExample Example
499+
has, err := engine.Where("cs_unique_v1(encrypted_text_field) = cs_unique_v1($1)", query).Get(&returnedExample)
500+
if err != nil {
501+
t.Fatalf("Could not retrieve example: %v", err)
502+
}
503+
504+
if !has {
505+
t.Errorf("Expected has to equal true, got: %v", has)
506+
}
507+
508+
assert.Equal(t, returnedExample.EncryptedTextField, expected, "EncryptedText field should match")
509+
}

languages/go/xorm/run.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,23 @@ subproject_teardown() {
2929
subproject_examples() {
3030
# reset db
3131
go run . setupDev
32-
# start proxy
3332

3433
# run examples queries
3534
go run . runExamples
3635
}
3736

37+
subproject_tests(){
38+
# start postgres and proxy
39+
docker compose up -d
40+
# reset db
41+
go run . setupDev
42+
# run e2e tests
43+
make gotest
44+
# run goeql tests
45+
cd ../goeql
46+
go test
47+
}
48+
3849
subproject_start_proxy() {
3950
docker run --env-file .env -p 6432:6432 cipherstash/cipherstash-proxy:latest
4051
}
@@ -49,14 +60,14 @@ case $subcommand in
4960
subproject_teardown
5061
;;
5162

52-
start_proxy)
53-
subproject_start_proxy
54-
;;
55-
5663
examples)
5764
subproject_examples
5865
;;
5966

67+
tests)
68+
subproject_tests
69+
;;
70+
6071
*)
6172
echo "Unknown run subcommand '$subcommand'"
6273
exit 1

0 commit comments

Comments
 (0)