...

Source file src/github.com/sigstore/rekor/pkg/pki/factory_test.go

Documentation: github.com/sigstore/rekor/pkg/pki

     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 pki
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	"go.uber.org/goleak"
    23  )
    24  
    25  func TestMain(m *testing.M) {
    26  	goleak.VerifyTestMain(m)
    27  }
    28  
    29  func TestFactoryNewKey(t *testing.T) {
    30  	type TestCase struct {
    31  		name              string
    32  		format            string
    33  		keyFile           string
    34  		sigFile           string
    35  		expectSuccess     bool
    36  		expectValidFormat bool
    37  	}
    38  
    39  	testCases := []TestCase{
    40  		{
    41  			name:              "valid pgp",
    42  			format:            "pgp",
    43  			keyFile:           "pgp/testdata/valid_armored_public.pgp",
    44  			sigFile:           "pgp/testdata/hello_world.txt.asc.sig",
    45  			expectSuccess:     true,
    46  			expectValidFormat: true,
    47  		},
    48  		{
    49  			name:              "valid minisign",
    50  			format:            "minisign",
    51  			keyFile:           "minisign/testdata/minisign.pub",
    52  			sigFile:           "minisign/testdata/hello_world.txt.minisig",
    53  			expectSuccess:     true,
    54  			expectValidFormat: true,
    55  		},
    56  		{
    57  			name:              "valid x509",
    58  			format:            "x509",
    59  			keyFile:           "x509/testdata/ec.pub",
    60  			sigFile:           "x509/testdata/hello_world.txt.sig",
    61  			expectSuccess:     true,
    62  			expectValidFormat: true,
    63  		},
    64  		{
    65  			name:              "valid ssh",
    66  			format:            "ssh",
    67  			keyFile:           "ssh/testdata/id_rsa.pub",
    68  			sigFile:           "ssh/testdata/hello_world.txt.sig",
    69  			expectSuccess:     true,
    70  			expectValidFormat: true,
    71  		},
    72  		{
    73  			name:              "invalid ssh signature",
    74  			format:            "ssh",
    75  			keyFile:           "ssh/testdata/id_rsa.pub",
    76  			sigFile:           "ssh/testdata/hello_world.txt",
    77  			expectSuccess:     false,
    78  			expectValidFormat: true,
    79  		},
    80  		{
    81  			name:              "invalid ssh key",
    82  			format:            "ssh",
    83  			keyFile:           "ssh/testdata/hello_world.txt",
    84  			sigFile:           "ssh/testdata/hello_world.txt.sig",
    85  			expectSuccess:     false,
    86  			expectValidFormat: true,
    87  		},
    88  		{
    89  			format:            "bogus",
    90  			expectSuccess:     false,
    91  			expectValidFormat: false,
    92  		},
    93  	}
    94  
    95  	for _, tc := range testCases {
    96  		tc := tc
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			factory, err := NewArtifactFactory(Format(tc.format))
    99  			if tc.expectValidFormat != (err == nil) {
   100  				t.Fatalf("unexpected error initializing factory for %v", tc.format)
   101  			}
   102  			if factory != nil {
   103  				keyFile, _ := os.Open(tc.keyFile)
   104  				_, newKeyErr := factory.NewPublicKey(keyFile)
   105  
   106  				sigFile, _ := os.Open(tc.sigFile)
   107  				_, newSigErr := factory.NewSignature(sigFile)
   108  
   109  				if tc.expectSuccess {
   110  					if newKeyErr != nil || newSigErr != nil {
   111  						t.Errorf("unexpected error generating public key %v or signature %v", newKeyErr, newSigErr)
   112  					}
   113  				} else { // expect a failure{
   114  					if newKeyErr == nil && newSigErr == nil {
   115  						t.Error("expected error generating public key and signature. got none")
   116  					}
   117  				}
   118  			}
   119  		})
   120  	}
   121  }
   122  

View as plain text