storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/certs/ca-certs_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package certs
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  func TestGetRootCAs(t *testing.T) {
    27  	emptydir, err := ioutil.TempDir("", "test-get-root-cas")
    28  	if err != nil {
    29  		t.Fatalf("Unable create temp directory. %v", emptydir)
    30  	}
    31  	defer os.RemoveAll(emptydir)
    32  
    33  	dir1, err := ioutil.TempDir("", "test-get-root-cas")
    34  	if err != nil {
    35  		t.Fatalf("Unable create temp directory. %v", dir1)
    36  	}
    37  	defer os.RemoveAll(dir1)
    38  	if err = os.Mkdir(filepath.Join(dir1, "empty-dir"), 0755); err != nil {
    39  		t.Fatalf("Unable create empty dir. %v", err)
    40  	}
    41  
    42  	dir2, err := ioutil.TempDir("", "test-get-root-cas")
    43  	if err != nil {
    44  		t.Fatalf("Unable create temp directory. %v", dir2)
    45  	}
    46  	defer os.RemoveAll(dir2)
    47  	if err = ioutil.WriteFile(filepath.Join(dir2, "empty-file"), []byte{}, 0644); err != nil {
    48  		t.Fatalf("Unable create test file. %v", err)
    49  	}
    50  
    51  	testCases := []struct {
    52  		certCAsDir  string
    53  		expectedErr error
    54  	}{
    55  		// ignores non-existent directories.
    56  		{"nonexistent-dir", nil},
    57  		// Ignores directories.
    58  		{dir1, nil},
    59  		// Ignore empty directory.
    60  		{emptydir, nil},
    61  		// Loads the cert properly.
    62  		{dir2, nil},
    63  	}
    64  
    65  	for _, testCase := range testCases {
    66  		_, err := GetRootCAs(testCase.certCAsDir)
    67  
    68  		if testCase.expectedErr == nil {
    69  			if err != nil {
    70  				t.Fatalf("error: expected = <nil>, got = %v", err)
    71  			}
    72  		} else if err == nil {
    73  			t.Fatalf("error: expected = %v, got = <nil>", testCase.expectedErr)
    74  		} else if testCase.expectedErr.Error() != err.Error() {
    75  			t.Fatalf("error: expected = %v, got = %v", testCase.expectedErr, err)
    76  		}
    77  	}
    78  }