istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/cmd/pilot-agent/options/security_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package options
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"istio.io/istio/pkg/security"
    22  )
    23  
    24  func TestCheckGkeWorkloadCertificate(t *testing.T) {
    25  	cert, err := os.CreateTemp("", "existing-cert-file")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	defer os.Remove(cert.Name())
    30  
    31  	tests := []struct {
    32  		name     string
    33  		paths    []string
    34  		expected bool
    35  	}{
    36  		{
    37  			name: "non-existing cert paths",
    38  			paths: []string{
    39  				"/this-is-a-nonexisting-path-1", "/this-is-a-nonexisting-path-2",
    40  				"/this-is-a-nonexisting-path-3",
    41  			},
    42  			expected: false,
    43  		},
    44  		{
    45  			name:     "existing cert paths",
    46  			paths:    []string{cert.Name(), cert.Name(), cert.Name()},
    47  			expected: true,
    48  		},
    49  		{
    50  			name:     "mixed non-existing and existing cert paths",
    51  			paths:    []string{cert.Name(), "/this-is-a-nonexisting-path-1", "/this-is-a-nonexisting-path-2"},
    52  			expected: false,
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		result := security.CheckWorkloadCertificate(tt.paths[0], tt.paths[1], tt.paths[2])
    57  		if result != tt.expected {
    58  			t.Errorf("Test %s failed, expected: %t got: %t", tt.name, tt.expected, result)
    59  		}
    60  	}
    61  }