blob: b77cacbccd5f407263c9066c21c32122120b54a0 [file] [log] [blame]
Han-Wen Nienhuyse62b2ae2013-09-13 14:25:14 -04001package ssh
2
3import (
4 "crypto"
5 "crypto/dsa"
6 "crypto/rand"
7 "crypto/rsa"
8 "reflect"
9 "testing"
10)
11
12func TestRSAMarshal(t *testing.T) {
13 k0 := &rsakey.PublicKey
14 k1 := NewRSAPublicKey(k0)
15 k2, rest, ok := ParsePublicKey(MarshalPublicKey(k1))
16 if !ok {
17 t.Errorf("could not parse back Blob output")
18 }
19 if len(rest) > 0 {
20 t.Errorf("trailing junk in RSA Blob() output")
21 }
22 if !reflect.DeepEqual(k0, k2.RawKey().(*rsa.PublicKey)) {
23 t.Errorf("got %#v in roundtrip, want %#v", k2.RawKey(), k0)
24 }
25}
26
27func TestRSAKeyVerify(t *testing.T) {
28 pub := NewRSAPublicKey(&rsakey.PublicKey)
29
30 data := []byte("sign me")
31 h := crypto.SHA1.New()
32 h.Write(data)
33 digest := h.Sum(nil)
34
35 sig, err := rsa.SignPKCS1v15(rand.Reader, rsakey, crypto.SHA1, digest)
36 if err != nil {
37 t.Fatalf("SignPKCS1v15: %v", err)
38 }
39
40 if !pub.Verify(data, sig) {
41 t.Errorf("publicKey.Verify failed")
42 }
43}
44
45func TestDSAMarshal(t *testing.T) {
46 k0 := &dsakey.PublicKey
47 k1 := NewDSAPublicKey(k0)
48 k2, rest, ok := ParsePublicKey(MarshalPublicKey(k1))
49 if !ok {
50 t.Errorf("could not parse back Blob output")
51 }
52 if len(rest) > 0 {
53 t.Errorf("trailing junk in DSA Blob() output")
54 }
55 if !reflect.DeepEqual(k0, k2.RawKey().(*dsa.PublicKey)) {
56 t.Errorf("got %#v in roundtrip, want %#v", k2.RawKey(), k0)
57 }
58}
59
60// TODO(hanwen): test for ECDSA marshal.