File: x509_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20200923.4646bfe-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,324 kB
  • sloc: makefile: 37
file content (61 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2016 Canonical ltd.
// Copyright 2016 Cloudbase solutions
// Licensed under the LGPLv3, see licence file for details.

package winrm_test

import (
	"io/ioutil"
	"os"
	"path"

	"github.com/juju/utils/v2/winrm"

	gc "gopkg.in/check.v1"
)

var (
	certFileName = "winrmcert.pem"
	keyFileName  = "winrmkey.pem"
)

func (w *WinRMSuite) PathCredentials(c *gc.C) (string, string, string) {
	base := c.MkDir()
	return base, path.Join(base, certFileName), path.Join(base, keyFileName)
}
func (w *WinRMSuite) TestLoadClientCert(c *gc.C) {
	cert := winrm.NewX509()
	// path and certs dosen't exist and generate it
	base, certPath, keyPath := w.PathCredentials(c)
	err := cert.LoadClientCert(certPath, keyPath)
	c.Assert(err, gc.IsNil)

	cert.Reset()

	// read/load the already generated certs
	err = cert.LoadClientCert(certPath, keyPath)
	// check if the're the same cert and keys
	c.Assert(err, gc.IsNil)
	err = os.RemoveAll(base)
	c.Assert(err, gc.IsNil)
}

func (w *WinRMSuite) TestLoadCACert(c *gc.C) {
	cert := winrm.NewX509()
	base, _, _ := w.PathCredentials(c)
	err := os.MkdirAll(base, 0755)
	c.Assert(err, gc.IsNil)

	cacertPath := path.Join(base + "winrmcacert.pem")
	err = ioutil.WriteFile(cacertPath, []byte("content"), 0755)
	c.Assert(err, gc.IsNil)

	err = cert.LoadCACert(cacertPath)
	c.Assert(err, gc.IsNil)

	ca := cert.CACert()
	c.Assert(len(ca) > 1, gc.Equals, true)

	err = os.RemoveAll(base)
	c.Assert(err, gc.IsNil)
}