github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/crypto/keys/pem/proto/register_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     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 proto
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  	"github.com/google/trillian/crypto/keys"
    23  	"github.com/google/trillian/crypto/keys/testonly"
    24  	"github.com/google/trillian/crypto/keyspb"
    25  )
    26  
    27  func TestProtoHandler(t *testing.T) {
    28  	ctx := context.Background()
    29  
    30  	for _, test := range []struct {
    31  		desc     string
    32  		keyProto proto.Message
    33  		wantErr  bool
    34  	}{
    35  		{
    36  			desc: "PEMKeyFile",
    37  			keyProto: &keyspb.PEMKeyFile{
    38  				Path:     "../../../../testdata/log-rpc-server.privkey.pem",
    39  				Password: "towel",
    40  			},
    41  		},
    42  		{
    43  			desc: "PemKeyFile with non-existent file",
    44  			keyProto: &keyspb.PEMKeyFile{
    45  				Path: "non-existent.pem",
    46  			},
    47  			wantErr: true,
    48  		},
    49  		{
    50  			desc: "PemKeyFile with wrong password",
    51  			keyProto: &keyspb.PEMKeyFile{
    52  				Path:     "../../../../testdata/log-rpc-server.privkey.pem",
    53  				Password: "wrong-password",
    54  			},
    55  			wantErr: true,
    56  		},
    57  		{
    58  			desc: "PemKeyFile with missing password",
    59  			keyProto: &keyspb.PEMKeyFile{
    60  				Path: "../../../../testdata/log-rpc-server.privkey.pem",
    61  			},
    62  			wantErr: true,
    63  		},
    64  	} {
    65  		signer, err := keys.NewSigner(ctx, test.keyProto)
    66  		if gotErr := err != nil; gotErr != test.wantErr {
    67  			t.Errorf("%v: NewSigner(_, %#v) = (_, %q), want (_, nil)", test.desc, test.keyProto, err)
    68  			continue
    69  		} else if gotErr {
    70  			continue
    71  		}
    72  
    73  		// Check that the returned signer can produce signatures successfully.
    74  		if err := testonly.SignAndVerify(signer, signer.Public()); err != nil {
    75  			t.Errorf("%v: SignAndVerify() = %q, want nil", test.desc, err)
    76  		}
    77  	}
    78  }