|
| 1 | +package encrypt_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cipherstash/stack/languages/golang/encrypt" |
| 9 | + "github.com/cipherstash/stack/languages/golang/encrypt/internal/testmember" |
| 10 | + "github.com/cipherstash/stack/languages/golang/encrypt/internal/testpolicy" |
| 11 | + "github.com/cipherstash/stack/languages/golang/encrypt/internal/testusers" |
| 12 | +) |
| 13 | + |
| 14 | +// The generator writes a file three ways: from a struct's own tags, from |
| 15 | +// tags declared for a type in another package (-for), and from a policy |
| 16 | +// (stashgen.Generate). The golden tests only compile the second and third; |
| 17 | +// this runs them against the deterministic guest, beside the tag path, for |
| 18 | +// one declaration (testusers.User's) over one set of values, and holds the |
| 19 | +// three to the same bytes. |
| 20 | + |
| 21 | +var members = []testmember.Member{ |
| 22 | + {ID: 1, Age: 34, Email: "alice@example.com", Notes: "likes cats"}, |
| 23 | + {ID: 2, Age: 29, Email: "bob@example.com", Notes: "likes dogs"}, |
| 24 | +} |
| 25 | + |
| 26 | +func asUsers(ms []testmember.Member) []testusers.User { |
| 27 | + out := make([]testusers.User, len(ms)) |
| 28 | + for i, m := range ms { |
| 29 | + out[i] = testusers.User{ID: m.ID, Age: m.Age, Email: m.Email, Notes: m.Notes} |
| 30 | + } |
| 31 | + return out |
| 32 | +} |
| 33 | + |
| 34 | +func TestGeneratedCodeFromForAndFromAPolicyRunsAgainstTheGuest(t *testing.T) { |
| 35 | + c := deterministicClient(t) |
| 36 | + ctx := context.Background() |
| 37 | + cipher := c.DefaultKeyset() |
| 38 | + |
| 39 | + tagged, err := testusers.Encrypt(ctx, cipher, asUsers(members)) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + forPath, err := testusers.EncryptMember(ctx, cipher, members) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("the -for path: %v", err) |
| 46 | + } |
| 47 | + policyPath, err := testpolicy.Encrypt(ctx, cipher, members) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("the policy path: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // One declaration, three authors, the same terms. |
| 53 | + for i := range members { |
| 54 | + for name, got := range map[string]struct{ ageEq, ageOre, emailEq, emailMatch []byte }{ |
| 55 | + "the -for path": {forPath[i].Age.Equality, forPath[i].Age.Ore, forPath[i].Email.Equality, forPath[i].Email.Match}, |
| 56 | + "the policy path": {policyPath[i].Age.Equality, policyPath[i].Age.Ore, policyPath[i].Email.Equality, policyPath[i].Email.Match}, |
| 57 | + } { |
| 58 | + want := tagged[i] |
| 59 | + if !bytes.Equal(got.ageEq, want.Age.Equality) || !bytes.Equal(got.ageOre, want.Age.Ore) || !bytes.Equal(got.emailEq, want.Email.Equality) || !bytes.Equal(got.emailMatch, want.Email.Match) { |
| 60 | + t.Errorf("row %d: %s derives other terms than the tag path", i, name) |
| 61 | + } |
| 62 | + } |
| 63 | + if len(forPath[i].Notes.Ciphertext) == 0 || len(policyPath[i].Notes.Ciphertext) == 0 || forPath[i].ID != members[i].ID || policyPath[i].ID != members[i].ID { |
| 64 | + t.Errorf("row %d: outputs missing: %+v %+v", i, forPath[i], policyPath[i]) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Each path opens its own rows, through the cipher and the client. |
| 69 | + for _, d := range []struct { |
| 70 | + name string |
| 71 | + by encrypt.Decrypter |
| 72 | + }{{"cipher", cipher}, {"client", c}} { |
| 73 | + back, err := testusers.DecryptMember(ctx, d.by, forPath) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("DecryptMember through the %s: %v", d.name, err) |
| 76 | + } |
| 77 | + for i := range members { |
| 78 | + if back[i] != members[i] { |
| 79 | + t.Fatalf("the -for path through the %s: row %d = %+v, want %+v", d.name, i, back[i], members[i]) |
| 80 | + } |
| 81 | + } |
| 82 | + back, err = testpolicy.Decrypt(ctx, d.by, policyPath) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("testpolicy.Decrypt through the %s: %v", d.name, err) |
| 85 | + } |
| 86 | + for i := range members { |
| 87 | + if back[i] != members[i] { |
| 88 | + t.Fatalf("the policy path through the %s: row %d = %+v, want %+v", d.name, i, back[i], members[i]) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // And each opens the tag path's rows: the stored shape is one shape. |
| 94 | + crossed := make([]testusers.EncryptedMember, len(tagged)) |
| 95 | + for i, e := range tagged { |
| 96 | + crossed[i] = testusers.EncryptedMember{ |
| 97 | + ID: e.ID, |
| 98 | + Age: testusers.EncryptedMemberAge(e.Age), |
| 99 | + Email: testusers.EncryptedMemberEmail(e.Email), |
| 100 | + Notes: testusers.EncryptedMemberNotes(e.Notes), |
| 101 | + } |
| 102 | + } |
| 103 | + if back, err := testusers.DecryptMember(ctx, cipher, crossed); err != nil || back[0] != members[0] { |
| 104 | + t.Fatalf("the -for path opening the tag path's rows: %v %+v", err, back) |
| 105 | + } |
| 106 | + viaPolicy := make([]testpolicy.EncryptedMember, len(tagged)) |
| 107 | + for i, e := range tagged { |
| 108 | + viaPolicy[i] = testpolicy.EncryptedMember{ |
| 109 | + ID: e.ID, |
| 110 | + Age: testpolicy.EncryptedMemberAge(e.Age), |
| 111 | + Email: testpolicy.EncryptedMemberEmail(e.Email), |
| 112 | + Notes: testpolicy.EncryptedMemberNotes(e.Notes), |
| 113 | + } |
| 114 | + } |
| 115 | + if back, err := testpolicy.Decrypt(ctx, cipher, viaPolicy); err != nil || back[1] != members[1] { |
| 116 | + t.Fatalf("the policy path opening the tag path's rows: %v %+v", err, back) |
| 117 | + } |
| 118 | + |
| 119 | + // A term derived through each path's Fields is the tag path's. |
| 120 | + probe, err := testusers.Fields.Email.Equality(ctx, cipher, "bob@example.com") |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + forProbe, err := testusers.MemberFields.Email.Equality(ctx, cipher, "bob@example.com") |
| 125 | + if err != nil || !forProbe.Equal(probe) { |
| 126 | + t.Fatalf("the -for path's probe: %v, equal=%v", err, forProbe.Equal(probe)) |
| 127 | + } |
| 128 | + policyProbe, err := testpolicy.Fields.Email.Equality(ctx, cipher, "bob@example.com") |
| 129 | + if err != nil || !policyProbe.Equal(probe) { |
| 130 | + t.Fatalf("the policy path's probe: %v, equal=%v", err, policyProbe.Equal(probe)) |
| 131 | + } |
| 132 | + ore, err := testpolicy.Fields.Age.Ore(ctx, cipher, 29) |
| 133 | + if err != nil || !bytes.Equal(ore, tagged[1].Age.Ore) { |
| 134 | + t.Fatalf("the policy path's ORE probe: %v", err) |
| 135 | + } |
| 136 | + if !probe.Equal(tagged[1].Email.Equality) || probe.Equal(tagged[0].Email.Equality) { |
| 137 | + t.Error("the probe does not single out bob's row") |
| 138 | + } |
| 139 | +} |
0 commit comments