1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package tls
16
17 import (
18 "crypto"
19 "crypto/dsa"
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