forked from Ouest-France/gofortiadc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_certificate_local_test.go
114 lines (94 loc) · 2.32 KB
/
system_certificate_local_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
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
112
113
114
package gofortiadc
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"testing"
"time"
)
func TestClient_SystemGetLocalCertificates(t *testing.T) {
if os.Getenv("TEST_LENS") != "true" {
t.Skip()
}
client, err := NewClientHelper()
if err != nil {
t.Fatal(err)
}
res, err := client.SystemGetLocalCertificates()
if err == nil {
t.Logf("%+v", res)
t.Fatal(err)
}
}
func TestClient_SystemGetLocalCertificate(t *testing.T) {
if os.Getenv("TEST_LENS") != "true" {
t.Skip()
}
client, err := NewClientHelper()
if err != nil {
t.Fatal(err)
}
_, err = client.SystemGetLocalCertificate("Factory")
if err != nil {
t.Fatal(err)
}
}
func TestClient_SystemCreateLocalCertificate(t *testing.T) {
if os.Getenv("TEST_LENS") != "true" {
t.Skip()
}
client, err := NewClientHelper()
if err != nil {
t.Fatal(err)
}
cert, key, err := generateCertificate()
if err != nil {
t.Fatal(err)
}
err = client.SystemCreateLocalCertificate("gofortiadc", "", cert, key)
if err != nil {
t.Fatal(err)
}
}
func TestClient_SystemDeleteLocalCertificate(t *testing.T) {
if os.Getenv("TEST_LENS") != "true" {
t.Skip()
}
client, err := NewClientHelper()
if err != nil {
t.Fatal(err)
}
err = client.SystemDeleteLocalCertificate("gofortiadc")
if err != nil {
t.Fatal(err)
}
}
func generateCertificate() (cert, key []byte, err error) {
rawKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return cert, key, err
}
template := x509.Certificate{
Subject: pkix.Name{
Organization: []string{"Gofortiadc Test"},
},
SerialNumber: big.NewInt(1),
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Minute * 30),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: []string{"gofortiadc.test.tld"},
}
rawCert, err := x509.CreateCertificate(rand.Reader, &template, &template, &rawKey.PublicKey, rawKey)
if err != nil {
return cert, key, err
}
cert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rawCert})
key = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rawKey)})
return cert, key, nil
}