...
1
18
19 package s2a
20
21 import (
22 "testing"
23
24 "github.com/google/go-cmp/cmp"
25 s2apb "github.com/google/s2a-go/internal/proto/common_go_proto"
26 "google.golang.org/protobuf/testing/protocmp"
27 )
28
29 func TestToProtoIdentity(t *testing.T) {
30 for _, tc := range []struct {
31 identity Identity
32 outIdentity *s2apb.Identity
33 }{
34 {
35 identity: NewSpiffeID("test_spiffe_id"),
36 outIdentity: &s2apb.Identity{
37 IdentityOneof: &s2apb.Identity_SpiffeId{SpiffeId: "test_spiffe_id"},
38 },
39 },
40 {
41 identity: NewHostname("test_hostname"),
42 outIdentity: &s2apb.Identity{
43 IdentityOneof: &s2apb.Identity_Hostname{Hostname: "test_hostname"},
44 },
45 },
46 {
47 identity: NewUID("test_uid"),
48 outIdentity: &s2apb.Identity{
49 IdentityOneof: &s2apb.Identity_Uid{Uid: "test_uid"},
50 },
51 },
52 {
53 identity: nil,
54 outIdentity: nil,
55 },
56 } {
57 t.Run(tc.outIdentity.String(), func(t *testing.T) {
58 protoSpiffeID, err := toProtoIdentity(tc.identity)
59 if err != nil {
60 t.Errorf("toProtoIdentity(%v) failed: %v", tc.identity, err)
61 }
62 if got, want := protoSpiffeID, tc.outIdentity; !cmp.Equal(got, want, protocmp.Transform()) {
63 t.Errorf("toProtoIdentity(%v) = %v, want %v", tc.outIdentity, got, want)
64 }
65 })
66 }
67 }
68
View as plain text