~nskaggs/+junk/juju-packaging-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/ssh/generate_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-27 20:23:11 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161027202311-sux4jk2o73p1d6rg
Re-add src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package ssh_test
 
5
 
 
6
import (
 
7
        "crypto/rsa"
 
8
        "io"
 
9
 
 
10
        "github.com/juju/testing"
 
11
        jc "github.com/juju/testing/checkers"
 
12
        gc "gopkg.in/check.v1"
 
13
 
 
14
        "github.com/juju/utils/ssh"
 
15
)
 
16
 
 
17
type GenerateSuite struct {
 
18
        testing.IsolationSuite
 
19
}
 
20
 
 
21
var _ = gc.Suite(&GenerateSuite{})
 
22
 
 
23
var pregeneratedKey *rsa.PrivateKey
 
24
 
 
25
// overrideGenerateKey patches out rsa.GenerateKey to create a single testing
 
26
// key which is saved and used between tests to save computation time.
 
27
func overrideGenerateKey(c *gc.C) testing.Restorer {
 
28
        restorer := testing.PatchValue(ssh.RSAGenerateKey, func(random io.Reader, bits int) (*rsa.PrivateKey, error) {
 
29
                if pregeneratedKey != nil {
 
30
                        return pregeneratedKey, nil
 
31
                }
 
32
                // Ignore requested bits and just use 512 bits for speed
 
33
                key, err := rsa.GenerateKey(random, 512)
 
34
                if err != nil {
 
35
                        return nil, err
 
36
                }
 
37
                key.Precompute()
 
38
                pregeneratedKey = key
 
39
                return key, nil
 
40
        })
 
41
        return restorer
 
42
}
 
43
 
 
44
func (s *GenerateSuite) TestGenerate(c *gc.C) {
 
45
        defer overrideGenerateKey(c).Restore()
 
46
        private, public, err := ssh.GenerateKey("some-comment")
 
47
 
 
48
        c.Check(err, jc.ErrorIsNil)
 
49
        c.Check(private, jc.HasPrefix, "-----BEGIN RSA PRIVATE KEY-----\n")
 
50
        c.Check(private, jc.HasSuffix, "-----END RSA PRIVATE KEY-----\n")
 
51
        c.Check(public, jc.HasPrefix, "ssh-rsa ")
 
52
        c.Check(public, jc.HasSuffix, " some-comment\n")
 
53
}