...

Source file src/github.com/google/certificate-transparency-go/tls/hash_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  	"encoding/hex"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/google/certificate-transparency-go/testdata"
    23  )
    24  
    25  func TestGenerateHash(t *testing.T) {
    26  	var tests = []struct {
    27  		in     string // hex encoded
    28  		algo   HashAlgorithm
    29  		want   string // hex encoded
    30  		errstr string
    31  	}{
    32  		// Empty hash values
    33  		{"", MD5, "d41d8cd98f00b204e9800998ecf8427e", ""},
    34  		{"", SHA1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", ""},
    35  		{"", SHA224, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", ""},
    36  		{"", SHA256, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
    37  		{"", SHA384, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", ""},
    38  		{"", SHA512, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", ""},
    39  		{"", 999, "", "unsupported"},
    40  
    41  		// Hashes of "abcd".
    42  		{"61626364", MD5, testdata.AbcdMD5, ""},
    43  		{"61626364", SHA1, testdata.AbcdSHA1, ""},
    44  		{"61626364", SHA224, testdata.AbcdSHA224, ""},
    45  		{"61626364", SHA256, testdata.AbcdSHA256, ""},
    46  		{"61626364", SHA384, testdata.AbcdSHA384, ""},
    47  		{"61626364", SHA512, testdata.AbcdSHA512, ""},
    48  	}
    49  	for _, test := range tests {
    50  		got, _, err := generateHash(test.algo, testdata.FromHex(test.in))
    51  		if test.errstr != "" {
    52  			if err == nil {
    53  				t.Errorf("generateHash(%s)=%s,nil; want error %q", test.in, hex.EncodeToString(got), test.errstr)
    54  			} else if !strings.Contains(err.Error(), test.errstr) {
    55  				t.Errorf("generateHash(%s)=nil,%q; want error %q", test.in, test.errstr, err.Error())
    56  			}
    57  			continue
    58  		}
    59  		if err != nil {
    60  			t.Errorf("generateHash(%s)=nil,%q; want %s", test.in, err, test.want)
    61  		} else if hex.EncodeToString(got) != test.want {
    62  			t.Errorf("generateHash(%s)=%s,nil; want %s", test.in, hex.EncodeToString(got), test.want)
    63  		}
    64  	}
    65  }
    66  

View as plain text