-
Notifications
You must be signed in to change notification settings - Fork 19
/
certificate_test.go
86 lines (67 loc) · 1.66 KB
/
certificate_test.go
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/afero"
)
func TestFindAll(t *testing.T) {
fs := afero.NewMemMapFs()
fs.Mkdir(CertificatePath, os.ModeDir)
certs := []string{"a.tld.pem", "b.tld.pem", "c.tld"}
for _, newCert := range certs {
fs.Create(filepath.Join("/certs", newCert))
}
foundCerts, err := FindAllCertificates(fs)
if err != nil {
t.Error(err)
}
if len(foundCerts) != 2 {
t.Errorf("Unexpected cert count, got %d", len(foundCerts))
}
}
func TestVerify(t *testing.T) {
fs := afero.NewMemMapFs()
err := VerifyCertificates(fs)
if err == nil {
t.Error("Expected error")
}
fs.Create("/certs/a.tld.pem")
err = VerifyCertificates(fs)
if err != nil {
t.Error(err)
}
}
func TestFindForHostname(t *testing.T) {
fs := afero.NewMemMapFs()
fs.Create("/certs/schaper.io.pem")
cert, err := FindCertificateForHost("schaper.io", fs)
if err != nil {
t.Error(err)
}
if cert.Name != "schaper.io.pem" {
t.Errorf("Unexpected cert for hostname: %s", cert.Name)
}
}
func TestBelongsToHost(t *testing.T) {
fs := afero.NewMemMapFs()
cert := NewCertificate("hostname.com.pem", fs)
belongs := cert.belongsToHost("hostname.com")
if !belongs {
t.Errorf("Expected cert and host to belong")
}
belongs = cert.belongsToHost("horsename.com")
if belongs {
t.Errorf("Expected cert to not belong")
}
}
func TestFullPath(t *testing.T) {
fs := afero.NewMemMapFs()
name := "mysite.pem"
cert := NewCertificate(name, fs)
expected := strings.Join([]string{CertificatePath, name}, "/")
if cert.FullPath() != expected {
t.Errorf("Unexpected certificate path, got %s want %s", cert.FullPath(), CertificatePath)
}
}