launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/utils/ssh/clientkeys_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ssh_test 5 6 import ( 7 "io/ioutil" 8 gc "launchpad.net/gocheck" 9 "os" 10 11 "launchpad.net/juju-core/testing" 12 jc "launchpad.net/juju-core/testing/checkers" 13 "launchpad.net/juju-core/testing/testbase" 14 "launchpad.net/juju-core/utils" 15 "launchpad.net/juju-core/utils/ssh" 16 ) 17 18 type ClientKeysSuite struct { 19 testbase.LoggingSuite 20 } 21 22 var _ = gc.Suite(&ClientKeysSuite{}) 23 24 func (s *ClientKeysSuite) SetUpTest(c *gc.C) { 25 s.LoggingSuite.SetUpTest(c) 26 fakeHome := testing.MakeEmptyFakeHome(c) 27 s.AddCleanup(func(*gc.C) { fakeHome.Restore() }) 28 s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() }) 29 } 30 31 func checkFiles(c *gc.C, obtained, expected []string) { 32 var err error 33 for i, e := range expected { 34 expected[i], err = utils.NormalizePath(e) 35 c.Assert(err, gc.IsNil) 36 } 37 c.Assert(obtained, jc.SameContents, expected) 38 } 39 40 func checkPublicKeyFiles(c *gc.C, expected ...string) { 41 keys := ssh.PublicKeyFiles() 42 checkFiles(c, keys, expected) 43 } 44 45 func checkPrivateKeyFiles(c *gc.C, expected ...string) { 46 keys := ssh.PrivateKeyFiles() 47 checkFiles(c, keys, expected) 48 } 49 50 func (s *ClientKeysSuite) TestPublicKeyFiles(c *gc.C) { 51 // LoadClientKeys will create the specified directory 52 // and populate it with a key pair. 53 err := ssh.LoadClientKeys("~/.juju/ssh") 54 c.Assert(err, gc.IsNil) 55 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 56 // All files ending with .pub in the client key dir get picked up. 57 priv, pub, err := ssh.GenerateKey("whatever") 58 c.Assert(err, gc.IsNil) 59 err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600) 60 c.Assert(err, gc.IsNil) 61 err = ssh.LoadClientKeys("~/.juju/ssh") 62 c.Assert(err, gc.IsNil) 63 // The new public key won't be observed until the 64 // corresponding private key exists. 65 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 66 err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600) 67 c.Assert(err, gc.IsNil) 68 err = ssh.LoadClientKeys("~/.juju/ssh") 69 c.Assert(err, gc.IsNil) 70 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub") 71 } 72 73 func (s *ClientKeysSuite) TestPrivateKeyFiles(c *gc.C) { 74 // Create/load client keys. They will be cached in memory: 75 // any files added to the directory will not be considered 76 // unless LoadClientKeys is called again. 77 err := ssh.LoadClientKeys("~/.juju/ssh") 78 c.Assert(err, gc.IsNil) 79 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 80 priv, pub, err := ssh.GenerateKey("whatever") 81 c.Assert(err, gc.IsNil) 82 err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600) 83 c.Assert(err, gc.IsNil) 84 err = ssh.LoadClientKeys("~/.juju/ssh") 85 c.Assert(err, gc.IsNil) 86 // The new private key won't be observed until the 87 // corresponding public key exists. 88 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 89 err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600) 90 c.Assert(err, gc.IsNil) 91 // new keys won't be reported until we call LoadClientKeys again 92 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 93 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 94 err = ssh.LoadClientKeys("~/.juju/ssh") 95 c.Assert(err, gc.IsNil) 96 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub") 97 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa", "~/.juju/ssh/whatever") 98 } 99 100 func (s *ClientKeysSuite) TestLoadClientKeysDirExists(c *gc.C) { 101 err := os.MkdirAll(testing.HomePath(".juju", "ssh"), 0755) 102 c.Assert(err, gc.IsNil) 103 err = ssh.LoadClientKeys("~/.juju/ssh") 104 c.Assert(err, gc.IsNil) 105 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 106 }