...

Source file src/github.com/google/s2a-go/s2a_options_test.go

Documentation: github.com/google/s2a-go

     1  /*
     2   *
     3   * Copyright 2021 Google LLC
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     https://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    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