File: certificates_test.go

package info (click to toggle)
golang-github-docker-libtrust 0.0~git20150526.0.9cbd2a1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 288 kB
  • ctags: 297
  • sloc: makefile: 3
file content (111 lines) | stat: -rw-r--r-- 2,056 bytes parent folder | download | duplicates (3)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package libtrust

import (
	"encoding/pem"
	"io/ioutil"
	"net"
	"os"
	"path"
	"testing"
)

func TestGenerateCertificates(t *testing.T) {
	key, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	_, err = GenerateSelfSignedServerCert(key, []string{"localhost"}, []net.IP{net.ParseIP("127.0.0.1")})
	if err != nil {
		t.Fatal(err)
	}

	_, err = GenerateSelfSignedClientCert(key)
	if err != nil {
		t.Fatal(err)
	}
}

func TestGenerateCACertPool(t *testing.T) {
	key, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	caKey1, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	caKey2, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	_, err = GenerateCACertPool(key, []PublicKey{caKey1.PublicKey(), caKey2.PublicKey()})
	if err != nil {
		t.Fatal(err)
	}
}

func TestLoadCertificates(t *testing.T) {
	key, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	caKey1, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}
	caKey2, err := GenerateECP256PrivateKey()
	if err != nil {
		t.Fatal(err)
	}

	cert1, err := GenerateCACert(caKey1, key)
	if err != nil {
		t.Fatal(err)
	}
	cert2, err := GenerateCACert(caKey2, key)
	if err != nil {
		t.Fatal(err)
	}

	d, err := ioutil.TempDir("/tmp", "cert-test")
	if err != nil {
		t.Fatal(err)
	}
	caFile := path.Join(d, "ca.pem")
	f, err := os.OpenFile(caFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		t.Fatal(err)
	}

	err = pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: cert1.Raw})
	if err != nil {
		t.Fatal(err)
	}
	err = pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: cert2.Raw})
	if err != nil {
		t.Fatal(err)
	}
	f.Close()

	certs, err := LoadCertificateBundle(caFile)
	if err != nil {
		t.Fatal(err)
	}
	if len(certs) != 2 {
		t.Fatalf("Wrong number of certs received, expected: %d, received %d", 2, len(certs))
	}

	pool, err := LoadCertificatePool(caFile)
	if err != nil {
		t.Fatal(err)
	}

	if len(pool.Subjects()) != 2 {
		t.Fatalf("Invalid certificate pool")
	}
}