...

Source file src/github.com/google/certificate-transparency-go/tls/types_test.go

Documentation: github.com/google/certificate-transparency-go/tls

     1  // Copyright 2016 Google LLC. 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 tls
    16  
    17  import (
    18  	"crypto"
    19  	"crypto/dsa" //nolint:staticcheck
    20  	"crypto/ecdsa"
    21  	"crypto/rsa"
    22  	"testing"
    23  )
    24  
    25  func TestHashAlgorithmString(t *testing.T) {
    26  	var tests = []struct {
    27  		algo HashAlgorithm
    28  		want string
    29  	}{
    30  		{None, "None"},
    31  		{MD5, "MD5"},
    32  		{SHA1, "SHA1"},
    33  		{SHA224, "SHA224"},
    34  		{SHA256, "SHA256"},
    35  		{SHA384, "SHA384"},
    36  		{SHA512, "SHA512"},
    37  		{99, "UNKNOWN(99)"},
    38  	}
    39  	for _, test := range tests {
    40  		if got := test.algo.String(); got != test.want {
    41  			t.Errorf("%v.String()=%q; want %q", test.algo, got, test.want)
    42  		}
    43  	}
    44  }
    45  
    46  func TestSignatureAlgorithmString(t *testing.T) {
    47  	var tests = []struct {
    48  		algo SignatureAlgorithm
    49  		want string
    50  	}{
    51  		{Anonymous, "Anonymous"},
    52  		{RSA, "RSA"},
    53  		{DSA, "DSA"},
    54  		{ECDSA, "ECDSA"},
    55  		{99, "UNKNOWN(99)"},
    56  	}
    57  	for _, test := range tests {
    58  		if got := test.algo.String(); got != test.want {
    59  			t.Errorf("%v.String()=%q; want %q", test.algo, got, test.want)
    60  		}
    61  	}
    62  }
    63  
    64  func TestDigitallySignedString(t *testing.T) {
    65  	var tests = []struct {
    66  		ds   DigitallySigned
    67  		want string
    68  	}{
    69  		{
    70  			ds:   DigitallySigned{Algorithm: SignatureAndHashAlgorithm{Hash: SHA1, Signature: RSA}, Signature: []byte{0x01, 0x02}},
    71  			want: "Signature: HashAlgo=SHA1 SignAlgo=RSA Value=0102",
    72  		},
    73  		{
    74  			ds:   DigitallySigned{Algorithm: SignatureAndHashAlgorithm{Hash: 99, Signature: 99}, Signature: []byte{0x03, 0x04}},
    75  			want: "Signature: HashAlgo=UNKNOWN(99) SignAlgo=UNKNOWN(99) Value=0304",
    76  		},
    77  	}
    78  	for _, test := range tests {
    79  		if got := test.ds.String(); got != test.want {
    80  			t.Errorf("%v.String()=%q; want %q", test.ds, got, test.want)
    81  		}
    82  	}
    83  }
    84  
    85  func TestSignatureAlgorithm(t *testing.T) {
    86  	for _, test := range []struct {
    87  		name string
    88  		key  crypto.PublicKey
    89  		want SignatureAlgorithm
    90  	}{
    91  		{name: "ECDSA", key: new(ecdsa.PublicKey), want: ECDSA},
    92  		{name: "RSA", key: new(rsa.PublicKey), want: RSA},
    93  		{name: "DSA", key: new(dsa.PublicKey), want: DSA},
    94  		{name: "Other", key: "foo", want: Anonymous},
    95  	} {
    96  		if got := SignatureAlgorithmFromPubKey(test.key); got != test.want {
    97  			t.Errorf("%v: SignatureAlgorithm() = %v, want %v", test.name, got, test.want)
    98  		}
    99  	}
   100  }
   101  

View as plain text