...

Source file src/github.com/sigstore/cosign/v2/pkg/blob/load_test.go

Documentation: github.com/sigstore/cosign/v2/pkg/blob

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     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  package blob
    17  
    18  import (
    19  	"bytes"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"os"
    23  	"path"
    24  	"runtime"
    25  	"testing"
    26  )
    27  
    28  func TestLoadFile(t *testing.T) {
    29  	if runtime.GOOS == "windows" {
    30  		t.Skip("Skipping on Windows due to https://github.com/golang/go/issues/51442")
    31  	}
    32  	temp := t.TempDir()
    33  	fname := "filename.txt"
    34  	path := path.Join(temp, fname)
    35  	data := []byte("test")
    36  	defer os.Remove(path)
    37  	os.WriteFile(path, data, 0400)
    38  
    39  	// absolute path
    40  	actual, err := LoadFileOrURL(path)
    41  	if err != nil {
    42  		t.Errorf("Reading from absolute path %s failed: %v", path, err)
    43  	} else if !bytes.Equal(actual, data) {
    44  		t.Errorf("LoadFileOrURL(absolute path) = '%s'; want '%s'", actual, data)
    45  	}
    46  
    47  	if err = os.Chdir(temp); err != nil {
    48  		t.Fatalf("Chdir('%s'): %v", temp, err)
    49  	}
    50  	actual, err = LoadFileOrURL(fname)
    51  	if err != nil {
    52  		t.Errorf("Reading from relative path %s failed: %v", fname, err)
    53  	} else if !bytes.Equal(actual, data) {
    54  		t.Errorf("LoadFileOrURL(relative path) = '%s'; want '%s'", actual, data)
    55  	}
    56  }
    57  
    58  func TestLoadURL(t *testing.T) {
    59  	data := []byte("test")
    60  
    61  	server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
    62  		rw.Write(data)
    63  	}))
    64  	defer server.Close()
    65  
    66  	actual, err := LoadFileOrURL(server.URL)
    67  	if err != nil {
    68  		t.Errorf("Reading from HTTP failed: %v", err)
    69  	} else if !bytes.Equal(actual, data) {
    70  		t.Errorf("LoadFileOrURL(HTTP) = '%s'; want '%s'", actual, data)
    71  	}
    72  
    73  	os.Setenv("MY_ENV_VAR", string(data))
    74  	actual, err = LoadFileOrURL("env://MY_ENV_VAR")
    75  	if err != nil {
    76  		t.Errorf("Reading from environment failed: %v", err)
    77  	} else if !bytes.Equal(actual, data) {
    78  		t.Errorf("LoadFileOrURL(env) = '%s'; want '%s'", actual, data)
    79  	}
    80  
    81  	os.Setenv("MY_ENV_VAR", "")
    82  	actual, err = LoadFileOrURL("env://MY_ENV_VAR")
    83  	if err != nil {
    84  		t.Errorf("Reading from environment failed: %v", err)
    85  	} else if !bytes.Equal(actual, make([]byte, 0)) {
    86  		t.Errorf("LoadFileOrURL(env) = '%s'; should be empty", actual)
    87  	}
    88  
    89  	os.Unsetenv("MY_ENV_VAR")
    90  	_, err = LoadFileOrURL("env://MY_ENV_VAR")
    91  	if err == nil {
    92  		t.Error("LoadFileOrURL(): expected error for unset env var")
    93  	}
    94  
    95  	_, err = LoadFileOrURL("invalid://url")
    96  	if err == nil {
    97  		t.Error("LoadFileOrURL(): expected error for invalid scheme")
    98  	}
    99  }
   100  

View as plain text