github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/crypto/keys/generate_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 keys_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/google/trillian/crypto/keys"
    21  	"github.com/google/trillian/crypto/keys/testonly"
    22  	"github.com/google/trillian/crypto/keyspb"
    23  )
    24  
    25  func TestNewFromSpec(t *testing.T) {
    26  	for _, test := range []struct {
    27  		desc    string
    28  		keySpec *keyspb.Specification
    29  		wantErr bool
    30  	}{
    31  		{
    32  			desc: "ECDSA with default params",
    33  			keySpec: &keyspb.Specification{
    34  				Params: &keyspb.Specification_EcdsaParams{},
    35  			},
    36  		},
    37  		{
    38  			desc: "ECDSA with explicit params",
    39  			keySpec: &keyspb.Specification{
    40  				Params: &keyspb.Specification_EcdsaParams{
    41  					EcdsaParams: &keyspb.Specification_ECDSA{
    42  						Curve: keyspb.Specification_ECDSA_P521,
    43  					},
    44  				},
    45  			},
    46  		},
    47  		{
    48  			desc: "RSA with default params",
    49  			keySpec: &keyspb.Specification{
    50  				Params: &keyspb.Specification_RsaParams{},
    51  			},
    52  		},
    53  		{
    54  			desc: "RSA with explicit params",
    55  			keySpec: &keyspb.Specification{
    56  				Params: &keyspb.Specification_RsaParams{
    57  					RsaParams: &keyspb.Specification_RSA{
    58  						Bits: 4096,
    59  					},
    60  				},
    61  			},
    62  		},
    63  		{
    64  			desc: "RSA with negative key size",
    65  			keySpec: &keyspb.Specification{
    66  				Params: &keyspb.Specification_RsaParams{
    67  					RsaParams: &keyspb.Specification_RSA{
    68  						Bits: -4096,
    69  					},
    70  				},
    71  			},
    72  			wantErr: true,
    73  		},
    74  		{
    75  			desc: "RSA with insufficient key size",
    76  			keySpec: &keyspb.Specification{
    77  				Params: &keyspb.Specification_RsaParams{
    78  					RsaParams: &keyspb.Specification_RSA{
    79  						Bits: MinRsaKeySizeInBits - 1,
    80  					},
    81  				},
    82  			},
    83  			wantErr: true,
    84  		},
    85  		{
    86  			desc:    "No params",
    87  			keySpec: &keyspb.Specification{},
    88  			wantErr: true,
    89  		},
    90  		{
    91  			desc:    "Nil KeySpec",
    92  			wantErr: true,
    93  		},
    94  	} {
    95  		key, err := NewFromSpec(test.keySpec)
    96  		if gotErr := err != nil; gotErr != test.wantErr {
    97  			t.Errorf("%v: NewFromSpec() = (_, %v), want err? %v", test.desc, err, test.wantErr)
    98  			continue
    99  		} else if gotErr {
   100  			continue
   101  		}
   102  
   103  		if err := testonly.CheckKeyMatchesSpec(key, test.keySpec); err != nil {
   104  			t.Errorf("%v: CheckKeyMatchesSpec(): %v", test.desc, err)
   105  		}
   106  	}
   107  }