|
| 1 | +package encrypttest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cipherstash/stack/languages/golang/encrypt" |
| 11 | + "github.com/cipherstash/stack/languages/golang/encrypt/encrypttest" |
| 12 | + "github.com/cipherstash/stack/languages/golang/encrypt/gensupport" |
| 13 | +) |
| 14 | + |
| 15 | +// recorder is a testing.TB whose Fatalf records the message instead of |
| 16 | +// stopping the test, so the failure text of AssertContexts can be checked. |
| 17 | +type recorder struct { |
| 18 | + testing.TB |
| 19 | + failed string |
| 20 | +} |
| 21 | + |
| 22 | +func (r *recorder) Helper() {} |
| 23 | +func (r *recorder) Fatalf(format string, args ...any) { |
| 24 | + r.failed = strings.TrimSpace(fmt.Sprintf(format, args...)) |
| 25 | +} |
| 26 | + |
| 27 | +func users() gensupport.Declaration { |
| 28 | + return gensupport.Declare("users"). |
| 29 | + EncryptIndex("email", gensupport.String, encrypt.Equality). |
| 30 | + Encrypt("notes", gensupport.String) |
| 31 | +} |
| 32 | + |
| 33 | +func skipWithoutGuest(t *testing.T) { |
| 34 | + t.Helper() |
| 35 | + if _, err := encrypttest.Contexts(context.Background(), users()); errors.Is(err, encrypt.ErrGuestNotBuilt) { |
| 36 | + t.Skip(err) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestAssertContextsPassesOnTheEnginesText(t *testing.T) { |
| 41 | + skipWithoutGuest(t) |
| 42 | + encrypttest.AssertContexts(t, users(), |
| 43 | + "email\n context: users / email\n"+ |
| 44 | + "notes\n context: users / notes\n") |
| 45 | + // A trailing newline is optional. |
| 46 | + encrypttest.AssertContexts(t, users(), |
| 47 | + "email\n context: users / email\n"+ |
| 48 | + "notes\n context: users / notes") |
| 49 | +} |
| 50 | + |
| 51 | +func TestAssertContextsNamesTheFirstLineThatDiffers(t *testing.T) { |
| 52 | + skipWithoutGuest(t) |
| 53 | + r := &recorder{TB: t} |
| 54 | + encrypttest.AssertContexts(r, users(), |
| 55 | + "email\n context: users / email\n"+ |
| 56 | + "notes\n context: users / name\n") |
| 57 | + if !strings.Contains(r.failed, "differ at line 4") || !strings.Contains(r.failed, `want: " context: users / name"`) { |
| 58 | + t.Fatalf("failure text: %q", r.failed) |
| 59 | + } |
| 60 | + r = &recorder{TB: t} |
| 61 | + encrypttest.AssertContexts(r, users(), "email\n context: users / email\n") |
| 62 | + if !strings.Contains(r.failed, "have 4 lines, and 2 were expected") { |
| 63 | + t.Fatalf("failure text: %q", r.failed) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestAssertContextsReportsARefusedDeclaration(t *testing.T) { |
| 68 | + skipWithoutGuest(t) |
| 69 | + bad := gensupport.Declare("users").EncryptIndex("age", gensupport.Uint32, encrypt.Match()) |
| 70 | + r := &recorder{TB: t} |
| 71 | + encrypttest.AssertContexts(r, bad, "") |
| 72 | + if !strings.Contains(r.failed, "the engine refused the declaration") { |
| 73 | + t.Fatalf("failure text: %q", r.failed) |
| 74 | + } |
| 75 | +} |
0 commit comments